|
2 | 2 | # License: MIT |
3 | 3 |
|
4 | 4 | import unittest |
| 5 | +from typing import Literal |
5 | 6 |
|
6 | 7 | import numpy as np |
7 | 8 |
|
8 | | -from uncertaintyx.f.jax import ToF |
| 9 | +from uncertaintyx.f.jax import Cigar |
| 10 | +from uncertaintyx.f.jax import DifferentPowers |
| 11 | +from uncertaintyx.f.jax import Ellipsoid |
| 12 | +from uncertaintyx.f.jax import Rosenbrock |
| 13 | +from uncertaintyx.f.jax import Sphere |
| 14 | +from uncertaintyx.f.jax import Tablet |
9 | 15 | from uncertaintyx.retrieve.oe.jax import OE |
10 | 16 |
|
11 | 17 |
|
12 | | -class Parabola(ToF): |
13 | | - """The parabolic test function.""" |
| 18 | +class OptimalEstimationTest(unittest.TestCase): |
| 19 | + """ |
| 20 | + Tests optimal estimation. |
| 21 | + """ |
14 | 22 |
|
15 | | - def __init__(self): |
16 | | - def f(x): |
17 | | - """The test function.""" |
18 | | - return x * x |
| 23 | + def setUp(self): |
| 24 | + self.rng = np.random.default_rng(5489) |
| 25 | + self.M = 50 |
| 26 | + self.m = 10 |
19 | 27 |
|
20 | | - super().__init__(f) |
| 28 | + def test_sphere(self): |
| 29 | + """The sphere function has a unique minimum at zero.""" |
| 30 | + f = Sphere() |
21 | 31 |
|
| 32 | + x = np.square(self.fuzzy(1.0, "x")) |
| 33 | + y = np.square(self.fuzzy(0.0, "y")) |
| 34 | + result = OE().retrieve(f, x, y) |
22 | 35 |
|
23 | | -class OptimalEstimationTest(unittest.TestCase): |
24 | | - """ |
25 | | - Tests EIV regression. |
26 | | - """ |
| 36 | + self.assertTrue(np.all(result.info == 0)) |
| 37 | + self.assertTrue(np.allclose(f.eval(result.xopt), y)) |
| 38 | + self.assertTrue(np.all(np.isfinite(result.xcov))) |
| 39 | + self.assertTrue(np.all(np.isfinite(result.xunc))) |
| 40 | + self.assertTrue(np.allclose(result.zvar, 0.0)) |
| 41 | + self.assertTrue(np.allclose(result.cost, 0.0)) |
| 42 | + |
| 43 | + def test_ellipsoid(self): |
| 44 | + """The ellipsoid function has a unique minimum at zero.""" |
| 45 | + f = Ellipsoid() |
27 | 46 |
|
28 | | - def test_parabola(self): |
| 47 | + x = self.fuzzy(1.0, "x") |
| 48 | + y = self.sharp(0.0, "y") |
| 49 | + result = OE().retrieve(f, x, y) |
| 50 | + |
| 51 | + self.assertTrue(np.all(result.info == 0)) |
| 52 | + self.assertTrue(np.allclose(result.xopt, 0.0)) |
| 53 | + self.assertTrue(np.all(np.isfinite(result.xcov))) |
| 54 | + self.assertTrue(np.all(np.isfinite(result.xunc))) |
| 55 | + self.assertTrue(np.allclose(result.zvar, 0.0)) |
| 56 | + self.assertTrue(np.allclose(result.cost, 0.0)) |
| 57 | + |
| 58 | + def test_cigar(self): |
| 59 | + """The cigar function has a unique minimum at zero.""" |
| 60 | + f = Cigar() |
| 61 | + |
| 62 | + x = self.fuzzy(1.0, "x") |
| 63 | + y = self.sharp(0.0, "y") |
| 64 | + result = OE().retrieve(f, x, y) |
| 65 | + |
| 66 | + self.assertTrue(np.all(result.info == 0)) |
| 67 | + self.assertTrue(np.allclose(result.xopt, 0.0)) |
| 68 | + self.assertTrue(np.all(np.isfinite(result.xcov))) |
| 69 | + self.assertTrue(np.all(np.isfinite(result.xunc))) |
| 70 | + self.assertTrue(np.allclose(result.zvar, 0.0)) |
| 71 | + self.assertTrue(np.allclose(result.cost, 0.0)) |
| 72 | + |
| 73 | + def test_tablet(self): |
| 74 | + """The tablet function has a unique minimum at zero.""" |
| 75 | + f = Tablet() |
| 76 | + |
| 77 | + x = self.fuzzy(1.0, "x") |
| 78 | + y = self.sharp(0.0, "y") |
| 79 | + result = OE().retrieve(f, x, y) |
| 80 | + |
| 81 | + self.assertTrue(np.all(result.info == 0)) |
| 82 | + self.assertTrue(np.allclose(result.xopt, 0.0)) |
| 83 | + self.assertTrue(np.all(np.isfinite(result.xcov))) |
| 84 | + self.assertTrue(np.all(np.isfinite(result.xunc))) |
| 85 | + self.assertTrue(np.allclose(result.zvar, 0.0)) |
| 86 | + self.assertTrue(np.allclose(result.cost, 0.0)) |
| 87 | + |
| 88 | + def test_rosenbrock(self): |
29 | 89 | """ |
30 | | - Tests the EIV retrieval with a simple parabolic test function. |
| 90 | + The Rosenbrock function has a global and a local minimum. The |
| 91 | + minimization uses initial values biased toward the global minimum |
| 92 | + to establish an unambiguous test condition. |
31 | 93 | """ |
32 | | - n = 100 |
| 94 | + f = Rosenbrock() |
33 | 95 |
|
34 | | - x = np.ones((n, 1)) |
35 | | - y = np.zeros((n, 1)) |
36 | | - result = OE().retrieve(Parabola(), x, y) |
| 96 | + x = self.fuzzy(3.0, "x") |
| 97 | + y = self.sharp(0.0, "y") |
| 98 | + result = OE().retrieve(f, x, y) |
37 | 99 |
|
38 | 100 | self.assertTrue(np.all(result.info == 0)) |
39 | | - self.assertTrue(np.allclose(result.xopt, 0.0)) |
40 | | - self.assertTrue(np.allclose(result.xcov, 0.0)) |
41 | | - self.assertTrue(np.allclose(result.xunc, 0.0)) |
| 101 | + self.assertTrue(np.allclose(result.xopt, 1.0)) |
| 102 | + self.assertTrue(np.all(np.isfinite(result.xcov))) |
| 103 | + self.assertTrue(np.all(np.isfinite(result.xunc))) |
42 | 104 | self.assertTrue(np.allclose(result.zvar, 0.0)) |
43 | 105 | self.assertTrue(np.allclose(result.cost, 0.0)) |
44 | 106 |
|
| 107 | + def test_different_powers(self): |
| 108 | + """ |
| 109 | + The different power function has a unique minimum at zero. |
| 110 | + Only the value of the cost function is tested, since the |
| 111 | + problem is badly scaled. |
| 112 | + """ |
| 113 | + f = DifferentPowers() |
| 114 | + |
| 115 | + x = self.fuzzy(1.0, "x") |
| 116 | + y = self.sharp(0.0, "y") |
| 117 | + result = OE().retrieve(f, x, y) |
| 118 | + |
| 119 | + self.assertTrue(np.allclose(result.cost, 0.0)) |
| 120 | + |
| 121 | + def fuzzy(self, val, role: Literal["x", "y"]) -> np.ndarray: |
| 122 | + """Returns an array filled with fuzzy values.""" |
| 123 | + return self.rng.normal( |
| 124 | + val, 1.0, (self.M, self.m) if role == "x" else (self.M,) |
| 125 | + ) |
| 126 | + |
| 127 | + def sharp(self, val, role: Literal["x", "y"]) -> np.ndarray: |
| 128 | + """Returns an array filled with sharp values.""" |
| 129 | + return np.full((self.M, self.m) if role == "x" else (self.M,), val) |
| 130 | + |
45 | 131 |
|
46 | 132 | if __name__ == "__main__": |
47 | 133 | unittest.main() |
0 commit comments