Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

indentify

  • Loading branch information...
commit a07bd34c233e48b1875c0ec8077cad7528b2f4a1 1 parent 0af136e
Eric Xu authored

Showing 3 changed files with 533 additions and 479 deletions. Show diff stats Hide diff stats

  1. +18 14 src/callback.c
  2. +14 24 src/hook.h
  3. +501 441 src/pyipopt.c
32 src/callback.c
@@ -45,15 +45,18 @@ logger (char *str)
45 45
46 46
47 47
48   -void logger(const char* fmt,...) {
49   - if (user_log_level == VERBOSE) {
50   - va_list ap;
51   - va_start(ap,fmt);
52   - vprintf(fmt, ap);
53   - va_end(ap);
54   - printf("\n");
55   - fflush(stdout);
56   - }
  48 +void
  49 +logger (const char *fmt, ...)
  50 +{
  51 + if (user_log_level == VERBOSE)
  52 + {
  53 + va_list ap;
  54 + va_start (ap, fmt);
  55 + vprintf (fmt, ap);
  56 + va_end (ap);
  57 + printf ("\n");
  58 + fflush (stdout);
  59 + }
57 60 }
58 61
59 62 Bool
@@ -147,7 +150,7 @@ eval_f (Index n, Number * x, Bool new_x, Number * obj_value, UserDataPtr data)
147 150
148 151 // import_array ();
149 152
150   - import_array1(FALSE);
  153 + import_array1 (FALSE);
151 154 PyObject *arrayx =
152 155 PyArray_SimpleNewFromData (1, dims, PyArray_DOUBLE, (char *) x);
153 156 if (!arrayx)
@@ -221,7 +224,7 @@ eval_grad_f (Index n, Number * x, Bool new_x,
221 224 dims[0] = n;
222 225 // import_array ();
223 226
224   - import_array1(FALSE);
  227 + import_array1 (FALSE);
225 228
226 229 /*
227 230 * PyObject *arrayx = PyArray_FromDimsAndData(1, dims, PyArray_DOUBLE
@@ -297,7 +300,7 @@ eval_g (Index n, Number * x, Bool new_x,
297 300 dims[0] = n;
298 301 // import_array ();
299 302
300   - import_array1(FALSE);
  303 + import_array1 (FALSE);
301 304
302 305 /*
303 306 * PyObject *arrayx = PyArray_FromDimsAndData(1, dims, PyArray_DOUBLE
@@ -380,7 +383,7 @@ eval_jac_g (Index n, Number * x, Bool new_x,
380 383 if (values == NULL)
381 384 {
382 385 /* import_array (); */
383   - import_array1(FALSE);
  386 + import_array1 (FALSE);
384 387
385 388 PyObject *arrayx =
386 389 PyArray_SimpleNewFromData (1, dims, PyArray_DOUBLE, (char *) x);
@@ -413,7 +416,8 @@ eval_jac_g (Index n, Number * x, Bool new_x,
413 416
414 417 if (!row || !col || !PyArray_Check (row) || !PyArray_Check (col))
415 418 {
416   - logger ("[Error] there are problems with row or col in eval_jac_g.\n");
  419 + logger
  420 + ("[Error] there are problems with row or col in eval_jac_g.\n");
417 421 PyErr_Print ();
418 422 }
419 423 rowd = (long *) row->data;
38 src/hook.h
@@ -10,30 +10,20 @@
10 10 #define PY_IPOPT_HOOK_
11 11
12 12 // A series of callback functions used by Ipopt C Interface
13   -Bool eval_f(
14   - Index n,
15   - Number* x,
16   - Bool new_x,
17   - Number* obj_value,
18   - UserDataPtr user_data);
  13 +Bool eval_f (Index n,
  14 + Number * x,
  15 + Bool new_x, Number * obj_value, UserDataPtr user_data);
19 16
20   -Bool eval_grad_f(
21   - Index n,
22   - Number* x,
23   - Bool new_x,
24   - Number* grad_f,
25   - UserDataPtr user_data);
  17 +Bool eval_grad_f (Index n,
  18 + Number * x,
  19 + Bool new_x, Number * grad_f, UserDataPtr user_data);
26 20
27   -Bool eval_g(
28   - Index n,
29   - Number* x,
30   - Bool new_x,
31   - Index m,
32   - Number * g,
33   - UserDataPtr user_data);
  21 +Bool eval_g (Index n,
  22 + Number * x,
  23 + Bool new_x, Index m, Number * g, UserDataPtr user_data);
34 24
35   -Bool eval_jac_g(Index n, Number * x, Bool new_x,
36   - Index m, Index nele_jac,
  25 +Bool eval_jac_g (Index n, Number * x, Bool new_x,
  26 + Index m, Index nele_jac,
37 27 Index * iRow, Index * jCol, Number * values,
38 28 UserDataPtr user_data);
39 29
@@ -64,14 +54,14 @@ typedef struct
64 54 } DispatchData;
65 55
66 56
67   -PyObject *problem_getattr(PyObject * self, char *attrname);
  57 +PyObject *problem_getattr (PyObject * self, char *attrname);
68 58
69 59 /* Logging */
70 60 #define VERBOSE 2
71 61 #define IPOPT_OUTPUT 1
72 62 #define TERSE 0
73 63 extern int user_log_level;
74   -void logger (const char *fmt,...);
  64 +void logger (const char *fmt, ...);
75 65
76 66 typedef struct
77 67 {
@@ -79,4 +69,4 @@ typedef struct
79 69 DispatchData *data;
80 70 } problem;
81 71
82   -#endif // PY_IPOPT_HOOK_
  72 +#endif // PY_IPOPT_HOOK_
942 src/pyipopt.c
@@ -10,14 +10,16 @@ int user_log_level = TERSE;
10 10
11 11 /* Object Section */
12 12 /* sig of this is void foo(PyO*) */
13   -static void problem_dealloc(PyObject * self) {
14   - problem *temp = (problem *) self;
15   - free(temp->data);
  13 +static void
  14 +problem_dealloc (PyObject * self)
  15 +{
  16 + problem *temp = (problem *) self;
  17 + free (temp->data);
16 18 }
17 19
18   -PyObject *solve(PyObject * self, PyObject * args);
19   -PyObject *set_intermediate_callback(PyObject * self, PyObject * args);
20   -PyObject *close_model(PyObject * self, PyObject * args);
  20 +PyObject *solve (PyObject * self, PyObject * args);
  21 +PyObject *set_intermediate_callback (PyObject * self, PyObject * args);
  22 +PyObject *close_model (PyObject * self, PyObject * args);
21 23
22 24 static char PYIPOPT_SOLVE_DOC[] = "solve(x) -> (x, ml, mu, obj)\n \
23 25 \n \
@@ -26,7 +28,7 @@ static char PYIPOPT_SOLVE_DOC[] = "solve(x) -> (x, ml, mu, obj)\n \
26 28 bound for multiplier, final objective function obj, \n \
27 29 and the return status of ipopt. \n";
28 30
29   -static char PYIPOPT_SET_INTERMEDIATE_CALLBACK_DOC[] =
  31 +static char PYIPOPT_SET_INTERMEDIATE_CALLBACK_DOC[] =
30 32 "set_intermediate_callback(callback_function)\n \
31 33 \n \
32 34 Set the intermediate callback function. \
@@ -38,130 +40,155 @@ static char PYIPOPT_ADD_STR_OPTION_DOC[] =
38 40 "Set the String option for Ipopt. See the document for Ipopt for more information.\n";
39 41
40 42
41   -static PyObject * add_str_option(PyObject * self, PyObject * args) {
42   - problem *temp = (problem *) self;
43   - IpoptProblem nlp = (IpoptProblem) (temp->nlp);
44   - char *param;
45   - char *value;
46   - Bool ret;
47   -
48   - if (!PyArg_ParseTuple(args, "ss:str_option", &param, &value)) {
49   - return NULL;
50   - }
51   - ret = AddIpoptStrOption(nlp, (char *) param, value);
52   - if (ret) {
53   - Py_INCREF(Py_True);
54   - return Py_True;
55   - } else {
56   - return PyErr_Format(PyExc_ValueError, "%s is not a valid string option", param);
57   - }
  43 +static PyObject *
  44 +add_str_option (PyObject * self, PyObject * args)
  45 +{
  46 + problem *temp = (problem *) self;
  47 + IpoptProblem nlp = (IpoptProblem) (temp->nlp);
  48 + char *param;
  49 + char *value;
  50 + Bool ret;
  51 +
  52 + if (!PyArg_ParseTuple (args, "ss:str_option", &param, &value))
  53 + {
  54 + return NULL;
  55 + }
  56 + ret = AddIpoptStrOption (nlp, (char *) param, value);
  57 + if (ret)
  58 + {
  59 + Py_INCREF (Py_True);
  60 + return Py_True;
  61 + }
  62 + else
  63 + {
  64 + return PyErr_Format (PyExc_ValueError,
  65 + "%s is not a valid string option", param);
  66 + }
58 67 }
59 68
60 69
61   -static char PYIPOPT_ADD_INT_OPTION_DOC[] =
  70 +static char PYIPOPT_ADD_INT_OPTION_DOC[] =
62 71 "Set the Int option for Ipopt. See the document for Ipopt for more information.\n";
63 72
64 73 static PyObject *
65   -add_int_option(PyObject * self, PyObject * args)
  74 +add_int_option (PyObject * self, PyObject * args)
66 75 {
67 76
68   - problem *temp = (problem *) self;
69   - IpoptProblem nlp = (IpoptProblem) (temp->nlp);
70   -
71   - char *param;
72   - int value;
73   -
74   - Bool ret;
75   -
76   - if (!PyArg_ParseTuple(args, "si:int_option", &param, &value)) {
77   - return NULL;
78   - }
79   - ret = AddIpoptIntOption(nlp, (char *) param, value);
80   - if (ret) {
81   - Py_INCREF(Py_True);
82   - return Py_True;
83   - } else {
84   - return PyErr_Format(PyExc_ValueError, "%s is not a valid int option", param);
85   - }
  77 + problem *temp = (problem *) self;
  78 + IpoptProblem nlp = (IpoptProblem) (temp->nlp);
  79 +
  80 + char *param;
  81 + int value;
  82 +
  83 + Bool ret;
  84 +
  85 + if (!PyArg_ParseTuple (args, "si:int_option", &param, &value))
  86 + {
  87 + return NULL;
  88 + }
  89 + ret = AddIpoptIntOption (nlp, (char *) param, value);
  90 + if (ret)
  91 + {
  92 + Py_INCREF (Py_True);
  93 + return Py_True;
  94 + }
  95 + else
  96 + {
  97 + return PyErr_Format (PyExc_ValueError, "%s is not a valid int option",
  98 + param);
  99 + }
86 100 }
87 101
88 102
89   -static char PYIPOPT_ADD_NUM_OPTION_DOC[] =
  103 +static char PYIPOPT_ADD_NUM_OPTION_DOC[] =
90 104 "Set the Number/double option for Ipopt. See the document for Ipopt for more information.\n";
91 105
92 106 static PyObject *
93   -add_num_option(PyObject * self, PyObject * args)
  107 +add_num_option (PyObject * self, PyObject * args)
94 108 {
95   - problem *temp = (problem *) self;
96   - IpoptProblem nlp = (IpoptProblem) (temp->nlp);
97   -
98   - char *param;
99   - double value;
100   -
101   - Bool ret;
102   -
103   - if (!PyArg_ParseTuple(args, "sd:num_option", &param, &value)) {
104   - return NULL;
105   - }
106   - ret = AddIpoptNumOption(nlp, (char *) param, value);
107   - if (ret) {
108   - Py_INCREF(Py_True);
109   - return Py_True;
110   - } else {
111   - return PyErr_Format(PyExc_ValueError, "%s is not a valid num option", param);
112   - }
  109 + problem *temp = (problem *) self;
  110 + IpoptProblem nlp = (IpoptProblem) (temp->nlp);
  111 +
  112 + char *param;
  113 + double value;
  114 +
  115 + Bool ret;
  116 +
  117 + if (!PyArg_ParseTuple (args, "sd:num_option", &param, &value))
  118 + {
  119 + return NULL;
  120 + }
  121 + ret = AddIpoptNumOption (nlp, (char *) param, value);
  122 + if (ret)
  123 + {
  124 + Py_INCREF (Py_True);
  125 + return Py_True;
  126 + }
  127 + else
  128 + {
  129 + return PyErr_Format (PyExc_ValueError, "%s is not a valid num option",
  130 + param);
  131 + }
113 132 }
114 133
115 134
116 135
117   -PyMethodDef problem_methods[] = {
118   - {"solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC},
119   - {"set_intermediate_callback", set_intermediate_callback, METH_VARARGS, PYIPOPT_SET_INTERMEDIATE_CALLBACK_DOC},
120   - {"close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC},
121   - {"int_option", add_int_option, METH_VARARGS, PYIPOPT_ADD_INT_OPTION_DOC},
122   - {"str_option", add_str_option, METH_VARARGS, PYIPOPT_ADD_STR_OPTION_DOC},
123   - {"num_option", add_num_option, METH_VARARGS, PYIPOPT_ADD_NUM_OPTION_DOC},
124   - {NULL, NULL},
  136 +PyMethodDef problem_methods[] = {
  137 + {"solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC}
  138 + ,
  139 + {"set_intermediate_callback", set_intermediate_callback, METH_VARARGS,
  140 + PYIPOPT_SET_INTERMEDIATE_CALLBACK_DOC}
  141 + ,
  142 + {"close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC}
  143 + ,
  144 + {"int_option", add_int_option, METH_VARARGS, PYIPOPT_ADD_INT_OPTION_DOC}
  145 + ,
  146 + {"str_option", add_str_option, METH_VARARGS, PYIPOPT_ADD_STR_OPTION_DOC}
  147 + ,
  148 + {"num_option", add_num_option, METH_VARARGS, PYIPOPT_ADD_NUM_OPTION_DOC}
  149 + ,
  150 + {NULL, NULL}
  151 + ,
125 152 };
126 153
127   -PyObject *
128   -problem_getattr(PyObject * self, char *attrname)
  154 +PyObject *
  155 +problem_getattr (PyObject * self, char *attrname)
129 156 {
130   - PyObject *result = NULL;
131   - result = Py_FindMethod(problem_methods, self, attrname);
132   - return result;
  157 + PyObject *result = NULL;
  158 + result = Py_FindMethod (problem_methods, self, attrname);
  159 + return result;
133 160 }
134 161
135 162 /*
136 163 * had to replace PyObject_HEAD_INIT(&PyType_Type) in order to get this to
137 164 * compile on Windows
138 165 */
139   -PyTypeObject IpoptProblemType = {
140   - PyObject_HEAD_INIT(NULL)
141   - 0, /* ob_size */
142   - "pyipopt.Problem", /* tp_name */
143   - sizeof(problem), /* tp_basicsize */
144   - 0, /* tp_itemsize */
145   - problem_dealloc, /* tp_dealloc */
146   - 0, /* tp_print */
147   - problem_getattr, /* tp_getattr */
148   - 0, /* tp_setattr */
149   - 0, /* tp_compare */
150   - 0, /* tp_repr */
151   - 0, /* tp_as_number */
152   - 0, /* tp_as_sequence */
153   - 0, /* tp_as_mapping */
154   - 0, /* tp_hash */
155   - 0, /* tp_call */
156   - 0, /* tp_str */
157   - 0, /* tp_getattro */
158   - 0, /* tp_setattro */
159   - 0, /* tp_as_buffer */
160   - Py_TPFLAGS_DEFAULT, /* tp_flags */
161   - "The IPOPT problem object in python", /* tp_doc */
  166 +PyTypeObject IpoptProblemType = {
  167 + PyObject_HEAD_INIT (NULL) 0, /* ob_size */
  168 + "pyipopt.Problem", /* tp_name */
  169 + sizeof (problem), /* tp_basicsize */
  170 + 0, /* tp_itemsize */
  171 + problem_dealloc, /* tp_dealloc */
  172 + 0, /* tp_print */
  173 + problem_getattr, /* tp_getattr */
  174 + 0, /* tp_setattr */
  175 + 0, /* tp_compare */
  176 + 0, /* tp_repr */
  177 + 0, /* tp_as_number */
  178 + 0, /* tp_as_sequence */
  179 + 0, /* tp_as_mapping */
  180 + 0, /* tp_hash */
  181 + 0, /* tp_call */
  182 + 0, /* tp_str */
  183 + 0, /* tp_getattro */
  184 + 0, /* tp_setattro */
  185 + 0, /* tp_as_buffer */
  186 + Py_TPFLAGS_DEFAULT, /* tp_flags */
  187 + "The IPOPT problem object in python", /* tp_doc */
162 188 };
163 189
164   -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 \
  190 +static char PYIPOPT_CREATE_DOC[] =
  191 + "create(n, xl, xu, m, gl, gu, nnzj, nnzh, eval_f, eval_grad_f, eval_g, eval_jac_g) -> Boolean\n \
165 192 \n \
166 193 Create a problem instance and return True if succeed \n \
167 194 \n \
@@ -200,364 +227,397 @@ static char PYIPOPT_LOG_DOC[] = "set_loglevel(level)\n \
200 227 2: Verbose, logs for both ipopt and pyipopt. \n";
201 228
202 229 static PyObject *
203   -set_loglevel(PyObject* obj, PyObject * args)
  230 +set_loglevel (PyObject * obj, PyObject * args)
204 231 {
205   - int l;
206   - if (!PyArg_ParseTuple(args, "i", &l)) {
207   - printf("l is %d \n", l);
208   - return NULL;
209   - }
210   - if (l<0 || l >2) {
211   - return NULL;
212   - }
213   - user_log_level = l;
214   - Py_INCREF(Py_True);
215   - return Py_True;
216   -}
  232 + int l;
  233 + if (!PyArg_ParseTuple (args, "i", &l))
  234 + {
  235 + printf ("l is %d \n", l);
  236 + return NULL;
  237 + }
  238 + if (l < 0 || l > 2)
  239 + {
  240 + return NULL;
  241 + }
  242 + user_log_level = l;
  243 + Py_INCREF (Py_True);
  244 + return Py_True;
  245 +}
217 246
218 247 static PyObject *
219   -create(PyObject * obj, PyObject * args)
  248 +create (PyObject * obj, PyObject * args)
220 249 {
221   - PyObject *f = NULL;
222   - PyObject *gradf = NULL;
223   - PyObject *g = NULL;
224   - PyObject *jacg = NULL;
225   - PyObject *h = NULL;
226   - PyObject *applynew = NULL;
227   -
228   - DispatchData myowndata;
229   -
230   - /*
231   - * I have to create a new python object here, return this python
232   - * object
233   - */
234   -
235   - int n; /* Number of var */
236   - PyArrayObject *xL = NULL;
237   - PyArrayObject *xU = NULL;
238   - int m; /* Number of con */
239   - PyArrayObject *gL = NULL;
240   - PyArrayObject *gU = NULL;
241   -
242   - problem *object = NULL;
243   -
244   - int nele_jac;
245   - int nele_hess;
246   -
247   - Number *x_L = NULL; /* lower bounds on x */
248   - Number *x_U = NULL; /* upper bounds on x */
249   - Number *g_L = NULL; /* lower bounds on g */
250   - Number *g_U = NULL; /* upper bounds on g */
251   -
252   - double *xldata, *xudata;
253   - double *gldata, *gudata;
254   -
255   - int i;
256   -
257   - DispatchData *dp = NULL;
258   -
259   - PyObject *retval = NULL;
260   -
261   - /* Init the myowndata field */
262   - myowndata.eval_f_python = NULL;
263   - myowndata.eval_grad_f_python = NULL;
264   - myowndata.eval_g_python = NULL;
265   - myowndata.eval_jac_g_python = NULL;
266   - myowndata.eval_h_python = NULL;
267   - myowndata.apply_new_python = NULL;
268   - myowndata.userdata = NULL;
269   -
270   - /* "O!", &PyArray_Type &a_x */
271   - if (!PyArg_ParseTuple(args, "iO!O!iO!O!iiOOOO|OO:pyipoptcreate",
272   - &n, &PyArray_Type, &xL,
273   - &PyArray_Type, &xU,
274   - &m,
275   - &PyArray_Type, &gL,
276   - &PyArray_Type, &gU,
277   - &nele_jac, &nele_hess,
278   - &f, &gradf, &g, &jacg,
279   - &h, &applynew)) {
280   - retval = NULL;
281   - goto done;
  250 + PyObject *f = NULL;
  251 + PyObject *gradf = NULL;
  252 + PyObject *g = NULL;
  253 + PyObject *jacg = NULL;
  254 + PyObject *h = NULL;
  255 + PyObject *applynew = NULL;
  256 +
  257 + DispatchData myowndata;
  258 +
  259 + /*
  260 + * I have to create a new python object here, return this python
  261 + * object
  262 + */
  263 +
  264 + int n; /* Number of var */
  265 + PyArrayObject *xL = NULL;
  266 + PyArrayObject *xU = NULL;
  267 + int m; /* Number of con */
  268 + PyArrayObject *gL = NULL;
  269 + PyArrayObject *gU = NULL;
  270 +
  271 + problem *object = NULL;
  272 +
  273 + int nele_jac;
  274 + int nele_hess;
  275 +
  276 + Number *x_L = NULL; /* lower bounds on x */
  277 + Number *x_U = NULL; /* upper bounds on x */
  278 + Number *g_L = NULL; /* lower bounds on g */
  279 + Number *g_U = NULL; /* upper bounds on g */
  280 +
  281 + double *xldata, *xudata;
  282 + double *gldata, *gudata;
  283 +
  284 + int i;
  285 +
  286 + DispatchData *dp = NULL;
  287 +
  288 + PyObject *retval = NULL;
  289 +
  290 + /* Init the myowndata field */
  291 + myowndata.eval_f_python = NULL;
  292 + myowndata.eval_grad_f_python = NULL;
  293 + myowndata.eval_g_python = NULL;
  294 + myowndata.eval_jac_g_python = NULL;
  295 + myowndata.eval_h_python = NULL;
  296 + myowndata.apply_new_python = NULL;
  297 + myowndata.userdata = NULL;
  298 +
  299 + /* "O!", &PyArray_Type &a_x */
  300 + if (!PyArg_ParseTuple (args, "iO!O!iO!O!iiOOOO|OO:pyipoptcreate",
  301 + &n, &PyArray_Type, &xL,
  302 + &PyArray_Type, &xU,
  303 + &m,
  304 + &PyArray_Type, &gL,
  305 + &PyArray_Type, &gU,
  306 + &nele_jac, &nele_hess,
  307 + &f, &gradf, &g, &jacg, &h, &applynew))
  308 + {
  309 + retval = NULL;
  310 + goto done;
  311 + }
  312 + if (!PyCallable_Check (f) ||
  313 + !PyCallable_Check (gradf) ||
  314 + !PyCallable_Check (g) || !PyCallable_Check (jacg))
  315 + {
  316 + PyErr_SetString (PyExc_TypeError,
  317 + "Need a callable object for callback functions");
  318 + retval = NULL;
  319 + goto done;
  320 + }
  321 + myowndata.eval_f_python = f;
  322 + myowndata.eval_grad_f_python = gradf;
  323 + myowndata.eval_g_python = g;
  324 + myowndata.eval_jac_g_python = jacg;
  325 +
  326 + if (h != NULL)
  327 + {
  328 + if (PyCallable_Check (h))
  329 + {
  330 + myowndata.eval_h_python = h;
282 331 }
283   - if (!PyCallable_Check(f) ||
284   - !PyCallable_Check(gradf) ||
285   - !PyCallable_Check(g) ||
286   - !PyCallable_Check(jacg)) {
287   - PyErr_SetString(PyExc_TypeError,
288   - "Need a callable object for callback functions");
289   - retval = NULL;
290   - goto done;
  332 + else
  333 + {
  334 + PyErr_SetString (PyExc_TypeError,
  335 + "Need a callable object for function h.");
  336 + retval = NULL;
  337 + goto done;
291 338 }
292   - myowndata.eval_f_python = f;
293   - myowndata.eval_grad_f_python = gradf;
294   - myowndata.eval_g_python = g;
295   - myowndata.eval_jac_g_python = jacg;
296   -
297   - if (h != NULL) {
298   - if (PyCallable_Check(h)) {
299   - myowndata.eval_h_python = h;
300   - } else {
301   - PyErr_SetString(PyExc_TypeError,
302   - "Need a callable object for function h.");
303   - retval = NULL;
304   - goto done;
305   - }
306   - } else {
307   - logger("[PyIPOPT] Ipopt will use Hessian approximation.\n");
  339 + }
  340 + else
  341 + {
  342 + logger ("[PyIPOPT] Ipopt will use Hessian approximation.\n");
  343 + }
  344 +
  345 + if (applynew != NULL)
  346 + {
  347 + if (PyCallable_Check (applynew))
  348 + {
  349 + myowndata.apply_new_python = applynew;
308 350 }
309   -
310   - if (applynew != NULL) {
311   - if (PyCallable_Check(applynew)) {
312   - myowndata.apply_new_python = applynew;
313   - } else {
314   - PyErr_SetString(PyExc_TypeError,
  351 + else
  352 + {
  353 + PyErr_SetString (PyExc_TypeError,
315 354 "Need a callable object for function applynew.");
316   - retval = NULL;
317   - goto done;
318   - }
319   - }
320   - if (m < 0 || n < 0) {
321   - PyErr_SetString(PyExc_TypeError, "m or n can't be negative");
322   - retval = NULL;
323   - goto done;
  355 + retval = NULL;
  356 + goto done;
324 357 }
325   - x_L = (Number *) malloc(sizeof(Number) * n);
326   - x_U = (Number *) malloc(sizeof(Number) * n);
327   - if (!x_L || !x_U) {
328   - retval = PyErr_NoMemory();
329   - goto done;
330   - }
331   - xldata = (double *) xL->data;
332   - xudata = (double *) xU->data;
333   - for (i = 0; i < n; i++) {
334   - x_L[i] = xldata[i];
335   - x_U[i] = xudata[i];
336   - }
337   -
338   - g_L = (Number *) malloc(sizeof(Number) * m);
339   - g_U = (Number *) malloc(sizeof(Number) * m);
340   - if (!g_L || !g_U)
341   - PyErr_NoMemory();
342   -
343   - gldata = (double *) gL->data;
344   - gudata = (double *) gU->data;
345   -
346   - for (i = 0; i < m; i++) {
347   - g_L[i] = gldata[i];
348   - g_U[i] = gudata[i];
349   - }
350   -
351   - /* create the Ipopt Problem */
352   -
353   - int C_indexstyle = 0;
354   - IpoptProblem thisnlp = CreateIpoptProblem(n,
355   - x_L, x_U, m, g_L, g_U,
356   - nele_jac, nele_hess, C_indexstyle,
  358 + }
  359 + if (m < 0 || n < 0)
  360 + {
  361 + PyErr_SetString (PyExc_TypeError, "m or n can't be negative");
  362 + retval = NULL;
  363 + goto done;
  364 + }
  365 + x_L = (Number *) malloc (sizeof (Number) * n);
  366 + x_U = (Number *) malloc (sizeof (Number) * n);
  367 + if (!x_L || !x_U)
  368 + {
  369 + retval = PyErr_NoMemory ();
  370 + goto done;
  371 + }
  372 + xldata = (double *) xL->data;
  373 + xudata = (double *) xU->data;
  374 + for (i = 0; i < n; i++)
  375 + {
  376 + x_L[i] = xldata[i];
  377 + x_U[i] = xudata[i];
  378 + }
  379 +
  380 + g_L = (Number *) malloc (sizeof (Number) * m);
  381 + g_U = (Number *) malloc (sizeof (Number) * m);
  382 + if (!g_L || !g_U)
  383 + PyErr_NoMemory ();
  384 +
  385 + gldata = (double *) gL->data;
  386 + gudata = (double *) gU->data;
  387 +
  388 + for (i = 0; i < m; i++)
  389 + {
  390 + g_L[i] = gldata[i];
  391 + g_U[i] = gudata[i];
  392 + }
  393 +
  394 + /* create the Ipopt Problem */
  395 +
  396 + int C_indexstyle = 0;
  397 + IpoptProblem thisnlp = CreateIpoptProblem (n,
  398 + x_L, x_U, m, g_L, g_U,
  399 + nele_jac, nele_hess,
  400 + C_indexstyle,
357 401 &eval_f, &eval_g, &eval_grad_f,
358   - &eval_jac_g, &eval_h);
359   - logger("[PyIPOPT] Problem created");
360   - if (!thisnlp) {
361   - PyErr_SetString(PyExc_MemoryError,
362   - "Cannot create IpoptProblem instance");
363   - retval = NULL;
364   - goto done;
365   - }
366   - object = PyObject_NEW(problem, &IpoptProblemType);
367   -
368   - if (object != NULL) {
369   - object->nlp = thisnlp;
370   - dp = malloc(sizeof(DispatchData));
371   - if (!dp) {
372   - retval = PyErr_NoMemory();
373   - goto done;
374   - }
375   - memcpy((void *) dp, (void *) &myowndata, sizeof(DispatchData));
376   - object->data = dp;
377   - retval = (PyObject *) object;
378   - goto done;
379   - } else {
380   - PyErr_SetString(PyExc_MemoryError,
381   - "Can't create a new Problem instance");
382   - retval = NULL;
383   - goto done;
  402 + &eval_jac_g, &eval_h);
  403 + logger ("[PyIPOPT] Problem created");
  404 + if (!thisnlp)
  405 + {
  406 + PyErr_SetString (PyExc_MemoryError,
  407 + "Cannot create IpoptProblem instance");
  408 + retval = NULL;
  409 + goto done;
  410 + }
  411 + object = PyObject_NEW (problem, &IpoptProblemType);
  412 +
  413 + if (object != NULL)
  414 + {
  415 + object->nlp = thisnlp;
  416 + dp = malloc (sizeof (DispatchData));
  417 + if (!dp)
  418 + {
  419 + retval = PyErr_NoMemory ();
  420 + goto done;
384 421 }
  422 + memcpy ((void *) dp, (void *) &myowndata, sizeof (DispatchData));
  423 + object->data = dp;
  424 + retval = (PyObject *) object;
  425 + goto done;
  426 + }
  427 + else
  428 + {
  429 + PyErr_SetString (PyExc_MemoryError,
  430 + "Can't create a new Problem instance");
  431 + retval = NULL;
  432 + goto done;
  433 + }
385 434
386 435 done:
387   - /* Clean up and return */
388   - free(x_L);
389   - free(x_U);
390   - free(g_L);
391   - free(g_U);
  436 + /* Clean up and return */
  437 + free (x_L);
  438 + free (x_U);
  439 + free (g_L);
  440 + free (g_U);
392 441
393   - return retval;
  442 + return retval;
394 443 }
395 444
396 445
397 446
398   -PyObject *
399   -set_intermediate_callback(PyObject * self, PyObject * args)
  447 +PyObject *
  448 +set_intermediate_callback (PyObject * self, PyObject * args)
400 449 {
401   - PyObject *intermediate_callback;
402   - problem *temp = (problem *) self;
403   - IpoptProblem nlp = (IpoptProblem) (temp->nlp);
404   - DispatchData myowndata;
405   - DispatchData *bigfield = (DispatchData *) (temp->data);
406   -
407   - /* Init the myowndata field */
408   - myowndata.eval_intermediate_callback_python = NULL;
409   -
410   - if (!PyArg_ParseTuple(args, "O", &intermediate_callback)) {
411   - return NULL;
412   - }
413   - if (!PyCallable_Check(intermediate_callback)) {
414   - PyErr_SetString(PyExc_TypeError,
415   - "Need a callable object for function!");
416   - return NULL;
417   - } else {
418   -
419   - bigfield->eval_intermediate_callback_python = intermediate_callback;
420   -
421   - /* Put a Python function object into this data structure */
422   - /*
423   - * myowndata.eval_intermediate_callback_python =
424   - * intermediate_callback;
425   - */
426   -
427   - /* DispatchData *dp = malloc(sizeof(DispatchData)); */
428   - /*
429   - * memcpy((void*)dp, (void*)&myowndata,
430   - * sizeof(DispatchData));
431   - */
432   - /* bigfield = dp; */
433   - /*
434   - * logger( "qqq: inside set_intermediate_callback, bigfield
435   - * is %p\n", bigfield ) ;
436   - */
437   - /*
438   - * logger("[PyIPOPT] User specified data field to callback
439   - * function.\n");
440   - */
441   -
442   -
443   - SetIntermediateCallback(nlp, eval_intermediate_callback);
444   - Py_INCREF(Py_True);
445   - return Py_True;
446   - }
  450 + PyObject *intermediate_callback;
  451 + problem *temp = (problem *) self;
  452 + IpoptProblem nlp = (IpoptProblem) (temp->nlp);
  453 + DispatchData myowndata;
  454 + DispatchData *bigfield = (DispatchData *) (temp->data);
  455 +
  456 + /* Init the myowndata field */
  457 + myowndata.eval_intermediate_callback_python = NULL;
  458 +
  459 + if (!PyArg_ParseTuple (args, "O", &intermediate_callback))
  460 + {
  461 + return NULL;
  462 + }
  463 + if (!PyCallable_Check (intermediate_callback))
  464 + {
  465 + PyErr_SetString (PyExc_TypeError,
  466 + "Need a callable object for function!");
  467 + return NULL;
  468 + }
  469 + else
  470 + {
  471 +
  472 + bigfield->eval_intermediate_callback_python = intermediate_callback;
  473 +
  474 + /* Put a Python function object into this data structure */
  475 + /*
  476 + * myowndata.eval_intermediate_callback_python =
  477 + * intermediate_callback;
  478 + */
  479 +
  480 + /* DispatchData *dp = malloc(sizeof(DispatchData)); */
  481 + /*
  482 + * memcpy((void*)dp, (void*)&myowndata,
  483 + * sizeof(DispatchData));
  484 + */
  485 + /* bigfield = dp; */
  486 + /*
  487 + * logger( "qqq: inside set_intermediate_callback, bigfield
  488 + * is %p\n", bigfield ) ;
  489 + */
  490 + /*
  491 + * logger("[PyIPOPT] User specified data field to callback
  492 + * function.\n");
  493 + */
  494 +
  495 +
  496 + SetIntermediateCallback (nlp, eval_intermediate_callback);
  497 + Py_INCREF (Py_True);
  498 + return Py_True;
  499 + }
447 500 }
448 501
449 502
450 503
451   -PyObject *
452   -solve(PyObject * self, PyObject * args)
  504 +PyObject *
  505 +solve (PyObject * self, PyObject * args)
453 506 {
454   - enum ApplicationReturnStatus status; /* Solve return code */
455   - int i;
456   - int n;
457   -
458   - /* Return values */
459   - problem *temp = (problem *) self;
460   -
461   - IpoptProblem nlp = (IpoptProblem) (temp->nlp);
462   - DispatchData *bigfield = (DispatchData *) (temp->data);
463   -
464   - /* int dX[1]; */
465   - npy_intp dX[1];
466   -
467   - PyArrayObject *x = NULL, *mL = NULL, *mU = NULL;
468   - Number obj; /* objective value */
469   -
470   - PyObject *retval = NULL;
471   - PyArrayObject *x0 = NULL;
472   -
473   - PyObject *myuserdata = NULL;
474   -
475   - Number *newx0 = NULL;
476   -
477   - if (!PyArg_ParseTuple(args, "O!|O", &PyArray_Type, &x0, &myuserdata)) {
478   - retval = NULL;
479   - goto done;
480   - }
481   - if (myuserdata != NULL) {
482   - bigfield->userdata = myuserdata;
483   - /*
484   - * logger("[PyIPOPT] User specified data field to callback
485   - * function.\n");
486   - */
487   - }
488   - if (nlp == NULL) {
489   - PyErr_SetString(PyExc_TypeError,
490   - "nlp objective passed to solve is NULL\n Problem created?\n");
491   - retval = NULL;
492   - goto done;
493   - }
494   - if (bigfield->eval_h_python == NULL) {
495   - AddIpoptStrOption(nlp, "hessian_approximation", "limited-memory");
496   - /* logger("Can't find eval_h callback function\n"); */
497   - }
498   - /* allocate space for the initial point and set the values */
499   - npy_intp *dim = ((PyArrayObject *) x0)->dimensions;
500   - n = dim[0];
501   - dX[0] = n;
502   -
503   - x = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
504   - if (!x) {
505   - retval = PyErr_NoMemory();
506   - goto done;
507   - }
508   - newx0 = (Number *) malloc(sizeof(Number) * n);
509   - if (!newx0) {
510   - retval = PyErr_NoMemory();
511   - goto done;
512   - }
513   - double *xdata = (double *) x0->data;
514   - for (i = 0; i < n; i++)
515   - newx0[i] = xdata[i];
516   -
517   - mL = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
518   - mU = (PyArrayObject *) PyArray_SimpleNew(1, dX, PyArray_DOUBLE);
519   -
520   -
521   - /* For status code, see IpReturnCodes_inc.h in Ipopt */
522   -
523   - status = IpoptSolve(nlp, newx0, NULL, &obj, NULL, (double *) mL->data, (double *) mU->data, (UserDataPtr) bigfield);
524   - double* return_x_data = (double *) x->data;
525   - for (i = 0; i < n; i++) {
526   - return_x_data[i] = newx0[i];
527   - }
528   - retval = Py_BuildValue(
529   - "OOOdO",
530   - PyArray_Return(x),
531   - PyArray_Return(mL),
532   - PyArray_Return(mU),
533   - obj,
534   - Py_BuildValue("i", status)
535   - );
536   - goto done;
  507 + enum ApplicationReturnStatus status; /* Solve return code */
  508 + int i;
  509 + int n;
  510 +
  511 + /* Return values */
  512 + problem *temp = (problem *) self;
  513 +
  514 + IpoptProblem nlp = (IpoptProblem) (temp->nlp);
  515 + DispatchData *bigfield = (DispatchData *) (temp->data);
  516 +
  517 + /* int dX[1]; */
  518 + npy_intp dX[1];
  519 +
  520 + PyArrayObject *x = NULL, *mL = NULL, *mU = NULL;
  521 + Number obj; /* objective value */
  522 +
  523 + PyObject *retval = NULL;
  524 + PyArrayObject *x0 = NULL;
  525 +
  526 + PyObject *myuserdata = NULL;
  527 +
  528 + Number *newx0 = NULL;
  529 +
  530 + if (!PyArg_ParseTuple (args, "O!|O", &PyArray_Type, &x0, &myuserdata))
  531 + {
  532 + retval = NULL;
  533 + goto done;
  534 + }
  535 + if (myuserdata != NULL)
  536 + {
  537 + bigfield->userdata = myuserdata;
  538 + /*
  539 + * logger("[PyIPOPT] User specified data field to callback
  540 + * function.\n");
  541 + */
  542 + }
  543 + if (nlp == NULL)
  544 + {
  545 + PyErr_SetString (PyExc_TypeError,
  546 + "nlp objective passed to solve is NULL\n Problem created?\n");
  547 + retval = NULL;
  548 + goto done;
  549 + }
  550 + if (bigfield->eval_h_python == NULL)
  551 + {
  552 + AddIpoptStrOption (nlp, "hessian_approximation", "limited-memory");
  553 + /* logger("Can't find eval_h callback function\n"); */
  554 + }
  555 + /* allocate space for the initial point and set the values */
  556 + npy_intp *dim = ((PyArrayObject *) x0)->dimensions;
  557 + n = dim[0];
  558 + dX[0] = n;
  559 +
  560 + x = (PyArrayObject *) PyArray_SimpleNew (1, dX, PyArray_DOUBLE);
  561 + if (!x)
  562 + {
  563 + retval = PyErr_NoMemory ();
  564 + goto done;
  565 + }
  566 + newx0 = (Number *) malloc (sizeof (Number) * n);
  567 + if (!newx0)
  568 + {
  569 + retval = PyErr_NoMemory ();
  570 + goto done;
  571 + }
  572 + double *xdata = (double *) x0->data;
  573 + for (i = 0; i < n; i++)
  574 + newx0[i] = xdata[i];
  575 +
  576 + mL = (PyArrayObject *) PyArray_SimpleNew (1, dX, PyArray_DOUBLE);
  577 + mU = (PyArrayObject *) PyArray_SimpleNew (1, dX, PyArray_DOUBLE);
  578 +
  579 +
  580 + /* For status code, see IpReturnCodes_inc.h in Ipopt */
  581 +
  582 + status =
  583 + IpoptSolve (nlp, newx0, NULL, &obj, NULL, (double *) mL->data,
  584 + (double *) mU->data, (UserDataPtr) bigfield);
  585 + double *return_x_data = (double *) x->data;
  586 + for (i = 0; i < n; i++)
  587 + {
  588 + return_x_data[i] = newx0[i];
  589 + }
  590 + retval = Py_BuildValue ("OOOdO",
  591 + PyArray_Return (x),
  592 + PyArray_Return (mL),
  593 + PyArray_Return (mU),
  594 + obj, Py_BuildValue ("i", status));
  595 + goto done;
537 596
538 597 done:
539   - /* clean up and return */
540   - if (retval == NULL) {
541   - Py_XDECREF(x);
542   - Py_XDECREF(mL);
543   - Py_XDECREF(mU);
544   - }
545   - free(newx0);
546   -
547   - return retval;
  598 + /* clean up and return */
  599 + if (retval == NULL)
  600 + {
  601 + Py_XDECREF (x);
  602 + Py_XDECREF (mL);
  603 + Py_XDECREF (mU);
  604 + }
  605 + free (newx0);
  606 +
  607 + return retval;
548 608 }
549 609
550 610
551 611
552 612
553   -PyObject *
554   -close_model(PyObject * self, PyObject * args)
  613 +PyObject *
  614 +close_model (PyObject * self, PyObject * args)
555 615 {
556   - problem *obj = (problem *) self;
557   - FreeIpoptProblem(obj->nlp);
558   - obj->nlp = NULL;
559   - Py_INCREF(Py_True);
560   - return Py_True;
  616 + problem *obj = (problem *) self;
  617 + FreeIpoptProblem (obj->nlp);
  618 + obj->nlp = NULL;
  619 + Py_INCREF (Py_True);
  620 + return Py_True;
561 621 }
562 622
563 623
@@ -576,27 +636,27 @@ close_model(PyObject * self, PyObject * args)
576 636
577 637 /* Begin Python Module code section */
578 638 static PyMethodDef ipoptMethods[] = {
579   - /* { "solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC}, */
580   - {"create", create, METH_VARARGS, PYIPOPT_CREATE_DOC},
581   - /* { "close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC}, */
582   - /* { "test", test, METH_VARARGS, PYTEST}, */
583   - { "set_loglevel", set_loglevel, METH_VARARGS, PYIPOPT_LOG_DOC},
584   - {NULL, NULL}
  639 + /* { "solve", solve, METH_VARARGS, PYIPOPT_SOLVE_DOC}, */
  640 + {"create", create, METH_VARARGS, PYIPOPT_CREATE_DOC},
  641 + /* { "close", close_model, METH_VARARGS, PYIPOPT_CLOSE_DOC}, */
  642 + /* { "test", test, METH_VARARGS, PYTEST}, */
  643 + {"set_loglevel", set_loglevel, METH_VARARGS, PYIPOPT_LOG_DOC},
  644 + {NULL, NULL}
585 645 };
586 646
587 647 PyMODINIT_FUNC
588   -initpyipopt(void)
  648 +initpyipopt (void)
589 649 {
590 650
591   - Py_InitModule3("pyipopt", ipoptMethods,
592   - "A hook between Ipopt and Python");
  651 + Py_InitModule3 ("pyipopt", ipoptMethods, "A hook between Ipopt and Python");
593 652
594   - import_array(); /* Initialize the Numarray module. */
595   - /* A segfault will occur if I use numarray without this.. */
  653 + import_array (); /* Initialize the Numarray module. */
  654 + /* A segfault will occur if I use numarray without this.. */
596 655
597   - if (PyErr_Occurred())
598   - Py_FatalError("Unable to initialize module pyipopt");
  656 + if (PyErr_Occurred ())
  657 + Py_FatalError ("Unable to initialize module pyipopt");
599 658
600   - return;
  659 + return;
601 660 }
  661 +
602 662 /* End Python Module code section */

0 comments on commit a07bd34

Please sign in to comment.
Something went wrong with that request. Please try again.