/* Latin Hypercube Search (Multi-dimensional Grid Search) */ /* Switching between deterministic and stochastic search */ /* with flexible focus ratios. */ new; cls; xmin={1,0,0}; xmax={10,10,10}; nn=2; status={1,0,0}; /* 0 if deterministic, 1 if stochastic search */ p={0.2,0.1,0.1}; /* the last one is void */ fn f(x)=sin(x[1])-cos(x[2])-x[3]; print sLHSsearch(xmin,xmax,nn,status,p,&f); /* ** LHSCH03.txt - Switching Latin Hypercube Search(Multi-dimensional Grid Search). ** Switching search types with flexible focus ratio. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets maximum values of function f(x) between xmin and xmax initially. ** ** Format: xstar=sLHSsearch(xmin,xmax,nn,status,p,&f); ** ** Input: xmin vector, nn x 1 vector of initial minimum values to search ** ** xmax vector, nn x 1 vector of initial maximum values to search ** ** nn scalar, number of segments ** ** status vector, 0 if deterministic, 1 if stochastic search ** (the number of rows of this is the number of steps to focus in) ** ** p vector, focus ratios in each step (the last one is void) ** ** &f pointer to a procedure of objective function f(x) to be maximized ** ** ** Output: xstar vector, nn x 1 vector of maximum values ** ** Notice: It takes lots of memory to run. Light version of GAUSS will not work in most cases. ** If you have some trouble to run, start over GAUSS again. */ proc sLHSsearch(xmin,xmax,nn,status,p,&f); local dim,y1,y2,y3,range,step,zmax,zmaxj,xstar,i,j,k,index,x,z,d,U,nsteps; local f:proc; /* indexing */ dim=rows(xmin); y1=dimindex0(nn,dim); /* location index */ y2=dimindex0(nn+1,dim); /* sub-location index */ y3=dimindex0(nn,dim)+1; /* stochastic index beginning from 1 */ /* initial step settings */ range=xmax-xmin; step=range/nn; zmax=f(xmin); xstar=xmin; /* main */ nsteps=rows(status); /* # of steps to focus in */ d=1/(nn); k=1; do while k<=nsteps; print; j=1; do while j<=nn^dim; if status[k]==0; /* deterministic search */ /* regular grid including endpoints(allow overlaps) */ x=xmin'+y1[j,.].*step'+y2.*step'/nn; elseif status[k]==1; /* stochastic search */ /* LHS before scaling by parameter vector 'step' */ U=(d*y3-d*(y3-1)).*rndu(nn^dim,dim)+d*(y3-1); /* [ location ] + [ U(0,1) inside each cube] */ x=xmin'+(y3[j,.]-1).*step'+U.*step'; else; errorlog "ERROR: Elements of 'steps' must be 0 or 1."; retp("."); endif; /* calculation f(x) in each hypercube */ z=zeros(nn^dim,1); i=1; do while i<=nn^dim; z[i]=f(x[i,.]'); i=i+1; endo; zmaxj=maxc(z); /* uptate xstar if larger solution zmaxj is found */ if zmax