public void FindParabolaMinimumWithDerivative() { using (var solver = new NLoptSolver(NLoptAlgorithm.LD_AUGLAG, 1, 0.01, 100, NLoptAlgorithm.LN_NELDERMEAD)) { solver.SetLowerBounds(new[] { -10.0 }); solver.SetUpperBounds(new[] { 100.0 }); solver.AddLessOrEqualZeroConstraint((variables, gradient) => { if (gradient != null) { gradient[0] = 1.0; } return(variables[0] - 100.0); }); solver.SetMinObjective((variables, gradient) => { if (gradient != null) { gradient[0] = (variables[0] - 3.0) * 2.0; } return(Math.Pow(variables[0] - 3.0, 2.0) + 4.0); }); double?finalScore; var initialValue = new[] { 2.0 }; var result = solver.Optimize(initialValue, out finalScore); Assert.AreEqual(3.0, initialValue[0], 0.01); Assert.AreEqual(4.0, finalScore.GetValueOrDefault(), 0.01); } }
public bool DisablingAllowed; //if user want us to disable components that are not necessary in recomputation // CONSTRUCTOR FOR RADICAL public RadicalOptimizer(Design design, RadicalWindow radwindow) { this.Design = design; this.RadicalWindow = radwindow; this.RadicalVM = this.RadicalWindow.RadicalVM; this.MainAlg = this.RadicalVM.PrimaryAlgorithm; this.DisablingAllowed = !this.RadicalVM.DisablingNotAllowed; //this.SecondaryAlg = NLoptAlgorithm.LN_COBYLA; BuildWrapper(); SetBounds(); StoredMainValues = new ChartValues <double>(); StoredConstraintValues = new ChartValues <ChartValues <double> >(); if (this.DisablingAllowed) { FindWhichOnesToDisable(); } if (Design.Constraints != null) { foreach (Constraint c in Design.Constraints) { if (c.IsActive) { StoredConstraintValues.Add(new ChartValues <double>()); if (c.MyType == Constraint.ConstraintType.lessthan) { Solver.AddLessOrEqualZeroConstraint((x) => constraint(x, c)); } else if (c.MyType == Constraint.ConstraintType.morethan) { Solver.AddLessOrEqualZeroConstraint((x) => - constraint(x, c)); } else { Solver.AddEqualZeroConstraint((x) => constraint(x, c)); } } } } Solver.SetMinObjective((x) => Objective(x)); }