Skip to content

Commit

Permalink
Merge pull request libprima#192 from nbelakovski/return_code_types
Browse files Browse the repository at this point in the history
Change return types to prima_rc_t. Thank @nbelakovski !
  • Loading branch information
zaikunzhang authored Apr 19, 2024
2 parents 97ac4c3 + b5ad679 commit c13a9cc
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 34 deletions.
2 changes: 1 addition & 1 deletion c/examples/bobyqa/bobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_BOBYQA, problem, options, &result);
const prima_rc_t rc = prima_minimize(PRIMA_BOBYQA, problem, options, &result);

// Print the result
printf("x* = {%g, %g}, rc = %d, msg = '%s', evals = %d\n", result.x[0], result.x[1], rc, result.message, result.nf);
Expand Down
2 changes: 1 addition & 1 deletion c/examples/cobyla/cobyla_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_COBYLA, problem, options, &result);
const prima_rc_t rc = prima_minimize(PRIMA_COBYLA, problem, options, &result);

// Print the 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);
Expand Down
2 changes: 1 addition & 1 deletion c/examples/lincoa/lincoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_LINCOA, problem, options, &result);
const prima_rc_t rc = prima_minimize(PRIMA_LINCOA, problem, options, &result);

// Print the 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);
Expand Down
2 changes: 1 addition & 1 deletion c/examples/newuoa/newuoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_NEWUOA, problem, options, &result);
const prima_rc_t rc = prima_minimize(PRIMA_NEWUOA, problem, options, &result);

// Print the result
printf("x* = {%g, %g}, rc = %d, msg = '%s', evals = %d\n", result.x[0], result.x[1], rc, result.message, result.nf);
Expand Down
2 changes: 1 addition & 1 deletion c/examples/uobyqa/uobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main(int argc, char * argv[])

// Run the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_UOBYQA, problem, options, &result);
const prima_rc_t rc = prima_minimize(PRIMA_UOBYQA, problem, options, &result);

// Print the result
printf("x* = {%g, %g}, rc = %d, msg = '%s', evals = %d\n", result.x[0], result.x[1], rc, result.message, result.nf);
Expand Down
9 changes: 5 additions & 4 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef enum {

// Possible return values
typedef enum {
PRIMA_RC_DFT = 0,
PRIMA_SMALL_TR_RADIUS = 0,
PRIMA_FTARGET_ACHIEVED = 1,
PRIMA_TRSUBP_FAILED = 2,
Expand Down Expand Up @@ -188,7 +189,7 @@ typedef struct {

// Function to initialize the problem
PRIMAC_API
int prima_init_problem(prima_problem_t *const problem, const int n);
prima_rc_t prima_init_problem(prima_problem_t *const problem, const int n);


// Structure to hold the options
Expand Down Expand Up @@ -246,7 +247,7 @@ typedef struct {

// Function to initialize the options
PRIMAC_API
int prima_init_options(prima_options_t *const options);
prima_rc_t prima_init_options(prima_options_t *const options);


// Structure to hold the result
Expand Down Expand Up @@ -281,7 +282,7 @@ typedef struct {

// Function to free the result
PRIMAC_API
int prima_free_result(prima_result_t *const result);
prima_rc_t prima_free_result(prima_result_t *const result);


/*
Expand All @@ -296,7 +297,7 @@ int prima_free_result(prima_result_t *const result);
* return : see prima_rc_t enum for return codes
*/
PRIMAC_API
int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result);
prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result);


#ifdef __cplusplus
Expand Down
28 changes: 14 additions & 14 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@


// Function to initialize the problem
int prima_init_problem(prima_problem_t *const problem, const int n)
prima_rc_t prima_init_problem(prima_problem_t *const problem, const int n)
{
if (!problem)
return PRIMA_NULL_PROBLEM;

memset(problem, 0, sizeof(prima_problem_t));
problem->n = n;
problem->f0 = NAN;
return 0;
return PRIMA_RC_DFT;
}


// Function to initialize the options
int prima_init_options(prima_options_t *const options)
prima_rc_t prima_init_options(prima_options_t *const options)
{
if (!options)
return PRIMA_NULL_OPTIONS;
Expand All @@ -58,12 +58,12 @@ int prima_init_options(prima_options_t *const options)
options->iprint = PRIMA_MSG_NONE;
options->ftarget = -INFINITY;
options->ctol = NAN; // Will be interpreted by Fortran as not present
return 0;
return PRIMA_RC_DFT;
}


// Function to check whether the problem matches the algorithm
int prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm)
prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm)
{
if (algorithm != PRIMA_COBYLA && (problem.calcfc || problem.nlconstr0 || problem.m_nlcon > 0))
return PRIMA_PROBLEM_SOLVER_MISMATCH_NONLINEAR_CONSTRAINTS;
Expand All @@ -81,12 +81,12 @@ int prima_check_problem(const prima_problem_t problem, const prima_algorithm_t a
if ((algorithm == PRIMA_COBYLA && !problem.calcfc) || (algorithm != PRIMA_COBYLA && !problem.calfun))
return PRIMA_NULL_FUNCTION;

return 0;
return PRIMA_RC_DFT;
}


// Function to initialize the result
int prima_init_result(prima_result_t *const result, const prima_problem_t problem)
prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem)
{
if (!result)
return PRIMA_NULL_RESULT;
Expand Down Expand Up @@ -124,12 +124,12 @@ int prima_init_result(prima_result_t *const result, const prima_problem_t proble
for (int i = 0; i < problem.m_nlcon; i++)
result->nlconstr[i] = NAN;

return 0;
return PRIMA_RC_DFT;
}


// Function to free the result
int prima_free_result(prima_result_t *const result)
prima_rc_t prima_free_result(prima_result_t *const result)
{
if (!result)
return PRIMA_NULL_RESULT;
Expand All @@ -142,7 +142,7 @@ int prima_free_result(prima_result_t *const result)
free(result->x);
result->x = NULL;
}
return 0;
return PRIMA_RC_DFT;
}


Expand Down Expand Up @@ -230,14 +230,14 @@ int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], doub


