My favorites
▼
|
Sign in
pyipopt
An IPOPT connector to python
Project Home
Wiki
Issues
Source
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
16
attachment: pyipopt.c
(18.0 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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/* 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.
*/
/*
Modifications made by OpenMDAO at NASA Glenn Research Center, 2010 and 2011
*/
#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* set_intermediate_callback (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_SET_INTERMEDIATE_CALLBACK_DOC[] = "set_intermediate_callback(callback_function)\n \
\n \
Set the intermediate callback function. This gets called each iteration.";
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";
static 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:str_option", ¶m, &value))
{
return NULL ;
}
ret = AddIpoptStrOption(nlp, (char*) param, value);
if (ret)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
return PyErr_Format(PyExc_ValueError, "%s is not a valid string option", param);
}
}
static char PYIPOPT_ADD_INT_OPTION_DOC[] = "Set the Int option for Ipopt. See the document for Ipopt for more information.\n";
static 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:int_option", ¶m, &value))
{
return NULL;
}
ret = AddIpoptIntOption(nlp, (char*) param, value);
if (ret)
{
Py_INCREF(Py_True);
return Py_True;
}
else
{
return PyErr_Format(PyExc_ValueError, "%s is not a valid int option", param);
}
}
static char PYIPOPT_ADD_NUM_OPTION_DOC[] = "Set the Number/double option for Ipopt. See the document for Ipopt for more information.\n";
static PyObject *add_num_option(PyObject *self, PyObject *args)
{
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
char* param;
double value;
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
{
return PyErr_Format(PyExc_ValueError, "%s is not a valid num option", param);
}
}
PyMethodDef problem_methods[] = {
{ "solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC},
{ "set_intermediate_callback", set_intermediate_callback, METH_VARARGS, PYIPOPT_SET_INTERMEDIATE_CALLBACK_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;
}
/* had to replace
PyObject_HEAD_INIT(&PyType_Type)
in order to get this to compile on Windows
*/
PyTypeObject IpoptProblemType = {
PyObject_HEAD_INIT(NULL)
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 = NULL;
PyObject *gradf = NULL ;
PyObject *g = NULL ;
PyObject *jacg = NULL ;
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 = NULL ;
PyArrayObject *xU = NULL ;
int m; /* Number of con */
PyArrayObject *gL = NULL ;
PyArrayObject *gU = NULL;
problem *object = NULL;
int nele_jac;
int nele_hess;
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 */
double* xldata, *xudata;
double* gldata, *gudata;
int i;
DispatchData *dp = NULL ;
PyObject *retval = NULL ;
/* 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:pyipoptcreate",
&n, &PyArray_Type, &xL,
&PyArray_Type, &xU,
&m,
&PyArray_Type, &gL,
&PyArray_Type, &gU,
&nele_jac, &nele_hess,
&f, &gradf, &g, &jacg,
&h, &applynew))
{
retval = NULL ;
goto done ;
}
if (!PyCallable_Check(f) ||
!PyCallable_Check(gradf) ||
!PyCallable_Check(g) ||
!PyCallable_Check(jacg))
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for callback functions");
retval = NULL ;
goto done ;
}
myowndata.eval_f_python = f;
myowndata.eval_grad_f_python = gradf;
myowndata.eval_g_python = g;
myowndata.eval_jac_g_python = jacg;
if (h != NULL )
{
if (PyCallable_Check(h))
{
myowndata.eval_h_python = h;
}
else
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function h.");
retval = NULL ;
goto done ;
}
}
else
{
printf("[PyIPOPT] Ipopt will use Hessian approximation.\n");
}
if ( applynew != NULL )
{
if (PyCallable_Check(applynew))
{
myowndata.apply_new_python = applynew;
}
else
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function applynew.");
retval = NULL ;
goto done ;
}
}
if (m <0 || n<0 )
{
PyErr_SetString(PyExc_TypeError, "m or n can't be negative");
retval = NULL ;
goto done ;
}
x_L = (Number*)malloc(sizeof(Number)*n);
x_U = (Number*)malloc(sizeof(Number)*n);
if (!x_L || !x_U) {
retval = PyErr_NoMemory();
goto done ;
}
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_NoMemory();
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;
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"); */
if ( !thisnlp )
{
PyErr_SetString(PyExc_MemoryError,
"Cannot create IpoptProblem instance");
retval = NULL ;
goto done ;
}
object = PyObject_NEW(problem , &IpoptProblemType);
if (object != NULL)
{
object->nlp = thisnlp;
dp = malloc(sizeof(DispatchData));
if ( !dp )
{
retval = PyErr_NoMemory();
goto done ;
}
memcpy((void*)dp, (void*)&myowndata, sizeof(DispatchData));
object->data = dp;
retval = (PyObject *)object;
goto done ;
}
else
{
PyErr_SetString(PyExc_MemoryError,
"Can't create a new Problem instance");
retval = NULL ;
goto done ;
}
done:
/* Clean up and return*/
free(x_L);
free(x_U);
free(g_L);
free(g_U);
return retval ;
}
PyObject *set_intermediate_callback(PyObject *self, PyObject *args)
{
PyObject *intermediate_callback;
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
DispatchData myowndata;
DispatchData* bigfield = (DispatchData*)(temp->data);
/* Init the myowndata field */
myowndata.eval_intermediate_callback_python = NULL;
if (!PyArg_ParseTuple(args, "O", &intermediate_callback))
{
return NULL ;
}
if (!PyCallable_Check(intermediate_callback))
{
PyErr_SetString(PyExc_TypeError,
"Need a callable object for function!");
return NULL ;
}
else
{
bigfield->eval_intermediate_callback_python = intermediate_callback ;
/* Put a Python function object into this data structure */
/* myowndata.eval_intermediate_callback_python = intermediate_callback; */
/* DispatchData *dp = malloc(sizeof(DispatchData)); */
/* memcpy((void*)dp, (void*)&myowndata, sizeof(DispatchData)); */
/* bigfield = dp; */
/* printf( "qqq: inside set_intermediate_callback, bigfield is %p\n", bigfield ) ; */
/* printf("[PyIPOPT] User specified data field to callback function.\n"); */
SetIntermediateCallback( nlp, eval_intermediate_callback ) ;
Py_INCREF(Py_True);
return Py_True;
}
}
PyObject *solve(PyObject *self, PyObject *args)
{
enum ApplicationReturnStatus status; /* Solve return code */
int i;
int n;
/* Return values */
problem* temp = (problem*)self;
IpoptProblem nlp = (IpoptProblem)(temp->nlp);
DispatchData* bigfield = (DispatchData*)(temp->data);
/* int dX[1]; */
npy_intp dX[1];
PyArrayObject *x = NULL , *mL = NULL , *mU = NULL ;
Number obj; /* objective value */
PyObject* retval = NULL ;
PyArrayObject *x0 = NULL ;
PyObject* myuserdata = NULL;
Number* newx0 = NULL ;
if (!PyArg_ParseTuple(args, "O!|O", &PyArray_Type, &x0, &myuserdata))
{
retval = NULL ;
goto done;
}
if (myuserdata != NULL)
{
bigfield->userdata = myuserdata;
/* printf("[PyIPOPT] User specified data field to callback function.\n"); */
}
if (nlp == NULL)
{
PyErr_SetString(PyExc_TypeError,
"nlp objective passed to solve is NULL\n Problem created?\n");
retval = NULL ;
goto done ;
}
if (bigfield->eval_h_python == NULL)
{
AddIpoptStrOption(nlp, "hessian_approximation","limited-memory");
/* printf("Can't find eval_h callback function\n"); */
}
/* allocate space for the initial point and set the values */
npy_intp* dim = ((PyArrayObject*)x0)->dimensions;
n = dim[0];
dX[0] = n;
x = (PyArrayObject *)PyArray_SimpleNew( 1, dX, PyArray_DOUBLE );
if (!x)
{
retval = PyErr_NoMemory();
goto done ;
}
newx0 = (Number*)malloc(sizeof(Number)*n);
if (!newx0)
{
retval = PyErr_NoMemory();
goto done ;
}
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 );
freopen( "/dev/null", "w", stdout ) ;
status = IpoptSolve(nlp, newx0, NULL, &obj, NULL, (double*)mL->data, (double*)mU->data, (UserDataPtr)bigfield);
/* The final parameter is the userdata (void * type) */
/* For status code, see IpReturnCodes_inc.h in Ipopt */
if (status == Solve_Succeeded || status == Solved_To_Acceptable_Level )
{
double* xdata = (double*) x->data;
for (i =0; i< n; i++)
xdata[i] = newx0[i];
/* FreeIpoptProblem(nlp); */
}
retval = Py_BuildValue(
"NNNdO",
PyArray_Return( x ),
PyArray_Return( mL ),
PyArray_Return( mU ),
obj,
Py_BuildValue("i", status)
);
goto done ;
done:
/* clean up and return */
if ( retval == NULL ) {
Py_XDECREF(x);
Py_XDECREF(mL);
Py_XDECREF(mU);
}
free(newx0);
return retval ;
}
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)
{
Py_InitModule3("pyipopt", ipoptMethods,
"A hook between Ipopt and Python");
import_array( ); /* Initialize the Numarray module. */
/* A segfault will occur if I use numarray without this.. */
if (PyErr_Occurred())
Py_FatalError("Unable to initialize module pyipopt");
return;
}
/* End Python Module code section */
Powered by
Google Project Hosting