-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_wolfe.py
53 lines (40 loc) · 1.52 KB
/
test_wolfe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from nlp.model.nlpmodel import UnconstrainedNLPModel
from nlp.model.linemodel import C1LineModel
from nlp.ls.wolfe import StrongWolfeLineSearch
import numpy as np
import pytest
class Rosenbrock(UnconstrainedNLPModel):
def __init__(self, nvar, **kwargs):
assert (nvar > 1)
super(Rosenbrock, self).__init__(nvar, **kwargs)
def obj(self, x):
return np.sum(100 * (x[1:] - x[:-1]**2)**2 + (1 - x[:-1])**2)
def grad(self, x):
n = self.nvar
g = np.empty(n)
g[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0])
g[-1] = 200 * (x[-1] - x[-2]**2)
g[1:-1] = 200 * (x[1:-1] - x[:-2]**2) - \
400 * x[1:-1] * (x[2:] - x[1:-1]**2) - 2 * (1 - x[1:-1])
return g
@pytest.fixture(params=[2, 5, 10])
def rosenbrock_wolfe(request):
model = Rosenbrock(request.param)
x = np.zeros(request.param)
g = model.grad(x)
c1model = C1LineModel(model, x, -g) # steepest descent direction
return StrongWolfeLineSearch(c1model)
def test_c1rosenbrock(rosenbrock_wolfe):
with pytest.raises(StopIteration):
while True:
rosenbrock_wolfe.next()
@pytest.fixture(params=[2, 5, 10])
def rosenbrock_wolfe_ascent(request):
model = Rosenbrock(request.param)
x = np.zeros(request.param)
g = model.grad(x)
c1model = C1LineModel(model, x, g) # ascent direction!
return c1model
def test_c1rosenbrock_ascent(rosenbrock_wolfe_ascent):
with pytest.raises(ValueError):
StrongWolfeLineSearch(rosenbrock_wolfe_ascent)