/* Latin Hypercube Search (Multi-dimensional Grid Search) plain prototype */ new; cls; xmin={0,0,0}; xmax={10,10,10}; nn=5; nsteps=1; p=0.2; fn f(x)=sin(x[1])-cos(x[2])-x[3]; print LHsearch(xmin,xmax,nn,nsteps,p,&f); /* ** LHSCH01.txt - Latin Hypercube Search(Multi-dimensional Grid Search). A prototype. ** Using two index matrices. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets maximum values of function f(x) between xmin and xmax initially. ** ** Format: xstar=LHsearch2(xmin,xmax,nn,nsteps,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 ** ** nstep scalar, number of steps to focus in ** ** p scalar, focus ratio in each step ** ** &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 LHsearch(xmin,xmax,nn,nsteps,p,&f); local dim,y1,y2,range,step,zmax,zmaxj,xstar,i,j,k,index,x,z; local f:proc; /* indexing */ dim=rows(xmin); y1=dimindex0(nn,dim); /* location index */ y2=dimindex0(nn+1,dim); /* sub-location index */ /* initial step settings */ range=xmax-xmin; step=range/nn; zmax=f(xmin); xstar=xmin; k=1; do while k<=nsteps; print; j=1; do while j<=nn^dim; /* regular grid including endpoints(allow overlaps) */ x=xmin'+y1[j,.].*step'+y2.*step'/nn; /* calculation f(x) in each hypercube */ z=zeros((nn+1)^dim,1); i=1; do while i<=(nn+1)^dim; z[i]=f(x[i,.]'); i=i+1; endo; zmaxj=maxc(z); /* uptate xstar if larger solution zmaxj is found */ if zmax