| 1 |
"""Tests for module optim fro OT optimization""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import numpy as np |
| 8 |
import ot |
| 9 |
|
| 10 |
|
| 11 |
def test_conditional_gradient(nx): |
| 12 |
n_bins = 100 |
| 13 |
|
| 14 |
x = np.arange(n_bins, dtype=np.float64) |
| 15 |
|
| 16 |
|
| 17 |
a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) |
| 18 |
b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) |
| 19 |
|
| 20 |
|
| 21 |
M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) |
| 22 |
M /= M.max() |
| 23 |
|
| 24 |
def f(G): |
| 25 |
return 0.5 * np.sum(G**2) |
| 26 |
|
| 27 |
def df(G): |
| 28 |
return G |
| 29 |
|
| 30 |
def fb(G): |
| 31 |
return 0.5 * nx.sum(G**2) |
| 32 |
|
| 33 |
ab, bb, Mb = nx.from_numpy(a, b, M) |
| 34 |
|
| 35 |
reg = 1e-1 |
| 36 |
|
| 37 |
G, log = ot.optim.cg(a, b, M, reg, f, df, verbose=True, log=True) |
| 38 |
Gb, log = ot.optim.cg(ab, bb, Mb, reg, fb, df, verbose=True, log=True) |
| 39 |
Gb = nx.to_numpy(Gb) |
| 40 |
|
| 41 |
np.testing.assert_allclose(Gb, G) |
| 42 |
np.testing.assert_allclose(a, Gb.sum(1)) |
| 43 |
np.testing.assert_allclose(b, Gb.sum(0)) |
| 44 |
|
| 45 |
|
| 46 |
def test_conditional_gradient_itermax(nx): |
| 47 |
n = 100 |
| 48 |
|
| 49 |
mu_s = np.array([0, 0]) |
| 50 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 51 |
|
| 52 |
mu_t = np.array([4, 4]) |
| 53 |
cov_t = np.array([[1, -0.8], [-0.8, 1]]) |
| 54 |
|
| 55 |
xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) |
| 56 |
xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) |
| 57 |
|
| 58 |
a, b = np.ones((n,)) / n, np.ones((n,)) / n |
| 59 |
|
| 60 |
|
| 61 |
M = ot.dist(xs, xt) |
| 62 |
M /= M.max() |
| 63 |
|
| 64 |
def f(G): |
| 65 |
return 0.5 * np.sum(G**2) |
| 66 |
|
| 67 |
def df(G): |
| 68 |
return G |
| 69 |
|
| 70 |
def fb(G): |
| 71 |
return 0.5 * nx.sum(G**2) |
| 72 |
|
| 73 |
ab, bb, Mb = nx.from_numpy(a, b, M) |
| 74 |
|
| 75 |
reg = 1e-1 |
| 76 |
|
| 77 |
G, log = ot.optim.cg( |
| 78 |
a, b, M, reg, f, df, numItermaxEmd=10000, verbose=True, log=True |
| 79 |
) |
| 80 |
Gb, log = ot.optim.cg( |
| 81 |
ab, bb, Mb, reg, fb, df, numItermaxEmd=10000, verbose=True, log=True |
| 82 |
) |
| 83 |
Gb = nx.to_numpy(Gb) |
| 84 |
|
| 85 |
np.testing.assert_allclose(Gb, G) |
| 86 |
np.testing.assert_allclose(a, Gb.sum(1)) |
| 87 |
np.testing.assert_allclose(b, Gb.sum(0)) |
| 88 |
|
| 89 |
|
| 90 |
def test_generalized_conditional_gradient(nx): |
| 91 |
n_bins = 100 |
| 92 |
|
| 93 |
x = np.arange(n_bins, dtype=np.float64) |
| 94 |
|
| 95 |
|
| 96 |
a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) |
| 97 |
b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10) |
| 98 |
|
| 99 |
|
| 100 |
M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1))) |
| 101 |
M /= M.max() |
| 102 |
|
| 103 |
def f(G): |
| 104 |
return 0.5 * np.sum(G**2) |
| 105 |
|
| 106 |
def df(G): |
| 107 |
return G |
| 108 |
|
| 109 |
def fb(G): |
| 110 |
return 0.5 * nx.sum(G**2) |
| 111 |
|
| 112 |
reg1 = 1e-3 |
| 113 |
reg2 = 1e-1 |
| 114 |
|
| 115 |
ab, bb, Mb = nx.from_numpy(a, b, M) |
| 116 |
|
| 117 |
G, log = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True, log=True) |
| 118 |
Gb, log = ot.optim.gcg(ab, bb, Mb, reg1, reg2, fb, df, verbose=True, log=True) |
| 119 |
Gb = nx.to_numpy(Gb) |
| 120 |
|
| 121 |
np.testing.assert_allclose(Gb, G, atol=1e-12) |
| 122 |
np.testing.assert_allclose(a, Gb.sum(1), atol=1e-05) |
| 123 |
np.testing.assert_allclose(b, Gb.sum(0), atol=1e-05) |
| 124 |
|
| 125 |
|
| 126 |
def test_solve_1d_linesearch_quad_funct(): |
| 127 |
np.testing.assert_allclose(ot.optim.solve_1d_linesearch_quad(1, -1), 0.5) |
| 128 |
np.testing.assert_allclose(ot.optim.solve_1d_linesearch_quad(-1, 5), 0) |
| 129 |
np.testing.assert_allclose(ot.optim.solve_1d_linesearch_quad(-1, 0.5), 1) |
| 130 |
|
| 131 |
|
| 132 |
def test_line_search_armijo(nx): |
| 133 |
xk = np.array([[0.25, 0.25], [0.25, 0.25]]) |
| 134 |
pk = np.array([[-0.25, 0.25], [0.25, -0.25]]) |
| 135 |
gfk = np.array([[23.04273441, 23.0449082], [23.04273441, 23.0449082]]) |
| 136 |
old_fval = -123.0 |
| 137 |
|
| 138 |
xkb, pkb, gfkb = nx.from_numpy(xk, pk, gfk) |
| 139 |
|
| 140 |
def f(x): |
| 141 |
return 1.0 |
| 142 |
|
| 143 |
|
| 144 |
alpha, a, b = ot.optim.line_search_armijo(f, xkb, pkb, gfkb, old_fval) |
| 145 |
alpha_np, anp, bnp = ot.optim.line_search_armijo(f, xk, pk, gfk, old_fval) |
| 146 |
assert a == anp |
| 147 |
assert b == bnp |
| 148 |
assert alpha == 0.0 |
| 149 |
|
| 150 |
|
| 151 |
def f(x): |
| 152 |
return nx.sum((x - 5.0) ** 2) |
| 153 |
|
| 154 |
def grad(x): |
| 155 |
return 2 * (x - 5.0) |
| 156 |
|
| 157 |
xk = nx.from_numpy(np.array([[[-5.0, -5.0]]])) |
| 158 |
pk = nx.from_numpy(np.array([[[100.0, 100.0]]])) |
| 159 |
gfk = grad(xk) |
| 160 |
old_fval = f(xk) |
| 161 |
|
| 162 |
|
| 163 |
alpha, _, _ = ot.optim.line_search_armijo(f, xk, pk, gfk, old_fval) |
| 164 |
np.testing.assert_allclose(alpha, 0.1) |
| 165 |
|
| 166 |
|
| 167 |
pk = nx.from_numpy(np.array([[[3.0, 3.0]]])) |
| 168 |
alpha, _, _ = ot.optim.line_search_armijo(f, xk, pk, gfk, old_fval, alpha0=1.0) |
| 169 |
np.testing.assert_allclose(alpha, 1.0) |
| 170 |
|
| 171 |
|
| 172 |
alpha, _, _ = ot.optim.line_search_armijo(f, xk, -pk, gfk, old_fval) |
| 173 |
assert alpha <= 0 |
| 174 |
|
| 175 |
|
| 176 |
xk = nx.from_numpy(np.array(-5.0)) |
| 177 |
pk = nx.from_numpy(np.array(100.0)) |
| 178 |
gfk = grad(xk) |
| 179 |
old_fval = f(xk) |
| 180 |
alpha, _, _ = ot.optim.line_search_armijo(f, xk, pk, gfk, old_fval) |
| 181 |
np.testing.assert_allclose(alpha, 0.1) |
| 182 |
|
| 183 |
|
| 184 |
def test_line_search_armijo_dtype_device(nx): |
| 185 |
for tp in nx.__type_list__: |
| 186 |
|
| 187 |
def f(x): |
| 188 |
return nx.sum((x - 5.0) ** 2) |
| 189 |
|
| 190 |
def grad(x): |
| 191 |
return 2 * (x - 5.0) |
| 192 |
|
| 193 |
xk = np.array([[[-5.0, -5.0]]]) |
| 194 |
pk = np.array([[[100.0, 100.0]]]) |
| 195 |
xkb, pkb = nx.from_numpy(xk, pk, type_as=tp) |
| 196 |
gfkb = grad(xkb) |
| 197 |
old_fval = f(xkb) |
| 198 |
|
| 199 |
|
| 200 |
alpha, _, fval = ot.optim.line_search_armijo(f, xkb, pkb, gfkb, old_fval) |
| 201 |
alpha = nx.to_numpy(alpha) |
| 202 |
np.testing.assert_allclose(alpha, 0.1) |
| 203 |
nx.assert_same_dtype_device(old_fval, fval) |
| 204 |
|
| 205 |
|
| 206 |
pk = np.array([[[3.0, 3.0]]]) |
| 207 |
pkb = nx.from_numpy(pk, type_as=tp) |
| 208 |
alpha, _, fval = ot.optim.line_search_armijo( |
| 209 |
f, xkb, pkb, gfkb, old_fval, alpha0=1.0 |
| 210 |
) |
| 211 |
alpha = nx.to_numpy(alpha) |
| 212 |
np.testing.assert_allclose(alpha, 1.0) |
| 213 |
nx.assert_same_dtype_device(old_fval, fval) |
| 214 |
|
| 215 |
|
| 216 |
alpha, _, fval = ot.optim.line_search_armijo(f, xkb, -pkb, gfkb, old_fval) |
| 217 |
alpha = nx.to_numpy(alpha) |
| 218 |
|
| 219 |
assert alpha <= 0 |
| 220 |
nx.assert_same_dtype_device(old_fval, fval) |
| 221 |
|
| 222 |
|
| 223 |
xkb = nx.from_numpy(np.array(-5.0), type_as=tp) |
| 224 |
pkb = nx.from_numpy(np.array(100), type_as=tp) |
| 225 |
gfkb = grad(xkb) |
| 226 |
old_fval = f(xkb) |
| 227 |
alpha, _, fval = ot.optim.line_search_armijo(f, xkb, pkb, gfkb, old_fval) |
| 228 |
alpha = nx.to_numpy(alpha) |
| 229 |
|
| 230 |
np.testing.assert_allclose(alpha, 0.1) |
| 231 |
nx.assert_same_dtype_device(old_fval, fval) |