Skip to content

Commit

Permalink
result
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Oct 25, 2023
1 parent ebd4442 commit 35c3c73
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 79 deletions.
8 changes: 4 additions & 4 deletions c/examples/bobyqa/bobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ int main(int argc, char * argv[])
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_results results;
const int rc = prima_bobyqa(&fun, n, x, &options, &results);
prima_result result;
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, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
8 changes: 4 additions & 4 deletions c/examples/cobyla/cobyla_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
options.xl = xl;
options.xu = xu;
prima_results results;
const int rc = prima_cobyla(&fun, n, x, &options, &results);
prima_result result;
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], results.f, results.cstrv, results.nlconstr[0], rc, msg, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
8 changes: 4 additions & 4 deletions c/examples/lincoa/lincoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
options.xl = xl;
options.xu = xu;
prima_results results;
const int rc = prima_lincoa(&fun, n, x, &options, &results);
prima_result result;
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], results.f, results.cstrv, rc, msg, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
8 changes: 4 additions & 4 deletions c/examples/newuoa/newuoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ int main(int argc, char * argv[])
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_results results;
const int rc = prima_newuoa(&fun, n, x, &options, &results);
prima_result result;
const int rc = prima_newuoa(&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, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
8 changes: 4 additions & 4 deletions c/examples/uobyqa/uobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ int main(int argc, char * argv[])
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_results results;
const int rc = prima_uobyqa(&fun, n, x, &options, &results);
prima_result result;
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, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
18 changes: 9 additions & 9 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ typedef struct {
// size of nlconstr (private, do not use)
int _m_nlcon;

} prima_results;
} prima_result;


/* Free results after optimization */
/* Free result after optimization */
PRIMAC_API
int prima_free_results(prima_results * results);
int prima_free_result(prima_result * result);

/*
* calfun : function to minimize (see prima_obj)
Expand All @@ -174,24 +174,24 @@ int prima_free_results(prima_results * results);
* x : on input, initial estimate
* on output, the solution
* options : optimization options (see prima_options)
* result : optimization results (see prima_result)
* result : optimization result (see prima_result)
* return : see prima_rc enum for return codes
*/

PRIMAC_API
int prima_bobyqa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_results *results);
int prima_bobyqa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_result *result);

PRIMAC_API
int prima_newuoa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_results *results);
int prima_newuoa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_result *result);

PRIMAC_API
int prima_uobyqa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_results *results);
int prima_uobyqa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_result *result);

PRIMAC_API
int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_options *options, prima_results *results);
int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_options *options, prima_result *result);

PRIMAC_API
int prima_lincoa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_results *results);
int prima_lincoa(const prima_obj calfun, const int n, double x[], prima_options *options, prima_result *result);

#ifdef __cplusplus
}
Expand Down
68 changes: 34 additions & 34 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,28 +103,28 @@ int prima_free_options(prima_options *opt)
return PRIMA_NULL_OPTIONS;
}

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

int prima_free_results(prima_results *results)
int prima_free_result(prima_result *result)
{
if (results)
if (result)
{
if (results->nlconstr)
if (result->nlconstr)
{
free(results->nlconstr);
results->nlconstr = NULL;
results->_m_nlcon = 0;
free(result->nlconstr);
result->nlconstr = NULL;
result->_m_nlcon = 0;
}
return 0;
}
Expand All @@ -133,22 +133,22 @@ int prima_free_results(prima_results *results)
}

