Skip to content

Commit 291ced2

Browse files
committed
Refactor prima_is_success to accept options.ctol
This is in reference to libprima#195
1 parent c44deb8 commit 291ced2

File tree

5 files changed

+13
-16
lines changed

5 files changed

+13
-16
lines changed

c/cobyla_c.f90

+2-4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
7171
real(RP) :: beq_loc(m_eq)
7272
real(RP) :: bineq_loc(m_ineq)
7373
real(RP) :: cstrv_loc
74+
real(RP) :: ctol_loc
7475
real(RP) :: f_loc
7576
real(RP) :: ftarget_loc
7677
real(RP) :: nlconstr_loc(m_nlcon)
@@ -79,7 +80,6 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
7980
real(RP), allocatable :: nlconstr0_loc(:)
8081
real(RP), allocatable :: rhobeg_loc
8182
real(RP), allocatable :: rhoend_loc
82-
real(RP), allocatable :: ctol_loc
8383
real(RP), allocatable :: xl_loc(:)
8484
real(RP), allocatable :: xu_loc(:)
8585

@@ -131,9 +131,7 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, data_ptr, n, x, f, cstrv, nlconstr, m_
131131
maxfun_loc = int(maxfun, kind(maxfun_loc))
132132
end if
133133
iprint_loc = int(iprint, kind(iprint_loc))
134-
if (.not. is_nan(ctol)) then
135-
ctol_loc = real(ctol, kind(ctol_loc))
136-
end if
134+
ctol_loc = real(ctol, kind(ctol_loc))
137135

138136
! Call the Fortran code
139137
if (c_associated(callback_ptr)) then

c/include/prima/prima.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
300300

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

305305
#ifdef __cplusplus
306306
}

c/lincoa_c.f90

+2-4
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ subroutine lincoa_c(cobj_ptr, data_ptr, n, x, f, cstrv, m_ineq, Aineq, bineq, m_
6868
real(RP) :: beq_loc(m_eq)
6969
real(RP) :: bineq_loc(m_ineq)
7070
real(RP) :: cstrv_loc
71+
real(RP) :: ctol_loc
7172
real(RP) :: f_loc
7273
real(RP) :: ftarget_loc
7374
real(RP) :: x_loc(n)
7475
real(RP), allocatable :: rhobeg_loc
7576
real(RP), allocatable :: rhoend_loc
76-
real(RP), allocatable :: ctol_loc
7777
real(RP), allocatable :: xl_loc(:)
7878
real(RP), allocatable :: xu_loc(:)
7979

@@ -120,9 +120,7 @@ subroutine lincoa_c(cobj_ptr, data_ptr, n, x, f, cstrv, m_ineq, Aineq, bineq, m_
120120
npt_loc = int(npt, kind(npt_loc))
121121
end if
122122
iprint_loc = int(iprint, kind(iprint_loc))
123-
if (.not. is_nan(ctol)) then
124-
ctol_loc = real(ctol, kind(ctol_loc))
125-
end if
123+
ctol_loc = real(ctol, kind(ctol_loc))
126124

127125
! Call the Fortran code
128126
if (c_associated(callback_ptr)) then

c/prima.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ prima_rc_t prima_init_options(prima_options_t *const options)
5757
options->rhoend = NAN; // Will be interpreted by Fortran as not present
5858
options->iprint = PRIMA_MSG_NONE;
5959
options->ftarget = -INFINITY;
60-
options->ctol = NAN; // Will be interpreted by Fortran as not present
60+
options->ctol = sqrt(DBL_EPSILON);
6161
return PRIMA_RC_DFT;
6262
}
6363

@@ -268,8 +268,8 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
268268
return info;
269269
}
270270

271-
bool prima_is_success(const prima_result_t result)
271+
bool prima_is_success(const prima_result_t result, const prima_options_t options)
272272
{
273-
return (result.status == PRIMA_SMALL_TR_RADIUS ||
274-
result.status == PRIMA_FTARGET_ACHIEVED) && (result.cstrv <= sqrt(DBL_EPSILON));
273+
return ((result.status == PRIMA_SMALL_TR_RADIUS && result.cstrv <= options.ctol) ||
274+
(result.status == PRIMA_FTARGET_ACHIEVED));
275275
}

python/_prima.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ class SelfCleaningPyObject {
3030

3131
struct PRIMAResult {
3232
// Construct PRIMAResult from prima_result_t
33-
PRIMAResult(const prima_result_t& result, const int num_vars, const int num_constraints, const std::string method) :
33+
PRIMAResult(const prima_result_t& result, const int num_vars, const int num_constraints, const std::string method
34+
const prima_options_t& options) :
3435
x(num_vars, result.x),
35-
success(prima_is_success(result)),
36+
success(prima_is_success(result, options)),
3637
status(result.status),
3738
message(result.message),
3839
fun(result.f),
@@ -325,7 +326,7 @@ PYBIND11_MODULE(_prima, m) {
325326
// Initialize the result, call the function, convert the return type, and return it.
326327
prima_result_t result;
327328
const prima_rc_t rc = prima_minimize(algorithm, problem, options, &result);
328-
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>());
329+
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>(), options);
329330
prima_free_result(&result);
330331
return result_copy;
331332
}, "fun"_a, "x0"_a, "args"_a=py::tuple(), "method"_a=py::none(),

0 commit comments

Comments
 (0)