fix(optim): refuse non-finite bounds in differential evolution

Assisted-by: DeepSeek V4.1 Flash
This commit is contained in:
2026-09-28 14:07:46 +02:00
parent 3a44be625f
commit b2e69ff57b
3 changed files with 31 additions and 2 deletions
+22
View File
@@ -112,3 +112,25 @@ func TestDEErrors(t *testing.T) {
t.Error("NaN objective accepted")
}
}
// TestDERejectsInfiniteBounds pins that an infinite side is refused: the
// sampler draws uniformly inside the box, so an open side would populate
// it with NaN and hand back a NaN point with a nil error.
func TestDERejectsInfiniteBounds(t *testing.T) {
zero, _ := core.FromFloats([]float64{0}, 1)
one, _ := core.FromFloats([]float64{1}, 1)
constF := func(a *core.Array) (float64, error) { return 1, nil }
for _, tc := range []struct {
name string
lo, hi *core.Array
}{
{"both sides open", mustBound(math.Inf(-1)), mustBound(math.Inf(1))},
{"upper open", zero, mustBound(math.Inf(1))},
{"lower open", mustBound(math.Inf(-1)), one},
} {
x, _, err := MinimiseDifferentialEvolution(constF, tc.lo, tc.hi, DifferentialEvolutionOptions{Generations: 5})
if err == nil {
t.Errorf("%s: infinite bound accepted, returned x=%g", tc.name, x.FloatAt(0))
}
}
}