=3) if(optim.maximizer_indicator) fprintf('though lower, point accepted\n'); else fprintf('though higher, point accepted\n'); end end x=xp; f=fp; n_accepted_draws=n_accepted_draws+1; nacp(h)=nacp(h)+1; ndown=ndown+1; else nrej=nrej+1; if(optim.verbosity >=3) if(optim.maximizer_indicator) fprintf('lower point rejected\n'); else fprintf('higher point rejected\n'); end end end end h=h+1; end j=j+1; end %* Adjust vm so that approximately half of all evaluations are accepted. * i=1; while i<=n ratio=nacp(i)/optim.ns; if(ratio>.6) vm(i)=vm(i)*(1.+c(i)*(ratio-.6)/.4); elseif(ratio<.4) vm(i)=vm(i)/(1.+c(i)*((.4-ratio)/.4)); end if(vm(i)>(ub(i)-lb(i))) vm(i)=ub(i)-lb(i); end i=i+1; end if(optim.verbosity >=2) fprintf('intermediate results after step length adjustment\n'); fprintf('new step length(vm) %4.3f', vm(:)'); fprintf('current optimal x %4.3f', xopt(:)'); fprintf('current x %4.3f', x(:)'); end nacp=zeros(n,1); m=m+1; end if(optim.verbosity >=1) print_intermediate_statistics(optim.maximizer_indicator,t,xopt,vm,fopt,nup,ndown,nrej,lnobds,nnew); end %* Check termination criteria. * quit=0; fstar(1)=f; if((fopt-fstar(1))<=optim.TolFun) quit=1; end if(sum(abs(f-fstar)>optim.TolFun)>0) quit=0; end %* Terminate SA if appropriate. * if(quit) exitflag=0; if(optim.maximizer_indicator==0) fopt=-fopt; end if(optim.verbosity >=1) fprintf('SA achieved termination criteria.exitflag=0\n'); end return end %* If termination criteria are not met, prepare for another loop. * t=optim.rt*t; i=optim.neps; while i>=2 fstar(i)=fstar(i-1); i=i-1; end f=fopt; x=xopt; %* Loop again. * end end function print_current_invalid_try(max,xp,x,fp,f) fprintf('\n'); disp(['Current x ' num2str(x(:)')]); if(max) disp(['Current f ' num2str(f)]); else disp(['Current f ' num2str(-f)]); end disp(['Trial x ' num2str(xp(:)')]); disp 'Point rejected since out of bounds'; end function print_current_valid_try(max,xp,x,fp,f) disp(['Current x ' num2str(x(:)')]); if(max) disp(['Current f ' num2str(f)]); disp(['Trial x ' num2str(xp(:)')]); disp(['Resulting f ' num2str(fp)]); else disp(['Current f ' num2str(-f)]); disp(['Trial x ' num2str(xp(:)')]); disp(['Resulting f ' num2str(-fp)]); end end function print_intermediate_statistics(max,t,xopt,vm,fopt,nup,ndown,nrej,lnobds,nnew) totmov=nup+ndown+nrej; fprintf('\nIntermediate results before next temperature reduction\n'); disp(['current temperature ' num2str(t)]); if(max) disp(['Max function value so far ' num2str(fopt)]); disp(['Total moves ' num2str(totmov)]); disp(['Uphill ' num2str(nup)]); disp(['Accepted downhill ' num2str(ndown)]); disp(['Rejected downhill ' num2str(nrej)]); disp(['Out of bounds trials ' num2str(lnobds)]); disp(['New maxima this temperature ' num2str(nnew)]); else disp(['Min function value so far ' num2str(-fopt)]); disp(['Total moves ' num2str(totmov)]); disp(['Downhill ' num2str(nup)]); disp(['Accepted uphill ' num2str(ndown)]); disp(['Rejected uphill ' num2str(nrej)]); disp(['Trials out of bounds ' num2str(lnobds)]); disp(['New minima this temperature ' num2str(nnew)]); end xopt1=xopt(1:round(length(xopt)/2)); xopt2=xopt(round(length(xopt)/2)+1:end); disp(['Current optimal x1 ' num2str(xopt1')]); disp(['Current optimal x2 ' num2str(xopt2')]); disp(['Strength(vm) ' num2str(vm')]); fprintf('\n'); end