-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpyipopt.c
597 lines (490 loc) · 16.7 KB
/
pyipopt.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* Copyright (c) 2008, Eric You Xu, Washington University
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Washington University nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "hook.h"
/* Object Section */
// sig of this is void foo(PyO*)
static void problem_dealloc(PyObject* self)
{
problem* temp = (problem*)self;
free(temp->data);
return;
}
PyObject* solve (PyObject* self, PyObject* args);
PyObject* close_model (PyObject* self, PyObject* args);
static char PYIPOPT_SOLVE_DOC[] = "solve(x) -> (x, ml, mu, obj)\n \
\n \
Call Ipopt to solve problem created before and return \n \
a tuple that contains final solution x, upper and lower\n \
bound for multiplier and final objective function obj. ";
static char PYIPOPT_CLOSE_DOC[] = "After all the solving, close the model\n";
static char PYIPOPT_ADD_STR_OPTION_DOC[] = "Set the String option for Ipopt. See the document for Ipopt for more information.\n";
PyObject *add_str_option(PyObject *self, PyObject *args)
{
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
char* param;
char* value;
Bool ret;
if (!PyArg_ParseTuple(args, "ss", ¶m, &value))
{
Py_INCREF (Py_False);
return Py_False;
}
ret = AddIpoptStrOption(nlp, (char*) param, value);
if (ret)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
Py_INCREF(Py_False);
return Py_False;
}
}
static char PYIPOPT_ADD_INT_OPTION_DOC[] = "Set the Int option for Ipopt. See the document for Ipopt for more information.\n";
PyObject *add_int_option(PyObject *self, PyObject *args)
{
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
char* param;
int value;
Bool ret;
if (!PyArg_ParseTuple(args, "si", ¶m, &value))
{
Py_INCREF(Py_False);
return Py_False;
}
ret = AddIpoptIntOption(nlp, (char*) param, value);
if (ret)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
Py_INCREF(Py_False);
return Py_False;
}
}
static char PYIPOPT_ADD_NUM_OPTION_DOC[] = "Set the Number/double option for Ipopt. See the document for Ipopt for more information.\n";
PyObject *add_num_option(PyObject *self, PyObject *args)
{
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
char* param;
double value = 1.;
Bool ret;
if (!PyArg_ParseTuple(args, "sd:num_option", ¶m, &value))
{
return NULL;
}
ret = AddIpoptNumOption(nlp, (char*) param, value);
if (ret)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
Py_INCREF(Py_False);
return Py_False;
}
}
PyMethodDef problem_methods[] = {
{ "solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC},
{ "close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC},
{ "int_option", add_int_option, METH_VARARGS, PYIPOPT_ADD_INT_OPTION_DOC},
{ "str_option", add_str_option, METH_VARARGS, PYIPOPT_ADD_STR_OPTION_DOC},
{ "num_option", add_num_option, METH_VARARGS, PYIPOPT_ADD_NUM_OPTION_DOC},
{NULL, NULL},
};
PyObject *problem_getattr(PyObject* self, char* attrname)
{
PyObject *result = NULL;
result = Py_FindMethod(problem_methods, self, attrname);
return result;
}
PyTypeObject IpoptProblemType = {
PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/
"pyipopt.Problem", /*tp_name*/
sizeof(problem), /*tp_basicsize*/
0, /*tp_itemsize*/
problem_dealloc, /*tp_dealloc*/
0, /*tp_print*/
problem_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"The IPOPT problem object in python", /* tp_doc */
};
static char PYIPOPT_CREATE_DOC[] = "create(n, xl, xu, m, gl, gu, nnzj, nnzh, eval_f, eval_grad_f, eval_g, eval_jac_g) -> Boolean\n \
\n \
Create a problem instance and return True if succeed \n \
\n \
n is the number of variables, \n \
xl is the lower bound of x as bounded constraints \n \
xu is the upper bound of x as bounded constraints \n \
both xl, xu should be one dimension arrays with length n \n \
\n \
m is the number of constraints, \n \
gl is the lower bound of constraints \n \
gu is the upper bound of constraints \n \
both gl, gu should be one dimension arrays with length m \n \
nnzj is the number of nonzeros in Jacobi matrix \n \
nnzh is the number of non-zeros in Hessian matrix, you can set it to 0 \n \
\n \
eval_f is the call back function to calculate objective value, \n \
it takes one single argument x as input vector \n \
eval_grad_f calculates gradient for objective function \n \
eval_g calculates the constraint values and return an array \n \
eval_jac_g calculates the Jacobi matrix. It takes two arguments, \n \
the first is the variable x and the second is a Boolean flag \n \
if the flag is true, it supposed to return a tuple (row, col) \n \
to indicate the sparse Jacobi matrix's structure. \n \
if the flag is false if returns the values of the Jacobi matrix \n \
with length nnzj \n \
eval_h calculates the hessian matrix, it's optional. \n \
if omitted, please set nnzh to 0 and Ipopt will use approximated hessian \n \
which will make the convergence slower. ";
static PyObject *create(PyObject *obj, PyObject *args)
{
PyObject *f;
PyObject *gradf;
PyObject *g;
PyObject *jacg;
PyObject *h = NULL;
PyObject *applynew = NULL;
DispatchData myowndata;
// I have to create a new python object here, return this python object
int n; // Number of var
PyArrayObject *xL;
PyArrayObject *xU;
int m; // Number of con
PyArrayObject *gL;
PyArrayObject *gU;
int nele_jac;
int nele_hess;
double* xldata, *xudata;
double* gldata, *gudata;
double result;
int i;
// Init the myowndata field
myowndata.eval_f_python = NULL;
myowndata.eval_grad_f_python = NULL;
myowndata.eval_g_python = NULL;
myowndata.eval_jac_g_python = NULL;
myowndata.eval_h_python = NULL;
myowndata.apply_new_python = NULL;
myowndata.userdata = NULL;
// "O!", &PyArray_Type &a_x
if (!PyArg_ParseTuple(args, "iO!O!iO!O!iiOOOO|OO",
&n, &PyArray_Type, &xL,
&PyArray_Type, &xU,
&m,
&PyArray_Type, &gL,
&PyArray_Type, &gU,
&nele_jac, &nele_hess,
&f, &gradf, &g, &jacg,
&h, &applynew))
{
return NULL;
}
if (!PyCallable_Check(f) ||
!PyCallable_Check(gradf) ||
!PyCallable_Check(g) ||
!PyCallable_Check(jacg))
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function!");
return NULL;
}
myowndata.eval_f_python = f;
myowndata.eval_grad_f_python = gradf;
myowndata.eval_g_python = g;
myowndata.eval_jac_g_python = jacg;
// logger("D field assigned %p\n", &myowndata);
// logger("D field assigned %p\n",myowndata.eval_jac_g_python );
if (h !=NULL )
{
if (!PyCallable_Check(h))
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function h.");
return NULL;
}
myowndata.eval_h_python = h;
}
else
{
logger("[PyIPOPT] Ipopt will use Hessian approximation.\n");
}
if (applynew != NULL)
{
if (!PyCallable_Check(applynew))
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function applynew.");
return NULL;
}
myowndata.apply_new_python = applynew;
}
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 (n<0) {
PyErr_SetString(PyExc_ValueError, "Input dimension must be greater than 1");
return NULL;
}
if (m<0) {
PyErr_SetString(PyExc_ValueError, "Number of constraints be positive or zero");
return NULL;
}
x_L = (Number*)malloc(sizeof(Number)*n);
x_U = (Number*)malloc(sizeof(Number)*n);
if (!x_L || !x_U)
{
PyErr_SetString(PyExc_SystemError, "Cannot allocate memory");
return NULL;
}
xldata = (double*)xL->data;
xudata = (double*)xU->data;
for (i = 0; i< n; i++) {
x_L[i] = xldata[i];
x_U[i] = xudata[i];
}
g_L = (Number*)malloc(sizeof(Number)*m);
g_U = (Number*)malloc(sizeof(Number)*m);
if (!g_L || !g_U)
{
PyErr_SetString(PyExc_SystemError, "Cannot allocate memory");
return NULL;
}
gldata = (double*)gL->data;
gudata = (double*)gU->data;
for (i = 0; i< m; i++)
{
g_L[i] = gldata[i];
g_U[i] = gudata[i];
}
/* create the Ipopt Problem */
int C_indexstyle = 0;
logger("[PyIPOPT] nele_hess is %d\n", nele_hess);
IpoptProblem thisnlp = CreateIpoptProblem(n, x_L, x_U, m, g_L, g_U, nele_jac, nele_hess, C_indexstyle, &eval_f, &eval_g, &eval_grad_f, &eval_jac_g, &eval_h);
logger("[PyIPOPT] Problem created");
// AddIpoptStrOption(thisnlp, "max_iter", 200);
problem *object = NULL;
object = PyObject_NEW(problem , &IpoptProblemType);
if (!object) return NULL;
object->nlp = thisnlp;
object->n = n;
object->m = m;
DispatchData *dp = malloc(sizeof(DispatchData));
memcpy((void*)dp, (void*)&myowndata, sizeof(DispatchData));
object->data = dp;
free(x_L);
free(x_U);
free(g_L);
free(g_U);
return (PyObject *)object;
}
static PyObject *pyexctype = NULL, *pyexcval = NULL, *pyexctb = NULL;
static PyObject *PyExc_SolveError = NULL, *PyExc_SolveExceedMaxIter = NULL;
void save_python_exception(void)
{
PyObject *exc = NULL, *val = NULL, *tb = NULL;
PyErr_Fetch(&exc, &val, &tb);
if (NULL == exc) return;
PyErr_NormalizeException(&exc, &val, &tb);
Py_XDECREF(pyexctype);
Py_XDECREF(pyexcval);
Py_XDECREF(pyexctb);
pyexctype = exc;
pyexcval = val;
pyexctb = tb;
}
int restore_python_exception(void)
{
if (!pyexctype) return FALSE;
PyErr_Restore(pyexctype, pyexcval, pyexctb);
pyexctype=pyexcval=pyexctb=NULL;
return TRUE;
}
PyObject *solve(PyObject *self, PyObject *args)
{
enum ApplicationReturnStatus status; /* Solve return code */
int i;
/* Return values */
problem* temp = (problem*)self;
const int n=temp->n;
const int m=temp->m;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
DispatchData* bigfield = (DispatchData*)(temp->data);
npy_intp dX[1] = {n};
npy_intp dL[1] = {m};
PyArrayObject *x, *mL, *mU, *lambda, *con;
Number obj; /* objective value */
PyArrayObject *x0;
PyObject* myuserdata = NULL;
if (!PyArg_ParseTuple(args, "O!|O", &PyArray_Type, &x0, &myuserdata))
{
return NULL;
}
if (myuserdata != NULL)
{
bigfield->userdata = myuserdata;
logger("[PyIPOPT] User specified data field to callback function.\n");
}
if (nlp == NULL)
{
PyErr_SetString(PyExc_ValueError, "nlp objective passed to solve is NULL. Problem created?");
return NULL;
}
/* set some options */
// AddIpoptNumOption(nlp, "tol", 1e-8);
// AddIpoptStrOption(nlp, "mu_strategy", "adaptive");
if (bigfield->eval_h_python == NULL)
{
AddIpoptStrOption(nlp, "hessian_approximation","limited-memory");
//logger("Can't find eval_h callback function\n");
}
/* allocate space for the initial point and set the values */
// logger("n is %d, m is %d\n", n, m);
x = (PyArrayObject *)PyArray_SimpleNew( 1, dX, PyArray_DOUBLE );
Number* newx0 = (Number*)malloc(sizeof(Number)*temp->n);
double* xdata = (double*) x0->data;
for (i =0; i< n; i++)
newx0[i] = xdata[i];
mL = (PyArrayObject *)PyArray_SimpleNew( 1, dX, PyArray_DOUBLE );
mU = (PyArrayObject *)PyArray_SimpleNew( 1, dX, PyArray_DOUBLE );
lambda = (PyArrayObject *)PyArray_SimpleNew( 1, dL, PyArray_DOUBLE );
con = (PyArrayObject *)PyArray_SimpleNew( 1, dL, PyArray_DOUBLE );
// logger("Ready to go\n");
status = IpoptSolve(nlp, newx0, (double*)con->data, &obj,
(double*)lambda->data,
(double*)mL->data,
(double*)mU->data,
(UserDataPtr)bigfield);
// The final parameter is the userdata (void * type)
// For status code, see: IpReturnCodes_inc.h
if (status == Solve_Succeeded ||
status == Solved_To_Acceptable_Level ||
status == User_Requested_Stop ||
status == Maximum_Iterations_Exceeded ) {
logger("Problem solved\n");
double* xdata = (double*) x->data;
for (i =0; i< n; i++)
xdata[i] = newx0[i];
// FreeIpoptProblem(nlp);
if (newx0) free(newx0);
/* A fix for the mem-leak problem */
PyObject *r =
Py_BuildValue( "{sNsNsNsNsNsd}",
"x", PyArray_Return( x ),
"mult_xL", PyArray_Return( mL ),
"mult_xU", PyArray_Return( mU ),
"mult_g", PyArray_Return( lambda ),
"g", con,
"f", obj);
if (!r) return NULL;
if (status != Maximum_Iterations_Exceeded)
return r;
PyErr_SetObject(PyExc_SolveExceedMaxIter, r);
Py_DECREF(r);
return NULL;
}
else {
// FreeIpoptProblem(nlp);
printf("[Error] Ipopt faied in solving problem instance\n");
if (!restore_python_exception())
PyErr_SetString(PyExc_SolveError, "Ipopt search failed");
return NULL;
}
}
PyObject *close_model(PyObject *self, PyObject *args)
{
problem* obj = (problem*) self;
FreeIpoptProblem(obj->nlp);
obj->nlp = NULL;
Py_INCREF(Py_True);
return Py_True;
}
static char PYTEST[] = "TestCreate\n";
static PyObject *test(PyObject *self, PyObject *args)
{
IpoptProblem thisnlp = NULL;
problem *object = NULL;
object = PyObject_NEW(problem , &IpoptProblemType);
if (object != NULL)
object->nlp = thisnlp;
// problem *object = problem_new(thisnlp);
return (PyObject *)object;
}
/* Begin Python Module code section */
static PyMethodDef ipoptMethods[] = {
// { "solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC},
{ "create", create, METH_VARARGS, PYIPOPT_CREATE_DOC},
// { "close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC},
// { "test", test, METH_VARARGS, PYTEST},
{ NULL, NULL }
};
PyMODINIT_FUNC
initpyipopt(void)
{
PyObject* m =
Py_InitModule3("pyipopt", ipoptMethods,
"A hooker between Ipopt and Python");
if (!m) goto error;
import_array( ); /* Initialize the Numarray module. */
/* A segfault will occur if I use numarray without this.. */
PyExc_SolveError = PyErr_NewException("pyipopt.SolveError",
NULL,NULL);
if (!PyExc_SolveError) goto error;
PyExc_SolveExceedMaxIter = PyErr_NewException("pyipopt.SolveExceedMaxIter",
PyExc_SolveError,NULL);
if (!PyExc_SolveExceedMaxIter) goto error;
if (-1 == PyObject_SetAttrString(m,"SolveError",PyExc_SolveError)) goto error;
if (-1 == PyObject_SetAttrString(m,"SolveExceedMaxIter",PyExc_SolveExceedMaxIter)) goto error;
if (PyErr_Occurred())
Py_FatalError("Unable to initialize module pyipopt");
return;
error:
PyErr_Print();
Py_FatalError("Unable to initialize module pyipopt");
}
/* End Python Module code section */