protected override void Evaluate() { // here we could directly overwrite the existing matrix cells instead. // note: values must then be initialized manually first, if null. Value = RosenbrockFunction.Value(Point); Gradient = RosenbrockFunction.Gradient(Point); Hessian = RosenbrockFunction.Hessian(Point); }
public void FindMinimum_Rosenbrock_Hard() { var obj = ObjectiveFunction.GradientHessian(point => Tuple.Create(RosenbrockFunction.Value(point), RosenbrockFunction.Gradient(point), RosenbrockFunction.Hessian(point))); var solver = new NewtonMinimizer(1e-5, 1000); var result = solver.FindMinimum(obj, new DenseVector(new[] { -1.2, 1.0 })); Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3)); Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3)); }
public virtual ActionResult RosenbrockFunctionPlot(PlotViewModel model) { if (!ModelState.IsValid) { return(HttpNotFound()); } var f = new RosenbrockFunction(); return(plot(f, model)); }
public void TestRosenbrockFunction() { var minimizer = new QNMinimizer(); var f = new RosenbrockFunction(); var x = minimizer.Minimize(f); var minValue = f.ValueAt(x); Assert.AreEqual(x[0], 1.0, 1e-5); Assert.AreEqual(x[1], 1.0, 1e-5); Assert.AreEqual(minValue, 0, 1e-10); }
public void TestGradient() { var input = new DenseVector(new[] { -0.9, -0.5 }); var v1 = RosenbrockFunction.Value(input); var g = RosenbrockFunction.Gradient(input); var eps = 1e-5; var eps0 = (new DenseVector(new[] { 1.0, 0.0 })) * eps; var eps1 = (new DenseVector(new[] { 0.0, 1.0 })) * eps; var g0 = (RosenbrockFunction.Value(input + eps0) - RosenbrockFunction.Value(input - eps0)) / (2 * eps); var g1 = (RosenbrockFunction.Value(input + eps1) - RosenbrockFunction.Value(input - eps1)) / (2 * eps); Assert.That(Math.Abs(g0 - g[0]) < 1e-3); Assert.That(Math.Abs(g1 - g[1]) < 1e-3); }
public void TestHessian() { var input = new DenseVector(new[] { -0.9, -0.5 }); var v1 = RosenbrockFunction.Value(input); var h = RosenbrockFunction.Hessian(input); var eps = 1e-5; var eps0 = (new DenseVector(new[] { 1.0, 0.0 })) * eps; var eps1 = (new DenseVector(new[] { 0.0, 1.0 })) * eps; var epsuu = (new DenseVector(new[] { 1.0, 1.0 })) * eps; var epsud = (new DenseVector(new[] { 1.0, -1.0 })) * eps; var h00 = (RosenbrockFunction.Value(input + eps0) - 2 * RosenbrockFunction.Value(input) + RosenbrockFunction.Value(input - eps0)) / (eps * eps); var h11 = (RosenbrockFunction.Value(input + eps1) - 2 * RosenbrockFunction.Value(input) + RosenbrockFunction.Value(input - eps1)) / (eps * eps); var h01 = (RosenbrockFunction.Value(input + epsuu) - RosenbrockFunction.Value(input + epsud) - RosenbrockFunction.Value(input - epsud) + RosenbrockFunction.Value(input - epsuu)) / (4 * eps * eps); Assert.That(Math.Abs(h00 - h[0, 0]) < 1e-3); Assert.That(Math.Abs(h11 - h[1, 1]) < 1e-3); Assert.That(Math.Abs(h01 - h[0, 1]) < 1e-3); Assert.That(Math.Abs(h01 - h[1, 0]) < 1e-3); }
protected override void EvaluateHessian() { Hessian = RosenbrockFunction.Hessian(Point); }
protected override void EvaluateGradient() { Gradient = RosenbrockFunction.Gradient(Point); }
protected override void EvaluateValue() { Value = RosenbrockFunction.Value(Point); }
private static void CheckRosenbrock(double a, double b, double expectedMin) { var x = BfgsSolver.Solve(new DenseVector(new[] { a, b }), RosenbrockFunction.Value, RosenbrockFunction.Gradient); Numerics.Precision.AlmostEqual(expectedMin, RosenbrockFunction.Value(x), Precision); }