11// Dedicated to the late Professor M. J. D. Powell FRS (1936--2015).
22
33
4- #include "prima/prima .h"
5- #include <float .h>
4+ #include "prima/prima_internal .h"
5+ #include <limits .h>
66#include <math.h>
77#include <stdio.h>
88#include <stdlib.h>
@@ -65,6 +65,16 @@ prima_rc_t prima_init_options(prima_options_t *const options)
6565// Function to check whether the problem matches the algorithm
6666prima_rc_t prima_check_problem (const prima_problem_t problem , const prima_algorithm_t algorithm )
6767{
68+ if (algorithm != PRIMA_COBYLA && (problem .calcfc || problem .nlconstr0 || problem .m_nlcon > 0 ))
69+ return PRIMA_PROBLEM_SOLVER_MISMATCH_NONLINEAR_CONSTRAINTS ;
70+
71+ if ((algorithm != PRIMA_COBYLA && algorithm != PRIMA_LINCOA ) &&
72+ (problem .m_ineq > 0 || problem .m_eq > 0 || problem .Aineq || problem .bineq || problem .Aeq || problem .beq ))
73+ return PRIMA_PROBLEM_SOLVER_MISMATCH_LINEAR_CONSTRAINTS ;
74+
75+ if ((algorithm != PRIMA_COBYLA && algorithm != PRIMA_LINCOA && algorithm != PRIMA_BOBYQA ) && (problem .xl || problem .xu ))
76+ return PRIMA_PROBLEM_SOLVER_MISMATCH_BOUNDS ;
77+
6878 if (!problem .x0 )
6979 return PRIMA_NULL_X0 ;
7080
@@ -90,10 +100,10 @@ prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t
90100 result -> cstrv = NAN ;
91101
92102 // nf: number of function evaluations
93- result -> nf = 0 ;
103+ result -> nf = INT_MIN ;
94104
95105 // status: return code
96- result -> status = PRIMA_RESULT_INITIALIZED ;
106+ result -> status = INT_MIN ;
97107
98108 // message: exit message
99109 result -> message = NULL ;
@@ -182,37 +192,17 @@ const char *prima_get_rc_string(const prima_rc_t rc)
182192 return "NULL result" ;
183193 case PRIMA_NULL_FUNCTION :
184194 return "NULL function" ;
195+ case PRIMA_PROBLEM_SOLVER_MISMATCH_NONLINEAR_CONSTRAINTS :
196+ return "Nonlinear constraints were provided for an algorithm that cannot handle them" ;
197+ case PRIMA_PROBLEM_SOLVER_MISMATCH_LINEAR_CONSTRAINTS :
198+ return "Linear constraints were provided for an algorithm that cannot handle them" ;
199+ case PRIMA_PROBLEM_SOLVER_MISMATCH_BOUNDS :
200+ return "Bounds were provided for an algorithm that cannot handle them" ;
185201 default :
186202 return "Invalid return code" ;
187203 }
188204}
189205
190-
191- // Functions implemented in Fortran (*_c.f90)
192- int cobyla_c (const int m_nlcon , const prima_objcon_t calcfc , const void * data , const int n , double x [], double * const f , double * const cstrv , double nlconstr [],
193- const int m_ineq , const double Aineq [], const double bineq [],
194- const int m_eq , const double Aeq [], const double beq [],
195- const double xl [], const double xu [],
196- const double f0 , const double nlconstr0 [],
197- int * const nf , const double rhobeg , const double rhoend , const double ftarget , const int maxfun , const int iprint , const double ctol ,
198- const prima_callback_t callback , int * const info );
199-
200- int bobyqa_c (prima_obj_t calfun , const void * data , const int n , double x [], double * const f , const double xl [], const double xu [],
201- int * const nf , const double rhobeg , const double rhoend , const double ftarget , const int maxfun , const int npt , const int iprint , const prima_callback_t callback , int * const info );
202-
203- int newuoa_c (prima_obj_t calfun , const void * data , const int n , double x [], double * const f ,
204- int * const nf , const double rhobeg , const double rhoend , const double ftarget , const int maxfun , const int npt , const int iprint , const prima_callback_t callback , int * const info );
205-
206- int uobyqa_c (prima_obj_t calfun , const void * data , const int n , double x [], double * const f ,
207- int * const nf , const double rhobeg , const double rhoend , const double ftarget , const int maxfun , const int iprint , const prima_callback_t callback , int * const info );
208-
209- int lincoa_c (prima_obj_t calfun , const void * data , const int n , double x [], double * const f ,
210- double * const cstrv , const int m_ineq , const double Aineq [], const double bineq [],
211- const int m_eq , const double Aeq [], const double beq [], const double xl [], const double xu [],
212- int * const nf , const double rhobeg , const double rhoend , const double ftarget , const int maxfun , const int npt , const int iprint , const double ctol ,
213- const prima_callback_t callback , int * const info );
214-
215-
216206// The function that does the minimization using a PRIMA solver
217207prima_rc_t prima_minimize (const prima_algorithm_t algorithm , const prima_problem_t problem , const prima_options_t options , prima_result_t * const result )
218208{
@@ -267,9 +257,3 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
267257
268258 return info ;
269259}
270-
271- bool prima_is_success (const prima_result_t result )
272- {
273- return (result .status == PRIMA_SMALL_TR_RADIUS ||
274- result .status == PRIMA_FTARGET_ACHIEVED ) && (result .cstrv <= sqrt (DBL_EPSILON ));
275- }
0 commit comments