Skip to content

Commit a1b7f19

Browse files
committed
240430.174247.HKT try prima_internal.h
1 parent daed3f3 commit a1b7f19

File tree

3 files changed

+64
-40
lines changed

3 files changed

+64
-40
lines changed

c/include/prima/prima_internal.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef PRIMA_INTERNAL_H
2+
#define PRIMA_INTERNAL_H
3+
4+
#include "prima/prima.h"
5+
6+
unsigned int get_random_seed(void);
7+
8+
// Function to check whether the problem matches the algorithm
9+
prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm);
10+
11+
// Function to initialize the result
12+
prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem);
13+
14+
// Functions implemented in Fortran (*_c.f90)
15+
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[],
16+
const int m_ineq, const double Aineq[], const double bineq[],
17+
const int m_eq, const double Aeq[], const double beq[],
18+
const double xl[], const double xu[],
19+
const double f0, const double nlconstr0[],
20+
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const double ctol,
21+
const prima_callback_t callback, int *const info);
22+
23+
int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, const double xl[], const double xu[],
24+
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);
25+
26+
int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
27+
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);
28+
29+
int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
30+
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);
31+
32+
int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
33+
double *const cstrv, const int m_ineq, const double Aineq[], const double bineq[],
34+
const int m_eq, const double Aeq[], const double beq[], const double xl[], const double xu[],
35+
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const double ctol,
36+
const prima_callback_t callback, int *const info);
37+
38+
#endif

c/prima.c

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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
6666
prima_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
217207
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)
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-
}

c/tests/stress.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ static void fun_con(const double x[], double *const f, double constr[], const vo
6161
}
6262

6363
// A function generating a seed that alters weekly
64-
unsigned int get_random_seed(void) {
64+
unsigned int get_random_seed(void)
65+
{
6566
// Set the random seed to year/week
6667
char buf[10] = {0};
6768
time_t t = time(NULL);
68-
struct tm *tmp = localtime(&t);
69-
int rc = strftime(buf, 10, "%y%W", tmp);
69+
struct tm timeinfo;
70+
localtime_s(&timeinfo, &t);
71+
int rc = strftime(buf, 10, "%y%W", &timeinfo);
7072
if (!rc)
7173
return 42;
7274
else
@@ -87,7 +89,7 @@ int main(int argc, char * argv[])
8789
printf("Debug = %d\n", debug);
8890

8991
unsigned int seed = get_random_seed();
90-
printf("Random seed = %d\n", seed);
92+
printf("Random seed = %u\n", seed);
9193
srand(seed);
9294

9395
// Set up the options

0 commit comments

Comments
 (0)