Project

Genetic Algorithm

REPORT

CODE EXPLANATION

The genetic algorithm is derived from the famous \"Theory of Evolution proposed by Sir Charles Darwin which states that organisms evolve over time through changes in their physical and biological traits that are heritable. This helps organisms have better adaptability and produce more offspring of the relative kind. Therefore, in short, a genetic algorithm is the process of solving constrained and unconstrained problems of optimization that is based on natural selection.

Steps Involved in Genetic Algorithm:

1. Initialization

2. Fitness assignment

3. Selection

4. Cross-Over

5. Mutation

6.Termination criteria

However, the ga code generates the global minima by default execution. To obtain the maxima we introduce a \'-\' align in the fopt command. This generates the peak for the global maxima.

The general syntax for \'ga\':

x=ga(fnc,n) finds a local unconstrained minimum, \'x\' to the objective function, \'fnc\' and \'n\' stands for the number of variables of the function \'fnc\'

Steps involved in coding the program and related observations:

1) Initially assign the variable arrays and the number of iterations

2) Then create xx and yy array by using the meshgrid command.

3) By using nested for loop, calculate the stalagmite function and store the value in f. The negative gives the global maxima of the study.

4) In Sludy-1, we calculate the global maxima by taking random samples from the functions. Here we use the tic-toc command for calculating the time taken for study one. Syntax for ga in study-1 is [inputs,fopt(i)]=ga(@stalagmite,2);

5) Study 2, calculates the global maxima by giving boundary limits for the inputs. Syntax for ga in study-2 is [inputs,fopt(i)]=ga(@stalagmite,2,[],[] ,[] ,[] ,[0;0],[1;1]); where the study is limited from 0 to 1.

6) Study 3, calculates the global maxima using population size by which we can find the specefic optimum point.

CODE

Main code

clear all

close all

clc

x = linspace(0,0.6,150);

y = linspace(0,0.6,150);

num_cases = 100;

%Creating 2D coordinate system

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

%Calculate the stalagmite function

for i= 1:length(xx)

for j= 1:length(yy)

m(1)= xx(i,j);

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

f(i,j) = stalagmite(m);

end

end

%Study-1

tic

%Taking inputs from the stalagmite function

for i = 1:num_cases

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

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

study1_time = toc

figure(1)

subplot(2,1,1)

hold on

%Obtaining surface plot and removing the grid

surfc(xx,yy,f)

shading interp

%plot nomenclature

title(\'Unbounded inputs\')

xlabel(\'x values\')

ylabel(\'y values\')

%Obtaining the marker points for the key points

plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'k\')

subplot(2,1,2)

%Planar plot for the inputs

plot(fopt)

xlabel(\'Iterations\')

ylabel(\'Function maximum\')

%Study-2-with constraints

%Using tic-toc command to determine the time taken for execution

tic

for i = 1:num_cases

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

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

study2_time = toc

figure(2)

subplot(2,1,1)

hold on

surfc(xx,yy,f)

shading interp

title(\'Bounded inputs\')

xlabel(\'x values\')

ylabel(\'y values\')

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

subplot(2,1,2)

plot(fopt)

xlabel(\'Iterations\')

ylabel(\'Function maximum\')

%Study-3-population limit

%Accepting populationlimit as the constraint

options= optimoptions(\'ga\')

options= optimoptions(options,\'PopulationSize\',170)

tic

for i = 1:num_cases

[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);

xopt(i) = inputs(1);

yopt(i) = inputs(2);

end

fopt = -fopt;

f =-f;

study2_time = toc

figure(3)

subplot(2,1,1)

hold on

surfc(xx,yy,f)

shading interp

xlabel(\'x values\')

ylabel(\'y values\')

plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'c\')

subplot(2,1,2)

plot(fopt)

xlabel(\'Iterations\')

ylabel(\'Function maximum\')

max_value = fopt

FUNCTION CODE

function [F] = stalagmite(m)

x = m(1);

y = m(2);

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

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

f3 = exp((-4*log(2)*((x-0.0667)^2))/(0.64));

f4 = exp((-4*log(2)*((y-0.0667)^2))/(0.64));

F = -(f1*f2*f3*f4);

end

Images

1. unconstrained data

\"\"

\"\"

2.constrained data

\"\"

\"\"

3.with population size

\"\"

\"\"

Mention the errors faced while programming

* Most of the common errors are typo errors.

* Difficulty in undertsnding the concept of genetic algorithm.

* Error in writing stagalamite function.

* Confusion in function input parametrs.

Steps taken to overcome those errors.

* Uses the MATLAB doc for correct syntax.

* Refer the video tutorial twice for bettter understanding of fuction.

* Refer to the book for more knowledge on genetic algorithm.

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.