-
Notifications
You must be signed in to change notification settings - Fork 4
/
pyipopt-list.c
427 lines (339 loc) · 10.8 KB
/
pyipopt-list.c
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* whatever licence, free
Eric You Xu, WUSTL
RoT: 1, start from a relative small core, make sure it works
2, check the objective warp twice
wrap a tuple of callable object
use "O" instead of 'o',
warp it with (), etc
3, Exam the python function before hook back
4, if that's a single vaule, use pyarg_parse
TODO:
0. Deference use valgrind: DONE
1. Use this code with AMPL, connect nlpy and pyipopt tomorrow. DONE
2nd dev requires some knowledge
2. Read openopt code and hook them
LESSIMPORTANT:
handle the reference count, is there any memory leak???
*/
#include "Python.h"
#include "IpStdCInterface.h"
#include <stdio.h>
#include "hook.h"
static PyObject *eval_f_python = NULL;
static PyObject *eval_grad_f_python = NULL;
static PyObject *eval_g_python = NULL;
static PyObject *eval_jac_g_python = NULL;
static PyObject *eval_h_python = NULL;
static IpoptProblem nlp = NULL; /* IpoptProblem */
/* Call back function section
Adapters that delegate actual function to Python
C callback function interface
|- Prepare Python object
|- Call Python Function
|- Return c type value
use pyclear to clean any data I created (I control the ownship)
use py-deref to decrease other data (get from other function, etc)
*/
Bool eval_f(Index n, Number* x, Bool new_x,
Number* obj_value, UserDataPtr user_data)
{
PyObject *newx = PyList_New(n);
if (!newx) return FALSE;
int i;
for (i=0; i<n; i++)
PyList_SetItem(newx, i, PyFloat_FromDouble(x[i]));
PyObject* arglist = Py_BuildValue("(O)", newx);
PyObject* result = PyObject_CallObject (eval_f_python ,arglist);
if (!PyFloat_Check(result))
PyErr_Print();
*obj_value = PyFloat_AsDouble(result);
Py_DECREF(result);
Py_CLEAR(newx);
Py_CLEAR(arglist);
return TRUE;
}
static Bool eval_grad_f(Index n, Number* x, Bool new_x,
Number* grad_f, UserDataPtr user_data)
{
if (eval_grad_f_python == NULL) PyErr_Print();
PyObject *newx = PyList_New(n);
int i;
for (i=0; i<n; i++)
PyList_SetItem(newx, i, PyFloat_FromDouble(x[i]));
PyObject* arglist = Py_BuildValue("(O)", newx);
PyObject* result = PyObject_CallObject (eval_grad_f_python, arglist);
if (!PyList_Check(result))
PyErr_Print();
for (i = 0; i < n; i++)
grad_f[i] = PyFloat_AsDouble(PyList_GetItem(result, i));
Py_DECREF(result);
Py_CLEAR(newx);
Py_CLEAR(arglist);
return TRUE;
}
Bool eval_g(Index n, Number* x, Bool new_x,
Index m, Number* g, UserDataPtr user_data)
{
if (eval_g_python == NULL) PyErr_Print();
PyObject *newx = PyList_New(n);
int i;
for (i=0; i<n; i++)
PyList_SetItem(newx, i, PyFloat_FromDouble(x[i]));
PyObject* arglist = Py_BuildValue("(O)", newx);
PyObject* result = PyObject_CallObject (eval_g_python, arglist);
if (!PyList_Check(result))
PyErr_Print();
for (i = 0; i < m; i++)
g[i] = PyFloat_AsDouble(PyList_GetItem(result, i));
Py_DECREF(result);
Py_CLEAR(newx);
Py_CLEAR(arglist);
return TRUE;
}
Bool eval_jac_g(Index n, Number *x, Bool new_x,
Index m, Index nele_jac,
Index *iRow, Index *jCol, Number *values,
UserDataPtr user_data)
{
int i;
if (eval_grad_f_python == NULL) PyErr_Print();
if (values == NULL) {
PyObject *newx = Py_True;
PyObject* arglist = Py_BuildValue("(OO)", newx, Py_True);
PyObject* result = PyObject_CallObject (eval_jac_g_python, arglist);
if (!PyTuple_Check(result))
PyErr_Print();
PyObject* row = PyTuple_GetItem(result, 0);
PyObject* col = PyTuple_GetItem(result, 1);
if (!PyList_Check(row) || !PyList_Check(col))
PyErr_Print();
for (i = 0; i < nele_jac; i++) {
PyArg_Parse(PyList_GetItem(row, i), "i", &iRow[i]);
PyArg_Parse(PyList_GetItem(col, i), "i", &jCol[i]);
}
Py_DECREF(result);
Py_CLEAR(arglist);
}
else { // Assign the jac_g
PyObject *newx = PyList_New(n);
for (i=0; i<n; i++)
PyList_SetItem(newx, i, PyFloat_FromDouble(x[i]));
PyObject* arglist = Py_BuildValue("(OO)", newx, Py_False);
PyObject* result = PyObject_CallObject (eval_jac_g_python, arglist);
if (!PyTuple_Check(result))
PyErr_Print();
for (i = 0; i < nele_jac; i++)
values[i] = PyFloat_AsDouble(PyList_GetItem(result, i));
Py_DECREF(result);
Py_CLEAR(newx);
Py_CLEAR(arglist);
}
return TRUE;
}
static Bool eval_h(Index n, Number *x, Bool new_x, Number obj_factor,
Index m, Number *lambda, Bool new_lambda,
Index nele_hess, Index *iRow, Index *jCol,
Number *values, UserDataPtr user_data)
{
int i;
if (eval_h_python == NULL)
return FALSE;
if (values == NULL) {
PyObject *newx = Py_True;
PyObject *objfactor = Py_BuildValue("d", obj_factor);
PyObject *lagrange = Py_True; // Booleans don't need the ref count
PyObject* arglist = Py_BuildValue("(OOOO)", newx, lagrange, objfactor, Py_True);
PyObject* result = PyObject_CallObject (eval_h_python, arglist);
if (!PyTuple_Check(result))
PyErr_Print();
PyObject* row = PyTuple_GetItem(result, 0); //steal
PyObject* col = PyTuple_GetItem(result, 1); //steal
if (!PyList_Check(row))
PyErr_Print();
if (!PyList_Check(col))
PyErr_Print();
for (i = 0; i < nele_hess; i++) {
PyArg_Parse(PyList_GetItem(row, i), "i", &iRow[i]);
PyArg_Parse(PyList_GetItem(col, i), "i", &jCol[i]);
}
// newx and other's are just transparent pointer to Py_True
Py_DECREF(objfactor);
Py_DECREF(result);
Py_CLEAR(arglist);
}
else { // Assign the hess
PyObject *newx = PyList_New(n);
PyObject *lagrange = PyList_New(m);
PyObject *objfactor = Py_BuildValue("d", obj_factor);
for (i=0; i<n; i++) {
PyList_SetItem(newx, i, PyFloat_FromDouble(x[i]));
}
for (i=0; i<m; i++) {
PyList_SetItem(lagrange, i, PyFloat_FromDouble(lambda[i]));
}
PyObject* arglist = Py_BuildValue("(OOOO)", newx, objfactor, lagrange, Py_False);
PyObject* result = PyObject_CallObject (eval_h_python, arglist);
// if (!result) printf("Null pointer\n");
for (i = 0; i < nele_hess; i++) {
values[i] = PyFloat_AsDouble(PyList_GetItem(result, i));
// printf("[DEBUG] values[%d] = %f", i, values[i]);
}
Py_CLEAR(newx);
Py_CLEAR(lagrange);
Py_CLEAR(objfactor);
// Py_DECREF(result);
Py_CLEAR(arglist);
}
return TRUE;
}
/* Ends Call back function section */
/* Interface to Python */
// Crate problem
static PyObject *create(PyObject *obj, PyObject *args)
{
PyObject *f;
PyObject *gradf;
PyObject *g;
PyObject *jacg;
PyObject *h = NULL;
int n; // Number of var
PyObject *xL;
PyObject *xU;
int m; // Number of con
PyObject *gL;
PyObject *gU;
int nele_jac;
int nele_hess;
double result;
int i;
if (!PyArg_ParseTuple(args, "iOOiOOiiOOOO|O",
&n, &xL, &xU,
&m, &gL, &gU,
&nele_jac, &nele_hess,
&f, &gradf, &g, &jacg, &h))
return Py_False;
if (!PyCallable_Check(f) ||
!PyCallable_Check(gradf) ||
!PyCallable_Check(g) ||
!PyCallable_Check(jacg))
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function!");
else {
eval_f_python = f;
eval_grad_f_python = gradf;
eval_g_python = g;
eval_jac_g_python = jacg;
if (h !=NULL )
{
if (!PyCallable_Check(h))
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function!");
else
eval_h_python = h;
}
else
{
printf("Ipopt will use Hessian approximation!\n");
}
Number* x_L = NULL; /* lower bounds on x */
Number* x_U = NULL; /* upper bounds on x */
Number* g_L = NULL; /* lower bounds on g */
Number* g_U = NULL; /* upper bounds on g */
if (m <=0 || n<=0 )
return Py_False;
x_L = (Number*)malloc(sizeof(Number)*n);
x_U = (Number*)malloc(sizeof(Number)*n);
/* set the values for the variable bounds */
for (i = 0; i< n; i++)
{
x_L[i] = PyFloat_AsDouble(PyList_GetItem(xL, i));
x_U[i] = PyFloat_AsDouble(PyList_GetItem(xU, i));
}
g_L = (Number*)malloc(sizeof(Number)*m);
g_U = (Number*)malloc(sizeof(Number)*m);
for (i = 0; i< m; i++)
{
g_L[i] = PyFloat_AsDouble(PyList_GetItem(gL, i));
g_U[i] = PyFloat_AsDouble(PyList_GetItem(gU, i));
}
/* create the IpoptProblem */
nlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, 0,
0, &eval_f, &eval_g, &eval_grad_f, &eval_jac_g, &eval_h);
printf("Problem created\n");
free(x_L);
free(x_U);
free(g_L);
free(g_U);
return Py_True;
} // end if
return Py_False;
}
static PyObject *solve(PyObject *self, PyObject *args)
{
enum ApplicationReturnStatus status; /* Solve return code */
int m, n, i;
Number* x = NULL; /* starting point and solution vector */
Number* mult_x_L = NULL;
Number* mult_x_U = NULL;
Number obj; /* objective value */
PyObject* result = Py_False;
PyObject *x0;
if (!PyArg_ParseTuple(args, "O", &x0))
return Py_False;
if (nlp == NULL)
return Py_False;
/* set some options */
AddIpoptNumOption(nlp, "tol", 1e-9);
AddIpoptStrOption(nlp, "mu_strategy", "adaptive");
if (eval_h_python == NULL)
AddIpoptStrOption(nlp, "hessian_approximation","limited-memory");
/* allocate space for the initial point and set the values */
n = Py_SAFE_DOWNCAST(PyList_Size(x0), Py_ssize_t, int);
x = (Number*)malloc(sizeof(Number)*n);
for (i =0; i< n; i++)
x[i] = PyFloat_AsDouble(PyList_GetItem(x0 , i));
/* allocate space to store the bound multipliers at the solution */
mult_x_L = (Number*)malloc(sizeof(Number)*n);
mult_x_U = (Number*)malloc(sizeof(Number)*n);
/* solve the problem */
// printf("I am calling IPOPT\n");
status = IpoptSolve(nlp, x, NULL, &obj, NULL, mult_x_L, mult_x_U, NULL);
/* Return a tuple of
(x, obj, z_L, z_U) */
if (status == Solve_Succeeded) {
printf("\n\nSolution of the primal variables, x\n");
for (i=0; i<n; i++) {
printf("x[%d] = %e\n", i, x[i]);
}
printf("\n\nSolution of the bound multipliers, z_L and z_U\n");
for (i=0; i<n; i++) {
printf("z_L[%d] = %e\n", i, mult_x_L[i]);
}
for (i=0; i<n; i++) {
printf("z_U[%d] = %e\n", i, mult_x_U[i]);
}
printf("\n\nObjective value\n");
printf("f(x*) = %e\n", obj);
}
else {
return Py_False;
}
/* free allocated memory */
FreeIpoptProblem(nlp);
free(x);
free(mult_x_L);
free(mult_x_U);
// return result;
}
/* Begin Python Module code section */
static PyMethodDef ipoptMethods[] = {
{ "solve", solve, METH_VARARGS},
{ "create", create, METH_VARARGS},
{ NULL, NULL }
};
PyMODINIT_FUNC
initpyipopt(void)
{
Py_InitModule("pyipopt", ipoptMethods);
}
/* End Python Module code section */