-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_nlpg.py
More file actions
91 lines (61 loc) · 2.13 KB
/
test_nlpg.py
File metadata and controls
91 lines (61 loc) · 2.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
os.chdir('/home/mark/projects/Nonlinear-Programming')
import NonlinearProgramming as nlpg
from NonlinearProgramming import functions as fun_nlpg
np = fun_nlpg.np
x = fun_nlpg.np.array([3,2])
f = fun_nlpg.Func_Object(fun_nlpg.fenton,
x)
# do an assert equal here
f.function(fun_nlpg.np.array([3,2]))
fun_nlpg.fenton(fun_nlpg.np.array([3,2]))
f.gradient(fun_nlpg.np.array([3,2]))
f.hessian(fun_nlpg.np.array([3,2]))
def make_cute_func(x):
N = x.shape[0]
I = np.arange(N-4,dtype=int);
return lambda z: sum( (-4*z[I]+3.0)**2 ) \
+ sum( ( z[I]**2 + 2*z[I+1]**2\
+ 3*z[I+2]**2 + 4*z[I+3]**2 \
+ 5*z[N-1]**2 )**2 );
x_cute = np.ones(7)
cute = fun_nlpg.Func_Object(nlpg.functions.make_cute_func(x_cute),
x_cute)
adolc = fun_nlpg.adolc
x_ones_hess = adolc.colpack\
.sparse_hess_no_repeat(0,np.ravel(np.ones(10)),[0,0])
cute = fun_nlpg.Func_Object(make_cute_func(x_cute),
x_cute)
sp_H=cute.sparse_hessian(x_cute)
H=cute.hessian(x_cute)
v = np.random.rand(7)
out_v = np.zeros(7)
for i in xrange(sp_H):
idx_1 = sp_H[1][i]
idx_2 = sp_H[2][i]
out_v[idx_1] += sp_H[3][i]*v[idx_2]
if idx_1 != idx_2:
out_v[idx_2] += sp_H[3][i]*v[idx_1]
adolc.colpack\
.sparse_hess_no_repeat(1,
np.ravel(x_cute),
[0,0])
adolc.colpack\
.sparse_hess_repeat(0,
np.ravel(x_cute),
[0,0])
x_cute2 = np.array([0.158,0.971,0.957,0.485,.23,.5])
cute2 = fun_nlpg.Func_Object(nlpg.functions.make_cute_func(x_cute2),
x_cute2)
sp_H=cute2.sparse_hessian(np.ones(6))
H=cute2.hessian(np.ones(6))
x_cute = np.ones(10**4)
cute = fun_nlpg.Func_Object(nlpg.functions.make_cute_func(x_cute),
x_cute)
sp_H=cute.sparse_hessian(x_cute)
x_cute = np.ones(10**3*5)
cute = fun_nlpg.Func_Object(nlpg.functions\
.make_cute_func(x_cute),
x_cute)
sp_H=cute.sparse_hessian(x_cute)
H=cute.hessian(x_cute)