chiark / gitweb /
use HUGE_VAL instead of 1e6 for "infinity" in DIRECT
[nlopt.git] / direct / direct_wrap.c
1 /* C-style API for DIRECT functions.  SGJ (August 2007). */
2
3 #include "direct-internal.h"
4
5 /* Perform global minimization using (Gablonsky implementation of) DIRECT
6    algorithm.   Arguments:
7
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)
16
17    dimension: the number of minimization variable dimensions
18    lower_bounds, upper_bounds: arrays of length dimension of variable bounds
19
20    x: an array of length dimension, set to optimum variables upon return
21    fmin: on return, set to minimum f value
22
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)
27
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
33
34    logfile: an output file to write diagnostic info to (NULL for no I/O)
35
36    algorithm: whether to use the original DIRECT algorithm (DIRECT_ORIGINAL)
37               or Gablonsky's "improved" version (DIRECT_GABLONSKY)
38 */
39 direct_return_code direct_optimize(
40      direct_objective_func f, void *f_data,
41      int dimension,
42      const double *lower_bounds, const double *upper_bounds,
43
44      double *x, double *fmin, 
45
46      int max_feval, int max_iter,
47      double reltol, double abstol,
48      double volume_reltol, double sigma_reltol,
49
50      double fglobal,
51      double fglobal_reltol,
52
53      FILE *logfile,
54      direct_algorithm algorithm)
55 {
56      integer algmethod = algorithm == DIRECT_GABLONSKY;
57      integer ierror;
58      doublereal *l, *u;
59      int i;
60
61      /* convert to percentages: */
62      volume_reltol *= 100;
63      sigma_reltol *= 100;
64      fglobal_reltol *= 100;
65
66      /* make sure these are ignored if <= 0 */
67      if (volume_reltol <= 0) volume_reltol = -1;
68      if (sigma_reltol <= 0) sigma_reltol = -1;
69
70      if (fglobal == DIRECT_UNKNOWN_FGLOBAL)
71           fglobal_reltol = DIRECT_UNKNOWN_FGLOBAL_RELTOL;
72
73      if (dimension < 1) return DIRECT_INVALID_ARGS;
74
75      l = (doublereal *) malloc(sizeof(doublereal) * dimension * 2);
76      if (!l) return DIRECT_OUT_OF_MEMORY;
77      u = l + dimension;
78      for (i = 0; i < dimension; ++i) {
79           l[i] = lower_bounds[i];
80           u[i] = upper_bounds[i];
81      }
82      
83      direct_direct_(f, x, &dimension, &reltol, abstol,
84                     &max_feval, &max_iter,
85                     fmin,
86                     l, u,
87                     &algmethod,
88                     &ierror,
89                     logfile,
90                     &fglobal, &fglobal_reltol,
91                     &volume_reltol, &sigma_reltol,
92                     f_data);
93
94      free(l);
95
96      return (direct_return_code) ierror;
97 }