/* these functions just call the fortran compatibility layer and return the status code */
int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_options *opt, prima_results *results)
int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_options *opt, prima_result *result)
{
int info = prima_check_options(opt, n, 1);
if (info == 0)
info = prima_init_result(results);
info = prima_init_result(result);
if (info == 0)
{
// reuse or (re)allocate nlconstr
if (results->_m_nlcon != opt->m_nlcon)
if (result->_m_nlcon != opt->m_nlcon)
{
if (results->nlconstr)
free(results->nlconstr);
results->nlconstr = (double*)calloc(opt->m_nlcon, sizeof(double));
if (!(results->nlconstr))
if (result->nlconstr)
free(result->nlconstr);
result->nlconstr = (double*)calloc(opt->m_nlcon, sizeof(double));
if (!(result->nlconstr))
info = PRIMA_MEMORY_ALLOCATION_FAILS;
results->_m_nlcon = opt->m_nlcon;
result->_m_nlcon = opt->m_nlcon;
}
}
if (info == 0)
Expand All @@ -168,59 +168,59 @@ int prima_cobyla(const prima_objcon calcfc, const int n, double x[], prima_optio
}
if (info == 0)
{
cobyla_c(opt->m_nlcon, calcfc, opt->data, n, x, &(results->f), &(results->cstrv), results->nlconstr,
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, &(results->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
opt->xl, opt->xu, opt->f0, opt->nlconstr0, &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
}
return info;
}

int prima_bobyqa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_results *results)
int prima_bobyqa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_result *result)
{
int info = prima_check_options(opt, n, 1);
if (info == 0)
info = prima_init_result(results);
info = prima_init_result(result);
if (info == 0)
{
bobyqa_c(calfun, opt->data, n, x, &(results->f), opt->xl, opt->xu, &(results->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
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);
}
return info;
}

int prima_newuoa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_results *results)
int prima_newuoa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_result *result)
{
int info = prima_check_options(opt, n, 0);
if (info == 0)
info = prima_init_result(results);
info = prima_init_result(result);
if (info == 0)
{
newuoa_c(calfun, opt->data, n, x, &(results->f), &(results->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
newuoa_c(calfun, opt->data, n, x, &(result->f), &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
}
return info;
}

int prima_uobyqa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_results *results)
int prima_uobyqa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_result *result)
{
int info = prima_check_options(opt, n, 0);
if (info == 0)
info = prima_init_result(results);
info = prima_init_result(result);
if (info == 0)
{
uobyqa_c(calfun, opt->data, n, x, &(results->f), &(results->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
uobyqa_c(calfun, opt->data, n, x, &(result->f), &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->iprint, &info);
}
return info;
}

int prima_lincoa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_results *results)
int prima_lincoa(const prima_obj calfun, const int n, double x[], prima_options *opt, prima_result *result)
{
int info = prima_check_options(opt, n, 1);
if (info == 0)
info = prima_init_result(results);
info = prima_init_result(result);
if (info == 0)
{
lincoa_c(calfun, opt->data, n, x, &(results->f), &(results->cstrv),
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, &(results->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
opt->xl, opt->xu, &(result->nf), opt->rhobeg, opt->rhoend, opt->ftarget, opt->maxfun, opt->npt, opt->iprint, &info);
}
return info;
}
Expand Down
16 changes: 8 additions & 8 deletions c/tests/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,28 @@ int main(int argc, char * argv[])
options.bineq = bineq;
options.xl = xl;
options.xu = xu;
prima_results results;
prima_result result;
int rc = 0;
if(strcmp(algo, "bobyqa") == 0)
{
rc = prima_bobyqa(&fun, n, x, &options, &results);
rc = prima_bobyqa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "cobyla") == 0)
{
options.m_nlcon = m_nlcon;
rc = prima_cobyla(&fun_con, n, x, &options, &results);
rc = prima_cobyla(&fun_con, n, x, &options, &result);
}
else if(strcmp(algo, "lincoa") == 0)
{
rc = prima_lincoa(&fun, n, x, &options, &results);
rc = prima_lincoa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "newuoa") == 0)
{
rc = prima_newuoa(&fun, n, x, &options, &results);
rc = prima_newuoa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "uobyqa") == 0)
{
rc = prima_uobyqa(&fun, n, x, &options, &results);
rc = prima_uobyqa(&fun, n, x, &options, &result);
}
else
{
Expand All @@ -115,8 +115,8 @@ 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", results.f, results.cstrv, results.nlconstr ? results.nlconstr[0] : 0.0, rc, msg, results.nf);
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_results(&results);
prima_free_result(&result);
return (fabs(x[0]-3)>2e-2 || fabs(x[1]-2)>2e-2);
}
16 changes: 8 additions & 8 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,34 @@ int main(int argc, char * argv[])
xl[i] = -1.0;
xu[i] = 1.0;
}
prima_results results;
prima_result result;
if(strcmp(algo, "bobyqa") == 0)
{
n = 1600;
rc = prima_bobyqa(&fun, n, x, &options, &results);
rc = prima_bobyqa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "cobyla") == 0)
{
n = 800;
options.m_nlcon = m_nlcon;
options.m_ineq = 600;
rc = prima_cobyla(&fun_con, n, x, &options, &results);
rc = prima_cobyla(&fun_con, n, x, &options, &result);
}
else if(strcmp(algo, "lincoa") == 0)
{
n = 1000;
options.m_ineq = 1000;
rc = prima_lincoa(&fun, n, x, &options, &results);
rc = prima_lincoa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "newuoa") == 0)
{
n = 1600;
rc = prima_newuoa(&fun, n, x, &options, &results);
rc = prima_newuoa(&fun, n, x, &options, &result);
}
else if(strcmp(algo, "uobyqa") == 0)
{
n = 100;
rc = prima_uobyqa(&fun, n, x, &options, &results);
rc = prima_uobyqa(&fun, n, x, &options, &result);
}
else
{
Expand All @@ -139,8 +139,8 @@ 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", results.f, results.cstrv, results.nlconstr ? results.nlconstr[0] : 0.0, rc, msg, results.nf);
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_results(&results);
prima_free_result(&result);
return 0;
}

0 comments on commit 35c3c73

Please sign in to comment.