Skip to content

Commit 9e81c1b

Browse files
committed
test: Add remaining difficult NIST problems
1 parent ee3d79c commit 9e81c1b

8 files changed

+308
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
namespace ExampleProblems.NISTProblems;
2+
3+
public class Bennett5Problem(DerivativeConfiguration derivativeConfiguration)
4+
: NistProblem(
5+
X,
6+
Y,
7+
Parameters,
8+
InitialValues,
9+
OptimumValues,
10+
Model,
11+
ModelGradient,
12+
ModelHessian,
13+
ModelHessianDiagonal,
14+
derivativeConfiguration)
15+
{
16+
// see https://www.itl.nist.gov/div898/strd/nls/data/bennett5.shtml
17+
18+
private static readonly IReadOnlyList<double> X =
19+
[
20+
7.447168, 8.102586, 8.452547, 8.711278, 8.916774, 9.087155, 9.23259, 9.359535, 9.472166, 9.573384, 9.665293,
21+
9.749461, 9.827092, 9.899128, 9.966321, 10.02928, 10.08851, 10.14443, 10.19738, 10.24767, 10.29556, 10.34125,
22+
10.38495, 10.42682, 10.467, 10.50564, 10.54283, 10.57869, 10.61331, 10.64678, 10.67915, 10.71052, 10.74092,
23+
10.77044, 10.7991, 10.82697, 10.85408, 10.88047, 10.90619, 10.93126, 10.95572, 10.97959, 11.00291, 11.0257,
24+
11.04798, 11.06977, 11.0911, 11.11198, 11.13244, 11.15248, 11.17213, 11.19141, 11.21031, 11.22887, 11.24709,
25+
11.26498, 11.28256, 11.29984, 11.31682, 11.33352, 11.34994, 11.3661, 11.382, 11.39766, 11.41307, 11.42824,
26+
11.4432, 11.45793, 11.47244, 11.48675, 11.50086, 11.51477, 11.52849, 11.54202, 11.55538, 11.56855, 11.58156,
27+
11.59442, 11.607121, 11.61964, 11.632, 11.64421, 11.65628, 11.6682, 11.67998, 11.69162, 11.70313, 11.71451,
28+
11.72576, 11.73688, 11.74789, 11.75878, 11.76955, 11.7802, 11.79073, 11.80116, 11.81148, 11.8217, 11.83181,
29+
11.84182, 11.85173, 11.86155, 11.87127, 11.88089, 11.89042, 11.89987, 11.90922, 11.91849, 11.92768, 11.93678,
30+
11.94579, 11.95473, 11.96359, 11.97237, 11.98107, 11.9897, 11.99826, 12.00674, 12.01515, 12.02349, 12.03176,
31+
12.03997, 12.0481, 12.05617, 12.06418, 12.07212, 12.08001, 12.08782, 12.09558, 12.10328, 12.11092, 12.1185,
32+
12.12603, 12.1335, 12.14091, 12.14827, 12.15557, 12.16283, 12.17003, 12.17717, 12.18427, 12.19132, 12.19832,
33+
12.20527, 12.21217, 12.21903, 12.22584, 12.2326, 12.23932, 12.24599, 12.25262, 12.2592, 12.26575, 12.27224
34+
];
35+
36+
private static readonly IReadOnlyList<double> Y =
37+
[
38+
-34.834702, -34.3932, -34.152901, -33.979099, -33.845901, -33.732899, -33.640301, -33.5592, -33.486801,
39+
-33.4231, -33.365101, -33.313, -33.260899, -33.2174, -33.176899, -33.139198, -33.101601, -33.066799, -33.035,
40+
-33.003101, -32.971298, -32.942299, -32.916302, -32.890202, -32.864101, -32.841, -32.817799, -32.797501,
41+
-32.7743, -32.757, -32.733799, -32.7164, -32.6991, -32.678799, -32.6614, -32.644001, -32.626701, -32.612202,
42+
-32.597698, -32.583199, -32.568699, -32.554298, -32.539799, -32.525299, -32.510799, -32.499199, -32.487598,
43+
-32.473202, -32.461601, -32.435501, -32.435501, -32.4268, -32.4123, -32.400799, -32.392101, -32.380501,
44+
-32.366001, -32.3573, -32.348598, -32.339901, -32.3284, -32.319698, -32.311001, -32.2994, -32.290699,
45+
-32.282001, -32.2733, -32.264599, -32.256001, -32.247299, -32.238602, -32.2299, -32.224098, -32.215401,
46+
-32.2038, -32.198002, -32.1894, -32.183601, -32.1749, -32.169102, -32.1633, -32.154598, -32.145901, -32.140099,
47+
-32.131401, -32.125599, -32.119801, -32.111198, -32.1054, -32.096699, -32.0909, -32.088001, -32.0793,
48+
-32.073502, -32.067699, -32.061901, -32.056099, -32.050301, -32.044498, -32.038799, -32.033001, -32.027199,
49+
-32.0243, -32.018501, -32.012699, -32.004002, -32.001099, -31.9953, -31.9895, -31.9837, -31.9779, -31.972099,
50+
-31.969299, -31.963501, -31.957701, -31.9519, -31.9461, -31.9403, -31.937401, -31.931601, -31.9258, -31.922899,
51+
-31.917101, -31.911301, -31.9084, -31.902599, -31.8969, -31.893999, -31.888201, -31.8853, -31.882401, -31.8766,
52+
-31.873699, -31.867901, -31.862101, -31.8592, -31.8563, -31.8505, -31.8447, -31.841801, -31.8389, -31.833099,
53+
-31.8302, -31.827299, -31.8216, -31.818701, -31.812901, -31.809999, -31.8071, -31.8013, -31.798401, -31.7955,
54+
-31.7897, -31.7868
55+
];
56+
57+
private static readonly IReadOnlyList<string> Parameters =
58+
["b1", "b2", "b3"];
59+
60+
private static readonly IReadOnlyList<double> InitialValues =
61+
[-2000, 50, 0.8];
62+
63+
private static readonly IReadOnlyList<double> OptimumValues =
64+
[-2.5235058043E+03, 4.6736564644E+01, 9.3218483193E-01];
65+
66+
private static readonly Func<double, IReadOnlyList<double>, double> Model = (x, p) =>
67+
{
68+
var (b1, b2, b3) = (p[0], p[1], p[2]);
69+
return b1 * Math.Pow(b2 + x, -1 / b3);
70+
};
71+
72+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelGradient = (x, p) =>
73+
{
74+
var (b1, b2, b3) = (p[0], p[1], p[2]);
75+
var g0 = Math.Pow(b2 + x, -1 / b3);
76+
var g1 = -b1 * Math.Pow(b2 + x, -(b3 + 1) / b3) / b3;
77+
var g2 = b1 * Math.Pow(b2 + x, -1 / b3) * Math.Log(b2 + x) / Math.Pow(b3, 2);
78+
return [g0, g1, g2];
79+
};
80+
81+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessian = (x, p) =>
82+
{
83+
var (b1, b2, b3) = (p[0], p[1], p[2]);
84+
var h01 = -Math.Pow(b2 + x, -(b3 + 1) / b3) / b3;
85+
var h02 = Math.Pow(b2 + x, -1 / b3) * Math.Log(b2 + x) / Math.Pow(b3, 2);
86+
var h11 = Math.Pow(b2 + x, -2 - 1 / b3) * (b1 * b3 + b1) / Math.Pow(b3, 2);
87+
var h12 = b1 * Math.Pow(b2 + x, -(b3 + 1) / b3) * (b3 - Math.Log(b2 + x)) / Math.Pow(b3, 3);
88+
var h22 = b1 * Math.Pow(b2 + x, -1 / b3) * (-2 * b3 + Math.Log(b2 + x)) * Math.Log(b2 + x) / Math.Pow(b3, 4);
89+
return
90+
[
91+
0.0, h01, h02,
92+
h01, h11, h12,
93+
h02, h12, h22
94+
];
95+
};
96+
97+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessianDiagonal = (x, p) =>
98+
{
99+
var (b1, b2, b3) = (p[0], p[1], p[2]);
100+
var h11 = Math.Pow(b2 + x, -2 - 1 / b3) * (b1 * b3 + b1) / Math.Pow(b3, 2);
101+
var h22 = b1 * Math.Pow(b2 + x, -1 / b3) * (-2 * b3 + Math.Log(b2 + x)) * Math.Log(b2 + x) / Math.Pow(b3, 4);
102+
return [0, h11, h22];
103+
};
104+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ExampleProblems.NISTProblems;
4+
5+
[SuppressMessage("ReSharper", "UnusedType.Global", Justification = "Crashes the native minimization processes. Needs further investigation.")]
6+
public class Eckerle4Problem(DerivativeConfiguration derivativeConfiguration)
7+
: NistProblem(
8+
X,
9+
Y,
10+
Parameters,
11+
InitialValues,
12+
OptimumValues,
13+
Model,
14+
ModelGradient,
15+
ModelHessian,
16+
ModelHessianDiagonal,
17+
derivativeConfiguration)
18+
{
19+
// see https://www.itl.nist.gov/div898/strd/nls/data/eckerle4.shtml
20+
21+
private static readonly IReadOnlyList<double> X =
22+
[
23+
400.0, 405.0, 410.0, 415.0, 420.0, 425.0, 430.0, 435.0, 436.5, 438.0, 439.5, 441.0, 442.5, 444.0, 445.5, 447.0,
24+
448.5, 450.0, 451.5, 453.0, 454.5, 456.0, 457.5, 459.0, 460.5, 462.0, 463.5, 465.0, 470.0, 475.0, 480.0, 485.0,
25+
490.0, 495.0, 500.0
26+
];
27+
28+
private static readonly IReadOnlyList<double> Y =
29+
[
30+
0.0001575, 0.0001699, 0.000235, 0.0003102, 0.0004917, 0.000871, 0.0017418, 0.00464, 0.0065895, 0.0097302,
31+
0.0149002, 0.023731, 0.0401683, 0.0712559, 0.1264458, 0.2073413, 0.2902366, 0.3445623, 0.3698049, 0.3668534,
32+
0.3106727, 0.2078154, 0.1164354, 0.0616764, 0.03372, 0.0194023, 0.0117831, 0.0074357, 0.0022732, 0.00088,
33+
0.0004579, 0.0002345, 0.0001586, 0.0001143, 7.1e-05
34+
];
35+
36+
private static readonly IReadOnlyList<string> Parameters =
37+
["b1", "b2", "b3"];
38+
39+
private static readonly IReadOnlyList<double> InitialValues =
40+
[1, 10, 500];
41+
42+
private static readonly IReadOnlyList<double> OptimumValues =
43+
[1.5543827178E+00, 4.0888321754E+00, 4.5154121844E+02];
44+
45+
private static readonly Func<double, IReadOnlyList<double>, double> Model = (x, p) =>
46+
{
47+
var (b1, b2, b3) = (p[0], p[1], p[2]);
48+
return b1 * Math.Exp(-0.5 * Math.Pow(-b3 + x, 2) / Math.Pow(b2, 2)) / b2;
49+
};
50+
51+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelGradient = (x, p) =>
52+
{
53+
var (b1, b2, b3) = (p[0], p[1], p[2]);
54+
var g0 = Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / b2;
55+
var g1 = b1 * (-Math.Pow(b2, 2) + 1.0 * Math.Pow(b3 - x, 2)) *
56+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 4);
57+
var g2 = -1.0 * b1 * (b3 - x) * Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 3);
58+
return [g0, g1, g2];
59+
};
60+
61+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessian = (x, p) =>
62+
{
63+
var (b1, b2, b3) = (p[0], p[1], p[2]);
64+
var h01 = (-Math.Pow(b2, 2) + 1.0 * Math.Pow(b3 - x, 2)) *
65+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 4);
66+
var h02 = 1.0 * (-b3 + x) * Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 3);
67+
var h11 = b1 * (2 * Math.Pow(b2, 4) - 5.0 * Math.Pow(b2, 2) * Math.Pow(b3 - x, 2) + 1.0 * Math.Pow(b3 - x, 4)) *
68+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 7);
69+
var h12 = b1 * (3.0 * Math.Pow(b2, 2) - 1.0 * Math.Pow(b3 - x, 2)) * (b3 - x) *
70+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 6);
71+
var h22 = 1.0 * b1 * (-Math.Pow(b2, 2) + Math.Pow(b3 - x, 2)) *
72+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 5);
73+
return
74+
[
75+
0.0, h01, h02,
76+
h01, h11, h12,
77+
h02, h12, h22
78+
];
79+
};
80+
81+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessianDiagonal = (x, p) =>
82+
{
83+
var (b1, b2, b3) = (p[0], p[1], p[2]);
84+
var h11 = b1 * (2 * Math.Pow(b2, 4) - 5.0 * Math.Pow(b2, 2) * Math.Pow(b3 - x, 2) + 1.0 * Math.Pow(b3 - x, 4)) *
85+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 7);
86+
var h22 = 1.0 * b1 * (-Math.Pow(b2, 2) + Math.Pow(b3 - x, 2)) *
87+
Math.Exp(-0.5 * Math.Pow(b3 - x, 2) / Math.Pow(b2, 2)) / Math.Pow(b2, 5);
88+
return [0, h11, h22];
89+
};
90+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace ExampleProblems.NISTProblems;
4+
5+
[SuppressMessage("ReSharper", "UnusedType.Global", Justification = "Crashes the native minimization processes. Needs further investigation.")]
6+
public class Mgh10Problem(DerivativeConfiguration derivativeConfiguration)
7+
: NistProblem(
8+
X,
9+
Y,
10+
Parameters,
11+
InitialValues,
12+
OptimumValues,
13+
Model,
14+
ModelGradient,
15+
ModelHessian,
16+
ModelHessianDiagonal,
17+
derivativeConfiguration)
18+
{
19+
// see https://www.itl.nist.gov/div898/strd/nls/data/mgh10.shtml
20+
21+
private static readonly IReadOnlyList<double> X =
22+
[50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0, 105.0, 110.0, 115.0, 120.0, 125.0];
23+
24+
private static readonly IReadOnlyList<double> Y =
25+
[
26+
34780.0, 28610.0, 23650.0, 19630.0, 16370.0, 13720.0, 11540.0, 9744.0, 8261.0, 7030.0, 6005.0, 5147.0, 4427.0,
27+
3820.0, 3307.0, 2872.0
28+
];
29+
30+
private static readonly IReadOnlyList<string> Parameters =
31+
["b1", "b2", "b3"];
32+
33+
private static readonly IReadOnlyList<double> InitialValues =
34+
[2, 400000, 25000];
35+
36+
private static readonly IReadOnlyList<double> OptimumValues =
37+
[5.6096364710E-03, 6.1813463463E+03, 3.4522363462E+02];
38+
39+
private static readonly Func<double, IReadOnlyList<double>, double> Model = (x, p) =>
40+
{
41+
var (b1, b2, b3) = (p[0], p[1], p[2]);
42+
return b1 * Math.Exp(b2 / (b3 + x));
43+
};
44+
45+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelGradient = (x, p) =>
46+
{
47+
var (b1, b2, b3) = (p[0], p[1], p[2]);
48+
var g0 = Math.Exp(b2 / (b3 + x));
49+
var g1 = b1 * Math.Exp(b2 / (b3 + x)) / (b3 + x);
50+
var g2 = -b1 * b2 * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 2);
51+
return [g0, g1, g2];
52+
};
53+
54+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessian = (x, p) =>
55+
{
56+
var (b1, b2, b3) = (p[0], p[1], p[2]);
57+
var h01 = Math.Exp(b2 / (b3 + x)) / (b3 + x);
58+
var h02 = -b2 * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 2);
59+
var h11 = b1 * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 2);
60+
var h12 = b1 * (-b2 - b3 - x) * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 3);
61+
var h22 = b1 * b2 * (b2 + 2 * b3 + 2 * x) * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 4);
62+
return
63+
[
64+
0.0, h01, h02,
65+
h01, h11, h12,
66+
h02, h12, h22
67+
];
68+
};
69+
70+
private static readonly Func<double, IReadOnlyList<double>, IReadOnlyList<double>> ModelHessianDiagonal = (x, p) =>
71+
{
72+
var (b1, b2, b3) = (p[0], p[1], p[2]);
73+
var h11 = b1 * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 2);
74+
var h22 = b1 * b2 * (b2 + 2 * b3 + 2 * x) * Math.Exp(b2 / (b3 + x)) / Math.Pow(b3 + x, 4);
75+
return [0, h11, h22];
76+
};
77+
}

