Challenge

Week 4.1 - Genetic Algorithm

Genetic Algorithm-

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each step to create the next generation from the current population:

1-Selection rules select the individuals, called parents, that contribute to the population at the next generation.

2-Crossover rules combine two parents to form children for the next generation.

3-Mutation rules apply random changes to individual parents to form children.

syntax of ga-

ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)

ga -genetic algorithm.

fun-objective function.

nvars- no. of variables.

A - Linear inequality constraints

b - Linear inequality constraints

Aeq - Linear equality constraints

beq — Linear equality constraints

lb — Lower bounds

ub — Upper bounds

nonlcon — Nonlinear constraints

intcon — Integer variables

options — Optimization options

global maxima -'ga' finds global minimum as default, to find the global maxima we inver the stalagmite function by equating the value to negetive

programming code for optimise of stalagmite function and find the global maxima-

%defining our search space
x =linspace(0,0.6,150);
y =linspace(0,0.6,150);

%creating a 2Dimensional function
[xx yy] = meshgrid(x,y);

% evaluvating stalagmite function
for i= 1:length(xx)
    for j= 1:length(yy)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite(input_vector);
    end
end



[inputs,fval]= ga(@stalagmite,2);
x_output=inputs(1);
y_input=inputs(2);


% case-1 sampling for 50 itteration
N=50;
%to calculate the time taken to run the itteration we use tic and toc command 
%case 1 for unbounded inputs
tic 

for i=1:N
    [inputs,f1_out(i)]= ga(@stalagmite,2);
    x1_output(i)=inputs(1);
    y1_output(i)=inputs(2);
    
end
study_time=toc


figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')

shading interp
%using plot3 command to draw 3-d plot
plot3(x1_output,y1_output,f1_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
%plot for itteration vs f maximum
plot(f1_out)
%labeling the plot
xlabel('iteration')
ylabel('f maximum')
zlabel('f maximum')

% case-2 sampling for 50 itteration for bounded inputs
N=50;
%to calculate the time taken to run the itteration we use tic and toc command 
tic 
for i=1:N
    [inputs,f2_out(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    x2_output(i)=inputs(1);
    y2_output(i)=inputs(2);
    
end
study2_time=toc


figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
%labeling the plot
xlabel('x value')
ylabel('y value')
zlabel('f maximum')

shading interp
%using plot3 command to draw 3-d plot
plot3(x2_output,y2_output,f2_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inpuuts')
subplot(2,1,2)
%plot for itteration vs f maximum
plot(f2_out)
xlabel('iteration')
ylabel('f maximum')


% case-3 sampling for 50 itteration for bounded and controled population
% size 
options=optimoptions('ga','PopulationSize',200)

N=50;
%to calculate the time taken to run the itteration we use tic and toc command 
tic 
for i=1:N
    [inputs,f3_out(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x3_output(i)=inputs(1);
    y3_output(i)=inputs(2);
    
end
study3_time=toc


figure(3)
subplot(2,1,1)
hold on
%labeling the plot
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
zlabel('f maximum')

shading interp
%using plot3 command to draw 3-d plot
plot3(x3_output,y3_output,f3_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inpuuts for a controlled population size')
subplot(2,1,2)
%plot for itteration vs f maximum
plot(f3_out)
xlabel('iteration')
ylabel('f maximum')

function of stalagmite-

function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1_x=(sin(5.1*pi*x+0.5))^6;
f1_y=(sin(5.1*pi*y+0.5))^6;
f2_x=exp((-4*log(2)*(x-0.0667)^2)/0.46);
f2_y=exp((-4*log(2)*(y-0.0667)^2)/0.46);
f=-f1_x*f2_x*f1_y*f2_y;
end


screenshot

case 1-

the figure 1 show several peak which are stalagmite peak repreasintin g global maximum and local minimum.the biggest peak represent global minimum. the study is done for a 50 itteration for a unbounded inputs.but here 'ga' takes random value it might go beyond the values of input as there are no boundry given.the study1_time shows the time taken to perform the case 1

case 2- for bounded input

in fig 2, in case 2 here we give a boundry condion to the 'ga' from [0;0] to [0.6;0.6] sot he output of f maximum is bounde in the given range.the study2_time shows the time taken to perform the case 2

case 3-for bounded and controlled population

in fig 3, here we further reduce the search space by optimisinng the ga by reducing the population size to 200.the study3_time shows the time taken to perform the case 3

referrence-

https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html

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.