ORIE5340_HW1_RoyLoLee - function [cost] = cost(s) s=...

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

View Full Document Right Arrow Icon
Sheet1 Page 1 function [cost] = cost(s) s= floor(s) cost=(400-(s-21).^2).*sin(s.*pi./6) else display('S must be between 0 and 500') end function [snew] = neighbor(s) x= randi([max(s-25,0),min(s+25,500)]) while x == s %prevents neighborhood function from selection 's' again x= randi([max(s-25,0),min(s+25,500)]) end snew= x else display('s not in valid range') %'s' must be between 0 and 500 end function solution= RW(sinitial, maxiter) solution = zeros(maxiter,5) scurrent= sinitial sbest= scurrent COSTbest =cost(scurrent) for i=1:maxiter scurrent= neighbor(scurrent) COSTcurrent= cost(scurrent) if COSTcurrent<COSTbest COSTbest=COSTcurrent sbest= scurrent end solution(i,1)= i solution(i,2)= scurrent solution(i,3)= sbest solution(i,4)= COSTcurrent solution(i,5)= COSTbest end else display('s not in valid range') end function solution = RS(sinitial, maxiter) solution = zeros(maxiter,5) snew= sinitial sbest= snew COSTnew= cost(snew) COSTbest= cost(snew) for i=1:maxiter x= randi([0,500]) %prevent S from staying the same
Background image of page 1

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

View Full DocumentRight Arrow Icon
Sheet1 Page 2 while x == snew x= randi([0 500]) end stest= x COSTtest= cost(stest) if COSTtest<COSTbest COSTbest= COSTtest
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.

This note was uploaded on 10/02/2011 for the course ORIE 5430 taught by Professor Shoemaker during the Fall '11 term at Cornell University (Engineering School).

Page1 / 5

ORIE5340_HW1_RoyLoLee - function [cost] = cost(s) s=...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online