-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper.c
More file actions
373 lines (301 loc) · 9.66 KB
/
wrapper.c
File metadata and controls
373 lines (301 loc) · 9.66 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
/*
* This file is part of the NloptWraper_Python-C distribution Copyright (c) 2017
* Jimmy Aguilar Mena.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <Python.h>
#include "structmember.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "numpy/arrayobject.h" // numpy objects
#include "nlopt.h"
#include "nlopt-enum.h"
// CountDict type
typedef struct {
PyObject_HEAD
nlopt_opt opt;
PyObject *callback;
} Nlopt;
// Exception handling
static PyObject *_checkNlopt(int out, const char file[],
int line, const char function[])
{
if (out != NLOPT_SUCCESS) {
PyErr_Format(PyExc_RuntimeError,
"%s:%d %s -> Nlopt C function returned: %d expected: %d\n",
file, line, function, out, NLOPT_SUCCESS);
return NULL;
}
return Py_BuildValue("i", out);
}
#define checkNlopt(out) _checkNlopt(out, __FILE__, __LINE__, __FUNCTION__);
// Object allocation in memory (no initialize)
static PyObject *Nlopt_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Nlopt *self = (Nlopt *) type->tp_alloc(type, 0);
self->opt = NULL;
return (PyObject *) self;
}
// Object initialization. (this receives parameters)
static int Nlopt_init(Nlopt *self, PyObject *args, PyObject *kwds)
{
int alg;
unsigned n;
static char *kwlist[] = {"algorithm", "n", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iI", kwlist, &alg, &n))
return -1;
if (alg >= NLOPT_NUM_ALGORITHMS)
return -2;
self->opt = nlopt_create(alg, n);
if (!self->opt)
return -3;
return 0;
}
// Destructor
static void Nlopt_dealloc(Nlopt *self)
{
nlopt_destroy(self->opt);
Py_TYPE(self)->tp_free((PyObject *) self);
}
// Here start the Wrappers.
static PyObject *Nlopt_set_lower_bounds(Nlopt *self, PyObject *arg)
{
#ifndef NDEBUG
if (!PyArray_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> variable not an array",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
PyArrayObject *array = (PyArrayObject *) arg;
if (!array) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> Is not an array",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
const double *data = (double *) PyArray_DATA(array);
const nlopt_result out = nlopt_set_lower_bounds(self->opt, data);
return checkNlopt(out);
}
static PyObject *Nlopt_set_upper_bounds(Nlopt *self, PyObject *arg)
{
#ifndef NDEBUG
if (!PyArray_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> variable not an array",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
PyArrayObject *array = (PyArrayObject *) arg;
if (!array) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> Is not an array",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
const double *data = (double *) PyArray_DATA(array);
const nlopt_result out = nlopt_set_upper_bounds(self->opt, data);
return checkNlopt(out);
}
static PyObject *Nlopt_set_maxeval(Nlopt *self, PyObject *arg)
{
#ifndef NDEBUG
if (!PyLong_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> arg not an int",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
const int maxeval = PyLong_AsLong(arg);
const nlopt_result out = nlopt_set_maxeval(self->opt, maxeval);
return checkNlopt(out);
}
static PyObject *Nlopt_set_stopval(Nlopt *self, PyObject *arg)
{
#ifndef NDEBUG
if (!PyFloat_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> arg not a float",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
const double stopval = PyFloat_AsDouble(arg);
const nlopt_result out = nlopt_set_stopval(self->opt, stopval);
return checkNlopt(out);
}
static PyObject *Nlopt_set_ftol_abs(Nlopt *self, PyObject *arg)
{
#ifndef NDEBUG
if (!PyFloat_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> arg not a float",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
const double tol = PyFloat_AsDouble(arg);
const nlopt_result out = nlopt_set_ftol_abs(self->opt, tol);
return checkNlopt(out);
}
static PyObject *Nlopt_optimize(Nlopt *self, PyObject *args, PyObject *kwds)
{
PyObject *Px;
double opt_f;
char *kwlist[] = {"x", "opt_f", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Of", kwlist,
&Px, &opt_f))
return NULL;
#ifndef NDEBUG
if (!PyArray_Check(Px)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> variable not an array",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
#endif
PyArrayObject *x = (PyArrayObject *) Px;
double *dx = (double *) PyArray_DATA(x);
#ifndef NDEBUG
const int n = nlopt_get_dimension(self->opt);
printf("Algorithm: %d\n", nlopt_get_algorithm(self->opt));
printf("Dimensions: %u\n", n);
double upper[n], lower[n];
nlopt_get_upper_bounds(self->opt, upper);
nlopt_get_lower_bounds(self->opt, lower);
printf("Upper [%lf; %lf]\n", upper[0], upper[1]);
printf("Lower [%lf; %lf]\n", lower[0], lower[1]);
printf("Maxeval %d\n", nlopt_get_maxeval(self->opt));
printf("Stopval %lf\n", nlopt_get_stopval(self->opt));
printf("Ftol_abs %lf\n", nlopt_get_ftol_abs(self->opt));
#endif
const nlopt_result out = nlopt_optimize(self->opt, dx, &opt_f);
if (out < 0) {
PyErr_Format(PyExc_RuntimeError,
"%s:%d %s -> Nlopt C function returned: %d expected: %d\n",
__FILE__, __LINE__, __FUNCTION__, out, NLOPT_SUCCESS);
return NULL;
}
return Py_BuildValue("f", opt_f);
}
static PyObject *Nlopt_set_local_optimizer(Nlopt *self, PyObject *arg)
{
if (!arg) {
PyErr_Format(PyExc_RuntimeError,
"%s:%d %s -> Input opt is null\n",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
Nlopt *local_opt = (Nlopt *) arg;
const nlopt_result out = nlopt_set_local_optimizer(self->opt, local_opt->opt);
return checkNlopt(out);
}
// Functions for the callback
double callback(unsigned n, const double *x, double *grad, void *func_data)
{
assert(func_data && n > 0);
npy_intp dims = n;
PyObject *Ox = PyArray_SimpleNewFromData(1, &dims, NPY_DOUBLE, (void *) x);
PyObject *Ograd = PyArray_SimpleNewFromData(1, &dims, NPY_DOUBLE, grad);
if (!Ox || !Ograd) {
PyErr_Format(PyExc_RuntimeError,
"%s:%d %s -> could not make PyArray from C array\n",
__FILE__, __LINE__, __FUNCTION__);
abort();
}
PyObject *result = PyObject_CallFunction(func_data, "OO", Ox, Ograd);
if (!result) {
PyErr_Format(PyExc_RuntimeError, "%s:%d %s -> result is null\n",
__FILE__, __LINE__, __FUNCTION__);
abort();
}
const double ret = PyFloat_AsDouble(result);
#ifndef NDEBUG
printf("f(%lf, %lf) = %lf\n", x[0], x[1], ret);
#endif
return ret;
}
// This sets the global callback object.
static PyObject *Nlopt_set_callback(Nlopt *self, PyObject *arg)
{
if (!PyCallable_Check(arg)) {
PyErr_Format(PyExc_TypeError, "%s:%d %s -> Needed callable parameter",
__FILE__, __LINE__, __FUNCTION__);
return NULL;
}
// Boilerplate to return "None"
const nlopt_result out = nlopt_set_min_objective(self->opt, callback, arg);
return checkNlopt(out);
}
// ====== Defining the type. This parte exposes the object to python =======
// Object Members
static PyMemberDef Nlopt_members[] = {
{NULL}
};
// Object functions
static PyMethodDef Nlopt_methods[] = {
{"set_lower_bounds", (PyCFunction) Nlopt_set_lower_bounds,
METH_O, "Set lower bounds."},
{"set_upper_bounds", (PyCFunction) Nlopt_set_upper_bounds,
METH_O, "Set upper bounds."},
{"set_local_optimizer", (PyCFunction) Nlopt_set_local_optimizer,
METH_O, "Set local optimizer."},
{"set_maxeval", (PyCFunction) Nlopt_set_maxeval,
METH_O, "Set maxeval."},
{"set_stopval", (PyCFunction) Nlopt_set_stopval,
METH_O, "Set stopval."},
{"set_ftol_abs", (PyCFunction) Nlopt_set_ftol_abs,
METH_O, "Set ftol abs."},
{"optimize", (PyCFunction) Nlopt_optimize,
METH_KEYWORDS | METH_VARARGS, "Execute Optimization."},
{"set_callback", (PyCFunction) Nlopt_set_callback,
METH_O, "Sets the callback for the min_objective."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
// This is the python object design to put all together.
// The PyObjectType it an object itself
static PyTypeObject PyNloptType = {
PyObject_HEAD_INIT(NULL)
.tp_name = "PyNlopt",
.tp_basicsize = sizeof(Nlopt),
.tp_dealloc = (destructor)Nlopt_dealloc,
.tp_doc = "PyNlopt Object",
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_new = Nlopt_new,
.tp_methods = Nlopt_methods,
.tp_members = Nlopt_members,
.tp_init = (initproc)Nlopt_init
};
// Defining the module
static PyModuleDef nloptmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "wnlopt",
.m_doc = "C wraper for nlopt.",
.m_size = -1,
};
PyMODINIT_FUNC PyInit_wnlopt(void)
{
#ifndef NDEBUG
fprintf(stderr, "PYNLOPT using debug mode\n");
#endif
// Tests the object type
if (PyType_Ready(&PyNloptType) < 0)
return NULL;
// Creates the module object
PyObject *m = PyModule_Create(&nloptmodule);
if (!m)
return NULL;
import_array(); // needed to use numpy
Py_INCREF(&PyNloptType);
// Add the python object to this module.
PyModule_AddObject(m, "PyNlopt" , (PyObject* )&PyNloptType);
ADDVALUES(m); // Adds the variables to the
return m;
}