{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2 - 3 SA.m function[solution sbest =...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
3. SA.m function [solution sbest] = SA(sinitial,Tinitial,alpha,beta,Minitial,maxiter) solution= zeros(maxiter,3); T= Tinitial; M= Minitial; scurrent= sinitial; sbest= scurrent; snew= scurrent; COSTcurrent= cost(scurrent); COSTbest= COSTcurrent; iter= 0; while (iter < maxiter) Mtemp= M; while (Mtemp>0) rnd= rand(); if (rnd > .5) snew(1)= neighbor(scurrent(1)); else snew(2)= neighbor(scurrent(2)); end COSTnew=cost(snew); deltaCOST=COSTnew-COSTcurrent; if (deltaCOST<0) scurrent= snew; COSTcurrent=COSTnew; if (COSTnew<COSTbest) sbest= snew; COSTbest=COSTnew; end else rnd2= rand(); if (rnd2<exp(-1*deltaCOST/T)) scurrent= snew; COSTcurrent=COSTnew; end end Mtemp=Mtemp-1; end iter= iter + M; T= alpha*T; M= beta*M; solution(time,1)= iter; solution(time,2)= COSTcurrent; solution(time,3)= COSTbest; end cost.m function [cost] = cost(S) if (size(x)~= [1 2])
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
display( 'input S must be a vector with 2 entries, s1 and s2' ) else s1= S(1); s2= S(2); if (s1<=127 && s1>=0 && s2<=127 && s2>=0) cost= (10^9)-(625-(s1-25)^2)*(1600-(s2- 10)^2)*sin(s1*pi/10)*sin(s2*pi/10); else display( 's1 and s2 must be between 0 and 127' ) end end neighbor.m function [snew] = neighbor(s) if
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}