1 /* C-style API for DIRECT functions. SGJ (August 2007). */
3 #include "direct-internal.h"
5 /* Perform global minimization using (Gablonsky implementation of) DIRECT
8 f, f_data: the objective function and any user data
9 -- the objective function f(n, x, undefined_flag, data) takes 4 args:
10 int n: the dimension, same as dimension arg. to direct_optimize
11 const double *x: array x[n] of point to evaluate
12 int *undefined_flag: set to 1 on return if x violates constraints
13 or don't touch otherwise
14 void *data: same as f_data passed to direct_optimize
15 return value = value of f(x)
17 dimension: the number of minimization variable dimensions
18 lower_bounds, upper_bounds: arrays of length dimension of variable bounds
20 x: an array of length dimension, set to optimum variables upon return
21 fmin: on return, set to minimum f value
23 max_feval, max_iter: maximum number of function evaluations & DIRECT iters
24 reltol, abstol: relative and absolute tolerances (0 if none)
25 volume_reltol: relative tolerance on hypercube volume (0 if none)
26 sigma_reltol: relative tolerance on hypercube "measure" (??) (0 if none)
28 fglobal: the global minimum of f, if known ahead of time
29 -- this is mainly for benchmarking, in most cases it
30 is not known and you should pass DIRECT_UNKNOWN_FGLOBAL
31 fglobal_reltol: relative tolerance on how close we should find fglobal
32 -- ignored if fglobal is DIRECT_UNKNOWN_FGLOBAL
34 logfile: an output file to write diagnostic info to (NULL for no I/O)
36 algorithm: whether to use the original DIRECT algorithm (DIRECT_ORIGINAL)
37 or Gablonsky's "improved" version (DIRECT_GABLONSKY)
39 direct_return_code direct_optimize(
40 direct_objective_func f, void *f_data,
42 const double *lower_bounds, const double *upper_bounds,
44 double *x, double *fmin,
46 int max_feval, int max_iter,
47 double reltol, double abstol,
48 double volume_reltol, double sigma_reltol,
51 double fglobal_reltol,
54 direct_algorithm algorithm)
56 integer algmethod = algorithm == DIRECT_GABLONSKY;
61 /* convert to percentages: */
64 fglobal_reltol *= 100;
66 /* make sure these are ignored if <= 0 */
67 if (volume_reltol <= 0) volume_reltol = -1;
68 if (sigma_reltol <= 0) sigma_reltol = -1;
70 if (fglobal == DIRECT_UNKNOWN_FGLOBAL)
71 fglobal_reltol = DIRECT_UNKNOWN_FGLOBAL_RELTOL;
73 if (dimension < 1) return DIRECT_INVALID_ARGS;
75 l = (doublereal *) malloc(sizeof(doublereal) * dimension * 2);
76 if (!l) return DIRECT_OUT_OF_MEMORY;
78 for (i = 0; i < dimension; ++i) {
79 l[i] = lower_bounds[i];
80 u[i] = upper_bounds[i];
83 direct_direct_(f, x, &dimension, &reltol, abstol,
84 &max_feval, &max_iter,
90 &fglobal, &fglobal_reltol,
91 &volume_reltol, &sigma_reltol,
96 return (direct_return_code) ierror;