Skip to content

Commit

Permalink
Merge pull request #114 from jschueller/algo
Browse files Browse the repository at this point in the history
Add algo enum; thank @jschueller and everyone who inputs.
  • Loading branch information
zaikunzhang authored Dec 4, 2023
2 parents 34f886e + e2b86e1 commit 2239699
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 142 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 @@ -18,17 +18,17 @@ int main(int argc, char * argv[])
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.x0 = x0;
problem.calfun = &fun;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_result result;
const int rc = prima_bobyqa(&problem, &options, &result);
prima_result_t result;
const int rc = prima_minimize(PRIMA_BOBYQA, &problem, &options, &result);
printf("x*={%g, %g} rc=%d msg='%s' evals=%d\n", result.x[0], result.x[1], rc, result.message, result.nf);
prima_free_problem(&problem);
prima_free_result(&result);
Expand Down
8 changes: 4 additions & 4 deletions c/examples/cobyla/cobyla_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ int main(int argc, char * argv[])
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calcfc = &fun;
problem.x0 = x0;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
Expand All @@ -46,8 +46,8 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
problem.xl = xl;
problem.xu = xu;
prima_result result;
const int rc = prima_cobyla(&problem, &options, &result);
prima_result_t result;
const int rc = prima_minimize(PRIMA_COBYLA, &problem, &options, &result);
printf("x*={%g, %g} f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", result.x[0], result.x[1], result.f, result.cstrv, result.nlconstr[0], rc, result.message, result.nf);
prima_free_problem(&problem);
prima_free_result(&result);
Expand Down
8 changes: 4 additions & 4 deletions c/examples/lincoa/lincoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ int main(int argc, char * argv[])
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
Expand All @@ -41,8 +41,8 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
problem.xl = xl;
problem.xu = xu;
prima_result result;
const int rc = prima_lincoa(&problem, &options, &result);
prima_result_t result;
const int rc = prima_minimize(PRIMA_LINCOA, &problem, &options, &result);
printf("x*={%g, %g} f*=%g cstrv=%g rc=%d msg='%s' evals=%d\n", result.x[0], result.x[1], result.f, result.cstrv, rc, result.message, result.nf);
prima_free_problem(&problem);
prima_free_result(&result);
Expand Down
8 changes: 4 additions & 4 deletions c/examples/newuoa/newuoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ int main(int argc, char * argv[])
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_result result;
const int rc = prima_newuoa(&problem, &options, &result);
prima_result_t result;
const int rc = prima_minimize(PRIMA_NEWUOA, &problem, &options, &result);
printf("x*={%g, %g} rc=%d msg='%s' evals=%d\n", result.x[0], result.x[1], rc, result.message, result.nf);
prima_free_problem(&problem);
prima_free_result(&result);
Expand Down
8 changes: 4 additions & 4 deletions c/examples/uobyqa/uobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ int main(int argc, char * argv[])
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
prima_result result;
const int rc = prima_uobyqa(&problem, &options, &result);
prima_result_t result;
const int rc = prima_minimize(PRIMA_UOBYQA, &problem, &options, &result);
printf("x*={%g, %g} rc=%d msg='%s' evals=%d\n", result.x[0], result.x[1], rc, result.message, result.nf);
prima_free_problem(&problem);
prima_free_result(&result);
Expand Down
60 changes: 31 additions & 29 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ typedef enum {
PRIMA_MSG_EXIT = 1, /* Exit reasons */
PRIMA_MSG_RHO = 2, /* Rho changes */
PRIMA_MSG_FEVL = 3, /* The object/constraint functions get evaluated */
} prima_message;
} prima_message_t;

/*
* Possible return values
Expand All @@ -58,13 +58,13 @@ typedef enum
PRIMA_NULL_X0 = 112,
PRIMA_NULL_RESULT = 113,
PRIMA_NULL_FUNCTION = 114,
} prima_rc;
} prima_rc_t;

/*
* Return code string
*/
PRIMAC_API
const char *prima_get_rc_string(int rc);
const char *prima_get_rc_string(const prima_rc_t rc);

/*
* A function as required by solvers
Expand All @@ -77,8 +77,8 @@ const char *prima_get_rc_string(int rc);
* NaN values can be passed to signal evaluation errors
* only for cobyla
*/
typedef void (*prima_obj)(const double x[], double *f, const void *data);
typedef void (*prima_objcon)(const double x[], double *f, double constr[], const void *data);
typedef void (*prima_obj_t)(const double x[], double *f, const void *data);
typedef void (*prima_objcon_t)(const double x[], double *f, double constr[], const void *data);


typedef struct {
Expand All @@ -92,7 +92,7 @@ typedef struct {
// maximum number of function evaluations (default=-1 interpreted as 500*n)
int maxfun;

// verbosity level, see the prima_message enum (default=PRIMA_MSG_NONE)
// verbosity level, see the prima_message_t enum (default=PRIMA_MSG_NONE)
int iprint;

// target function value; optimization stops when f <= ftarget for a feasible point (default=-inf)
Expand All @@ -105,22 +105,22 @@ typedef struct {
// user-data, will be passed through the objective function callback
void *data;

} prima_options;
} prima_options_t;

/* Initialize problem */
PRIMAC_API
int prima_init_options(prima_options *options);
int prima_init_options(prima_options_t *options);

typedef struct {

// dimension of the problem
int n;

// objective function to minimize (not cobyla)
prima_obj calfun;
prima_obj_t calfun;

// objective function to minimize with constraints (cobyla)
prima_objcon calcfc;
prima_objcon_t calcfc;

// starting point
double *x0;
Expand Down Expand Up @@ -157,15 +157,15 @@ typedef struct {
// whether prima had to allocate nlconstr0 (private, do not use)
int _allocated_nlconstr0;

} prima_problem;
} prima_problem_t;


/* Initialize/free problem */
PRIMAC_API
int prima_init_problem(prima_problem *problem, int n);
int prima_init_problem(prima_problem_t *problem, int n);

PRIMAC_API
int prima_free_problem(prima_problem *problem);
int prima_free_problem(prima_problem_t *problem);


typedef struct {
Expand Down Expand Up @@ -194,34 +194,36 @@ typedef struct {
// error message
const char *message;

} prima_result;
} prima_result_t;


/* Free result after optimization */
PRIMAC_API
int prima_free_result(prima_result * result);
int prima_free_result(prima_result_t * result);

/*
* Algorithm
*/
typedef enum
{
PRIMA_BOBYQA,
PRIMA_COBYLA,
PRIMA_LINCOA,
PRIMA_NEWUOA,
PRIMA_UOBYQA
} prima_algorithm_t;


/*
* algorithm : optimization algorithm (see prima_algorithm)
* problem : optimization problem (see prima_problem)
* options : optimization options (see prima_options)
* result : optimization result (see prima_result)
* return : see prima_rc enum for return codes
* return : see prima_rc_t enum for return codes
*/

PRIMAC_API
int prima_bobyqa(prima_problem *problem, prima_options *options, prima_result *result);

PRIMAC_API
int prima_newuoa(prima_problem *problem, prima_options *options, prima_result *result);

PRIMAC_API
int prima_uobyqa(prima_problem *problem, prima_options *options, prima_result *result);

PRIMAC_API
int prima_cobyla(prima_problem *problem, prima_options *options, prima_result *result);

PRIMAC_API
int prima_lincoa(prima_problem *problem, prima_options *options, prima_result *result);
int prima_minimize(const prima_algorithm_t algorithm, prima_problem_t *problem, prima_options_t *options, prima_result_t *result);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 2239699

Please sign in to comment.