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 minf: on return, set to minimum f value
23 magic_eps, magic_eps_abs: Jones' "magic" epsilon parameter, and
24 also an absolute version of the same
25 (not multipled by minf). Jones suggests
26 setting this to 1e-4, but 0 also works...
28 max_feval, max_iter: maximum number of function evaluations & DIRECT iters
29 volume_reltol: relative tolerance on hypercube volume (0 if none)
30 sigma_reltol: relative tolerance on hypercube "measure" (??) (0 if none)
32 fglobal: the global minimum of f, if known ahead of time
33 -- this is mainly for benchmarking, in most cases it
34 is not known and you should pass DIRECT_UNKNOWN_FGLOBAL
35 fglobal_reltol: relative tolerance on how close we should find fglobal
36 -- ignored if fglobal is DIRECT_UNKNOWN_FGLOBAL
38 logfile: an output file to write diagnostic info to (NULL for no I/O)
40 algorithm: whether to use the original DIRECT algorithm (DIRECT_ORIGINAL)
41 or Gablonsky's "improved" version (DIRECT_GABLONSKY)
43 direct_return_code direct_optimize(
44 direct_objective_func f, void *f_data,
46 const double *lower_bounds, const double *upper_bounds,
48 double *x, double *minf,
50 int max_feval, int max_iter,
51 double start, double maxtime,
52 double magic_eps, double magic_eps_abs,
53 double volume_reltol, double sigma_reltol,
57 double fglobal_reltol,
60 direct_algorithm algorithm)
62 integer algmethod = algorithm == DIRECT_GABLONSKY;
67 /* convert to percentages: */
70 fglobal_reltol *= 100;
72 /* make sure these are ignored if <= 0 */
73 if (volume_reltol <= 0) volume_reltol = -1;
74 if (sigma_reltol <= 0) sigma_reltol = -1;
76 if (fglobal == DIRECT_UNKNOWN_FGLOBAL)
77 fglobal_reltol = DIRECT_UNKNOWN_FGLOBAL_RELTOL;
79 if (dimension < 1) return DIRECT_INVALID_ARGS;
81 l = (doublereal *) malloc(sizeof(doublereal) * dimension * 2);
82 if (!l) return DIRECT_OUT_OF_MEMORY;
84 for (i = 0; i < dimension; ++i) {
85 l[i] = lower_bounds[i];
86 u[i] = upper_bounds[i];
89 direct_direct_(f, x, &dimension, &magic_eps, magic_eps_abs,
90 &max_feval, &max_iter,
91 start, maxtime, force_stop,
97 &fglobal, &fglobal_reltol,
98 &volume_reltol, &sigma_reltol,
103 return (direct_return_code) ierror;