MCPcopy Create free account
hub / github.com/PythonOT/POT / test_line_search_armijo

Function test_line_search_armijo

test/test_optim.py:132–181  ·  view source on GitHub ↗
(nx)

Source from the content-addressed store, hash-verified

130
131
132def 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 # Should not throw an exception and return 0. for alpha
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 # check line search armijo
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 # chech the case where the optimum is on the direction
163 alpha, _, _ = ot.optim.line_search_armijo(f, xk, pk, gfk, old_fval)
164 np.testing.assert_allclose(alpha, 0.1)
165
166 # check the case where the direction is not far enough
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 # check the case where checking the wrong direction
172 alpha, _, _ = ot.optim.line_search_armijo(f, xk, -pk, gfk, old_fval)
173 assert alpha <= 0
174
175 # check the case where the point is not a vector
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
184def test_line_search_armijo_dtype_device(nx):

Callers

nothing calls this directly

Calls 3

gradFunction · 0.85
from_numpyMethod · 0.80
fFunction · 0.70

Tested by

no test coverage detected