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