Challenge

Week 5 - Genetic Algorithm

% What is Genetic Algorithm in matlab.

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents 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 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 differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.

Classical Algorithm Genetic Algorithm
Generates a single point at each iteration. The sequence of points approaches an optimal solution. Generates a population of points at each iteration. The best point in the population approaches an optimal solution.
Selects the next point in the sequence by a deterministic computation.

Selects the next population by computation which uses random number generators.

% Stalagmite Code

function A = stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5)).^6;
f2=exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3=(sin(5.1*pi*y+0.5)).^6;
f4=exp((-4*log(2))*((y-0.0667).^2)/0.64);
A =-(f1*f2*f3*f4);
end
% Code
close all
clear all
clc
% Inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_spaces=50;
% Forming 2d array
[xx,yy]=meshgrid(x,y);
for i=1:length(x)
for j=1:length(y)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)=stagalmite(input_vector);
end
end
% Study 1- UNBOUNDED INPUTS
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time1=toc
figure(1)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% Study 2- BOUNDED INPUTS
tic
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6])
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
% Study3- INCREASED INITIAL POPULATION
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_spaces
[results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
xopt(i)=results(1)
yopt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
OUTPUTS :
1) UNBOUNDED INPUTS
2) BOUNDED INPUTS
3) INCREASED INITIAL POPULATION
PROGRAM EXPLANATION :
1) Firstly, we create the stalagmite function then we initialize the value of x and y by considering the no. of iteration 50
2) As we have to plot 3d surface so we will take 2d arrays using meshgrid command to store inputs so that it can be used to call the stalagmite function.
3) Now we make our first study which takes unbounded inputs with study time of 2.4997s
4) Next we go for the next study which is bounded inputs with study time of 3.3602s
5) Lastly we study our third type which is increased initial population with study time of 11.8696s
6) Thus, we conclude the stalagmite function challenge.
ERRORS AND SOLUTIONS:
1) BASIC SPELLING MISTAKES
Solved by going thoroughly to the codes
2) Forgot to save the stalagmite function in the same folder
Solved by asking in the forums and guided by the skill lync guide
3) Forgot to use 'end' command after using the 'for' command
Solved by getting errors in the codes
REFERNCES :
1) GOOGLE WIKIPEDIA
2) SKILLLYNC.COM
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.