Skip to content

Commit

Permalink
Refactor success to be part of the result struct
Browse files Browse the repository at this point in the history
  • Loading branch information
nbelakovski committed May 5, 2024
1 parent a08db54 commit e0800ad
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
7 changes: 3 additions & 4 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ typedef struct {
// status: return code
prima_rc_t status;

// success: whether the solver returned normally or ran into abnormal conditions
bool success;

// message: exit message
const char *message;

Expand All @@ -298,10 +301,6 @@ PRIMAC_API
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);


// Function to check if PRIMA returned normally or ran into abnormal conditions
PRIMAC_API
bool prima_is_success(const prima_result_t result, const prima_options_t options);

#ifdef __cplusplus
}
#endif
Expand Down
8 changes: 2 additions & 6 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,9 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
}

result->status = info;
result->success = ((result->status == PRIMA_SMALL_TR_RADIUS && result->cstrv <= options.ctol) ||
(result->status == PRIMA_FTARGET_ACHIEVED));
result->message = prima_get_rc_string(info);

return info;
}

bool prima_is_success(const prima_result_t result, const prima_options_t options)
{
return ((result.status == PRIMA_SMALL_TR_RADIUS && result.cstrv <= options.ctol) ||
(result.status == PRIMA_FTARGET_ACHIEVED));
}
7 changes: 3 additions & 4 deletions python/_prima.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ class SelfCleaningPyObject {

struct PRIMAResult {
// Construct PRIMAResult from prima_result_t
PRIMAResult(const prima_result_t& result, const int num_vars, const int num_constraints, const std::string method,
const prima_options_t& options) :
PRIMAResult(const prima_result_t& result, const int num_vars, const int num_constraints, const std::string method) :
x(num_vars, result.x),
success(prima_is_success(result, options)),
success(result.success),
status(result.status),
message(result.message),
fun(result.f),
Expand Down Expand Up @@ -326,7 +325,7 @@ PYBIND11_MODULE(_prima, m) {
// Initialize the result, call the function, convert the return type, and return it.
prima_result_t result;
const prima_rc_t rc = prima_minimize(algorithm, problem, options, &result);
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>(), options);
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>());
prima_free_result(&result);
return result_copy;
}, "fun"_a, "x0"_a, "args"_a=py::tuple(), "method"_a=py::none(),
Expand Down

0 comments on commit e0800ad

Please sign in to comment.