public void Run() { var function = new F4(); // Generate simplex with offset from 1 to 20 and in point [0.5, 0.5] Console.WriteLine("Case 1: Generate simplex with offset from 1 to 20 and in point [0.5, 0.5]."); for (int i = 1; i <= 20; i++) { var opt = new NelderMeadMinimizer(); opt.IsOutputEnabled = false; opt.IsOutputPerIterationEnabled = false; double[] min = opt.Minimize(function, new double[] { 0.5, 0.5 }, i); Console.WriteLine($"offset={i}, evals={function.Evaluations}, x_min={min.Format(16)}, f(x_min)={function.Value(min).ToString("F16")}"); function.Clear(); } Console.WriteLine(); // Generate simplex with offset from 1 to 20 and in point [20, 20] Console.WriteLine("Case 2: Generate simplex with offset from 1 to 20 and in point [20, 20]."); for (int i = 1; i <= 20; i++) { var opt = new NelderMeadMinimizer(); opt.IsOutputEnabled = false; opt.IsOutputPerIterationEnabled = false; double[] min = opt.Minimize(function, new double[] { 20, 20 }, i); Console.WriteLine($"offset={i}, evals={function.Evaluations}, x_min={min.Format(16)}, f(x_min)={function.Value(min).ToString("F16")}"); function.Clear(); } }
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 Run() { var result = new List <Tuple <string, string, string, string> >(); var functions = new List <Tuple <Function, double[]> >() { new Tuple <Function, double[]>(new F1(), new double[] { -1.9, 2.0 }), new Tuple <Function, double[]>(new F2(), new double[] { 0.1, 0.3 }), new Tuple <Function, double[]>(new F3(), new double[] { 0, 0, 0, 0, 0 }), new Tuple <Function, double[]>(new F4(), new double[] { 5.1, 1.1 }) }; for (int i = 0; i < functions.Count; i++) { var item = functions[i]; var opt1 = new NelderMeadMinimizer(); opt1.IsOutputEnabled = false; var min1 = opt1.Minimize(item.Item1, item.Item2.Copy()); var opt1Evals = item.Item1.Evaluations; var opt2 = new HookeJeevesMinimizer(); opt2.IsOutputEnabled = false; var min2 = opt2.Minimize(item.Item1, item.Item2.Copy()); var opt2Evals = item.Item1.Evaluations; var opt3 = new CoordinateDescentMinimizer(); opt3.IsOutputEnabled = false; var min3 = opt3.Minimize(item.Item1, item.Item2.Copy()); var opt3Evals = item.Item1.Evaluations; int outputPrecision = 6; result.Add(new Tuple <string, string, string, string>((i + 1).ToString(), "min: " + min1.Format(outputPrecision), "min: " + min2.Format(outputPrecision), "min: " + min3.Format(outputPrecision))); result.Add(new Tuple <string, string, string, string>(string.Empty, "f(min): " + item.Item1.Value(min1).ToString("F" + outputPrecision), "f(min): " + item.Item1.Value(min2).ToString("F" + outputPrecision), "f(min): " + item.Item1.Value(min3).ToString("F" + outputPrecision))); result.Add(new Tuple <string, string, string, string>(string.Empty, "evals: " + opt1Evals.ToString(), "evals: " + opt2Evals.ToString(), "evals: " + opt3Evals.ToString())); } Console.WriteLine(result.ToStringTable( new[] { "Function", "Nelder and Mead", "Hooke-Jeeves", "Coordinate Descent" }, a => a.Item1, a => a.Item2, a => a.Item3, a => a.Item4)); Console.WriteLine(); // Hooke-Jeeves will stuck at function F4 because of the nature of exploration which explores a better point only // in one axis, not all at once, and because at point [3.1 3.1] only [2.975 2.975] is fitter, the algorithm // can't determine that there is a better point in the environment }
public void Run() { var function = new F4(); // Nelder-Mead search var opt1 = new NelderMeadMinimizer(); opt1.IsOutputPerIterationEnabled = false; opt1.Minimize(function, new double[] { 5, 5 }); // Hooke-Jeeves pattern search var opt2 = new HookeJeevesMinimizer(); opt2.IsOutputPerIterationEnabled = false; opt2.Minimize(function, new double[] { 5, 5 }); }
public void TestNelderMead() { LinearFunction function = new LinearFunction(); List <Point> data = new List <Point>(); data.Add(new Point(0, 1)); data.Add(new Point(1, 3)); SquaredResidualsSumDistance distance = new SquaredResidualsSumDistance(data.ToArray()); var minimizer = new NelderMeadMinimizer(); var res = minimizer.Minimize(function, distance, 0.01, 100); function.SetParametersValues(res); Assert.AreEqual(res[0], 2, 1e-8); Assert.AreEqual(res[1], 1, 1e-8); }
public void Run() { var random = new Random(); var function = new F6(); int foundCounter = 0; for (int i = 0; i < 10000; i++) { var opt = new NelderMeadMinimizer(); opt.IsOutputEnabled = false; opt.IsOutputPerIterationEnabled = false; var final = opt.Minimize(function, new double[] { random.Next(-50, 51) }); if (function.Value(final) < 10e-4) { foundCounter++; } } Console.WriteLine("Probability: " + (foundCounter / (double)10000) + "%"); Console.WriteLine(); }