Skip to content

Commit

Permalink
Refactor prima_is_success to accept options.ctol
Browse files Browse the repository at this point in the history
This is in reference to libprima#195
  • Loading branch information
nbelakovski committed May 1, 2024
1 parent c44deb8 commit 13b16bc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
6 changes: 2 additions & 4 deletions c/cobyla_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
real(RP) :: beq_loc(m_eq)
real(RP) :: bineq_loc(m_ineq)
real(RP) :: cstrv_loc
real(RP) :: ctol_loc
real(RP) :: f_loc
real(RP) :: ftarget_loc
real(RP) :: nlconstr_loc(m_nlcon)
Expand All @@ -79,7 +80,6 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
real(RP), allocatable :: nlconstr0_loc(:)
real(RP), allocatable :: rhobeg_loc
real(RP), allocatable :: rhoend_loc
real(RP), allocatable :: ctol_loc
real(RP), allocatable :: xl_loc(:)
real(RP), allocatable :: xu_loc(:)

Expand Down Expand Up @@ -131,9 +131,7 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
maxfun_loc = int(maxfun, kind(maxfun_loc))
end if
iprint_loc = int(iprint, kind(iprint_loc))
if (.not. is_nan(ctol)) then
ctol_loc = real(ctol, kind(ctol_loc))
end if
ctol_loc = real(ctol, kind(ctol_loc))

! Call the Fortran code
if (c_associated(callback_ptr)) then
Expand Down
6 changes: 2 additions & 4 deletions c/lincoa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ subroutine lincoa_c(cobj_ptr, data_ptr, n, x, f, cstrv, m_ineq, Aineq, bineq, m_
real(RP) :: beq_loc(m_eq)
real(RP) :: bineq_loc(m_ineq)
real(RP) :: cstrv_loc
real(RP) :: ctol_loc
real(RP) :: f_loc
real(RP) :: ftarget_loc
real(RP) :: x_loc(n)
real(RP), allocatable :: rhobeg_loc
real(RP), allocatable :: rhoend_loc
real(RP), allocatable :: ctol_loc
real(RP), allocatable :: xl_loc(:)
real(RP), allocatable :: xu_loc(:)

Expand Down Expand Up @@ -120,9 +120,7 @@ subroutine lincoa_c(cobj_ptr, data_ptr, n, x, f, cstrv, m_ineq, Aineq, bineq, m_
npt_loc = int(npt, kind(npt_loc))
end if
iprint_loc = int(iprint, kind(iprint_loc))
if (.not. is_nan(ctol)) then
ctol_loc = real(ctol, kind(ctol_loc))
end if
ctol_loc = real(ctol, kind(ctol_loc))

! Call the Fortran code
if (c_associated(callback_ptr)) then
Expand Down
8 changes: 4 additions & 4 deletions c/prima.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ prima_rc_t prima_init_options(prima_options_t *const options)
options->rhoend = NAN; // Will be interpreted by Fortran as not present
options->iprint = PRIMA_MSG_NONE;
options->ftarget = -INFINITY;
options->ctol = NAN; // Will be interpreted by Fortran as not present
options->ctol = sqrt(DBL_EPSILON);
return PRIMA_RC_DFT;
}

Expand Down Expand Up @@ -268,8 +268,8 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
return info;
}

bool prima_is_success(const prima_result_t result)
bool prima_is_success(const prima_result_t result, const prima_options_t options)
{
return (result.status == PRIMA_SMALL_TR_RADIUS ||
result.status == PRIMA_FTARGET_ACHIEVED) && (result.cstrv <= sqrt(DBL_EPSILON));
return ((result.status == PRIMA_SMALL_TR_RADIUS && result.cstrv <= options.ctol) ||
(result.status == PRIMA_FTARGET_ACHIEVED));
}
7 changes: 4 additions & 3 deletions python/_prima.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ 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) :
PRIMAResult(const prima_result_t& result, const int num_vars, const int num_constraints, const std::string method
const prima_options_t& options) :
x(num_vars, result.x),
success(prima_is_success(result)),
success(prima_is_success(result, options)),
status(result.status),
message(result.message),
fun(result.f),
Expand Down Expand Up @@ -325,7 +326,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>());
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>(), options);
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 13b16bc

Please sign in to comment.