test/minuit2.net.Benchmarks/NistProblemsMigradBenchmarks.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ public IMinimizationResult Rat42Problem() =>
3737
[Benchmark]
3838
public IMinimizationResult Rat43Problem() =>
3939
new Rat43Problem(DerivativeConfiguration).MinimizeWithMigrad(Strategy);
40+
41+
[Benchmark]
42+
public IMinimizationResult Bennett5Problem() =>
43+
new Bennett5Problem(DerivativeConfiguration).MinimizeWithMigrad(Strategy);
4044
}

test/minuit2.net.UnitTests/Any_minimizer.spec.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ private static IEnumerable<TestCaseData> ChallengingMinimizationProblems()
6161
yield return TestCase(new BoxBodProblem(derivativeConfiguration), nameof(BoxBodProblem));
6262
yield return TestCase(new Rat42Problem(derivativeConfiguration), nameof(Rat42Problem));
6363
yield return TestCase(new Rat43Problem(derivativeConfiguration), nameof(Rat43Problem));
64+
yield return TestCase(new Bennett5Problem(derivativeConfiguration), nameof(Bennett5Problem));
6465
continue;
6566

6667
TestCaseData TestCase(IConfiguredProblem problem, string problemName) =>

test/minuit2.net.UnitTests/The_combined_minimizer.fails_for_the_following_challenging_problems.verified.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
[
2+
Bennett5Problem.WithGradient (Balanced),
3+
Bennett5Problem.WithGradient (Fast),
4+
Bennett5Problem.WithGradient (Rigorous),
5+
Bennett5Problem.WithGradient (VeryRigorous),
6+
Bennett5Problem.WithoutDerivatives (Balanced),
7+
Bennett5Problem.WithoutDerivatives (Fast),
8+
Bennett5Problem.WithoutDerivatives (Rigorous),
9+
Bennett5Problem.WithoutDerivatives (VeryRigorous),
210
BoxBodProblem.WithGradient (Rigorous),
311
BoxBodProblem.WithGradientAndHessian (Rigorous),
412
BoxBodProblem.WithGradientHessianAndHessianDiagonal (Rigorous),

test/minuit2.net.UnitTests/The_migrad_minimizer.fails_for_the_following_challenging_problems.verified.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
[
2+
Bennett5Problem.WithGradient (Balanced),
3+
Bennett5Problem.WithGradient (Fast),
4+
Bennett5Problem.WithGradient (Rigorous),
5+
Bennett5Problem.WithGradient (VeryRigorous),
6+
Bennett5Problem.WithoutDerivatives (Balanced),
7+
Bennett5Problem.WithoutDerivatives (Fast),
8+
Bennett5Problem.WithoutDerivatives (Rigorous),
9+
Bennett5Problem.WithoutDerivatives (VeryRigorous),
210
BoxBodProblem.WithGradient (Rigorous),
311
BoxBodProblem.WithGradientAndHessian (Rigorous),
412
BoxBodProblem.WithGradientHessianAndHessianDiagonal (Rigorous),

test/minuit2.net.UnitTests/The_simplex_minimizer.fails_for_the_following_challenging_problems.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
[
2+
Bennett5Problem.WithGradient (Balanced),
3+
Bennett5Problem.WithGradient (Fast),
4+
Bennett5Problem.WithGradient (Rigorous),
5+
Bennett5Problem.WithGradient (VeryRigorous),
6+
Bennett5Problem.WithGradientAndHessian (Balanced),
7+
Bennett5Problem.WithGradientAndHessian (Fast),
8+
Bennett5Problem.WithGradientAndHessian (Rigorous),
9+
Bennett5Problem.WithGradientAndHessian (VeryRigorous),
10+
Bennett5Problem.WithGradientHessianAndHessianDiagonal (Balanced),
11+
Bennett5Problem.WithGradientHessianAndHessianDiagonal (Fast),
12+
Bennett5Problem.WithGradientHessianAndHessianDiagonal (Rigorous),
13+
Bennett5Problem.WithGradientHessianAndHessianDiagonal (VeryRigorous),
14+
Bennett5Problem.WithoutDerivatives (Balanced),
15+
Bennett5Problem.WithoutDerivatives (Fast),
16+
Bennett5Problem.WithoutDerivatives (Rigorous),
17+
Bennett5Problem.WithoutDerivatives (VeryRigorous),
218
BoxBodProblem.WithGradient (Balanced),
319
BoxBodProblem.WithGradient (Fast),
420
BoxBodProblem.WithGradient (Rigorous),

0 commit comments

Comments
 (0)