public void Vardim() { // This function is described by Powell in his NEWOUA paper as one that caused problems for the simplest formulation of that minimizer. Func<IList<double>, double> f = (IList<double> x) => { double s1 = 0.0; double s2 = 0.0; for (int i = 0; i < x.Count; i++) { s1 += i * (x[i] - 1.0); s2 += MoreMath.Sqr(x[i] - 1.0); } return (s2 + s1 * s1 + s1 * s1 * s1 * s1); }; for (int n = 2; n < 8; n++) { Console.WriteLine("n = {0}", n); ColumnVector start = new ColumnVector(n); ColumnVector solution = new ColumnVector(n); solution.Fill((i, j) => 1.0); MultiExtremum min = MultiFunctionMath.FindLocalMinimum(f, start); Console.WriteLine(min.EvaluationCount); Console.WriteLine("{0} ({1}) ?= 0.0", min.Value, min.Precision); Assert.IsTrue(min.Dimension == n); Assert.IsTrue(TestUtilities.IsNearlyEqual(min.Value, 0.0, new EvaluationSettings() { AbsolutePrecision = 4.0 * min.Precision })); Assert.IsTrue(TestUtilities.IsNearlyEqual(min.Location, solution, new EvaluationSettings() { RelativePrecision = Math.Sqrt(4.0 * min.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)) })); } }