Skip to content

Commit

Permalink
apply remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 24, 2023
1 parent bedd22a commit ca76de9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ typedef struct {
// required accuracy for the variables (default=1e-6)
double rhoend;

// maximum number of function evaluations (default=100)
// maximum number of function evaluations (default=-1 interpreted as 500*n)
int maxfun;

// verbosity level, see the prima_message enum (default=PRIMA_MSG_NONE)
Expand All @@ -94,7 +94,7 @@ typedef struct {
// target function value; optimization stops when f <= ftarget for a feasible point (default=-inf)
double ftarget;

// number of points in the interpolation set n+2<=npt<=(n+1)(n+2)/2 (default=2*n+1)
// number of points in the interpolation set n+2<=npt<=(n+1)(n+2)/2 (default=-1 interpreted as 2*n+1)
// ignored for uobyqa & cobyla
int npt;

Expand Down
30 changes: 19 additions & 11 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int prima_init_options(prima_options * opt)
{
if (opt != NULL)
{
opt->maxfun = 100;
opt->maxfun = -1;// interpreted as 500*n
opt->rhobeg = 1.0;
opt->rhoend = 1e-6;
opt->iprint = PRIMA_MSG_NONE;
Expand Down Expand Up @@ -56,24 +56,30 @@ int lincoa_c(prima_obj calfun, const void *data, const int n, double x[], double
const double xl[], const double xu[],
int *nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, int *info);

int prima_check_options(prima_options * opt, int n)
int prima_check_options(prima_options * opt, int n, int alloc_bounds)
{
if (!opt)
return PRIMA_NULL_OPTIONS;
if (!opt->xl)
if (alloc_bounds && !opt->xl)
{
opt->xl = (double*)malloc(n * sizeof(double));
if (!opt->xl)
return PRIMA_MEMORY_ALLOCATION_FAILS;
opt->_allocated_xl = 1;
for (int i = 0; i< n; ++ i)
opt->xl[i] = -HUGE_VAL;
opt->xl[i] = -INFINITY;
}
if (!opt->xu)
if (alloc_bounds && !opt->xu)
{
opt->xu = (double*)malloc(n * sizeof(double));
if (!opt->xu)
return PRIMA_MEMORY_ALLOCATION_FAILS;
opt->_allocated_xu = 1;
for (int i = 0; i< n; ++ i)
opt->xu[i] = HUGE_VAL;
opt->xu[i] = INFINITY;
}
if (opt->maxfun < 0)
opt->maxfun = 500*n;
if (opt->npt < 0)
opt->npt = 2*n+1;
return 0;
Expand Down Expand Up @@ -109,7 +115,7 @@ int prima_cobyla(const prima_objcon calcfc, const int n, double x[], double *f,
double *cstrv, double nlconstr[],
int *nf, prima_options * opt)
{
int info = prima_check_options(opt, n);
int info = prima_check_options(opt, n, 1);
if (info == 0)
{
// evaluate f0, nlconstr0 if either one is not provided
Expand All @@ -118,6 +124,8 @@ int prima_cobyla(const prima_objcon calcfc, const int n, double x[], double *f,
if (!opt->nlconstr0)
{
opt->nlconstr0 = (double*)calloc(n, sizeof(double));
if (!opt->nlconstr0)
return PRIMA_MEMORY_ALLOCATION_FAILS;
opt->_allocated_nlconstr0 = 1;
}
calcfc(x, &(opt->f0), opt->nlconstr0, opt->data);
Expand All @@ -133,7 +141,7 @@ int prima_cobyla(const prima_objcon calcfc, const int n, double x[], double *f,
int prima_bobyqa(const prima_obj calfun, const int n, double x[], double *f,
int *nf, prima_options * opt)
{
int info = prima_check_options(opt, n);
int info = prima_check_options(opt, n, 1);
if (info == 0)
{
bobyqa_c(calfun, opt->data, n, x, f, opt->xl, opt->xu, nf, opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt < 0 ? 2*n+1 : opt->npt, opt->iprint, &info);
Expand All @@ -145,7 +153,7 @@ int prima_bobyqa(const prima_obj calfun, const int n, double x[], double *f,
int prima_newuoa(const prima_obj calfun, const int n, double x[], double *f,
int *nf, prima_options * opt)
{
int info = prima_check_options(opt, n);
int info = prima_check_options(opt, n, 0);
if (info == 0)
{
newuoa_c(calfun, opt->data, n, x, f, nf, opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt < 0 ? 2*n+1 : opt->npt, opt->iprint, &info);
Expand All @@ -157,7 +165,7 @@ int prima_newuoa(const prima_obj calfun, const int n, double x[], double *f,
int prima_uobyqa(const prima_obj calfun, const int n, double x[], double *f,
int *nf, prima_options * opt)
{
int info = prima_check_options(opt, n);
int info = prima_check_options(opt, n, 0);
if (info == 0)
{
uobyqa_c(calfun, opt->data, n, x, f, nf, opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
Expand All @@ -169,7 +177,7 @@ int prima_uobyqa(const prima_obj calfun, const int n, double x[], double *f,
int prima_lincoa(const prima_obj calfun, const int n, double x[], double *f, double *cstrv,
int *nf, prima_options * opt)
{
int info = prima_check_options(opt, n);
int info = prima_check_options(opt, n, 1);
if (info == 0)
{
lincoa_c(calfun, opt->data, n, x, f, cstrv,
Expand Down

0 comments on commit ca76de9

Please sign in to comment.