1 /* DIRect-transp.f -- translated by f2c (version 20050501).
3    f2c output hand-cleaned by SGJ (August 2007).
4 */
6 #include <math.h>
7 #include "direct-internal.h"
9 /* Common Block Declarations */
11 /* Table of constant values */
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, double starttime, double maxtime, int *force_stop, doublereal *minf, 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;
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;
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;
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; }
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;
106     MY_ALLOC(s, integer, MAXDIV * 2);
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;
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));
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(minf),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 information  | */
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)) = minf 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)) = minf 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 /* |    minf -- 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(minf - 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 minf 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 minf 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 /* +-----------------------------------------------------------------------+ */
323     --u;
324     --l;
325     --x;
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             minf, &minpos, thirds, levels, &MAXFUNC, &MAXDEEP, n, n, &
408             fmax, &ifeasiblef, &iinfesiblef, ierror, fcn_data, jones,
409                     starttime, maxtime, force_stop);
410 /* +-----------------------------------------------------------------------+ */
411 /* | Added error checking.                                                 | */
412 /* +-----------------------------------------------------------------------+ */
413     if (*ierror < 0) {
414         if (*ierror == -4) {
415             if (logfile)
416                  fprintf(logfile, "WARNING: Error occured in routine DIRsamplepoints.\n");
417             goto cleanup;
418         }
419         if (*ierror == -5) {
420             if (logfile)
421                  fprintf(logfile, "WARNING: Error occured in routine DIRsamplef..\n");
422             goto cleanup;
423         }
424         if (*ierror == -102) goto L100;
425     }
426     else if (*ierror == DIRECT_MAXTIME_EXCEEDED) goto L100;
427     numfunc = maxi + 1 + maxi;
428     actmaxdeep = 1;
429     oldpos = 0;
430     tstart = 2;
431 /* +-----------------------------------------------------------------------+ */
432 /* | If no feasible point has been found, give out the iteration, the      | */
433 /* | number of function evaluations and a warning. Otherwise, give out     | */
434 /* | the iteration, the number of function evaluations done and minf.      | */
435 /* +-----------------------------------------------------------------------+ */
436     if (ifeasiblef > 0) {
437          if (logfile)
438               fprintf(logfile, "No feasible point found in %d iterations "
439                       "and %d function evaluations.\n", tstart-1, numfunc);
440     } else {
441          if (logfile)
442               fprintf(logfile, "%d, %d, %g, %g\n",
443                       tstart-1, numfunc, *minf, fmax);
444     }
445 /* +-----------------------------------------------------------------------+ */
446 /* +-----------------------------------------------------------------------+ */
447 /* | Main loop!                                                            | */
448 /* +-----------------------------------------------------------------------+ */
449 /* +-----------------------------------------------------------------------+ */
450     i__1 = *maxt;
451     for (t = tstart; t <= i__1; ++t) {
452 /* +-----------------------------------------------------------------------+ */
453 /* | Choose the sample points. The indices of the sample points are stored | */
454 /* | in the list S.                                                        | */
455 /* +-----------------------------------------------------------------------+ */
456         actdeep = actmaxdeep;
457         direct_dirchoose_(anchor, s, &MAXDEEP, f, minf, *eps, epsabs, levels, &maxpos, length,
458                 &MAXFUNC, &MAXDEEP, &MAXDIV, n, logfile, &cheat, &
459                 kmax, &ifeasiblef, jones);
460 /* +-----------------------------------------------------------------------+ */
461 /* | Add other hyperrectangles to S, which have the same level and the same| */
462 /* | function value at the center as the ones found above (that are stored | */
463 /* | in S). This is only done if we use the original DIRECT algorithm.     | */
464 /* | JG 07/16/01 Added Errorflag.                                          | */
465 /* +-----------------------------------------------------------------------+ */
466         if (*algmethod == 0) {
467              direct_dirdoubleinsert_(anchor, s, &maxpos, point, f, &MAXDEEP, &MAXFUNC,
468                      &MAXDIV, ierror);
469             if (*ierror == -6) {
470                 if (logfile)
471                      fprintf(logfile,
472 "WARNING: Capacity of array S in DIRDoubleInsert reached. Increase maxdiv.\n"
473 "This means that there are a lot of hyperrectangles with the same function\n"
474 "value at the center. We suggest to use our modification instead (Jones = 1)\n"
475                           );
476                 goto cleanup;
477             }
478         }
479         oldpos = minpos;
480 /* +-----------------------------------------------------------------------+ */
481 /* | Initialise the number of sample points in this outer loop.            | */
482 /* +-----------------------------------------------------------------------+ */
483         newtosample = 0;
484         i__2 = maxpos;
485         for (j = 1; j <= i__2; ++j) {
486             actdeep = s[j + MAXDIV-1];
487 /* +-----------------------------------------------------------------------+ */
488 /* | If the actual index is a point to sample, do it.                      | */
489 /* +-----------------------------------------------------------------------+ */
490             if (s[j - 1] > 0) {
491 /* +-----------------------------------------------------------------------+ */
492 /* | JG 09/24/00 Calculate the value delta used for sampling points.       | */
493 /* +-----------------------------------------------------------------------+ */
494                 actdeep_div__ = direct_dirgetmaxdeep_(&s[j - 1], length, &MAXFUNC,
495                         n);
496                 delta = thirds[actdeep_div__ + 1];
497                 actdeep = s[j + MAXDIV-1];
498 /* +-----------------------------------------------------------------------+ */
499 /* | If the current dept of division is only one under the maximal allowed | */
500 /* | dept, stop the computation.                                           | */
501 /* +-----------------------------------------------------------------------+ */
502                 if (actdeep + 1 >= mdeep) {
503                     if (logfile)
504                          fprintf(logfile, "WARNING: Maximum number of levels reached. Increase maxdeep.\n");
505                     *ierror = -6;
506                     goto L100;
507                 }
508                 actmaxdeep = MAX(actdeep,actmaxdeep);
509                 help = s[j - 1];
510                 if (! (anchor[actdeep + 1] == help)) {
511                     pos1 = anchor[actdeep + 1];
512                     while(! (point[pos1 - 1] == help)) {
513                         pos1 = point[pos1 - 1];
514                     }
515                     point[pos1 - 1] = point[help - 1];
516                 } else {
517                     anchor[actdeep + 1] = point[help - 1];
518                 }
519                 if (actdeep < 0) {
520                     actdeep = (integer) f[(help << 1) - 2];
521                 }
522 /* +-----------------------------------------------------------------------+ */
523 /* | Get the Directions in which to decrease the intervall-length.         | */
524 /* +-----------------------------------------------------------------------+ */
525                 direct_dirget_i__(length, &help, arrayi, &maxi, n, &MAXFUNC);
526 /* +-----------------------------------------------------------------------+ */
527 /* | Sample the function. To do this, we first calculate the points where  | */
528 /* | we need to sample the function. After checking for errors, we then do | */
529 /* | the actual evaluation of the function, again followed by checking for | */
530 /* | errors.                                                               | */
531 /* +-----------------------------------------------------------------------+ */
532                 direct_dirsamplepoints_(c__, arrayi, &delta, &help, &start, length,
533                         logfile, f, &ifree, &maxi, point, &x[
534                         1], &l[1], minf, &minpos, &u[1], n, &MAXFUNC, &
535                         MAXDEEP, &oops);
536                 if (oops > 0) {
537                     if (logfile)
538                          fprintf(logfile, "WARNING: Error occured in routine DIRsamplepoints.\n");
539                     *ierror = -4;
540                     goto cleanup;
541                 }
542                 newtosample += maxi;
543 /* +-----------------------------------------------------------------------+ */
544 /* | JG 01/22/01 Added variable to keep track of the maximum value found.  | */
545 /* +-----------------------------------------------------------------------+ */
546                 direct_dirsamplef_(c__, arrayi, &delta, &help, &start, length,
547                             logfile, f, &ifree, &maxi, point, fcn, &x[
548                         1], &l[1], minf, &minpos, &u[1], n, &MAXFUNC, &
549                         MAXDEEP, &oops, &fmax, &ifeasiblef, &iinfesiblef,
550                                    fcn_data, force_stop);
551                 if (force_stop && *force_stop) {
552                      *ierror = -102;
553                      goto L100;
554                 }
555                 if (nlopt_stop_time_(starttime, maxtime)) {
556                      *ierror = DIRECT_MAXTIME_EXCEEDED;
557                      goto L100;
558                 }
559                 if (oops > 0) {
560                     if (logfile)
561                          fprintf(logfile, "WARNING: Error occured in routine DIRsamplef.\n");
562                     *ierror = -5;
563                     goto cleanup;
564                 }
565 /* +-----------------------------------------------------------------------+ */
566 /* | Divide the intervalls.                                                | */
567 /* +-----------------------------------------------------------------------+ */
568                 direct_dirdivide_(&start, &actdeep_div__, length, point, arrayi, &
569                         help, list2, w, &maxi, f, &MAXFUNC, &MAXDEEP, n);
570 /* +-----------------------------------------------------------------------+ */
571 /* | Insert the new intervalls into the list (sorted).                     | */
572 /* +-----------------------------------------------------------------------+ */
573                 direct_dirinsertlist_(&start, anchor, point, f, &maxi, length, &
574                         MAXFUNC, &MAXDEEP, n, &help, jones);
575 /* +-----------------------------------------------------------------------+ */
576 /* | Increase the number of function evaluations.                          | */
577 /* +-----------------------------------------------------------------------+ */
578                 numfunc = numfunc + maxi + maxi;
579             }
580 /* +-----------------------------------------------------------------------+ */
581 /* | End of main loop.                                                     | */
582 /* +-----------------------------------------------------------------------+ */
583 /* L20: */
584         }
585 /* +-----------------------------------------------------------------------+ */
586 /* | If there is a new minimum, show the actual iteration, the number of   | */
587 /* | function evaluations, the minimum value of f (so far) and the position| */
588 /* | in the array.                                                         | */
589 /* +-----------------------------------------------------------------------+ */
590         if (oldpos < minpos) {
591             if (logfile)
592                  fprintf(logfile, "%d, %d, %g, %g\n",
593                          t, numfunc, *minf, fmax);
594         }
595 /* +-----------------------------------------------------------------------+ */
596 /* | If no feasible point has been found, give out the iteration, the      | */
597 /* | number of function evaluations and a warning.                         | */
598 /* +-----------------------------------------------------------------------+ */
599         if (ifeasiblef > 0) {
600             if (logfile)
601                  fprintf(logfile, "No feasible point found in %d iterations "
602                          "and %d function evaluations\n", t, numfunc);
603         }
604 /* +-----------------------------------------------------------------------+ */
605 /* +-----------------------------------------------------------------------+ */
606 /* |                       Termination Checks                              | */
607 /* +-----------------------------------------------------------------------+ */
608 /* +-----------------------------------------------------------------------+ */
609 /* | JG 01/22/01 Calculate the index for the hyperrectangle at which       | */
610 /* |             minf is assumed. We then calculate the volume of this     | */
611 /* |             hyperrectangle and store it in delta. This delta can be   | */
612 /* |             used to stop DIRECT once the volume is below a certain    | */
613 /* |             percentage of the original volume. Since the original     | */
614 /* |             is 1 (scaled), we can stop once delta is below a certain  | */
615 /* |             percentage, given by volper.                              | */
616 /* +-----------------------------------------------------------------------+ */
617         *ierror = jones;
618         jones = 0;
619         actdeep_div__ = direct_dirgetlevel_(&minpos, length, &MAXFUNC, n, jones);
620         jones = *ierror;
621 /* +-----------------------------------------------------------------------+ */
622 /* | JG 07/16/01 Use precalculated values to calculate volume.             | */
623 /* +-----------------------------------------------------------------------+ */
624         delta = thirds[actdeep_div__] * 100;
625         if (delta <= *volper) {
626             *ierror = 4;
627             if (logfile)
628                  fprintf(logfile, "DIRECT stopped: Volume of S_min is "
629                          "%g%% < %g%% of the original volume.\n",
630                          delta, *volper);
631             goto L100;
632         }
633 /* +-----------------------------------------------------------------------+ */
634 /* | JG 01/23/01 Calculate the measure for the hyperrectangle at which     | */
635 /* |             minf is assumed. If this measure is smaller then sigmaper,| */
636 /* |             we stop DIRECT.                                           | */
637 /* +-----------------------------------------------------------------------+ */
638         actdeep_div__ = direct_dirgetlevel_(&minpos, length, &MAXFUNC, n, jones);
639         delta = levels[actdeep_div__];
640         if (delta <= *sigmaper) {
641             *ierror = 5;
642             if (logfile)
643                  fprintf(logfile, "DIRECT stopped: Measure of S_min "
644                          "= %g < %g.\n", delta, *sigmaper);
645             goto L100;
646         }
647 /* +-----------------------------------------------------------------------+ */
648 /* | If the best found function value is within fglper of the (known)      | */
649 /* | global minimum value, terminate. This only makes sense if this optimal| */
650 /* | value is known, that is, in test problems.                            | */
651 /* +-----------------------------------------------------------------------+ */
652         if ((*minf - *fglobal) * 100 / divfactor <= *fglper) {
653             *ierror = 3;
654             if (logfile)
655                  fprintf(logfile, "DIRECT stopped: minf within fglper of global minimum.\n");
656             goto L100;
657         }
658 /* +-----------------------------------------------------------------------+ */
659 /* | Find out if there are infeasible points which are near feasible ones. | */
660 /* | If this is the case, replace the function value at the center of the  | */
661 /* | hyper rectangle by the lowest function value of a nearby function.    | */
662 /* | If no infeasible points exist (IInfesiblef = 0), skip this.           | */
663 /* +-----------------------------------------------------------------------+ */
664         if (iinfesiblef > 0) {
665              direct_dirreplaceinf_(&ifree, &ifreeold, f, c__, thirds, length, anchor,
666                     point, &u[1], &l[1], &MAXFUNC, &MAXDEEP, n, n,
667                     logfile, &fmax, jones);
668         }
669         ifreeold = ifree;
670 /* +-----------------------------------------------------------------------+ */
671 /* | If iepschange = 1, we use the epsilon change formula from Jones.      | */
672 /* +-----------------------------------------------------------------------+ */
673         if (iepschange == 1) {
674 /* Computing MAX */
675             d__1 = fabs(*minf) * 1e-4;
676             *eps = MAX(d__1,epsfix);
677         }
678 /* +-----------------------------------------------------------------------+ */
679 /* | If no feasible point has been found yet, set the maximum number of    | */
680 /* | function evaluations to the number of evaluations already done plus   | */
681 /* | the budget given by the user.                                         | */
682 /* | If the budget has already be increased, increase it again. If a       | */
683 /* | feasible point has been found, remark that and reset flag. No further | */
684 /* | increase is needed.                                                   | */
685 /* +-----------------------------------------------------------------------+ */
686         if (increase == 1) {
687             *maxf = numfunc + oldmaxf;
688             if (ifeasiblef == 0) {
689                 if (logfile)
690                      fprintf(logfile, "DIRECT found a feasible point.  The "
691                              "adjusted budget is now set to %d.\n", *maxf);
692                 increase = 0;
693             }
694         }
695 /* +-----------------------------------------------------------------------+ */
696 /* | Check if the number of function evaluations done is larger than the   | */
697 /* | allocated budget. If this is the case, check if a feasible point was  | */
698 /* | found. If this is a case, terminate. If no feasible point was found,  | */
699 /* | increase the budget and set flag increase.                            | */
700 /* +-----------------------------------------------------------------------+ */
701         if (numfunc > *maxf) {
702             if (ifeasiblef == 0) {
703                 *ierror = 1;
704                 if (logfile)
705                      fprintf(logfile, "DIRECT stopped: numfunc >= maxf.\n");
706                 goto L100;
707             } else {
708                 increase = 1;
709                 if (logfile)
710                      fprintf(logfile,
711 "DIRECT could not find a feasible point after %d function evaluations.\n"
712 "DIRECT continues until a feasible point is found.\n", numfunc);
713                 *maxf = numfunc + oldmaxf;
714             }
715         }
716 /* L10: */
717     }
718 /* +-----------------------------------------------------------------------+ */
719 /* +-----------------------------------------------------------------------+ */
720 /* | End of main loop.                                                     | */
721 /* +-----------------------------------------------------------------------+ */
722 /* +-----------------------------------------------------------------------+ */
723 /* +-----------------------------------------------------------------------+ */
724 /* | The algorithm stopped after maxT iterations.                          | */
725 /* +-----------------------------------------------------------------------+ */
726     *ierror = 2;
727     if (logfile)
728          fprintf(logfile, "DIRECT stopped: maxT iterations.\n");
730 L100:
731 /* +-----------------------------------------------------------------------+ */
732 /* | Store the position of the minimum in x.                               | */
733 /* +-----------------------------------------------------------------------+ */
734     i__1 = *n;
735     for (i__ = 1; i__ <= i__1; ++i__) {
736         x[i__] = c__[i__ + minpos * i__1 - i__1-1] * l[i__] + l[i__] * u[i__];
737         u[i__] = oldu[i__ - 1];
738         l[i__] = oldl[i__ - 1];
739 /* L50: */
740     }
741 /* +-----------------------------------------------------------------------+ */
742 /* | Store the number of function evaluations in maxf.                     | */
743 /* +-----------------------------------------------------------------------+ */
744     *maxf = numfunc;
745 /* +-----------------------------------------------------------------------+ */
746 /* | Give out a summary of the run.                                        | */
747 /* +-----------------------------------------------------------------------+ */
748     direct_dirsummary_(logfile, &x[1], &l[1], &u[1], n, minf, fglobal, &numfunc,
749             ierror);
750 /* +-----------------------------------------------------------------------+ */
751 /* | Format statements.                                                    | */
752 /* +-----------------------------------------------------------------------+ */
754  cleanup:
755 #define MY_FREE(p) if (p) free(p)
756     MY_FREE(c__);
757     MY_FREE(f);
758     MY_FREE(s);
759     MY_FREE(w);
760     MY_FREE(oldl);
761     MY_FREE(oldu);
762     MY_FREE(list2);
763     MY_FREE(point);
764     MY_FREE(anchor);
765     MY_FREE(length);
766     MY_FREE(arrayi);
767     MY_FREE(levels);
768     MY_FREE(thirds);
769 } /* direct_ */