Skip to content

Commit

Permalink
make prima_free_options public
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 25, 2023
1 parent 2f34983 commit 8a00120
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions c/examples/bobyqa/bobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ int main(int argc, char * argv[])
const int rc = prima_bobyqa(&fun, n, x, &options, &result);
const char *msg = prima_get_rc_string(rc);
printf("x*={%g, %g} rc=%d msg='%s' evals=%d\n", x[0], x[1], rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
1 change: 1 addition & 0 deletions c/examples/cobyla/cobyla_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ int main(int argc, char * argv[])
const int rc = prima_cobyla(&fun, n, x, &options, &result);
const char *msg = prima_get_rc_string(rc);
printf("x*={%g, %g} f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", x[0], x[1], result.f, result.cstrv, result.nlconstr[0], rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
2 changes: 2 additions & 0 deletions c/examples/lincoa/lincoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ int main(int argc, char * argv[])
const int rc = prima_lincoa(&fun, n, x, &options, &result);
const char *msg = prima_get_rc_string(rc);
printf("x*={%g, %g} f*=%g cstrv=%g rc=%d msg='%s' evals=%d\n", x[0], x[1], result.f, result.cstrv, rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
2 changes: 2 additions & 0 deletions c/examples/uobyqa/uobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ int main(int argc, char * argv[])
const int rc = prima_uobyqa(&fun, n, x, &options, &result);
const char *msg = prima_get_rc_string(rc);
printf("x*={%g, %g} rc=%d msg='%s' evals=%d\n", x[0], x[1], rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
3 changes: 3 additions & 0 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ typedef enum
PRIMA_VALIDATION_FAILS = 102,
PRIMA_MEMORY_ALLOCATION_FAILS = 103,
PRIMA_NULL_OPTIONS = 200,
PRIMA_NULL_RESULT = 201,
} prima_rc;

/*
Expand Down Expand Up @@ -140,6 +141,8 @@ typedef struct {
PRIMAC_API
int prima_init_options(prima_options * options);

PRIMAC_API
int prima_free_options(prima_options * opt);

typedef struct {
// objective value
Expand Down
33 changes: 19 additions & 14 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)
if (opt)
{
memset(opt, 0, sizeof(prima_options));
opt->maxfun = -1;// interpreted as MAXFUN_DIM_DFT*n
Expand All @@ -22,7 +22,8 @@ int prima_init_options(prima_options * opt)
opt->f0 = NAN;
return 0;
}
return 1;
else
return PRIMA_NULL_OPTIONS;
}

/* implemented in fortran (*_c.f90) */
Expand Down Expand Up @@ -74,7 +75,7 @@ int prima_check_options(prima_options * opt, int n, int alloc_bounds)
return 0;
}

void prima_free_options(prima_options * opt)
int prima_free_options(prima_options * opt)
{
if (opt)
{
Expand All @@ -96,15 +97,23 @@ void prima_free_options(prima_options * opt)
opt->nlconstr0 = NULL;
opt->_allocated_nlconstr0 = 0;
}
return 0;
}
else
return PRIMA_NULL_OPTIONS;
}

int prima_init_result(prima_result * result)
{
memset(result, 0, sizeof(prima_result));
result->f = 0.0;
result->cstrv = 0.0;
return 0;
if (result)
{
memset(result, 0, sizeof(prima_result));
result->f = 0.0;
result->cstrv = 0.0;
return 0;
}
else
return PRIMA_NULL_RESULT;
}

int prima_free_result(prima_result * result)
Expand All @@ -117,8 +126,10 @@ int prima_free_result(prima_result * result)
result->nlconstr = NULL;
result->_allocated_nlconstr = 0;
}
return 0;
}
return 0;
else
return PRIMA_NULL_RESULT;
}

/* these functions just call the fortran compatibility layer and return the status code */
Expand Down Expand Up @@ -149,7 +160,6 @@ int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_optio
cobyla_c(opt->m_nlcon, calcfc, opt->data, n, x, &(result->f), &(result->cstrv), result->nlconstr,
opt->m_ineq, opt->Aineq, opt->bineq, opt->m_eq, opt->Aeq, opt->beq,
opt->xl, opt->xu, opt->f0, opt->nlconstr0, &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
prima_free_options(opt);
}
return info;
}
Expand All @@ -161,8 +171,6 @@ int prima_bobyqa(const prima_obj calfun, const int n, double x[], prima_options
{
prima_init_result(result);
bobyqa_c(calfun, opt->data, n, x, &(result->f), opt->xl, opt->xu, &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);

prima_free_options(opt);
}
return info;
}
Expand All @@ -174,7 +182,6 @@ int prima_newuoa(const prima_obj calfun, const int n, double x[], prima_options
{
prima_init_result(result);
newuoa_c(calfun, opt->data, n, x, &(result->f), &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
prima_free_options(opt);
}
return info;
}
Expand All @@ -186,7 +193,6 @@ int prima_uobyqa(const prima_obj calfun, const int n, double x[], prima_options
{
prima_init_result(result);
uobyqa_c(calfun, opt->data, n, x, &(result->f), &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
prima_free_options(opt);
}
return info;
}
Expand All @@ -200,7 +206,6 @@ int prima_lincoa(const prima_obj calfun, const int n, double x[], prima_options
lincoa_c(calfun, opt->data, n, x, &(result->f), &(result->cstrv),
opt->m_ineq, opt->Aineq, opt->bineq, opt->m_eq, opt->Aeq, opt->beq,
opt->xl, opt->xu, &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
prima_free_options(opt);
}
return info;
}
Expand Down
1 change: 1 addition & 0 deletions c/tests/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ int main(int argc, char * argv[])
const char *msg = prima_get_rc_string(rc);

printf("f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
1 change: 1 addition & 0 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ int main(int argc, char * argv[])
const char *msg = prima_get_rc_string(rc);

printf("f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, msg, result.nf);
prima_free_options(&options);
prima_free_result(&result);
return 0;
}

0 comments on commit 8a00120

Please sign in to comment.