Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change return types to prima_rc_t #192

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ int main(int argc, char * argv[])

// Call the solver
prima_result_t result;
rc = prima_minimize(algorithm, problem, options, &result);
const prima_rc_t rc2 = 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);
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, rc2, result.message, result.nf);

// Free the result
prima_free_result(&result);
Expand Down
Loading