/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* $Log: gridopt.c,v $ Revision 1.8 2006/05/31 16:52:40 ywang25 Updated for parallel random number generators. Revision 1.7 2003/01/09 19:42:08 borland Fixed bug in random walk minimizer. Revision 1.6 2002/10/14 00:06:09 borland Made code more efficient. Revision 1.5 2002/09/24 21:00:17 borland Added randomWalkMin() and randomSampleMin(). Fixed bugs in grid_sample_min(). Revision 1.4 2002/08/14 16:18:56 soliday Added Open License Revision 1.3 1999/05/25 16:52:50 borland Fixed a bug in while loop test statement that was from using an older version of advance_values. * Revision 1.2 1995/09/05 21:20:02 saunders * First test release of the SDDS1.5 package. * */ /* routine: grid_search_min() * purpose: implement grid search for the minimum value of a function * * Michael Borland, 1991. */ #include "mdb.h" #define OPTIM_ABORT 0x0001UL static unsigned long optimFlags = 0; long optimAbort(long abort) { if (abort) { /* if zero, then operation is a query */ optimFlags |= OPTIM_ABORT; } return optimFlags&OPTIM_ABORT ? 1 : 0; } long grid_search_min( double *best_result, double *xReturn, double *lower, double *upper, double *step, long n_dimen, double target, double (*func)(double *x, long *invalid) ) { static double *x = NULL, *best_x = NULL; static long last_n_dimen = 0; static long *index, *counter, *maxcount; double result; long flag, i, best_found; optimFlags = 0; if (last_n_dimen=upper[i]) { step[i] = 0; maxcount[i] = 0; } else { maxcount[i] = (upper[i]-lower[i])/step[i] + 1.5; if (maxcount[i]<=1) maxcount[i] = 2; step[i] = (upper[i]-lower[i])/(maxcount[i]-1); } } best_found = 0; do { if ((result=(*func)(x, &flag)) < *best_result && flag==0) { *best_result = result; for (i=0; i=0); if (best_found) for (i=0; i=upper[i]) { step[i] = 0; maxcount[i] = 0; } else { maxcount[i] = (upper[i]-lower[i])/step[i] + 1.5; if (maxcount[i]<=1) maxcount[i] = 2; step[i] = (upper[i]-lower[i])/(maxcount[i]-1); } } if (sample_fraction>=1) { double npoints = 1; for (i=0; i=0); if (best_found) for (i=0; iupper[i]) x[i] = upper[i]; } result = (*func)(x, &flag); if (flag==0 && result<*best_result) { *best_result = result; for (i=0; i