// The function that does the minimization using a PRIMA solver
int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result)
prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result)
{
int info = prima_init_result(result, problem);
prima_rc_t info = prima_init_result(result, problem);

if (info == 0)
if (info == PRIMA_RC_DFT)
info = prima_check_problem(problem, algorithm);

if (info == 0) {
if (info == PRIMA_RC_DFT) {
// We copy x0 into result->x only after prima_check_problem has succeeded,
// so that if prima_check_problem failed, result->x will not contained a
// seemingly valid value.
Expand Down
2 changes: 1 addition & 1 deletion c/tests/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
int rc = prima_minimize(algorithm, problem, options, &result);
const prima_rc_t rc = prima_minimize(algorithm, problem, options, &result);

// Print the result
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, result.message, result.nf);
Expand Down
24 changes: 14 additions & 10 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ static void fun_con(const double x[], double *const f, double constr[], const vo
(void)data;
}

unsigned int get_random_seed(void) {
// Set the random seed to year/week
char buf[10] = {0};
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
int rc = strftime(buf, 10, "%y%W", tmp);
if (!rc)
return 42;
else
return atoi(buf);
}

// Main function
int main(int argc, char * argv[])
{
Expand All @@ -75,15 +87,7 @@ int main(int argc, char * argv[])
debug = (strcmp(argv[2], "debug") == 0);
printf("Debug = %d\n", debug);

// Set the random seed to year/week
// FIXME: Implement this as a function
char buf[10] = {0};
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
int rc = strftime(buf, 10, "%y%W", tmp);
if (!rc)
return 1;
unsigned seed = atoi(buf);
unsigned int seed = get_random_seed();
printf("Random seed = %d\n", seed);
srand(seed);

Expand Down Expand Up @@ -168,7 +172,7 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
rc = prima_minimize(algorithm, problem, options, &result);
const prima_rc_t rc = prima_minimize(algorithm, problem, options, &result);

// Print the result
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, result.message, result.nf);
Expand Down

0 comments on commit c13a9cc

Please sign in to comment.