/// <summary> /// Find value x that minimizes the scalar function f(x), constrained within bounds, using the Golden Section algorithm. /// For more options and diagnostics consider to use <see cref="GoldenSectionMinimizer"/> directly. /// </summary> public static double OfScalarFunctionConstrained(Func <double, double> function, double lowerBound, double upperBound, double tolerance = 1e-5, int maxIterations = 1000) { var objective = ObjectiveFunction.ScalarValue(function); var result = GoldenSectionMinimizer.Minimum(objective, lowerBound, upperBound, tolerance, maxIterations); return(result.MinimizingPoint); }
public void Run() { var function = new F3(); // var function = new DelegateFunction(x => Math.Pow(x[0] - 3, 2)); for (int i = 10; i <= 15; i++) { // Coordinate descent method var opt1 = new CoordinateDescentMinimizer(); opt1.IsOutputPerIterationEnabled = false; opt1.Minimize(function, new double[] { i }); // Golden section search var opt2 = new GoldenSectionMinimizer(); opt2.IsOutputPerIterationEnabled = false; opt2.Minimize(function, i); // Nelder-Mead search var opt3 = new NelderMeadMinimizer(); opt3.IsOutputPerIterationEnabled = false; opt3.Minimize(function, new double[] { i }); // Hooke-Jeeves pattern search var opt4 = new HookeJeevesMinimizer(); opt4.IsOutputPerIterationEnabled = false; opt4.Minimize(function, new double[] { i }); } }
public void Test_ExpansionWorks() { var algorithm = new GoldenSectionMinimizer(1e-5, 1000); var f1 = new Func <double, double>(x => (x - 3) * (x - 3)); var obj = ObjectiveFunction.ScalarValue(f1); var r1 = algorithm.FindMinimum(obj, -5, 5); Assert.That(Math.Abs(r1.MinimizingPoint - 3.0), Is.LessThan(1e-4)); }
public void Test_Works() { var algorithm = new GoldenSectionMinimizer(1e-5, 1000); var f1 = new Func <double, double>(x => (x - 3) * (x - 3)); var obj = new SimpleObjectiveFunction1D(f1); var r1 = algorithm.FindMinimum(obj, -100, 100); Assert.That(Math.Abs(r1.MinimizingPoint - 3.0), Is.LessThan(1e-4)); }
private MTPA buildTableMaxtorquePerAmple() { string config = string.Format("{0},{1},{2},{3},{4}", R, p, Ld, Lq, psiM); if (_param_config == config) { return(_mtpa); } int stepcount = 100; //int beta_count = 90; var idqs = new List <Fdq>(); var max_torques = new List <double>(); int k = 0; for (int i = 0; i < stepcount + 1; i++) { double II = 1000 * i / stepcount; var minimizer = new GoldenSectionMinimizer(); var objfnc = new SimpleObjectiveFunction1D(beta_rad => { double id = II * Math.Cos(beta_rad); double iq = II * Math.Sin(beta_rad); var data = calculatePointdata(id, iq, 3000); k++; return(-data.torque); }); var min = minimizer.FindMinimum(objfnc, Math.PI / 2, Math.PI); if (min != null) { double beta_rad = min.MinimizingPoint; double max_t = -min.FunctionInfoAtMinimum.Value; max_torques.Add(max_t); idqs.Add(new Fdq { d = II * Math.Cos(beta_rad), q = II * Math.Sin(beta_rad), }); } } Console.WriteLine("Call calc count = " + k); MTPA mtpa = new MTPA() { idqs = idqs, max_torques = max_torques }; _mtpa = mtpa; _param_config = config; return(mtpa); }
// try this for multiparameter optimization: https://numerics.mathdotnet.com/api/MathNet.Numerics.Optimization.TrustRegion/index.htm // Golden Section Minimizer public static Value Argmin(Value function, Value lowerBound, Value upperBound, Value tolerance, Netlist netlist, Style style, int s) { if (!(lowerBound is NumberValue) || !(upperBound is NumberValue)) { throw new Error("argmin: expecting numbers for lower and upper bounds"); } double lower = (lowerBound as NumberValue).value; double upper = (upperBound as NumberValue).value; if (lower > upper) { throw new Error("argmin: lower bound greater than upper bound"); } if (!(function is FunctionValue)) { throw new Error("argmin: expecting a function as first argument"); } FunctionValue closure = function as FunctionValue; if (closure.parameters.parameters.Count != 1) { throw new Error("argmin: initial values and function parameters have different lengths"); } IScalarObjectiveFunction objectiveFunction = ObjectiveFunction.ScalarValue( (double parameter) => { List <Value> arguments = new List <Value>(); arguments.Add(new NumberValue(parameter)); bool autoContinue = netlist.autoContinue; netlist.autoContinue = true; Value result = closure.ApplyReject(arguments, netlist, style, s); if (result == null) { throw new Error("Objective function returned null"); } netlist.autoContinue = autoContinue; if (!(result is NumberValue)) { throw new Error("Objective function must return a number, not: " + result.Format(style)); } KGui.gui.GuiOutputAppendText("argmin: parameter=" + Style.FormatSequence(arguments, ", ", x => x.Format(style)) + " => cost=" + result.Format(style) + Environment.NewLine); return((result as NumberValue).value); }); try { ScalarMinimizationResult result = GoldenSectionMinimizer.Minimum(objectiveFunction, lower, upper); if (result.ReasonForExit == ExitCondition.Converged || result.ReasonForExit == ExitCondition.BoundTolerance) { KGui.gui.GuiOutputAppendText("argmin: converged with parameter=" + result.MinimizingPoint + " and reason '" + result.ReasonForExit + "'" + Environment.NewLine); return(new NumberValue(result.MinimizingPoint)); } else { throw new Error("reason '" + result.ReasonForExit.ToString() + "'"); } } catch (Exception e) { throw new Error("argmin ended: " + ((e.InnerException == null) ? e.Message : e.InnerException.Message)); } // somehow we need to recatch the inner exception coming from CostAndGradient }
private double CalculateOptimalStepSize(Function f, double[] currentPoint, double[] currentOffset) { var g = new DelegateFunction(alpha => { return(f.Value(currentPoint.Select((d, i) => d - alpha[0] * currentOffset[i]).ToArray())); }); var minimizer = new GoldenSectionMinimizer(); minimizer.IsOutputEnabled = false; return(minimizer.Minimize(g, 0)); }
public SubtaskResult Compute(SubtaskInput subtask) { var goldenSectionMinimizer = new GoldenSectionMinimizer(); var polynomial = new Polynomial(subtask.Coefficients); var function = new Func <double, double>(polynomial.Evaluate); var objectiveFunction = ObjectiveFunction.ScalarValue(function); var result = goldenSectionMinimizer.FindMinimum(objectiveFunction, subtask.LowerBound, subtask.UpperBound); return(new SubtaskResult { Point = result.MinimizingPoint, Value = result.FunctionInfoAtMinimum.Value }); }
/// <summary> /// 只有一个参数时的拟合方法 /// </summary> public double[] ScalarHuberLossSolver() { var f1 = new Func <double, double>(ScalarHuberLoss); var obj = ObjectiveFunction.ScalarValue(f1); var r1 = GoldenSectionMinimizer.Minimum(obj, -10, 10); var theta1 = r1.MinimizingPoint; Console.WriteLine("theta1 = " + theta1);//最小时的 x 值 -> theta1 Console.WriteLine("Minimum Loss = " + ScalarHuberLoss(theta1)); scalarPred = new double[_xArr.Length]; for (var i = 0; i < _xArr.Length; i++) { //输出预测的y值 scalarPred[i] = theta1 * _xArr[i]; } return(scalarPred); }