-
Notifications
You must be signed in to change notification settings - Fork 4
/
testhook2.py
73 lines (58 loc) · 1.68 KB
/
testhook2.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Python and ipopt play together
Eric You Xu, Washington University 2008
"""
import pyipopt
import amplpy, _amplpy
import sys, getopt
from numpy import *
PROGNAME = sys.argv[0]
def commandline_err( msg ):
sys.stderr.write( "%s: %s\n" % ( PROGNAME, msg ) )
sys.exit( 1 )
def parse_cmdline( arglist ):
if len( arglist ) != 1:
commandline_err( 'I am hungry for an NL file\n\t Try ./pycpre filename.nl.' )
return None
try: options, fname = getopt.getopt( arglist, '' )
except getopt.error, e:
commandline_err( "%s" % str( e ) )
return None
return fname[0]
ProblemName = parse_cmdline(sys.argv[1:])
nlp = amplpy.AmplModel( ProblemName , opts=1)
xl = nlp.Lvar.tolist()
xu = nlp.Uvar.tolist()
gl = nlp.Lcon.tolist()
gu = nlp.Ucon.tolist()
x0 = nlp.x0.tolist()
pi0 = nlp.pi0.tolist()
m = nlp.m
n = nlp.n
nnzj = nlp.nnzj
nnzh = nlp.nnzh
def eval_f(x):
return nlp.obj(array(x))
def eval_grad_f(x):
return nlp.grad(array(x)).tolist() # return ndarray
def eval_g(x):
return nlp.cons(array(x)).tolist()
def eval_jac_g(x, flag):
if flag:
dummy, row1, col1 = nlp.jac(array(x0))
return (row1.tolist(), col1.tolist())
else:
j, dummyr, dummyc = nlp.jac(array(x))
return j.tolist()
dummyh, hrow, hcol = _amplpy.eval_H( nlp.x0, nlp.pi0, 1, 1.0 )
print hrow, hcol
dummyh, hrow, hcol = nlp.hess( nlp.x0, nlp.pi0, 1.0 )
def eval_h(x, lagrange, obj_factor, flag):
if flag:
return (hrow.tolist(), hcol.tolist())
else:
temph, dummyr, dummyc = nlp.hess(array(x), array(lagrange), obj_factor)
assert len(temph) == nnzh
return temph.tolist()
pyipopt.create(n, xl, xu, m, gl, gu, nnzj, 0, eval_f, eval_grad_f, eval_g, eval_jac_g, eval_h)
pyipopt.solve(x0)