/******************************************************************************************************************/ public static IEnumerator sendVideoGameValues(string user, int level, int points, int velocity, int numJumps, int lifes, successFunction success, errorFunction error) { WWW request; Dictionary <string, string> args = new Dictionary <string, string>(); IEnumerator auxRet; do { auxRet = setBaseUrl(); if (auxRet != null) { yield return(auxRet); } } while (auxRet != null); args.Add("user", user); args.Add("level", level.ToString()); args.Add("points", points.ToString()); args.Add("velocity", velocity.ToString()); args.Add("numJumps", numJumps.ToString()); args.Add("lifes", lifes.ToString()); request = new WWW(BASE_URL + SAVE_VALUES + GetParameters(args)); yield return(request); if (request.error == null) { loginCoachDataResponse aux = JsonConvert.DeserializeObject <loginCoachDataResponse>(request.text); if (aux.response != "OK") { if (error != null) { error(); } } else { if (success != null) { success(); } } } else if (error != null) { error(); } }
/* * Sends the user to log in to coachdata app and receives data from * the server * Function redone from the conection.js of Mateo * * This function is a coroutine and must be used with startCoroutine instead of called directly * *@param user: the user string *@param loggedSuccesfully: function called if the user manages to log in (can be null) *@param error: function called if the user fails to log in (can be null) */ public static IEnumerator loginCoachData(string user, loggedFunction loggedSuccesfully, errorFunction error) { float evaluation = 0; WWW request; Dictionary <string, string> args = new Dictionary <string, string>(); IEnumerator auxRet; do { auxRet = setBaseUrl(); if (auxRet != null) { yield return(auxRet); } } while (auxRet != null); args.Add("user", user); request = new WWW(BASE_URL + GAME_LOGIN + GetParameters(args)); while (!request.isDone) { yield return(request); } /*Debug.LogError ("REQUEST:"+BASE_URL + GAME_LOGIN+GetParameters(args)); * Debug.LogError ("TEXT:"+request.text);*/ if (request.error == null) { loginCoachDataResponse aux = JsonConvert.DeserializeObject <loginCoachDataResponse>(request.text); if (aux.response == "OK") { if (aux.evaluated) { foreach (KeyValuePair <string, JToken> pair in aux.evaluations) { evaluation += pair.Value.ToObject <int>(); } evaluation /= (float)(aux.evaluations.Count * aux.max_mark); } if (loggedSuccesfully != null) { loggedSuccesfully(evaluation); } } else { if (error != null) { error(); } } } else if (error != null) { error(); } }
/********************************************************************************************************/ /********************************************************************************************************/ /********************************************************************************************************/ /********************************************************************************************************/ public static IEnumerator getVideoGameValues(string user, getTable3 gettable, errorFunction error) { WWW request; VideogameValues[] videoVal; Dictionary <string, string> args = new Dictionary <string, string>(); IEnumerator auxRet; do { auxRet = setBaseUrl(); if (auxRet != null) { yield return(auxRet); } } while (auxRet != null); args.Add("user", user); request = new WWW(BASE_URL + GET_VALUES + GetParameters(args)); Debug.Log(BASE_URL + GET_VALUES + GetParameters(args)); yield return(request); if (request.error == null) { getVideogameValues aux = JsonConvert.DeserializeObject <getVideogameValues>(request.text); if (aux.response == "OK") { VideogameValues vvs; videoVal = new VideogameValues[100]; vvs = new VideogameValues(); vvs.user = aux.user; vvs.level = aux.level; vvs.points = aux.points; vvs.velocity = aux.velocity; vvs.numJumps = aux.numJumps; vvs.lifes = aux.lifes; videoVal[0] = vvs; if (gettable != null) { gettable(videoVal); } } else { if (error != null) { error(); } } } else if (error != null) { error(); } }
public static IEnumerator getAllVideogameStatistics(string user, getTable2 gettable, errorFunction error) { WWW request; VideogameStatistics[] statisticsTable; setBaseUrl(); request = new WWW(BASE_URL + GET_ALL_STATISTICS); Debug.Log(BASE_URL + GET_ALL_STATISTICS); yield return(request); if (request.error == null) { getAllStatistics aux = JsonConvert.DeserializeObject <getAllStatistics>(request.text); if (aux.response == "OK") { VideogameStatistics vs; statisticsTable = new VideogameStatistics[aux.user.Count]; JToken auxToken; string number; for (int i = 0; i < aux.user.Count; i++) { vs = new VideogameStatistics(); number = i.ToString(); aux.user.TryGetValue(number, out auxToken); vs.user = auxToken.ToObject <string>(); aux.levelReached.TryGetValue(number, out auxToken); vs.levelReached = auxToken.ToObject <int>(); aux.yards.TryGetValue(number, out auxToken); vs.yards = auxToken.ToObject <float>(); aux.acquiredSpeed.TryGetValue(number, out auxToken); vs.acquiredSpeed = auxToken.ToObject <int>(); aux.acquiredJumps.TryGetValue(number, out auxToken); vs.acquiredJumps = auxToken.ToObject <int>(); aux.acquiredLifes.TryGetValue(number, out auxToken); vs.acquiredLifes = auxToken.ToObject <int>(); aux.timePlayedInSeconds.TryGetValue(number, out auxToken); vs.timePlayedInSeconds = auxToken.ToObject <float>(); aux.timePlayedInMinutes.TryGetValue(number, out auxToken); vs.timePlayedInMinutes = auxToken.ToObject <float>(); statisticsTable[i] = vs; } if (gettable != null) { gettable(statisticsTable); } } else { if (error != null) { error(); } } } else if (error != null) { error(); } }
/********************************************************************************************************/ /********************************************************************************************************/ /********************************************************************************************************/ /********************************************************************************************************/ public static IEnumerator getVideoGameStatistics(string user, getTable2 gettable, errorFunction error) { WWW request; VideogameStatistics[] videoStat; Dictionary <string, string> args = new Dictionary <string, string>(); IEnumerator auxRet; do { auxRet = setBaseUrl(); if (auxRet != null) { yield return(auxRet); } } while (auxRet != null); args.Add("user", user); request = new WWW(BASE_URL + GET_STATISTICS + GetParameters(args)); Debug.Log(BASE_URL + GET_STATISTICS + GetParameters(args)); yield return(request); if (request.error == null) { getStatistics aux = JsonConvert.DeserializeObject <getStatistics>(request.text); if (aux.response == "OK") { VideogameStatistics vs; videoStat = new VideogameStatistics[100]; vs = new VideogameStatistics(); vs.user = aux.user; vs.levelReached = aux.levelReached; vs.yards = aux.yards; vs.acquiredSpeed = aux.acquiredSpeed; vs.acquiredJumps = aux.acquiredJumps; vs.acquiredLifes = aux.acquiredLifes; vs.timePlayedInSeconds = aux.timePlayedInSeconds; vs.timePlayedInMinutes = aux.timePlayedInMinutes; videoStat[0] = vs; if (gettable != null) { gettable(videoStat); } } else { if (error != null) { error(); } } } else { if (error != null) { error(); } } }
/******************************************************************************************************************/ public static IEnumerator sendVideoGameStatistics(string user, int levelReached, float yards, int acquiredSpeed, int acquiredJumps, int acquiredLifes, float timePlayedInSeconds, successFunction success, errorFunction error) { WWW request; Dictionary <string, string> args = new Dictionary <string, string>(); IEnumerator auxRet; do { auxRet = setBaseUrl(); if (auxRet != null) { yield return(auxRet); } } while (auxRet != null); args.Add("user", user); args.Add("levelReached", levelReached.ToString()); args.Add("yards", yards.ToString()); args.Add("acquiredSpeed", acquiredSpeed.ToString()); args.Add("acquiredJumps", acquiredJumps.ToString()); args.Add("acquiredLifes", acquiredLifes.ToString()); args.Add("timePlayedInSeconds", timePlayedInSeconds.ToString()); args.Add("timePlayedInMinutes", (timePlayedInSeconds / 60).ToString()); request = new WWW(BASE_URL + SAVE_STATISTICS + GetParameters(args)); yield return(request); if (request.error == null) { loginCoachDataResponse aux = JsonConvert.DeserializeObject <loginCoachDataResponse>(request.text); if (aux.response != "OK") { if (error != null) { error(); } } else { if (success != null) { success(); } } } else if (error != null) { error(); } }
/// <summary></summary> /// <param name="eFnc"></param> /// <param name="ystart"></param> /// <param name="x1"></param> /// <param name="x2"></param> /// <param name="eps"></param> /// <param name="h1"></param> /// <param name="hmin"></param> public void Solve(errorFunction eFnc, ref double[] ystart, double x1, double x2, double eps, double h1, double hmin) { const int MAXSTP = 10000; const double TINY = 1.0e-30; double hdid = 0; double hnext = 0; double[] yscal = new double[ystart.Length]; double[] y = new double[ystart.Length]; double[] dydx = new double[ystart.Length]; double x = x1; double h = h1 * Math.Sign(x2 - x1); for (int i = 0; i < yscal.Length; i++) y[i] = ystart[i]; for (int nstp = 0; nstp < MAXSTP; nstp++) { eFnc(x, y, ref dydx); for (int i = 0; i < yscal.Length; i++) yscal[i] = Math.Abs(y[i]) + Math.Abs(dydx[i] * h) + TINY; if ((x + h - x2) * (x + h - x1) > 0.0) h = x2 - x; //終端部の場合 rungeKutta4QualityControl(eFnc, y, dydx, ref x, h, eps, yscal, ref hdid, ref hnext); if ((x - x2) * (x2 - x1) >= 0.0) { for (int i = 0; i < yscal.Length; i++) ystart[i] = y[i]; return; } if (Math.Abs(hnext) <= hmin) throw new Exception("Step size too small"); h = hnext; } throw new Exception("Too many steps"); }
/// <summary></summary> /// <param name="eFnc">誤差評価関数</param> /// <param name="y">yの初期値</param> /// <param name="dydx">dy/dxの初期値</param> /// <param name="x">xの初期値</param> /// <param name="htry">刻幅hの初期候補値</param> /// <param name="eps">許容誤差</param> /// <param name="yscal">許容誤差を調整するベクトル</param> /// <param name="hdid">実際の刻幅</param> /// <param name="hNext">次のステップの刻幅候補値</param> private void rungeKutta4QualityControl(errorFunction eFnc, double[] y, double[] dydx, ref double x, double htry, double eps, double[] yscal, ref double hdid, ref double hNext) { //初期値を保存しておく double xsav = x; for (int i = 0; i < y.Length; i++) { ysav[i] = y[i]; dysav[i] = dydx[i]; } double h = htry; while (true) { double hh = 0.5 * h; rungeKutta4(eFnc, ysav, dysav, hh, xsav, ref ytemp); x = xsav + hh; eFnc(x, ytemp, ref dydx); rungeKutta4(eFnc, ytemp, dydx, hh, x, ref y); x = xsav + h; if (x == xsav) throw new Exception("Step size too small"); rungeKutta4(eFnc, ysav, dysav, h, xsav, ref ytemp); double errmax = 0.0; for (int i = 0; i < y.Length; i++) { ytemp[i] = y[i] - ytemp[i]; double temp = Math.Abs(ytemp[i] / yscal[i]); if (errmax < temp) errmax = temp; } errmax /= eps; if (errmax <= 1.0) { hdid = h; hNext = (errmax > 6.0e-4 ? 0.9 * h * Math.Exp(-0.2 * Math.Log(errmax)) : 4.0 * h); break; } h = 0.9 * h * Math.Exp(-0.2 * Math.Log(errmax)); } for (int i = 0; i < y.Length; i++) y[i] += ytemp[i] * 0.06666666; }
/// <summary></summary> /// <param name="eFnc"></param> /// <param name="y"></param> /// <param name="dydx"></param> /// <param name="h"></param> /// <param name="x"></param> /// <param name="yout"></param> private void rungeKutta4(errorFunction eFnc, double[] y, double[] dydx, double h, double x, ref double[] yout) { double hh = h * 0.5f; double h6 = h / 6.0f; double xh = x + hh; for (int i = 0; i < dydx.Length; i++) yt[i] = y[i] + hh * dydx[i]; //k1 eFnc(xh, yt, ref dyt); //k2計算用の微分値 for (int i = 0; i < dydx.Length; i++) yt[i] = y[i] + hh * dyt[i]; //k2 eFnc(xh, yt, ref dym); //k3計算用の微分値 for (int i = 0; i < dydx.Length; i++) { yt[i] = y[i] + h * dym[i]; dym[i] += dyt[i]; } eFnc(x + h, yt, ref dyt); //k4計算用の微分値 for (int i = 0; i < dydx.Length; i++) { yout[i] = y[i] + h6 * (dydx[i] + dyt[i] + 2.0f * dym[i]); } }
/// <summary>非線形連立代数方程式ソルバ</summary> /// <param name="eFnc">誤差関数</param> /// <param name="x">入力変数</param> /// <param name="aError">絶対誤差許容値</param> /// <param name="rError">相対誤差許容値(変化量が小さくなった場合の停止判定)</param> /// <param name="num">反復回数上限値</param> /// <param name="iteration">反復回数</param> public void Solve(errorFunction eFnc, ref double[] x, double aError, double rError, int num, out uint iteration) { //指定回数繰り返す for (iteration = 0; iteration < num; iteration++) { //ヤコビアンを計算 calculateJacobian(eFnc, x, ref outputs1, ref outputs2, ref jacobian); double errf = 0; for (uint j = 0; j < VariableNumber; j++) { errf += Math.Abs(outputs1[j]); fnc.SetValue(j, -outputs1[j]); } if (errf < aError) return; //LU分解で方程式を解く jacobian.LUDecomposition(); jacobian.LUSolve(ref fnc); double errx = 0; for (uint j = 0; j < VariableNumber; j++) { errx += Math.Abs(fnc.GetValue(j)); x[j] += fnc.GetValue(j); } if (errx < rError) return; } }
/// <summary>ヤコビアンを計算する</summary> /// <param name="eFnc"></param> /// <param name="x"></param> /// <param name="y1"></param> /// <param name="y2">計算用配列</param> /// <param name="jacobian"></param> private static void calculateJacobian(errorFunction eFnc, double[] x, ref double[] y1, ref double[] y2, ref Matrix jacobian) { uint varNum = jacobian.Columns; //現在の入力条件での出力を取得 eFnc(x, ref y1); //Make jacobian matrix for (uint j = 0; j < varNum; j++) { //差分を戻す if (j != 0) x[j - 1] -= DELTA_X; x[j] += DELTA_X; //出力計算 eFnc(x, ref y2); for (uint i = 0; i < varNum; i++) { jacobian.SetValue(i, j, (y2[i] - y1[i]) / DELTA_X); } } //差分を戻す x[varNum - 1] -= DELTA_X; }