Code covered by the BSD License  

Highlights from
Random Search Algorithm

image thumbnail

Random Search Algorithm

by

 

Random search belongs to the fields of Stochastic Optimization and Global Optimization.

RSA_visual.m
%% Random Search Algorithm (Pure Random Search Algorithm)
% by : Reza Ahmadzadeh (seyedreza_ahmadzadeh@yahoo.com - reza.ahmadzadeh@iit.it)
% 14-10-2012
% This code tests the RSA algorithm on a convex function in 2 dimensions
clc
close all
clear all
% 1- Draw the objective function - because the example has 2 dimensions we can draw it
% Make a grid space
x1=linspace(-5,5,100);				
x2=linspace(-5,5,100);
k=1;
for i=1:length(x1)
    for j=1:length(x2)
        gridspace(1,k)=x1(i);
        gridspace(2,k)=x2(j);
        k=k+1;
    end
end
% evaluate the function in grid space
z=gridspace(1,:).^2 + gridspace(2,:).^2;
% draw using plot3
plot3(gridspace(1,:),gridspace(2,:),z,'.');hold on

%% RSA with some modification to visualize the process
dim=2;
popsize=100;
ftarget=0.01;
numIter=100;
ObjFun=@(x) sum(x.^2);
for i=1:numIter
    candidate=10*rand(dim,popsize)-5;
    cost=feval(ObjFun,candidate);
    [best,ind]=min(cost);
    plot3(candidate(1,ind),candidate(2,ind),best,'xr');hold on;drawnow
    if best <= ftarget
        break;
    end
end
disp(best);


Contact us