/* Finer Multi-Candidates Latin Hypercube Search (Multi-dimensional Grid Search) */ new; cls; xmin={0,0,0}; xmax={10,10,10}; nc=10; /* This number can be very big even in GAUSS Light. */ nn=2; nsteps=2; p=0.2; fn f(x)=sin(x[1])-cos(x[2])-x[3]; print fLHSsearch(xmin,xmax,nc,nn,nsteps,p,&f); /* ** LHSCH02m.txt - Finer Multi-Candidates Latin Hypercube Sampling Search ** (Multi-dimensional Grid Search). Stochastic Search. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets maximum values of function f(x) between xmin and xmax initially. ** ** Format: xstarstar=fLHSsearch(xmin,xmax,nc,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 ** ** nc scalar, number of initial segments(i.e. nc^dim candidates) ** ** 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: xstarstar vector, nn x 1 vector of maximum values ** ** Notice: This algorithm is very powerful even in GAUSS Light. Initial partition does not ** depends on any matrix expansion. Parameter nc could be very big. ** If you have some trouble to run, start over GAUSS again. ** */ proc fLHSsearch(xmin,xmax,nc,nn,nsteps,p,&f); local dim,y,range0,range,step,zmax0,zmax,xmin0,zmaxj,xstar,i,j,k,index,x,z,d,U,c,xstarstar; local f:proc; /* indexing */ dim=rows(xmin); y=dimindex(nn,dim); /* initial step settings */ zmax0=-1e256; xmin0=xmin; range0=(xmax-xmin)/nc; /* main */ d=1/(nn); c=0; do while c<=nc^dim-1; xmin=xmin0'+locounter(nc,dim,c)'.*range0'; xmin=xmin'; range=range0; step=range/nn; zmax=f(xmin); xstar=xmin; k=1; do while k<=nsteps; print; j=1; do while j<=nn^dim; /* LHS before scaling by parameter vector 'step' */ U=(d*y-d*(y-1)).*rndu(nn^dim,dim)+d*(y-1); /* [ location ] + [ U(0,1) inside each cube] */ x=xmin'+(y[j,.]-1).*step'+U.*step'; z=zeros((nn+1)^dim,1); /* 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 zmaxnc^dim-1; errorlog "ERROR: Parameter i must be 0<=i<=nn^dim-1."; retp("."); endif; x=zeros(dim,1); j=dim; do until j==1; x[j]=floor(i/(nc^(j-1))); i=i-(x[j]*(nc^(j-1))); j=j-1; endo; x[1]=i; retp(x); endp; /* ** dimindex.txt - Dimension Indexing. ** (C) Copyright 2005 Yosuke Amijima. All Rights Reserved. ** ** Purpose: Gets index numbers for given dimension in a very easy way. ** ** Format: y=dimindex(nn,dim); ** ** Input: nn scalar, max number of index (1,2,3,...,nn) ** ** dim scalar, dimension (1,...,dim) ** ** ** Output: y matrix , (nn^dim) x (dim) of resulting index matrix ** ** Notice: It takes lots of memory to run. Light version of GAUSS will not work in most cases. ** Here, index starts from 1 as in GAUSS. */ proc dimindex(nn,dim); local x,b,n,z,y; /* convert x(base 10) to y(base b) */ x=seqa(1,1,nn^dim-1); b=nn; n=maxc(log(x)/log(b))+1; z=reshape(b,n,rows(x)); y=rev(recserrc(x,z))'; /* adjustments */ y=y+1; /* shift all elements by 1 */ y=ones(1,dim)|y; /* insert 1's at the 1st row */ retp(y); endp;