Permalink
Browse files

ENH: return the number of iterations in TNC

  • Loading branch information...
dlax committed Oct 20, 2012
1 parent 119d528 commit 5f163a2980887bb8c2deb00840be8f53e3effecc
Showing with 23 additions and 20 deletions.
  1. +5 −4 scipy/optimize/tnc.py
  2. +3 −3 scipy/optimize/tnc/moduleTNC.c
  3. +14 −12 scipy/optimize/tnc/tnc.c
  4. +1 −1 scipy/optimize/tnc/tnc.h
View
@@ -389,14 +389,15 @@ def func_and_grad(x):
if maxfun is None:
maxfun = max(100, 10*len(x0))
- rc, nf, x = moduleTNC.minimize(func_and_grad, x0, low, up, scale, offset,
- messages, maxCGit, maxfun, eta, stepmx, accuracy,
- fmin, ftol, xtol, pgtol, rescale, callback)
+ rc, nf, nit, x = moduleTNC.minimize(func_and_grad, x0, low, up, scale,
+ offset, messages, maxCGit, maxfun,
+ eta, stepmx, accuracy, fmin, ftol,
+ xtol, pgtol, rescale, callback)
xopt = array(x)
funv, jacv = func_and_grad(xopt)
- return Result(x=xopt, fun=funv, jac=jacv, nfev=nf, status=rc,
+ return Result(x=xopt, fun=funv, jac=jacv, nfev=nf, nit=nit, status=rc,
message=RCSTRINGS[rc], success=(-1 < rc < 3))
if __name__ == '__main__':
@@ -204,7 +204,7 @@ PyObject *moduleTNC_minimize(PyObject *self, PyObject *args)
int n, n1, n2, n3, n4;
tnc_callback *callback_function = NULL;
- int rc, msg, maxCGit, maxnfeval, nfeval = 0;
+ 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;
@@ -299,7 +299,7 @@ PyObject *moduleTNC_minimize(PyObject *self, PyObject *args)
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, callback_function);
+ &nfeval, &niter, callback_function);
Py_DECREF(py_function);
if (py_callback != Py_None) Py_DECREF(py_callback);
@@ -330,7 +330,7 @@ PyObject *moduleTNC_minimize(PyObject *self, PyObject *args)
return NULL;
}
- return Py_BuildValue("(iiN)", rc, nfeval, py_list);;
+ return Py_BuildValue("(iiiN)", rc, nfeval, niter, py_list);;
}
static PyMethodDef moduleTNC_methods[] =
View
@@ -112,7 +112,7 @@ static tnc_rc tnc_minimize(int n, double x[], double *f, double g[],
tnc_function *function, void *state,
double xscale[], double xoffset[], double *fscale,
double low[], double up[], tnc_message messages,
- int maxCGit, int maxnfeval, int *nfeval,
+ int maxCGit, int maxnfeval, int *nfeval, int *niter,
double eta, double stepmx, double accuracy,
double fmin, double ftol, double xtol, double pgtol, double rescale,
tnc_callback *callback);
@@ -236,7 +236,7 @@ extern int tnc(int n, double x[], double *f, double g[], tnc_function *function,
void *state, double low[], double up[], double scale[], double offset[],
int messages, int maxCGit, int maxnfeval, double eta, double stepmx,
double accuracy, double fmin, double ftol, double xtol, double pgtol,
- double rescale, int *nfeval, tnc_callback *callback)
+ double rescale, int *nfeval, int *niter, tnc_callback *callback)
{
int rc, frc, i, nc, nfeval_local,
free_low = TNC_FALSE, free_up = TNC_FALSE,
@@ -404,8 +404,8 @@ extern int tnc(int n, double x[], double *f, double g[], tnc_function *function,
/* Optimisation */
rc = tnc_minimize(n, x, f, g, function, state,
xscale, xoffset, &fscale, low, up, messages,
- maxCGit, maxnfeval, nfeval, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol,
- rescale, callback);
+ maxCGit, maxnfeval, nfeval, niter, eta, stepmx, accuracy, fmin, ftol,
+ xtol, pgtol, rescale, callback);
cleanup:
if (messages & TNC_MSG_EXIT)
@@ -501,20 +501,22 @@ static tnc_rc tnc_minimize(int n, double x[],
double *f, double gfull[], tnc_function *function, void *state,
double xscale[], double xoffset[], double *fscale,
double low[], double up[], tnc_message messages,
- int maxCGit, int maxnfeval, int *nfeval, double eta, double stepmx,
- double accuracy, double fmin, double ftol, double xtol, double pgtol,
- double rescale, tnc_callback *callback)
+ int maxCGit, int maxnfeval, int *nfeval, int *niter, double eta, double
+ stepmx, double accuracy, double fmin, double ftol, double xtol, double
+ pgtol, double rescale, tnc_callback *callback)
{
double fLastReset, difnew, epsmch, epsred, oldgtp,
difold, oldf, xnorm, newscale,
gnorm, ustpmax, fLastConstraint, spe, yrsr, yksk,
*temp = NULL, *sk = NULL, *yk = NULL, *diagb = NULL, *sr = NULL,
*yr = NULL, *oldg = NULL, *pk = NULL, *g = NULL;
double alpha = 0.0; /* Default unused value */
- int i, icycle, niter = 0, oldnfeval, *pivot = NULL, frc;
+ int i, icycle, oldnfeval, *pivot = NULL, frc;
logical lreset, newcon, upd1, remcon;
tnc_rc rc = TNC_ENOMEM; /* Default error */
+ *niter = 0;
+
/* Allocate temporary vectors */
oldg = malloc(sizeof(*oldg)*n);
if (oldg == NULL) goto cleanup;
@@ -579,7 +581,7 @@ static tnc_rc tnc_minimize(int n, double x[],
if (messages & TNC_MSG_ITER) fprintf(stderr,
" NIT NF F GTG\n");
if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
- niter, *nfeval, pivot);
+ *niter, *nfeval, pivot);
/* Set the diagonal of the approximate hessian to unity. */
for (i = 0; i < n; i++) diagb[i] = 1.0;
@@ -758,7 +760,7 @@ static tnc_rc tnc_minimize(int n, double x[],
fLastConstraint = *f;
}
- niter++;
+ (*niter)++;
/* Set up parameters used in convergence and resetting tests */
difold = difnew;
@@ -815,7 +817,7 @@ static tnc_rc tnc_minimize(int n, double x[],
project(n, g, pivot);
if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
- niter, *nfeval, pivot);
+ *niter, *nfeval, pivot);
/* Compute the change in the iterates and the corresponding change in the
gradients */
@@ -851,7 +853,7 @@ static tnc_rc tnc_minimize(int n, double x[],
}
if (messages & TNC_MSG_ITER) printCurrentIteration(n, *f / *fscale, gfull,
- niter, *nfeval, pivot);
+ *niter, *nfeval, pivot);
/* Unscaling */
unscalex(n, x, xscale, xoffset);
View
@@ -171,7 +171,7 @@ extern int tnc(int n, double x[], double *f, double g[],
double low[], double up[], double scale[], double offset[],
int messages, int maxCGit, int maxnfeval, double eta, double stepmx,
double accuracy, double fmin, double ftol, double xtol, double pgtol,
- double rescale, int *nfeval, tnc_callback *callback);
+ double rescale, int *nfeval, int *niter, tnc_callback *callback);
#ifdef __cplusplus
}

0 comments on commit 5f163a2

Please sign in to comment.