public void Bukin() { // Burkin has a narrow valley, not aligned with any axis, punctuated with many tiny "wells" along its bottom. // The deepest well is at (-10,1)-> 0. Func <IList <double>, double> function = (IList <double> x) => 100.0 * Math.Sqrt(Math.Abs(x[1] - 0.01 * x[0] * x[0])) + 0.01 * Math.Abs(x[0] + 10.0); IList <Interval> box = new Interval[] { Interval.FromEndpoints(-15.0, 0.0), Interval.FromEndpoints(-3.0, 3.0) }; EvaluationSettings settings = new EvaluationSettings() { RelativePrecision = 0.0, AbsolutePrecision = 1.0E-4, EvaluationBudget = 1000000 }; /* * settings.Update += (object result) => { * MultiExtremum e = (MultiExtremum) result; * Console.WriteLine("After {0} evaluations, best value {1}", e.EvaluationCount, e.Value); * }; */ MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(function, box, settings); Console.WriteLine(minimum.EvaluationCount); Console.WriteLine("{0} ({1})", minimum.Value, minimum.Precision); Console.WriteLine("{0} {1}", minimum.Location[0], minimum.Location[1]); // We do not end up finding the global minimum. }
public void ThompsonGlobal() { for (int n = 2; n < 8; n++) { Console.WriteLine("n={0}", n); Func <IList <double>, double> f = GetThompsonFunction(n); Interval[] box = new Interval[2 * (n - 1)]; for (int i = 0; i < (n - 1); i++) { box[2 * i] = Interval.FromEndpoints(-Math.PI, Math.PI); box[2 * i + 1] = Interval.FromEndpoints(-Math.PI / 2.0, Math.PI / 2.0); } MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(f, box); Console.WriteLine(minimum.EvaluationCount); Console.WriteLine("{0} ({1})", minimum.Value, minimum.Precision); for (int i = 0; i < (n - 1); i++) { Console.WriteLine("{0} {1}", minimum.Location[2 * i], minimum.Location[2 * i + 1]); } Console.WriteLine("0.0 0.0"); Assert.IsTrue(minimum.Dimension == 2 * (n - 1)); Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Value, thompsonSolutions[n], new EvaluationSettings() { AbsolutePrecision = 2.0 * minimum.Precision })); } }
public void GoldsteinPrice() { // Goldstein-Price has a valley with a complicated shape and a global minimum value of 3 at (0,-1). // It also has local minima, so we have to start close to this minimum if we expect to end at it. Func <IList <double>, double> fGoldsteinPrice = (IList <double> v) => { double x = v[0]; double y = v[1]; return( (1 + MoreMath.Pow(x + y + 1, 2) * (19 - 14 * x + 3 * x * x - 14 * y + 6 * x * y + 6 * y * y)) * (30 + MoreMath.Pow(2 * x - 3 * y, 2) * (18 - 32 * x + 12 * x * x + 48 * y - 36 * x * y + 27 * y * y)) ); }; ColumnVector start = new ColumnVector(0.5, -0.5); MultiExtremum min = MultiFunctionMath.FindLocalMinimum(fGoldsteinPrice, start); Console.WriteLine(min.EvaluationCount); Console.WriteLine(min.Value); Assert.IsTrue(min.Dimension == 2); Assert.IsTrue(TestUtilities.IsNearlyEqual(min.Value, 3.0)); Assert.IsTrue(TestUtilities.IsNearlyEqual(min.Location, new ColumnVector(0.0, -1.0), Math.Sqrt(TestUtilities.TargetPrecision))); MultiExtremum min2 = MultiFunctionMath.FindGlobalMinimum(fGoldsteinPrice, new Interval[] { Interval.FromEndpoints(-2.0, 2.0), Interval.FromEndpoints(-2.0, 2.0) }); Assert.IsTrue(min2.Dimension == 2); Assert.IsTrue(TestUtilities.IsNearlyEqual(min2.Value, 3.0, new EvaluationSettings() { AbsolutePrecision = min2.Precision })); //Assert.IsTrue(TestUtilities.IsNearlyEqual(min2.Location, new ColumnVector(0.0, -1.0), Math.Sqrt(TestUtilities.TargetPrecision))); }
public void Ackley() { // Ackley's function has many local minima, and a global minimum at (0, 0) -> 0. Func <IList <double>, double> function = (IList <double> x) => { double s = 0.0; double c = 0.0; for (int i = 0; i < x.Count; i++) { s += x[i] * x[i]; c += Math.Cos(2.0 * Math.PI * x[i]); } return(-20.0 * Math.Exp(-0.2 * Math.Sqrt(s / x.Count)) - Math.Exp(c / x.Count) + 20.0 + Math.E); }; EvaluationSettings settings = new EvaluationSettings() { AbsolutePrecision = 1.0E-8, EvaluationBudget = 10000000 }; for (int n = 2; n < 16; n = (int)Math.Round(AdvancedMath.GoldenRatio * n)) { Console.WriteLine("n={0}", n); Interval[] box = new Interval[n]; for (int i = 0; i < box.Length; i++) { box[i] = Interval.FromEndpoints(-32.0, 32.0); } MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(function, box, settings); Console.WriteLine(minimum.EvaluationCount); Console.WriteLine(minimum.Value); foreach (double coordinate in minimum.Location) { Console.WriteLine(coordinate); } Assert.IsTrue(minimum.Dimension == n); Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Value, 0.0, new EvaluationSettings() { AbsolutePrecision = 2.0 * minimum.Precision })); ColumnVector solution = new ColumnVector(n); Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Location, solution, new EvaluationSettings() { AbsolutePrecision = 2.0 * Math.Sqrt(minimum.Precision) })); } }
public void Schwefel() { // Test over [-500,500], minimum at (420.969,...) -> -418.983*d, many local minima Func <IList <double>, double> function = (IList <double> x) => { double s = 0.0; for (int i = 0; i < x.Count; i++) { s += x[i] * Math.Sin(Math.Sqrt(Math.Abs(x[i]))); } return(-s); }; for (int n = 2; n < 32; n = (int)Math.Round(AdvancedMath.GoldenRatio * n)) { Console.WriteLine("n={0}", n); Interval[] box = new Interval[n]; for (int i = 0; i < box.Length; i++) { box[i] = Interval.FromEndpoints(-500.0, 500.0); } MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(function, box); Assert.IsTrue(minimum.Dimension == n); Console.WriteLine(minimum.EvaluationCount); Console.WriteLine("{0} ({1}) ?= {2}", minimum.Value, minimum.Precision, -418.983 * n); Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Value, -418.983 * n, new EvaluationSettings() { AbsolutePrecision = minimum.Precision })); foreach (double coordinate in minimum.Location) { Console.WriteLine(coordinate); } ColumnVector solution = new ColumnVector(n); solution.Fill((int i, int j) => 420.969); Assert.IsTrue(TestUtilities.IsNearlyEqual(minimum.Location, solution, new EvaluationSettings() { RelativePrecision = 2.0 * Math.Sqrt(Math.Abs(minimum.Precision / minimum.Value)) })); } }
public void Griewank() { // See http://mathworld.wolfram.com/GriewankFunction.html for (int n = 2; n < 8; n++) { Console.WriteLine(n); Func <IList <double>, double> function = (IList <double> x) => { double s = 0.0; double p = 1.0; for (int i = 0; i < x.Count; i++) { s += MoreMath.Sqr(x[i]); p *= Math.Cos(x[i] / Math.Sqrt(i + 1.0)); } return(1.0 + s / 4000.0 - p); }; Interval[] box = new Interval[n]; for (int i = 0; i < n; i++) { box[i] = Interval.FromEndpoints(-100.0, 100.0); } EvaluationSettings settings = new EvaluationSettings() { AbsolutePrecision = 1.0E-6, EvaluationBudget = 1000000 }; MultiExtremum minimum = MultiFunctionMath.FindGlobalMinimum(function, box, settings); Console.WriteLine(minimum.Dimension); Console.WriteLine(minimum.EvaluationCount); Console.WriteLine("{0} ({1}) ?= 0.0", minimum.Value, minimum.Precision); Console.WriteLine("{0} {1} ...", minimum.Location[0], minimum.Location[1]); // We usually find the minimum at 0, but sometimes land a valley or two over. } }
public static void Optimization() { Interval range = Interval.FromEndpoints(0.0, 6.28); Extremum min = FunctionMath.FindMinimum(x => Math.Sin(x), range); Console.WriteLine($"Found minimum of {min.Value} at {min.Location}."); Console.WriteLine($"Required {min.EvaluationCount} evaluations."); MultiExtremum rosenbrock = MultiFunctionMath.FindLocalMinimum( x => MoreMath.Sqr(2.0 - x[0]) + 100.0 * MoreMath.Sqr(x[1] - x[0] * x[0]), new ColumnVector(0.0, 0.0) ); ColumnVector xm = rosenbrock.Location; Console.WriteLine($"Found minimum of {rosenbrock.Value} at ({xm[0]},{xm[1]})."); Console.WriteLine($"Required {rosenbrock.EvaluationCount} evaluations."); Func <IReadOnlyList <double>, double> leviFunction = z => { double x = z[0]; double y = z[1]; return( MoreMath.Sqr(MoreMath.Sin(3.0 * Math.PI * x)) + MoreMath.Sqr(x - 1.0) * (1.0 + MoreMath.Sqr(MoreMath.Sin(3.0 * Math.PI * y))) + MoreMath.Sqr(y - 1.0) * (1.0 + MoreMath.Sqr(MoreMath.Sin(2.0 * Math.PI * y))) ); }; Interval[] leviRegion = new Interval[] { Interval.FromEndpoints(-10.0, +10.0), Interval.FromEndpoints(-10.0, +10.0) }; MultiExtremum levi = MultiFunctionMath.FindGlobalMinimum(leviFunction, leviRegion); ColumnVector zm = levi.Location; Console.WriteLine($"Found minimum of {levi.Value} at ({zm[0]},{zm[1]})."); Console.WriteLine($"Required {levi.EvaluationCount} evaluations."); // Select a dimension int n = 6; // Define a function that takes 2n polar coordinates in the form // phi_1, theta_1, phi_2, theta_2, ..., phi_n, theta_n, computes // the sum of the potential energy 1/d for all pairs. Func <IReadOnlyList <double>, double> thompson = (IReadOnlyList <double> v) => { double e = 0.0; // iterate over all distinct pairs of points for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { // compute the chord length between points i and j double dx = Math.Cos(v[2 * j]) * Math.Cos(v[2 * j + 1]) - Math.Cos(v[2 * i]) * Math.Cos(v[2 * i + 1]); double dy = Math.Cos(v[2 * j]) * Math.Sin(v[2 * j + 1]) - Math.Cos(v[2 * i]) * Math.Sin(v[2 * i + 1]); double dz = Math.Sin(v[2 * j]) - Math.Sin(v[2 * i]); double d = Math.Sqrt(dx * dx + dy * dy + dz * dz); e += 1.0 / d; } } return(e); }; // Limit all polar coordinates to their standard ranges. Interval[] box = new Interval[2 * n]; for (int i = 0; i < n; i++) { box[2 * i] = Interval.FromEndpoints(-Math.PI, Math.PI); box[2 * i + 1] = Interval.FromEndpoints(-Math.PI / 2.0, Math.PI / 2.0); } // Use settings to monitor proress toward a rough solution. MultiExtremumSettings roughSettings = new MultiExtremumSettings() { RelativePrecision = 0.05, AbsolutePrecision = 0.0, Listener = r => { Console.WriteLine($"Minimum {r.Value} after {r.EvaluationCount} evaluations."); } }; MultiExtremum roughThompson = MultiFunctionMath.FindGlobalMinimum(thompson, box, roughSettings); // Use settings to monitor proress toward a refined solution. MultiExtremumSettings refinedSettings = new MultiExtremumSettings() { RelativePrecision = 1.0E-5, AbsolutePrecision = 0.0, Listener = r => { Console.WriteLine($"Minimum {r.Value} after {r.EvaluationCount} evaluations."); } }; MultiExtremum refinedThompson = MultiFunctionMath.FindLocalMinimum(thompson, roughThompson.Location, refinedSettings); Console.WriteLine($"Minimum potential energy {refinedThompson.Value}."); Console.WriteLine($"Required {roughThompson.EvaluationCount} + {refinedThompson.EvaluationCount} evaluations."); /* * // Define a function that takes 2n coordinates x1, y1, x2, y2, ... xn, yn * // and finds the smallest distance between two coordinate pairs. * Func<IReadOnlyList<double>, double> function = (IReadOnlyList<double> x) => { * double sMin = Double.MaxValue; * for (int i = 0; i < n; i++) { * for (int j = 0; j < i; j++) { * double s = MoreMath.Hypot(x[2 * i] - x[2 * j], x[2 * i + 1] - x[2 * j + 1]); * if (s < sMin) sMin = s; * } * } * return (sMin); * }; * * // Limit all coordinates to the unit box. * Interval[] box = new Interval[2 * n]; * for (int i = 0; i < box.Length; i++) box[i] = Interval.FromEndpoints(0.0, 1.0); * * // Use settings to monitor proress toward a rough solution. * MultiExtremumSettings roughSettings = new MultiExtremumSettings() { * RelativePrecision = 1.0E-2, AbsolutePrecision = 0.0, * Listener = r => { * Console.WriteLine($"Minimum {r.Value} after {r.EvaluationCount} evaluations."); * } * }; * MultiExtremum roughMaximum = MultiFunctionMath.FindGlobalMaximum(function, box, roughSettings); * * // Use settings to monitor proress toward a rough solution. * MultiExtremumSettings refinedSettings = new MultiExtremumSettings() { * RelativePrecision = 1.0E-8, AbsolutePrecision = 0.0, * Listener = r => { * Console.WriteLine($"Minimum {r.Value} after {r.EvaluationCount} evaluations."); * } * }; * MultiExtremum refinedMaximum = MultiFunctionMath.FindLocalMaximum(function, roughMaximum.Location, refinedSettings); */ }