int simple_tnc(int n, double x[], /*double _xopt[],*/ double *f, double g[], tnc_function *function, void* state, double ubound, double lbound) { int i, rc, maxCGit = 5, maxnfeval = 100, nfeval; double fopt = 1.0, *low, *up, eta = -1.0, stepmx = 10.0, accuracy = -1.0, fmin = 0.0, ftol = -1.0, xtol = -1.0, pgtol = -1.0, rescale = -1.0, maxv = -1.0; //xopt = (double*)malloc(sizeof(double)*n); //for(i=0;i<n;i++) xopt[i] = _xopt[i]; up = (double*)malloc(sizeof(double)*n); low = (double*)malloc(sizeof(double)*n); //for(i=0;i<n;i++) { if(x[i] > maxv) maxv = x[i]; } for(i=0;i<n;i++) { up[i] = ubound; low[i] = lbound; } rc = tnc(n, x, f, g, function, state, low, up, NULL, NULL, TNC_MSG_NONE, maxCGit, maxnfeval, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol, rescale, &nfeval); free(up); free(low); return rc; }
int main(int argc, char **argv) { int i, rc, maxCGit = 2, maxnfeval = 20, nfeval; double fopt = 1.0, f, g[2], x[2] = {-7.0, 3.0}, xopt[2] = {0.0, 1.0}, low[2], up[2], eta = -1.0, stepmx = -1.0, accuracy = -1.0, fmin = 0.0, ftol = -1.0, xtol = -1.0, pgtol = -1.0, rescale = -1.0; low[0] = - HUGE_VAL; low[1] = 1.0; up[0] = HUGE_VAL; up[1] = HUGE_VAL; rc = tnc(2, x, &f, g, function, NULL, low, up, NULL, NULL, TNC_MSG_ALL, maxCGit, maxnfeval, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol, rescale, &nfeval, NULL); printf("After %d function evaluations, TNC returned:\n%s\n", nfeval, tnc_rc_string[rc - TNC_MINRC]); for (i = 0; i < 2; i++) printf("x[%d] = %.15f / xopt[%d] = %.15f\n", i, x[i], i, xopt[i]); printf("\n"); printf("f = %.15f / fopt = %.15f\n", f, fopt); return 0; }
PyObject *moduleTNC_minimize(PyObject *self, PyObject *args) { PyObject *py_x0, *py_low, *py_up, *py_list, *py_scale, *py_offset; PyObject *py_function = NULL; PyObject *py_callback = NULL; pytnc_state py_state; int n, n1, n2, n3, n4; tnc_callback *callback_function = NULL; int rc, msg, maxCGit, maxnfeval, nfeval = 0, niter = 0; double *x, *low, *up, *scale = NULL, *offset = NULL; double f, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol, rescale; if (!PyArg_ParseTuple(args, "OO!O!O!O!O!iiiddddddddO", &py_function, &PyList_Type, &py_x0, &PyList_Type, &py_low, &PyList_Type, &py_up, &PyList_Type, &py_scale, &PyList_Type, &py_offset, &msg, &maxCGit, &maxnfeval, &eta, &stepmx, &accuracy, &fmin, &ftol, &xtol, &pgtol, &rescale, &py_callback )) return NULL; if (!PyCallable_Check(py_function)) { PyErr_SetString(PyExc_TypeError, "tnc: function must be callable"); return NULL; } scale = PyList_AsDoubleArray(py_scale, &n3); if (n3 != 0 && scale == NULL) { PyErr_SetString(PyExc_ValueError, "tnc: invalid scaling parameters."); return NULL; } offset = PyList_AsDoubleArray(py_offset, &n4); if (n4 != 0 && offset == NULL) { PyErr_SetString(PyExc_ValueError, "tnc: invalid offset parameters."); return NULL; } x = PyList_AsDoubleArray(py_x0, &n); if (n != 0 && x == NULL) { if (scale) free(scale); PyErr_SetString(PyExc_ValueError, "tnc: invalid initial vector."); return NULL; } low = PyList_AsDoubleArray(py_low, &n1); up = PyList_AsDoubleArray(py_up, &n2); if ((n1 != 0 && low == NULL) || (n2 != 0 && up == NULL)) { if (scale) free(scale); if (x) free(x); if (low) free(low); if (up) free(up); PyErr_SetString(PyExc_ValueError, "tnc: invalid bounds."); return NULL; } if (n1 != n2 || n != n1 || (scale != NULL && n != n3) || (offset != NULL && n != n4)) { if (scale) free(scale); if (offset) free(offset); if (x) free(x); if (low) free(low); if (up) free(up); PyErr_SetString(PyExc_ValueError, "tnc: vector sizes must be equal."); return NULL; } py_state.py_function = py_function; py_state.n = n; py_state.failed = 0; Py_INCREF(py_function); if (py_callback != Py_None) { if (!PyCallable_Check(py_callback)) { PyErr_SetString(PyExc_TypeError, "tnc: callback must be callable or None."); return NULL; } py_state.py_callback = py_callback; Py_INCREF(py_callback); callback_function = callback; } rc = tnc(n, x, &f, NULL, function, &py_state, low, up, scale, offset, msg, maxCGit, maxnfeval, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol, rescale, &nfeval, &niter, callback_function); Py_DECREF(py_function); if (py_callback != Py_None) Py_DECREF(py_callback); if (low) free(low); if (up) free(up); if (scale) free(scale); if (offset) free(offset); if (py_state.failed) { if (x) free(x); return NULL; } if (rc == TNC_ENOMEM) { PyErr_SetString(PyExc_MemoryError, "tnc: memory allocation failed."); if (x) free(x); return NULL; } py_list = PyDoubleArray_AsList(n, x); if (x) free(x); if (py_list == NULL) { PyErr_SetString(PyExc_MemoryError, "tnc: memory allocation failed."); return NULL; } return Py_BuildValue("(iiiN)", rc, nfeval, niter, py_list);; }