-
Notifications
You must be signed in to change notification settings - Fork 905
/
Copy pathNewtonMinimizerTests.cs
253 lines (216 loc) · 9.66 KB
/
NewtonMinimizerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// <copyright file="NewtonMinimizerTests.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2017 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Optimization;
using MathNet.Numerics.Optimization.ObjectiveFunctions;
using NUnit.Framework;
using MathNet.Numerics.Tests.OptimizationTests.TestFunctions;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using NUnit.Framework.Interfaces;
namespace MathNet.Numerics.Tests.OptimizationTests
{
public class LazyRosenbrockObjectiveFunction : LazyObjectiveFunctionBase
{
public LazyRosenbrockObjectiveFunction() : base(true, true) { }
public override IObjectiveFunction CreateNew()
{
return new LazyRosenbrockObjectiveFunction();
}
protected override void EvaluateValue()
{
Value = RosenbrockFunction.Value(Point);
}
protected override void EvaluateGradient()
{
Gradient = RosenbrockFunction.Gradient(Point);
}
protected override void EvaluateHessian()
{
Hessian = RosenbrockFunction.Hessian(Point);
}
}
public class RosenbrockObjectiveFunction : ObjectiveFunctionBase
{
public RosenbrockObjectiveFunction() : base(true, true) { }
public override IObjectiveFunction CreateNew()
{
return new RosenbrockObjectiveFunction();
}
protected override void Evaluate()
{
// here we could directly overwrite the existing matrix cells instead.
// note: values must then be initialized manually first, if null.
Value = RosenbrockFunction.Value(Point);
Gradient = RosenbrockFunction.Gradient(Point);
Hessian = RosenbrockFunction.Hessian(Point);
}
}
public class LazySixHumpCamelObjectiveFunction : LazyObjectiveFunctionBase
{
public LazySixHumpCamelObjectiveFunction() : base(true, true) { }
public override IObjectiveFunction CreateNew()
{
return new LazySixHumpCamelObjectiveFunction();
}
protected override void EvaluateValue()
{
Value = SixHumpCamelFunction.Value(Point);
}
protected override void EvaluateGradient()
{
Gradient = SixHumpCamelFunction.Gradient(Point);
}
protected override void EvaluateHessian()
{
Hessian = SixHumpCamelFunction.Hessian(Point);
}
}
[TestFixture]
public class NewtonMinimizerTests
{
[Test]
public void FindMinimum_Rosenbrock_Easy()
{
var obj = ObjectiveFunction.GradientHessian(RosenbrockFunction.Value, RosenbrockFunction.Gradient, RosenbrockFunction.Hessian);
var solver = new NewtonMinimizer(1e-5, 1000);
var result = solver.FindMinimum(obj, new DenseVector(new[] { 1.2, 1.2 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_Rosenbrock_Hard()
{
var obj = ObjectiveFunction.GradientHessian(point => (RosenbrockFunction.Value(point), RosenbrockFunction.Gradient(point), RosenbrockFunction.Hessian(point)));
var solver = new NewtonMinimizer(1e-5, 1000);
var result = solver.FindMinimum(obj, new DenseVector(new[] { -1.2, 1.0 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_Rosenbrock_Overton()
{
var obj = new LazyRosenbrockObjectiveFunction();
var solver = new NewtonMinimizer(1e-5, 1000);
var result = solver.FindMinimum(obj, new DenseVector(new[] { -0.9, -0.5 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_Linesearch_Rosenbrock_Easy()
{
var obj = new RosenbrockObjectiveFunction();
var solver = new NewtonMinimizer(1e-5, 1000, true);
var result = solver.FindMinimum(obj, new DenseVector(new[] { 1.2, 1.2 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_Linesearch_Rosenbrock_Hard()
{
var obj = new LazyRosenbrockObjectiveFunction();
var solver = new NewtonMinimizer(1e-5, 1000, true);
var result = solver.FindMinimum(obj, new DenseVector(new[] { -1.2, 1.0 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_Linesearch_Rosenbrock_Overton()
{
var obj = new LazyRosenbrockObjectiveFunction();
var solver = new NewtonMinimizer(1e-5, 1000, true);
var result = solver.FindMinimum(obj, new DenseVector(new[] { -0.9, -0.5 }));
Assert.That(Math.Abs(result.MinimizingPoint[0] - 1.0), Is.LessThan(1e-3));
Assert.That(Math.Abs(result.MinimizingPoint[1] - 1.0), Is.LessThan(1e-3));
}
[Test]
public void FindMinimum_SixHumpCamel_IndefiniteHessian()
{
var obj = new LazySixHumpCamelObjectiveFunction();
var solver = new NewtonMinimizer(1e-5, 1000, true, HessianModifiers.ReverseNegativeEigenvalues);
var result = solver.FindMinimum(obj, new DenseVector(new double[] { 1.0, -0.6 }));
Assert.That(result.MinimizingPoint[0], Is.EqualTo(0.0898).Within(1e-3));
Assert.That(result.MinimizingPoint[1], Is.EqualTo(-0.7126).Within(1e-3));
}
private class MghTestCaseEnumerator : IEnumerable<ITestCaseData>
{
private static readonly string[] _ignore_list =
{
"Beale fun (MGH #5) unbounded",
"Meyer fun (MGH #10) unbounded",
"Wood fun (MGH #14) unbounded",
};
private static bool in_ignore_list(string test_name)
{
return _ignore_list.Contains(test_name);
}
public IEnumerator<ITestCaseData> GetEnumerator()
{
return
RosenbrockFunction2.TestCases
.Concat(BealeFunction.TestCases)
.Concat(HelicalValleyFunction.TestCases)
.Concat(MeyerFunction.TestCases)
.Concat(PowellSingularFunction.TestCases)
.Concat(WoodFunction.TestCases)
.Concat(BrownAndDennisFunction.TestCases)
.Where(x => x.IsUnbounded)
.Select<TestCase,ITestCaseData>(x => new TestCaseData(x)
.SetName(x.FullName)
.IgnoreIf(in_ignore_list(x.FullName),"Algo error, not implementation error")
)
.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
[Test]
[TestCaseSource(typeof(MghTestCaseEnumerator))]
public void Mgh_Tests(TestFunctions.TestCase test_case)
{
var obj = new MghObjectiveFunction(test_case.Function, true, true);
var result = NewtonMinimizer.Minimum(obj, test_case.InitialGuess, 1e-8, 1000, useLineSearch: false);
if (test_case.MinimizingPoint != null)
{
Assert.That((result.MinimizingPoint - test_case.MinimizingPoint).L2Norm(), Is.LessThan(1e-3));
}
var val1 = result.FunctionInfoAtMinimum.Value;
var val2 = test_case.MinimalValue;
var abs_min = Math.Min(Math.Abs(val1), Math.Abs(val2));
var abs_err = Math.Abs(val1 - val2);
var rel_err = abs_err / abs_min;
var success = (abs_min <= 1 && abs_err < 1e-3) || (abs_min > 1 && rel_err < 1e-3);
Assert.That(success, "Minimal function value is not as expected.");
}
}
}