-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.py
127 lines (104 loc) · 2.81 KB
/
example.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
# Author: Eric Xu. Washington University
# The same model as Ipopt/examples/hs071
import pyipopt
from numpy import *
nvar = 4
x_L = ones((nvar), dtype=float_) * 1.0
x_U = ones((nvar), dtype=float_) * 5.0
ncon = 2
g_L = array([25.0, 40.0])
g_U = array([2.0*pow(10.0, 19), 40.0])
def eval_f(x, user_data = None):
assert len(x) == 4
return x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2]
def eval_grad_f(x, user_data = None):
assert len(x) == 4
grad_f = array([
x[0] * x[3] + x[3] * (x[0] + x[1] + x[2]) ,
x[0] * x[3],
x[0] * x[3] + 1.0,
x[0] * (x[0] + x[1] + x[2])
], float_)
return grad_f;
def eval_g(x, user_data= None):
assert len(x) == 4
return array([
x[0] * x[1] * x[2] * x[3],
x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3]
], float_)
nnzj = 8
def eval_jac_g(x, flag, user_data = None):
if flag:
return (array([0, 0, 0, 0, 1, 1, 1, 1]),
array([0, 1, 2, 3, 0, 1, 2, 3]))
else:
assert len(x) == 4
return array([ x[1]*x[2]*x[3],
x[0]*x[2]*x[3],
x[0]*x[1]*x[3],
x[0]*x[1]*x[2],
2.0*x[0],
2.0*x[1],
2.0*x[2],
2.0*x[3] ])
nnzh = 10
def eval_h(x, lagrange, obj_factor, flag, user_data = None):
if flag:
hrow = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3]
hcol = [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
return (array(hcol), array(hrow))
else:
values = zeros((10), float_)
values[0] = obj_factor * (2*x[3])
values[1] = obj_factor * (x[3])
values[2] = 0
values[3] = obj_factor * (x[3])
values[4] = 0
values[5] = 0
values[6] = obj_factor * (2*x[0] + x[1] + x[2])
values[7] = obj_factor * (x[0])
values[8] = obj_factor * (x[0])
values[9] = 0
values[1] += lagrange[0] * (x[2] * x[3])
values[3] += lagrange[0] * (x[1] * x[3])
values[4] += lagrange[0] * (x[0] * x[3])
values[6] += lagrange[0] * (x[1] * x[2])
values[7] += lagrange[0] * (x[0] * x[2])
values[8] += lagrange[0] * (x[0] * x[1])
values[0] += lagrange[1] * 2
values[2] += lagrange[1] * 2
values[5] += lagrange[1] * 2
values[9] += lagrange[1] * 2
return values
def apply_new(x):
return True
nlp = pyipopt.create(nvar, x_L, x_U, ncon, g_L, g_U, nnzj, nnzh, eval_f, eval_grad_f, eval_g, eval_jac_g)
x0 = array([1.0, 5.0, 5.0, 1.0])
pi0 = array([1.0, 1.0])
"""
print x0
print nvar, ncon, nnzj
print x_L, x_U
print g_L, g_U
print eval_f(x0)
print eval_grad_f(x0)
print eval_g(x0)
a = eval_jac_g(x0, True)
print "a = ", a[1], a[0]
print eval_jac_g(x0, False)
print eval_h(x0, pi0, 1.0, False)
print eval_h(x0, pi0, 1.0, True)
"""
r = nlp.solve(x0)
nlp.close()
print "Solution of the primal variables, x"
print r["x"]
print "Solution of the bound multipliers, z_L and z_U"
print r["mult_xL"], r["mult_xU"]
print "Solution of the constraint multiplier, lambda"
print r["mult_g"]
print "Objective value"
print "f(x*) =", r["f"]
print "Constraint value"
print "g(x*) =", r["g"]