Skip to content

Commit

Permalink
Add _t suffix to prima types
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Nov 30, 2023
1 parent de4d164 commit e2b86e1
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 57 deletions.
6 changes: 3 additions & 3 deletions c/examples/bobyqa/bobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ 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;
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);
Expand Down
6 changes: 3 additions & 3 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,7 +46,7 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
problem.xl = xl;
problem.xu = xu;
prima_result 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);
Expand Down
6 changes: 3 additions & 3 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,7 +41,7 @@ int main(int argc, char * argv[])
double xu[2] = {6.0, 6.0};
problem.xl = xl;
problem.xu = xu;
prima_result 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);
Expand Down
6 changes: 3 additions & 3 deletions c/examples/newuoa/newuoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ 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;
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);
Expand Down
6 changes: 3 additions & 3 deletions c/examples/uobyqa/uobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ 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;
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);
Expand Down
36 changes: 18 additions & 18 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,12 +194,12 @@ 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
Expand All @@ -211,19 +211,19 @@ typedef enum
PRIMA_LINCOA,
PRIMA_NEWUOA,
PRIMA_UOBYQA
} prima_algorithm;
} 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_minimize(prima_algorithm algorithm, 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
32 changes: 16 additions & 16 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

#define MAXFUN_DIM_DFT 500

int prima_init_options(prima_options *options)
int prima_init_options(prima_options_t *options)
{
if (options)
{
memset(options, 0, sizeof(prima_options));
memset(options, 0, sizeof(prima_options_t));
options->maxfun = -1;// interpreted as MAXFUN_DIM_DFT*n
options->rhobeg = 1.0;
options->rhoend = 1e-6;
Expand All @@ -25,11 +25,11 @@ int prima_init_options(prima_options *options)
return PRIMA_NULL_OPTIONS;
}

int prima_init_problem(prima_problem *problem, int n)
int prima_init_problem(prima_problem_t *problem, int n)
{
if (problem)
{
memset(problem, 0, sizeof(prima_problem));
memset(problem, 0, sizeof(prima_problem_t));
problem->n = n;
problem->f0 = NAN;
return 0;
Expand All @@ -39,26 +39,26 @@ int prima_init_problem(prima_problem *problem, int n)
}

/* implemented in fortran (*_c.f90) */
int cobyla_c(const int m_nlcon, const prima_objcon calcfc, const void *data, const int n, double x[], double *f, double *cstrv, double nlconstr[],
int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *f, double *cstrv, double nlconstr[],
const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[],
const double xl[], const double xu[],
double f0, const double nlconstr0[],
int *nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, int *info);
int bobyqa_c(prima_obj calfun, const void *data, const int n, double x[], double *f, const double xl[], const double xu[],
int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *f, 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 newuoa_c(prima_obj calfun, const void *data, const int n, double x[], double *f,
int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *f,
int *nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, int *info);
int uobyqa_c(prima_obj calfun, const void *data, const int n, double x[], double *f,
int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *f,
int *nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, int *info);
int lincoa_c(prima_obj calfun, const void *data, const int n, double x[], double *f,
int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *f,
double *cstrv,
const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[],
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_problem(prima_problem *problem, prima_options *options, int alloc_bounds, int use_constr)
int prima_check_problem(prima_problem_t *problem, prima_options_t *options, int alloc_bounds, int use_constr)
{
if (!problem)
return PRIMA_NULL_PROBLEM;
Expand Down Expand Up @@ -93,7 +93,7 @@ int prima_check_problem(prima_problem *problem, prima_options *options, int allo
return 0;
}

int prima_free_problem(prima_problem *problem)
int prima_free_problem(prima_problem_t *problem)
{
if (problem)
{
Expand Down Expand Up @@ -121,11 +121,11 @@ int prima_free_problem(prima_problem *problem)
return PRIMA_NULL_PROBLEM;
}

int prima_init_result(prima_result *result, prima_problem *problem)
int prima_init_result(prima_result_t *result, prima_problem_t *problem)
{
if (result)
{
memset(result, 0, sizeof(prima_result));
memset(result, 0, sizeof(prima_result_t));
result->f = 0.0;
result->cstrv = 0.0;
if (!problem)
Expand All @@ -142,7 +142,7 @@ int prima_init_result(prima_result *result, prima_problem *problem)
return PRIMA_NULL_RESULT;
}

int prima_free_result(prima_result *result)
int prima_free_result(prima_result_t *result)
{
if (result)
{
Expand All @@ -159,7 +159,7 @@ int prima_free_result(prima_result *result)
}

/* these functions just call the fortran compatibility layer and return the status code */
int prima_minimize(prima_algorithm algorithm, 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)
{
int alloc_bounds = (algorithm == PRIMA_COBYLA) || (algorithm == PRIMA_BOBYQA) || (algorithm == PRIMA_LINCOA);
int use_constr = (algorithm == PRIMA_COBYLA);
Expand Down Expand Up @@ -235,7 +235,7 @@ int prima_minimize(prima_algorithm algorithm, prima_problem *problem, prima_opti
return info;
}

const char *prima_get_rc_string(const int rc)
const char *prima_get_rc_string(const prima_rc_t rc)
{
switch (rc)
{
Expand Down
8 changes: 4 additions & 4 deletions c/tests/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ int main(int argc, char * argv[])
double x0[] = {0, 0};
double xl[] = {-6.0, -6.0};
double xu[] = {6.0, 6.0};
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calcfc = &fun_con;
problem.calfun = &fun;
problem.x0 = x0;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_RHO;
options.maxfun = 500*n;
Expand All @@ -90,8 +90,8 @@ int main(int argc, char * argv[])
problem.bineq = bineq;
problem.xl = xl;
problem.xu = xu;
prima_result result;
int algorithm = 0;
prima_result_t result;
prima_algorithm_t algorithm = 0;
if(strcmp(algo, "bobyqa") == 0)
{
algorithm = PRIMA_BOBYQA;
Expand Down
8 changes: 4 additions & 4 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ int main(int argc, char * argv[])
double x0[N_MAX];
double xl[N_MAX];
double xu[N_MAX];
prima_problem problem;
prima_problem_t problem;
prima_init_problem(&problem, N_MAX);
problem.x0 = x0;
problem.calcfc = &fun_con;
problem.calfun = &fun;
prima_options options;
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_RHO;
options.maxfun = 500*N_MAX;
Expand All @@ -109,8 +109,8 @@ int main(int argc, char * argv[])
xl[i] = -1.0;
xu[i] = 1.0;
}
int algorithm = 0;
prima_result result;
prima_algorithm_t algorithm = 0;
prima_result_t result;
if(strcmp(algo, "bobyqa") == 0)
{
algorithm = PRIMA_BOBYQA;
Expand Down

0 comments on commit e2b86e1

Please sign in to comment.