Skip to content

Commit

Permalink
Merge pull request #200 from nbelakovski/ctol
Browse files Browse the repository at this point in the history
Refactor prima_is_success to accept options.ctol
  • Loading branch information
zaikunzhang authored May 6, 2024
2 parents 403e5bf + 4b9d9fd commit 9bbc828
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 25 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
12 changes: 3 additions & 9 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ typedef enum {
} prima_rc_t;


// Function to get the message string corresponding to a return code
PRIMAC_API
const char *prima_get_rc_string(const prima_rc_t rc);


/*
* Objective function required by UOBYQA, NEWUOA, BOBYQA, and LINCOA
* x : on input, the vector of variables (should not be modified)
Expand Down Expand Up @@ -272,6 +267,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 +296,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);

#ifdef __cplusplus
}
#endif
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
10 changes: 3 additions & 7 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 @@ -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)
{
return (result.status == PRIMA_SMALL_TR_RADIUS ||
result.status == PRIMA_FTARGET_ACHIEVED) && (result.cstrv <= sqrt(DBL_EPSILON));
}
2 changes: 1 addition & 1 deletion python/_prima.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ 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) :
x(num_vars, result.x),
success(prima_is_success(result)),
success(result.success),
status(result.status),
message(result.message),
fun(result.f),
Expand Down

0 comments on commit 9bbc828

Please sign in to comment.