public uint?CalculateLastValueBeforeRelease() { Spline2D spline = new Spline2D(); float curT = Time.time; foreach (HistoricValue pair in history) { if (pair.value > deadzone + epsilon) { spline.AddPoint(new Vector2(curT - (float)pair.time, (float)pair.value)); } } /* * StringBuilder sb = new StringBuilder("Spline: "); * for (float t = 0.0f; t <= 1.0; t += 0.05f) * { * sb.Append("("); * Vector2 v = spline.Interpolate(t); * sb.Append(v.x); * sb.Append(", "); * sb.Append(v.y); * sb.Append("), "); * } * Debug.Log(sb.ToString()); */ float value = GradientAscent(spline); return((uint?)value); }
private float GradientAscent(Spline2D spline) { float gamma = 0.01f; float precision = 0.018f; float curAlpha = 0.0f; float previousStepSize = 1.0f; float prevAlpha = 0.0f; while (previousStepSize > precision && curAlpha <= 1.0f) { prevAlpha = curAlpha; float df = spline.Derivative(prevAlpha).magnitude; curAlpha += gamma * df; previousStepSize = Mathf.Abs(curAlpha - prevAlpha); // Debug.Log("df: " + df + " newAlpha: " + curAlpha + " prevStep: " + previousStepSize+" value: "+spline.Interpolate(prevAlpha).y); } return(spline.Interpolate(prevAlpha).y); }