Challenge

Week 4 - 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 uses three main types of rules at each step to create the next generation from the current population:

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

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

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

How the Genetic Algorithm Works

Outline of the Algorithm
The following outline summarizes how the genetic algorithm works:

1.The algorithm begins by creating a random initial population.

2.The algorithm then creates a sequence of new populations. At each step, the algorithm uses the individuals in the current generation to create the next population. To create the new population, the algorithm performs the following steps:

3.Scores each member of the current population by computing its fitness value. These values are called the raw fitness scores.

4.Scales the raw fitness scores to convert them into a more usable range of values. These scaled values are called expectation values.

5.Selects members, called parents, based on their expectation.

6.Some of the individuals in the current population that have lower fitness are chosen as elite. These elite individuals are passed to the next population.

7.Produces children from the parents. Children are produced either by making random changes to a single parent—mutation—or by combining the vector entries of a pair of parents—crossover.

8.Replaces the current population with the children to form the next generation.

9.The algorithm stops when one of the stopping criteria is met. See Stopping Conditions for the Algorithm.

code of the function in MATLAB to optimise :

function y=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.64)); 
    f2_y=exp(-4*log(2)*((y-0.0667)^2/0.64));
    y=-[f1_x*f1_y*f2_x*f2_y]; 
end

MAIN PROGRAM TO OBTAIN MAXIMUM AT BOUNDED UNBOUNDED AND POPULATION INCREASED:

clear all 
close all 
clc


%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

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

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

%study 1 
tic 
for i=1:num_case 
    [inputs,Fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1); 
    yopt(i)=inputs(2);

end

toc
figure(1)
subplot(2,1,1); 
hold on 
surfc(xx,yy,F);
shading interp 
ylabel('y data')
xlabel('x data')
plot3(xopt, yopt, Fopt, 'marker','o', 'markersize' ,5, 'markerfacecolor', 'k'); 
title('unbounded inputs')
subplot(2,1,2);
plot (Fopt);
xlabel('iterations'); 
ylabel('Function maximum');
%study 2 
tic
for i=1:num_case 
    [inputs, Fopt(i)]=ga(@stalagmite,2, [],[],[],[], [0:0], [1:1]); 
    xopt(i)=input(1);
    yopt(i)=input(2);
end

study_time_2=toc

figure(2)
subplot(2,1,1); 
hold on 
surfc(xx,yy,F); 
shading interp; 
ylabel('y data'); 
xlabel('x data');
plot3(xopt,yopt, Fopt, 'marker','o', 'markersize',5, 'markerfacecolor','k'); 
title('bounded inputs');
subplot(2,1,2); 
plot(Fopt); 
xlabel('iterations'); 
ylabel('Function maximum');

%study 3 
options=optimoptions('ga'); 
options=optimoptions (options, 'population size',450);
tic 
for i=150
    [inputs, Fopt(i)]=ga(@stalagmite, 2,[],[],[],[],[0:0],[1:1],[],[],options); 
    xopt(i)=inputs(1); 
    yopt (i)=input (2);
end

study_time_3=toc

figure(3) 
subplot(2,1,1); 
hold on 
surfc(xx,yy,F); 
shading interp; 
ylabel('y data');
xlabel('x data'); 
plot3(xopt, yopt, Fopt, 'marker','o', 'markersize',5, 'marker facecolor', 'k'); 
title('bounded inputs with increase population size'); 
subplot(2,1,2);
plot(Fopt); 
xlabel('iterations');
ylabel('Function maximum');

max_value=Fopt








PLOT'S:

EXPLANATION:

Step-wise explanation of the codes:

1. The code starts with defining x and y as inputs using linspace command which gets values between 0 to 0.6

2. A function known as Stalagmite.m is defined as shown in the 2nd stage of codes.

3. 2 for loops are incorporated in the main code so that all the combinations of x and y values is taken in the stalagmite function using the meshgrid command.

4. In the 1st study GA function runs for 50 iterations. Plots of the stalagmites and maximum values generated in each iteration is shown.

5. In the 2nd study GA function is incorporated with lower and upper bounds and runs for 50 iterations. Plot of the stalagmites and maximum values generated in each iteration is shown.

6. In the 3rd study GA function is incorporated with lower and upper bounds along with the population size. Plot of the stalagmites and maximum values generated in each iteration is shown.

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.