Challenge

Week 4.1 - Genetic Algorithm

AIM : To write matlab code to optimise the stalagmite function and find the global maxima of the function.

THEORY

Optimisation is a process of finding desirable solution of a given problem which leads to the best performance undergiven constraints. For example optimisation can be done in case of aeroplane for its designing to reducethe drag force and to maximize the lift force for all angle of flight .In short of a given input , how desirable outputcan be achived by using optimization .

Genetic algorithm is search based optimization technique based on the principles of genetics and natural selection. It is used to find optimum solution of complicated problem, which otherwise take very long time to solve . It uses techniques such as selection, mutation, inheritance and recombination to solve problem. In genetic algorithm group of individual is created randomly from given population .In GA population represents all posibles solution that given problem can have .

ADVANTAGES OF GA

  • Faster and more efficient mathod compared to other mathods available.
  • It gives all possible solutions or output for given inputs .
  • It can be used to find out solution of complicated problem which otherwise takes very long time to solve .
  • Useful when possible no. of solution is very large .

BASICS STEPS IN GA

  • Start with initial population
  • Selection of parent candidates solurtions for mating
  • Crossover and mutation are applied ro generate new candidates saolution
  • Determination of fitness of new candidates solution
  • Replacement of existing solution candidates with better solution got in previous step.
  • Repetation of above process ill opyimum solution is got .

Genatic algorithm syntax in matlab

Above is the syntax to determine minimum value of a function using matlab . Here GA represnts genetic algorithm , stalagmite _fun is the function name and to represent no of variables.

X = ga (fun,nvars,A,B,Aeq ,beq,lb,ub)

above syntax defines or specifies upper and lower bounds(lb)

MATLAB CODE

FUNCTION CODE

function f = stalagmite_fun(input)

X = input(1);

Y = input(2);

f1x = (sin(((5.1*pi).*x)+0.5)).^6);

f1y = (sin(((5.1*pi).*y)+0.5)).^6);

f2x = exp((((x-0.6667).^2).*(-4log(2)))./0.64);

f2y = exp((((y-0.667).^2).*(-4log(2)))./0.64);

f = -f1x*f1y*f2x*f2y;

MAIN CODE

clear all

close all

clc

% Defining search space

x =linspace (0,0.6,500);

y = linspace(0,0.6,500);

num_case = 50;

% creating 2d mesh

[xx yy] = meshgrid(x,y);

for i = 1:length(xx)

for j = 1:length(yy)

input(1) = xx(ij);

input(2) = yy(i,j);

f(i,j) = stalagmite_fun(innput);

end

end

%study 1 statistical behaviour of genetic algorithmic

for i = 1:num_cases

[inputs,fopt(i)] = ga(@stalagmite_fun,2);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

fopt = -opt

f = -f

study1_time = toc

figure(1subplot(2,1,1)

hold on

surfc(x,y,f)shading interp

plot3(xopt,yopt,fopt,'marker','0','markersize',5,'markerfacecolor','r')

title('unbounded inputs')

subplot(2,1,2)

plot(fopt)

xlabel('iteration')

ylabel('functions maxima')

% study 2 statistical bahaviour - with upper and lower bounds

for i = 1:num_caese

[inputs,fopt(i)] = ga(@stalagmite_fun,2,[],[],[],[],[0,0],[1,1]);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

fopt = -fopt

f = -f

figure(2)

subplot(2,1,1)

hold on

surfc(x,y,f)

shading interp

plot3(xopt,yopt,fopt,'marker','0','markersize',5,'markerfacecolor','r')

title('boundinputs')

subplot(2,1,2)

plot(fopt)

xlabel('iteration')

ylabel('functionsmaxima')

%study 3 increasing GA iteration

options = optimoptions ('ga');

options = optimoptions(options,'populationsize',500);

tic

for i = 1: num_cases

[inputs,fopt(i)] = ga(@stalagmite_fun,2,[],[],[],[],[0:0],[1:1],[],[],options);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

fopt = -fopt

f = -f

study3 time = toc

figure(3)

subplot(2,1,1)

hold on

surfc(x,y,f)

shading interp

\plot3(xopt,yopt,fopt,'marker','0','markersize',5,'markerfacecolor','r')

title('bounded inputs with modified population size')

subplot(fopt)

xlabel('iteration')

ylabel('function maxima')

COMMENT

Thanks for choosing to leave a comment .Please keep in mind that all comments are moderates according to our comment policy and your email address will not be published lets have a meaningful conversation.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.