chiark / gitweb /
use HUGE_VAL instead of 1e6 for "infinity" in DIRECT
[nlopt.git] / direct / DIRect.c
1 /* DIRect-transp.f -- translated by f2c (version 20050501).
2    
3    f2c output hand-cleaned by SGJ (August 2007). 
4 */
5
6 #include <math.h>
7 #include "direct-internal.h"
8
9 /* Common Block Declarations */
10
11 /* Table of constant values */
12
13 /* +-----------------------------------------------------------------------+ */
14 /* | Program       : Direct.f                                              | */
15 /* | Last modified : 07-16-2001                                            | */
16 /* | Written by    : Joerg Gablonsky (jmgablon@unity.ncsu.edu)             | */
17 /* |                 North Carolina State University                       | */
18 /* |                 Dept. of Mathematics                                  | */
19 /* | DIRECT is a method to solve problems of the form:                     | */
20 /* |              min f: Q --> R,                                          | */
21 /* | where f is the function to be minimized and Q is an n-dimensional     | */
22 /* | hyperrectangle given by the the following equation:                   | */
23 /* |                                                                       | */
24 /* |       Q={ x : l(i) <= x(i) <= u(i), i = 1,...,n }.                    | */
25 /* | Note: This version of DIRECT can also handle hidden constraints. By   | */
26 /* |       this we mean that the function may not be defined over the whole| */
27 /* |       hyperrectangle Q, but only over a subset, which is not given    | */
28 /* |       analytically.                                                   | */
29 /* |                                                                       | */
30 /* | We now give a brief outline of the algorithm:                         | */
31 /* |                                                                       | */
32 /* |   The algorithm starts with mapping the hyperrectangle Q to the       | */
33 /* |   n-dimensional unit hypercube. DIRECT then samples the function at   | */
34 /* |   the center of this hypercube and at 2n more points, 2 in each       | */
35 /* |   coordinate direction. Uisng these function values, DIRECT then      | */
36 /* |   divides the domain into hyperrectangles, each having exactly one of | */
37 /* |   the sampling points as its center. In each iteration, DIRECT chooses| */
38 /* |   some of the existing hyperrectangles to be further divided.         | */
39 /* |   We provide two different strategies of how to decide which          | */
40 /* |   hyperrectangles DIRECT divides and several different convergence    | */
41 /* |   criteria.                                                           | */
42 /* |                                                                       | */
43 /* |   DIRECT was designed to solve problems where the function f is       | */
44 /* |   Lipschitz continues. However, DIRECT has proven to be effective on  | */
45 /* |   more complex problems than these.                                   | */
46 /* +-----------------------------------------------------------------------+ */
47 /* Subroutine */ void direct_direct_(fp fcn, doublereal *x, integer *n, doublereal *eps, doublereal epsabs, integer *maxf, integer *maxt, doublereal *fmin, doublereal *l, 
48         doublereal *u, integer *algmethod, integer *ierror, FILE *logfile, 
49         doublereal *fglobal, doublereal *fglper, doublereal *volper, 
50         doublereal *sigmaper, void *fcn_data)
51 {
52     /* System generated locals */
53     integer i__1, i__2;
54     doublereal d__1;
55
56     /* constants (FIXME: change to variable?) */
57     const integer MAXFUNC = 90000;
58     const integer MAXDEEP = 600;
59     const integer MAXDIV = 3000;
60
61     /* Local variables */
62     integer increase;
63     doublereal *c__ = 0 /* was [90000][64] */, *f = 0   /* 
64             was [90000][2] */;
65     integer i__, j, *s = 0      /* was [3000][2] */, t;
66     doublereal *w = 0;
67     doublereal divfactor;
68     integer ifeasiblef, iepschange, actmaxdeep;
69     integer actdeep_div__, iinfesiblef;
70     integer pos1, newtosample;
71     integer ifree, help;
72     doublereal *oldl = 0, fmax;
73     integer maxi;
74     doublereal kmax, *oldu = 0;
75     integer oops, *list2 = 0    /* was [64][2] */, cheat;
76     doublereal delta;
77     integer mdeep, *point = 0, start;
78     integer *anchor = 0, *length = 0    /* was [90000][64] */, *arrayi = 0;
79     doublereal *levels = 0, *thirds = 0;
80     integer writed;
81     doublereal epsfix;
82     integer oldpos, minpos, maxpos, tstart, actdeep, ifreeold, oldmaxf;
83     integer numfunc, version;
84     integer jones;
85
86     /* FIXME: change sizes dynamically? */
87 #define MY_ALLOC(p, t, n) p = (t *) malloc(sizeof(t) * (n)); \
88                           if (!(p)) { *ierror = -100; goto cleanup; }
89
90     /* Note that I've transposed c__, length, and f relative to the 
91        original Fortran code.  e.g. length was length(maxfunc,n) 
92        in Fortran [ or actually length(maxfunc, maxdims), but by
93        using malloc I can just allocate n ], corresponding to
94        length[n][maxfunc] in C, but I've changed the code to access
95        it as length[maxfunc][n].  That is, the maxfunc direction
96        is the discontiguous one.  This makes it easier to resize
97        dynamically (by adding contiguous rows) using realloc, without
98        having to move data around manually. */
99     MY_ALLOC(c__, doublereal, MAXFUNC * (*n));
100     MY_ALLOC(length, integer, MAXFUNC * (*n));
101     MY_ALLOC(f, doublereal, MAXFUNC * 2);
102
103     MY_ALLOC(s, integer, MAXDIV * 2);
104     MY_ALLOC(w, doublereal, (*n));
105     MY_ALLOC(oldl, doublereal, (*n));
106     MY_ALLOC(oldu, doublereal, (*n));
107     MY_ALLOC(list2, integer, (*n) * 2);
108     MY_ALLOC(point, integer, MAXFUNC);
109     MY_ALLOC(anchor, integer, MAXDEEP + 2);
110     MY_ALLOC(arrayi, integer, (*n));
111     MY_ALLOC(levels, doublereal, MAXDEEP + 1);
112     MY_ALLOC(thirds, doublereal, MAXDEEP + 1);    
113
114 /* +-----------------------------------------------------------------------+ */
115 /* |    SUBROUTINE Direct                                                  | */
116 /* | On entry                                                              | */
117 /* |     fcn -- The argument containing the name of the user-supplied      | */
118 /* |            SUBROUTINE that returns values for the function to be      | */
119 /* |            minimized.                                                 | */
120 /* |       n -- The dimension of the problem.                              | */
121 /* |     eps -- Exceeding value. If eps > 0, we use the same epsilon for   | */
122 /* |            all iterations. If eps < 0, we use the update formula from | */
123 /* |            Jones:                                                     | */
124 /* |               eps = max(1.D-4*abs(fmin),epsfix),                      | */
125 /* |            where epsfix = abs(eps), the absolute value of eps which is| */
126 /* |            passed to the function.                                    | */
127 /* |    maxf -- The maximum number of function evaluations.                | */
128 /* |    maxT -- The maximum number of iterations.                          | */
129 /* |            Direct stops when either the maximum number of iterations  | */
130 /* |            is reached or more than maxf function-evalutions were made.| */
131 /* |       l -- The lower bounds of the hyperbox.                          | */
132 /* |       u -- The upper bounds of the hyperbox.                          | */
133 /* |algmethod-- Choose the method, that is either use the original method  | */
134 /* |            as described by Jones et.al. (0) or use our modification(1)| */
135 /* | logfile -- File-Handle for the logfile. DIRECT expects this file to be| */
136 /* |            opened and closed by the user outside of DIRECT. We moved  | */
137 /* |            this to the outside so the user can add extra informations | */
138 /* |            to this file before and after the call to DIRECT.          | */
139 /* | fglobal -- Function value of the global optimum. If this value is not | */
140 /* |            known (that is, we solve a real problem, not a testproblem)| */
141 /* |            set this value to -1.D100 and fglper (see below) to 0.D0.  | */
142 /* |  fglper -- Terminate the optimization when the percent error          | */
143 /* |                100(f_min - fglobal)/max(1,abs(fglobal)) < fglper.     | */
144 /* |  volper -- Terminate the optimization when the volume of the          | */
145 /* |            hyperrectangle S with f(c(S)) = fmin is less then volper   | */
146 /* |            percent of the volume of the original hyperrectangle.      | */
147 /* |sigmaper -- Terminate the optimization when the measure of the         | */
148 /* |            hyperrectangle S with f(c(S)) = fmin is less then sigmaper.| */
149 /* |                                                                       | */
150 /* | User data that is passed through without being changed:               | */
151 /* |  fcn_data - opaque pointer to any user data                           | */
152 /* |                                                                       | */
153 /* | On return                                                             | */
154 /* |                                                                       | */
155 /* |       x -- The final point obtained in the optimization process.      | */
156 /* |            X should be a good approximation to the global minimum     | */
157 /* |            for the function within the hyper-box.                     | */
158 /* |                                                                       | */
159 /* |    fmin -- The value of the function at x.                            | */
160 /* |  Ierror -- Error flag. If Ierror is lower 0, an error has occured. The| */
161 /* |            values of Ierror mean                                      | */
162 /* |            Fatal errors :                                             | */
163 /* |             -1   u(i) <= l(i) for some i.                             | */
164 /* |             -2   maxf is too large.                                   | */
165 /* |             -3   Initialization in DIRpreprc failed.                  | */
166 /* |             -4   Error in DIRSamplepoints, that is there was an error | */
167 /* |                  in the creation of the sample points.                | */
168 /* |             -5   Error in DIRSamplef, that is an error occured while  | */
169 /* |                  the function was sampled.                            | */
170 /* |             -6   Error in DIRDoubleInsert, that is an error occured   | */
171 /* |                  DIRECT tried to add all hyperrectangles with the same| */
172 /* |                  size and function value at the center. Either        | */
173 /* |                  increase maxdiv or use our modification (Jones = 1). | */
174 /* |            Termination values :                                       | */
175 /* |              1   Number of function evaluations done is larger then   | */
176 /* |                  maxf.                                                | */
177 /* |              2   Number of iterations is equal to maxT.               | */
178 /* |              3   The best function value found is within fglper of    | */
179 /* |                  the (known) global optimum, that is                  | */
180 /* |                   100(fmin - fglobal/max(1,|fglobal|))  < fglper.     | */
181 /* |                  Note that this termination signal only occurs when   | */
182 /* |                  the global optimal value is known, that is, a test   | */
183 /* |                  function is optimized.                               | */
184 /* |              4   The volume of the hyperrectangle with fmin at its    | */
185 /* |                  center is less than volper percent of the volume of  | */
186 /* |                  the original hyperrectangle.                         | */
187 /* |              5   The measure of the hyperrectangle with fmin at its   | */
188 /* |                  center is less than sigmaper.                        | */
189 /* |                                                                       | */
190 /* | SUBROUTINEs used :                                                    | */
191 /* |                                                                       | */
192 /* | DIRheader, DIRInitSpecific, DIRInitList, DIRpreprc, DIRInit, DIRChoose| */
193 /* | DIRDoubleInsert, DIRGet_I, DIRSamplepoints, DIRSamplef, DIRDivide     | */
194 /* | DIRInsertList, DIRreplaceInf, DIRWritehistbox, DIRsummary, Findareas  | */
195 /* |                                                                       | */
196 /* | Functions used :                                                      | */
197 /* |                                                                       | */
198 /* | DIRgetMaxdeep, DIRgetlevel                                            | */
199 /* +-----------------------------------------------------------------------+ */
200 /* +-----------------------------------------------------------------------+ */
201 /* | Parameters                                                            | */
202 /* +-----------------------------------------------------------------------+ */
203 /* +-----------------------------------------------------------------------+ */
204 /* | The maximum of function evaluations allowed.                          | */
205 /* | The maximum dept of the algorithm.                                    | */
206 /* | The maximum number of divisions allowed.                              | */
207 /* | The maximal dimension of the problem.                                 | */
208 /* +-----------------------------------------------------------------------+ */
209 /* +-----------------------------------------------------------------------+ */
210 /* | Global Variables.                                                     | */
211 /* +-----------------------------------------------------------------------+ */
212 /* +-----------------------------------------------------------------------+ */
213 /* | EXTERNAL Variables.                                                   | */
214 /* +-----------------------------------------------------------------------+ */
215 /* +-----------------------------------------------------------------------+ */
216 /* | User Variables.                                                       | */
217 /* | These can be used to pass user defined data to the function to be     | */
218 /* | optimized.                                                            | */
219 /* +-----------------------------------------------------------------------+ */
220 /* +-----------------------------------------------------------------------+ */
221 /* | Place to define, if needed, some application-specific variables.      | */
222 /* | Note: You should try to use the arrays defined above for this.        | */
223 /* +-----------------------------------------------------------------------+ */
224 /* +-----------------------------------------------------------------------+ */
225 /* | End of application - specific variables !                             | */
226 /* +-----------------------------------------------------------------------+ */
227 /* +-----------------------------------------------------------------------+ */
228 /* | Internal variables :                                                  | */
229 /* |       f -- values of functions.                                       | */
230 /* |divfactor-- Factor used for termination with known global minimum.     | */
231 /* |  anchor -- anchors of lists with deepness i, -1 is anchor for list of | */
232 /* |            NaN - values.                                              | */
233 /* |       S -- List of potentially optimal points.                        | */
234 /* |   point -- lists                                                      | */
235 /* |    ifree -- first free position                                        | */
236 /* |       c -- midpoints of arrays                                        | */
237 /* |  thirds -- Precalculated values of 1/3^i.                             | */
238 /* |  levels -- Length of intervals.                                       | */
239 /* |  length -- Length of intervall (index)                                | */
240 /* |       t -- actual iteration                                           | */
241 /* |       j -- loop-variable                                              | */
242 /* | actdeep -- the actual minimal interval-length index                   | */
243 /* |  Minpos -- position of the actual minimum                             | */
244 /* |    file -- The filehandle for a datafile.                             | */
245 /* |  maxpos -- The number of intervalls, which are truncated.             | */
246 /* |    help -- A help variable.                                           | */
247 /* | numfunc -- The actual number of function evaluations.                 | */
248 /* |   file2 -- The filehandle for an other datafile.                      | */
249 /* |  ArrayI -- Array with the indexes of the sides with maximum length.   | */
250 /* |    maxi -- Number of directions with maximal side length.             | */
251 /* |    oops -- Flag which shows if anything went wrong in the             | */
252 /* |            initialisation.                                            | */
253 /* |   cheat -- Obsolete. If equal 1, we don't allow Ktilde > kmax.        | */
254 /* |  writed -- If writed=1, store final division to plot with Matlab.     | */
255 /* |   List2 -- List of indicies of intervalls, which are to be truncated. | */
256 /* |       i -- Another loop-variable.                                     | */
257 /* |actmaxdeep-- The actual maximum (minimum) of possible Interval length. | */
258 /* |  oldpos -- The old index of the minimum. Used to print only, if there | */
259 /* |            is a new minimum found.                                    | */
260 /* |  tstart -- The start of the outer loop.                               | */
261 /* |   start -- The postion of the starting point in the inner loop.       | */
262 /* | Newtosample -- The total number of points to sample in the inner loop.| */
263 /* |       w -- Array used to divide the intervalls                        | */
264 /* |    kmax -- Obsolete. If cheat = 1, Ktilde was not allowed to be larger| */
265 /* |            than kmax. If Ktilde > kmax, we set ktilde = kmax.         | */
266 /* |   delta -- The distance to new points from center of old hyperrec.    | */
267 /* |    pos1 -- Help variable used as an index.                            | */
268 /* | version -- Store the version number of DIRECT.                        | */
269 /* | oldmaxf -- Store the original function budget.                        | */
270 /* |increase -- Flag used to keep track if function budget was increased   | */
271 /* |            because no feasible point was found.                       | */
272 /* | ifreeold -- Keep track which index was free before. Used with          | */
273 /* |            SUBROUTINE DIRReplaceInf.                                  | */
274 /* |actdeep_div-- Keep track of the current depths for divisions.          | */
275 /* |    oldl -- Array used to store the original bounds of the domain.     | */
276 /* |    oldu -- Array used to store the original bounds of the domain.     | */
277 /* |  epsfix -- If eps < 0, we use Jones update formula. epsfix stores the | */
278 /* |            absolute value of epsilon.                                 | */
279 /* |iepschange-- flag iepschange to store if epsilon stays fixed or is     | */
280 /* |             changed.                                                  | */
281 /* |DIRgetMaxdeep-- Function to calculate the level of a hyperrectangle.   | */
282 /* |DIRgetlevel-- Function to calculate the level and stage of a hyperrec. | */
283 /* |    fmax -- Keep track of the maximum value of the function found.     | */
284 /* |Ifeasiblef-- Keep track if a feasible point has  been found so far.    | */
285 /* |             Ifeasiblef = 0 means a feasible point has been found,     | */
286 /* |             Ifeasiblef = 1 no feasible point has been found.          | */
287 /* +-----------------------------------------------------------------------+ */
288 /* +-----------------------------------------------------------------------+ */
289 /* | JG 09/25/00 Version counter.                                          | */
290 /* +-----------------------------------------------------------------------+ */
291 /* +-----------------------------------------------------------------------+ */
292 /* | JG 09/24/00 Add another actdeep to keep track of the current depths   | */
293 /* |             for divisions.                                            | */
294 /* +-----------------------------------------------------------------------+ */
295 /* +-----------------------------------------------------------------------+ */
296 /* |JG 01/13/01 Added epsfix for epsilon update. If eps < 0, we use Jones  | */
297 /* |            update formula. epsfix stores the absolute value of epsilon| */
298 /* |            then. Also added flag iepschange to store if epsilon stays | */
299 /* |            fixed or is changed.                                       | */
300 /* +-----------------------------------------------------------------------+ */
301 /* +-----------------------------------------------------------------------+ */
302 /* | JG 01/22/01 fmax is used to keep track of the maximum value found.    | */
303 /* +-----------------------------------------------------------------------+ */
304 /* +-----------------------------------------------------------------------+ */
305 /* | JG 01/22/01 Ifeasiblef is used to keep track if a feasible point has  | */
306 /* |             been found so far. Ifeasiblef = 0 means a feasible point  | */
307 /* |             has been found, Ifeasiblef = 1 if not.                    | */
308 /* | JG 03/09/01 IInfeasible is used to keep track if an infeasible point  | */
309 /* |             has been found. IInfeasible > 0 means a infeasible point  | */
310 /* |             has been found, IInfeasible = 0 if not.                   | */
311 /* +-----------------------------------------------------------------------+ */
312 /* +-----------------------------------------------------------------------+ */
313 /* +-----------------------------------------------------------------------+ */
314 /* |                            Start of code.                             | */
315 /* +-----------------------------------------------------------------------+ */
316 /* +-----------------------------------------------------------------------+ */
317     /* Parameter adjustments */
318     --u;
319     --l;
320     --x;
321
322     /* Function Body */
323     writed = 0;
324     jones = *algmethod;
325 /* +-----------------------------------------------------------------------+ */
326 /* | Save the upper and lower bounds.                                      | */
327 /* +-----------------------------------------------------------------------+ */
328     i__1 = *n;
329     for (i__ = 1; i__ <= i__1; ++i__) {
330         oldu[i__ - 1] = u[i__];
331         oldl[i__ - 1] = l[i__];
332 /* L150: */
333     }
334 /* +-----------------------------------------------------------------------+ */
335 /* | Set version.                                                          | */
336 /* +-----------------------------------------------------------------------+ */
337     version = 204;
338 /* +-----------------------------------------------------------------------+ */
339 /* | Set parameters.                                                       | */
340 /* |    If cheat > 0, we do not allow \tilde{K} to be larger than kmax, and| */
341 /* |    set \tilde{K} to set value if necessary. Not used anymore.         | */
342 /* +-----------------------------------------------------------------------+ */
343     cheat = 0;
344     kmax = 1e10;
345     mdeep = MAXDEEP;
346 /* +-----------------------------------------------------------------------+ */
347 /* | Write the header of the logfile.                                      | */
348 /* +-----------------------------------------------------------------------+ */
349     direct_dirheader_(logfile, &version, &x[1], n, eps, maxf, maxt, &l[1], &u[1], 
350             algmethod, &MAXFUNC, &MAXDEEP, fglobal, fglper, ierror, &epsfix, &
351                       iepschange, volper, sigmaper);
352 /* +-----------------------------------------------------------------------+ */
353 /* | If an error has occured while writing the header (we do some checking | */
354 /* | of variables there), return to the main program.                      | */
355 /* +-----------------------------------------------------------------------+ */
356     if (*ierror < 0) {
357         goto cleanup;
358     }
359 /* +-----------------------------------------------------------------------+ */
360 /* | If the known global minimum is equal 0, we cannot divide by it.       | */
361 /* | Therefore we set it to 1. If not, we set the divisionfactor to the    | */
362 /* | absolute value of the global minimum.                                 | */
363 /* +-----------------------------------------------------------------------+ */
364     if (*fglobal == 0.) {
365         divfactor = 1.;
366     } else {
367         divfactor = fabs(*fglobal);
368     }
369 /* +-----------------------------------------------------------------------+ */
370 /* | Save the budget given by the user. The variable maxf will be changed  | */
371 /* | if in the beginning no feasible points are found.                     | */
372 /* +-----------------------------------------------------------------------+ */
373     oldmaxf = *maxf;
374     increase = 0;
375 /* +-----------------------------------------------------------------------+ */
376 /* | Initialiase the lists.                                                | */
377 /* +-----------------------------------------------------------------------+ */
378     direct_dirinitlist_(anchor, &ifree, point, f, &MAXFUNC, &MAXDEEP);
379 /* +-----------------------------------------------------------------------+ */
380 /* | Call the routine to initialise the mapping of x from the n-dimensional| */
381 /* | unit cube to the hypercube given by u and l. If an error occured,     | */
382 /* | give out a error message and return to the main program with the error| */
383 /* | flag set.                                                             | */
384 /* | JG 07/16/01 Changed call to remove unused data.                       | */
385 /* +-----------------------------------------------------------------------+ */
386     direct_dirpreprc_(&u[1], &l[1], n, &l[1], &u[1], &oops);
387     if (oops > 0) {
388         if (logfile)
389              fprintf(logfile,"WARNING: Initialization in DIRpreprc failed.\n");
390         *ierror = -3;
391         goto cleanup;
392     }
393     tstart = 2;
394 /* +-----------------------------------------------------------------------+ */
395 /* | Initialise the algorithm DIRECT.                                      | */
396 /* +-----------------------------------------------------------------------+ */
397 /* +-----------------------------------------------------------------------+ */
398 /* | Added variable to keep track of the maximum value found.              | */
399 /* +-----------------------------------------------------------------------+ */
400     direct_dirinit_(f, fcn, c__, length, &actdeep, point, anchor, &ifree,
401             logfile, arrayi, &maxi, list2, w, &x[1], &l[1], &u[1], 
402             fmin, &minpos, thirds, levels, &MAXFUNC, &MAXDEEP, n, n, &
403             fmax, &ifeasiblef, &iinfesiblef, ierror, fcn_data, jones);
404 /* +-----------------------------------------------------------------------+ */
405 /* | Added error checking.                                                 | */
406 /* +-----------------------------------------------------------------------+ */
407     if (*ierror < 0) {
408         if (*ierror == -4) {
409             if (logfile)
410                  fprintf(logfile, "WARNING: Error occured in routine DIRsamplepoints.\n");
411             goto cleanup;
412         }
413         if (*ierror == -5) {
414             if (logfile)
415                  fprintf(logfile, "WARNING: Error occured in routine DIRsamplef..\n");
416             goto cleanup;
417         }
418     }
419     numfunc = maxi + 1 + maxi;
420     actmaxdeep = 1;
421     oldpos = 0;
422     tstart = 2;
423 /* +-----------------------------------------------------------------------+ */
424 /* | If no feasible point has been found, give out the iteration, the      | */
425 /* | number of function evaluations and a warning. Otherwise, give out     | */
426 /* | the iteration, the number of function evaluations done and fmin.      | */
427 /* +-----------------------------------------------------------------------+ */
428     if (ifeasiblef > 0) {
429          if (logfile)
430               fprintf(logfile, "No feasible point found in %d iterations "
431                       "and %d function evaluations.\n", tstart-1, numfunc);
432     } else {
433          if (logfile)
434               fprintf(logfile, "%d, %d, %g, %g\n", 
435                       tstart-1, numfunc, *fmin, fmax);
436     }
437 /* +-----------------------------------------------------------------------+ */
438 /* +-----------------------------------------------------------------------+ */
439 /* | Main loop!                                                            | */
440 /* +-----------------------------------------------------------------------+ */
441 /* +-----------------------------------------------------------------------+ */
442     i__1 = *maxt;
443     for (t = tstart; t <= i__1; ++t) {
444 /* +-----------------------------------------------------------------------+ */
445 /* | Choose the sample points. The indices of the sample points are stored | */
446 /* | in the list S.                                                        | */
447 /* +-----------------------------------------------------------------------+ */
448         actdeep = actmaxdeep;
449         direct_dirchoose_(anchor, s, &MAXDEEP, f, fmin, *eps, epsabs, levels, &maxpos, length, 
450                 &MAXFUNC, &MAXDEEP, &MAXDIV, n, logfile, &cheat, &
451                 kmax, &ifeasiblef, jones);
452 /* +-----------------------------------------------------------------------+ */
453 /* | Add other hyperrectangles to S, which have the same level and the same| */
454 /* | function value at the center as the ones found above (that are stored | */
455 /* | in S). This is only done if we use the original DIRECT algorithm.     | */
456 /* | JG 07/16/01 Added Errorflag.                                          | */
457 /* +-----------------------------------------------------------------------+ */
458         if (*algmethod == 0) {
459              direct_dirdoubleinsert_(anchor, s, &maxpos, point, f, &MAXDEEP, &MAXFUNC,
460                      &MAXDIV, ierror);
461             if (*ierror == -6) {
462                 if (logfile)
463                      fprintf(logfile,
464 "WARNING: Capacity of array S in DIRDoubleInsert reached. Increase maxdiv.\n"
465 "This means that there are a lot of hyperrectangles with the same function\n"
466 "value at the center. We suggest to use our modification instead (Jones = 1)\n"
467                           );
468                 goto cleanup;
469             }
470         }
471         oldpos = minpos;
472 /* +-----------------------------------------------------------------------+ */
473 /* | Initialise the number of sample points in this outer loop.            | */
474 /* +-----------------------------------------------------------------------+ */
475         newtosample = 0;
476         i__2 = maxpos;
477         for (j = 1; j <= i__2; ++j) {
478             actdeep = s[j + 2999];
479 /* +-----------------------------------------------------------------------+ */
480 /* | If the actual index is a point to sample, do it.                      | */
481 /* +-----------------------------------------------------------------------+ */
482             if (s[j - 1] > 0) {
483 /* +-----------------------------------------------------------------------+ */
484 /* | JG 09/24/00 Calculate the value delta used for sampling points.       | */
485 /* +-----------------------------------------------------------------------+ */
486                 actdeep_div__ = direct_dirgetmaxdeep_(&s[j - 1], length, &MAXFUNC, 
487                         n);
488                 delta = thirds[actdeep_div__ + 1];
489                 actdeep = s[j + 2999];
490 /* +-----------------------------------------------------------------------+ */
491 /* | If the current dept of division is only one under the maximal allowed | */
492 /* | dept, stop the computation.                                           | */
493 /* +-----------------------------------------------------------------------+ */
494                 if (actdeep + 1 >= mdeep) {
495                     if (logfile)
496                          fprintf(logfile, "WARNING: Maximum number of levels reached. Increase maxdeep.\n");
497                     *ierror = -6;
498                     goto L100;
499                 }
500                 actmaxdeep = MAX(actdeep,actmaxdeep);
501                 help = s[j - 1];
502                 if (! (anchor[actdeep + 1] == help)) {
503                     pos1 = anchor[actdeep + 1];
504                     while(! (point[pos1 - 1] == help)) {
505                         pos1 = point[pos1 - 1];
506                     }
507                     point[pos1 - 1] = point[help - 1];
508                 } else {
509                     anchor[actdeep + 1] = point[help - 1];
510                 }
511                 if (actdeep < 0) {
512                     actdeep = (integer) f[(help << 1) - 2];
513                 }
514 /* +-----------------------------------------------------------------------+ */
515 /* | Get the Directions in which to decrease the intervall-length.         | */
516 /* +-----------------------------------------------------------------------+ */
517                 direct_dirget_i__(length, &help, arrayi, &maxi, n, &MAXFUNC);
518 /* +-----------------------------------------------------------------------+ */
519 /* | Sample the function. To do this, we first calculate the points where  | */
520 /* | we need to sample the function. After checking for errors, we then do | */
521 /* | the actual evaluation of the function, again followed by checking for | */
522 /* | errors.                                                               | */
523 /* +-----------------------------------------------------------------------+ */
524                 direct_dirsamplepoints_(c__, arrayi, &delta, &help, &start, length, 
525                         logfile, f, &ifree, &maxi, point, &x[
526                         1], &l[1], fmin, &minpos, &u[1], n, &MAXFUNC, &
527                         MAXDEEP, &oops);
528                 if (oops > 0) {
529                     if (logfile)
530                          fprintf(logfile, "WARNING: Error occured in routine DIRsamplepoints.\n");
531                     *ierror = -4;
532                     goto cleanup;
533                 }
534                 newtosample += maxi;
535 /* +-----------------------------------------------------------------------+ */
536 /* | JG 01/22/01 Added variable to keep track of the maximum value found.  | */
537 /* +-----------------------------------------------------------------------+ */
538                 direct_dirsamplef_(c__, arrayi, &delta, &help, &start, length,
539                             logfile, f, &ifree, &maxi, point, fcn, &x[
540                         1], &l[1], fmin, &minpos, &u[1], n, &MAXFUNC, &
541                         MAXDEEP, &oops, &fmax, &ifeasiblef, &iinfesiblef, 
542                         fcn_data);
543                 if (oops > 0) {
544                     if (logfile)
545                          fprintf(logfile, "WARNING: Error occured in routine DIRsamplef.\n");
546                     *ierror = -5;
547                     goto cleanup;
548                 }
549 /* +-----------------------------------------------------------------------+ */
550 /* | Divide the intervalls.                                                | */
551 /* +-----------------------------------------------------------------------+ */
552                 direct_dirdivide_(&start, &actdeep_div__, length, point, arrayi, &
553                         help, list2, w, &maxi, f, &MAXFUNC, &MAXDEEP, n);
554 /* +-----------------------------------------------------------------------+ */
555 /* | Insert the new intervalls into the list (sorted).                     | */
556 /* +-----------------------------------------------------------------------+ */
557                 direct_dirinsertlist_(&start, anchor, point, f, &maxi, length, &
558                         MAXFUNC, &MAXDEEP, n, &help, jones);
559 /* +-----------------------------------------------------------------------+ */
560 /* | Increase the number of function evaluations.                          | */
561 /* +-----------------------------------------------------------------------+ */
562                 numfunc = numfunc + maxi + maxi;
563             }
564 /* +-----------------------------------------------------------------------+ */
565 /* | End of main loop.                                                     | */
566 /* +-----------------------------------------------------------------------+ */
567 /* L20: */
568         }
569 /* +-----------------------------------------------------------------------+ */
570 /* | If there is a new minimum, show the actual iteration, the number of   | */
571 /* | function evaluations, the minimum value of f (so far) and the position| */
572 /* | in the array.                                                         | */
573 /* +-----------------------------------------------------------------------+ */
574         if (oldpos < minpos) {
575             if (logfile)
576                  fprintf(logfile, "%d, %d, %g, %g\n",
577                          t, numfunc, *fmin, fmax);
578         }
579 /* +-----------------------------------------------------------------------+ */
580 /* | If no feasible point has been found, give out the iteration, the      | */
581 /* | number of function evaluations and a warning.                         | */
582 /* +-----------------------------------------------------------------------+ */
583         if (ifeasiblef > 0) {
584             if (logfile)
585                  fprintf(logfile, "No feasible point found in %d iterations "
586                          "and %d function evaluations\n", t, numfunc);
587         }
588 /* +-----------------------------------------------------------------------+ */
589 /* +-----------------------------------------------------------------------+ */
590 /* |                       Termination Checks                              | */
591 /* +-----------------------------------------------------------------------+ */
592 /* +-----------------------------------------------------------------------+ */
593 /* | JG 01/22/01 Calculate the index for the hyperrectangle at which       | */
594 /* |             fmin is assumed. We then calculate the volume of this     | */
595 /* |             hyperrectangle and store it in delta. This delta can be   | */
596 /* |             used to stop DIRECT once the volume is below a certain    | */
597 /* |             percentage of the original volume. Since the original     | */
598 /* |             is 1 (scaled), we can stop once delta is below a certain  | */
599 /* |             percentage, given by volper.                              | */
600 /* +-----------------------------------------------------------------------+ */
601         *ierror = jones;
602         jones = 0;
603         actdeep_div__ = direct_dirgetlevel_(&minpos, length, &MAXFUNC, n, jones);
604         jones = *ierror;
605 /* +-----------------------------------------------------------------------+ */
606 /* | JG 07/16/01 Use precalculated values to calculate volume.             | */
607 /* +-----------------------------------------------------------------------+ */
608         delta = thirds[actdeep_div__] * 100;
609         if (delta <= *volper) {
610             *ierror = 4;
611             if (logfile)
612                  fprintf(logfile, "DIRECT stopped: Volume of S_min is "
613                          "%g%% < %g%% of the original volume.\n",
614                          delta, *volper);
615             goto L100;
616         }
617 /* +-----------------------------------------------------------------------+ */
618 /* | JG 01/23/01 Calculate the measure for the hyperrectangle at which     | */
619 /* |             fmin is assumed. If this measure is smaller then sigmaper,| */
620 /* |             we stop DIRECT.                                           | */
621 /* +-----------------------------------------------------------------------+ */
622         actdeep_div__ = direct_dirgetlevel_(&minpos, length, &MAXFUNC, n, jones);
623         delta = levels[actdeep_div__];
624         if (delta <= *sigmaper) {
625             *ierror = 5;
626             if (logfile)
627                  fprintf(logfile, "DIRECT stopped: Measure of S_min "
628                          "= %g < %g.\n", delta, *sigmaper);
629             goto L100;
630         }
631 /* +-----------------------------------------------------------------------+ */
632 /* | If the best found function value is within fglper of the (known)      | */
633 /* | global minimum value, terminate. This only makes sense if this optimal| */
634 /* | value is known, that is, in test problems.                            | */
635 /* +-----------------------------------------------------------------------+ */
636         if ((*fmin - *fglobal) * 100 / divfactor <= *fglper) {
637             *ierror = 3;
638             if (logfile)
639                  fprintf(logfile, "DIRECT stopped: fmin within fglper of global minimum.\n");
640             goto L100;
641         }
642 /* +-----------------------------------------------------------------------+ */
643 /* | Find out if there are infeasible points which are near feasible ones. | */
644 /* | If this is the case, replace the function value at the center of the  | */
645 /* | hyper rectangle by the lowest function value of a nearby function.    | */
646 /* | If no infeasible points exist (IInfesiblef = 0), skip this.           | */
647 /* +-----------------------------------------------------------------------+ */
648         if (iinfesiblef > 0) {
649              direct_dirreplaceinf_(&ifree, &ifreeold, f, c__, thirds, length, anchor, 
650                     point, &u[1], &l[1], &MAXFUNC, &MAXDEEP, n, n, 
651                     logfile, &fmax, jones);
652         }
653         ifreeold = ifree;
654 /* +-----------------------------------------------------------------------+ */
655 /* | If iepschange = 1, we use the epsilon change formula from Jones.      | */
656 /* +-----------------------------------------------------------------------+ */
657         if (iepschange == 1) {
658 /* Computing MAX */
659             d__1 = fabs(*fmin) * 1e-4;
660             *eps = MAX(d__1,epsfix);
661         }
662 /* +-----------------------------------------------------------------------+ */
663 /* | If no feasible point has been found yet, set the maximum number of    | */
664 /* | function evaluations to the number of evaluations already done plus   | */
665 /* | the budget given by the user.                                         | */
666 /* | If the budget has already be increased, increase it again. If a       | */
667 /* | feasible point has been found, remark that and reset flag. No further | */
668 /* | increase is needed.                                                   | */
669 /* +-----------------------------------------------------------------------+ */
670         if (increase == 1) {
671             *maxf = numfunc + oldmaxf;
672             if (ifeasiblef == 0) {
673                 if (logfile)
674                      fprintf(logfile, "DIRECT found a feasible point.  The "
675                              "adjusted budget is now set to %d.\n", *maxf);
676                 increase = 0;
677             }
678         }
679 /* +-----------------------------------------------------------------------+ */
680 /* | Check if the number of function evaluations done is larger than the   | */
681 /* | allocated budget. If this is the case, check if a feasible point was  | */
682 /* | found. If this is a case, terminate. If no feasible point was found,  | */
683 /* | increase the budget and set flag increase.                            | */
684 /* +-----------------------------------------------------------------------+ */
685         if (numfunc > *maxf) {
686             if (ifeasiblef == 0) {
687                 *ierror = 1;
688                 if (logfile)
689                      fprintf(logfile, "DIRECT stopped: numfunc >= maxf.\n");
690                 goto L100;
691             } else {
692                 increase = 1;
693                 if (logfile)
694                      fprintf(logfile, 
695 "DIRECT could not find a feasible point after %d function evaluations.\n"
696 "DIRECT continues until a feasible point is found.\n", numfunc);
697                 *maxf = numfunc + oldmaxf;
698             }
699         }
700 /* L10: */
701     }
702 /* +-----------------------------------------------------------------------+ */
703 /* +-----------------------------------------------------------------------+ */
704 /* | End of main loop.                                                     | */
705 /* +-----------------------------------------------------------------------+ */
706 /* +-----------------------------------------------------------------------+ */
707 /* +-----------------------------------------------------------------------+ */
708 /* | The algorithm stopped after maxT iterations.                          | */
709 /* +-----------------------------------------------------------------------+ */
710     *ierror = 2;
711     if (logfile)
712          fprintf(logfile, "DIRECT stopped: maxT iterations.\n");
713
714 L100:
715 /* +-----------------------------------------------------------------------+ */
716 /* | Store the position of the minimum in x.                               | */
717 /* +-----------------------------------------------------------------------+ */
718     i__1 = *n;
719     for (i__ = 1; i__ <= i__1; ++i__) {
720         x[i__] = c__[i__ + minpos * i__1 - i__1-1] * l[i__] + l[i__] * u[i__];
721         u[i__] = oldu[i__ - 1];
722         l[i__] = oldl[i__ - 1];
723 /* L50: */
724     }
725 /* +-----------------------------------------------------------------------+ */
726 /* | Store the number of function evaluations in maxf.                     | */
727 /* +-----------------------------------------------------------------------+ */
728     *maxf = numfunc;
729 /* +-----------------------------------------------------------------------+ */
730 /* | Give out a summary of the run.                                        | */
731 /* +-----------------------------------------------------------------------+ */
732     direct_dirsummary_(logfile, &x[1], &l[1], &u[1], n, fmin, fglobal, &numfunc, 
733             ierror);
734 /* +-----------------------------------------------------------------------+ */
735 /* | Format statements.                                                    | */
736 /* +-----------------------------------------------------------------------+ */
737
738  cleanup:
739 #define MY_FREE(p) if (p) free(p)
740     MY_FREE(c__);
741     MY_FREE(f);
742     MY_FREE(s);
743     MY_FREE(w);
744     MY_FREE(oldl);
745     MY_FREE(oldu);
746     MY_FREE(list2);
747     MY_FREE(point);
748     MY_FREE(anchor);
749     MY_FREE(length);
750     MY_FREE(arrayi);
751     MY_FREE(levels);
752     MY_FREE(thirds);
753 } /* direct_ */
754