function [xval] = simulated_annealing() tic for i= 0:3 x(i+1)=floor(random('unif',-10,10)); end xval=x(1)^2+x(2)^2+x(3)^2+x(4)^2; disp (x); t = 1 ; bool =true; while bool t=t*.9; disp (t); temp=true; count=0; while (temp && count<11) count=count+1; %random selection from neighbors newx(1)= floor(random('unif',x(1)-1,x(1)+1)); newx(2)= floor(random('unif',x(2)-1,x(2)+1)); newx(3)=floor( random('unif',x(3)-1,x(3)+1)); newx(4)= floor(random('unif',x(4)-1,x(4)+1)); %evaluate new x value newxval=x(1)^2+x(2)^2+x(3)^2+x(4)^2; disp(x); disp(newx); delta = newxval- xval ; if delta<0 x=newx; temp=false; else p =exp(-delta/t); if p > 0.3 x=newx; temp=false; end end end if count>11 bool=false; end end toc disp (toc);
randi
random
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)