Project

Genetic Algorithm

Objective:

To optimize the stalagmite function (see fig. 1) and find the global maxima of the function using a Genetic Algorithm (ga).

Fig.1 Stalagmite Function

Genetic Algorithm (ga) is a method for function optimization based on mechanic of natural evolution. Genetic algorithm operates on a population to produce a better and better approximation.

For this project we are using MATLAB genetic algorithm and the syntax is as follows:

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

fun= The function that we are trying to optimize

nvars= The number of variables in the function.

A,b= Finds the local minimum subject to the linear inequalities A*x ≤ b.

Aeq, beq= Finds the local minimum subject to the linear equalities Aeq*x = beq and A*x ≤ b.

Lb, ub = Find the local minimum subjected to lower bound and upper bound.

Nonlcon= Returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0.

Option= We must create an option by using optimoptions (see figure 2). It returns a set options with named parameters alter with the specified value.

Fig. 2 Optimoptions syntax

Since, we only using the upper lower bound we must use empty brackets (see figure 3)

Fig.3 ga syntax with only lower and upper bound restrain.

First Study of Maximization

The first attempt to maximize the function we avoided setting the lower and upper bound. The Genetic Algorithm (ga) prediction is all over the place (see fig. 4). The random prediction is due to random sampling.

Fig. 4 Maximization without bounds. Top graph shows the predictions points on the function and the bottom shows the iteration and the corresponding minimum.

Second Study

The second attempt to maximize the function we included the lower and upper bound. The Genetic Algorithm (ga) prediction is within the bounds (see fig. 5). However, ga will find the local minimum multiple times.

Fig.5 Maximization with bounds. Top graph shows the predictions points on the function and the bottom shows the iteration and the corresponding minimum.

Third Study

The third attempt to maximize the function we included the lower and upper bound. Also, increase the number of sampling by using the adding ‘option’ to the Genetic Algorithm and specifying the population size (see fig. 6). If we do not specific the population size, the default population is too small and will find the local minimum multiple times as seen in the second attempt. By increasing the population size, the genetic algorithm will find the local minimum only two times of the 50 iterations. The final global maximum occurs at (0.0948,0.0948).

Fig. 6 Maximization with bounds and population size of 140. Top graph shows the predictions points on the function and the bottom shows the iteration and the corresponding minimum.

MATLAB codes

First Study

clear all 
close all
clc

%% sets the range for y and x. from 0 to .06 in 150 steps
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);

%% This expression call the funtion get_stalagmite. it needs two inputs.
[xx , yy]= meshgrid(x,y);
f= get_stalagmite(xx,yy);
num_cases=50;


%%
for i= 1:num_cases
    
    [inputs , fopt(i)]= ga(@stalagmite, 2);
    xopt(i)= inputs(1);
    yopt(i)=inputs(2);
end

figure(1)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
plot3(xopt, yopt, fopt, \'marker\', \'o\', \'markersize\', 5 , \'markerfacecolor\', \'r\')
title(\'Unbounded Inputs\')
subplot (2,1,2)
plot (fopt)
xlabel(\'Iteration\')
ylabel(\'function minimun\')

Second study

clear all 
close all
clc

%% sets the range for y and x. from 0 to .06 in 150 steps
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);

%% This expression call the funtion get_stalagmite. it needs two inputs.
[xx , yy]= meshgrid(x,y);
f= get_stalagmite(xx,yy);
num_cases=50;


%%
for i= 1:num_cases
    
    [inputs , fopt(i)]= ga(@stalagmite, 2,[],[],[],[],[0 ; 0],[1; 1]);
    xopt(i)= inputs(1);
    yopt(i)=inputs(2);
end

figure(1)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
plot3(xopt, yopt, fopt, \'marker\', \'o\', \'markersize\', 5 , \'markerfacecolor\', \'r\')
title(\'Unbounded Inputs\')
subplot (2,1,2)
plot (fopt)
xlabel(\'Iteration\')
ylabel(\'function minimun\')

Third Study

clear all 
close all
clc

%% sets the range for y and x. from 0 to .06 in 150 steps
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);

%% This expression call the funtion get_stalagmite. it needs two inputs.
[xx , yy]= meshgrid(x,y);
f= get_stalagmite(xx,yy);
num_cases=50;
options = optimoptions ( \'ga\', \'populationsize\' , 180);

%%
for i= 1:num_cases
    
    [inputs , fopt(i)]=ga(@stalagmite, 2,[],[],[],[],[0 ; 0],[1; 1],[],[],options);
    xopt(i)= inputs(1);
    yopt(i)=inputs(2);
end


figure(1)
subplot(2,1,1)
hold on
surfc(xx, yy, f)
shading interp
plot3(xopt, yopt, fopt, \'marker\', \'o\', \'markersize\', 5 , \'markerfacecolor\', \'r\')
title(\'Unbounded Inputs\')
subplot (2,1,2)
plot (fopt)
xlabel(\'Iteration\')
ylabel(\'function minimun\')

Seperate Function

function f =get_stalagmite(xx , yy)



f1x= (  sin(5.1*pi.*  xx  +.05)  ).^6;
f1y= (  sin(5.1*pi.*  yy  +.05)  ).^6;

f2x= exp(-4*log(2).*((  xx  -0.0667).^2)/0.64);
f2y= exp(-4*log(2).*((  yy  -0.0667).^2)/0.64);

f= -f1x.*f1y.*f2x.*f2y;

Code Explanation

The following lines clears the command and the workspace. Also, closes all graphs.

clear all

close all

clc

the following sets the range for y and x. from 0 to .06 in 150 steps

x= linspace(0,0.6,150);

y= linspace(0,0.6,150);

The following produce grid coordinates based on the coordinates contained in vectors x and y. X is a matrix where each row is a copy of x, and Y is a matrix where each column is a copy of y. then, these two matrix are inputs to the get_stalagmite function to be able to use ‘surfc’ and plot the function.

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

f= get_stalagmite(xx,yy);

num_cases=50;

The following for-loop is used to extract the x and y predictions of the Genetic Algorithm for each of the cases. In this for loop the genetic algorithm is run 50 times. Also, ‘inputs’ represents the two variables of the prediction and ‘fopt’ represent the values of the function predicted.

for i= 1:num_cases

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

xopt(i)= inputs(1);

yopt(i)=inputs(2);

end

To show the plots in one figure the command ‘subplot’ is used. The command subplot(m,n,p) divides the current figure into an m-by-n grid and creates axes in the position specified by p.

Errors while programming

  1. I did not in included the ‘I’ in ‘fopt(i)’
    1. The workspace only showed only one value for ‘fopt’. It produced the following error:

  1. I did not write the lower and upper bound properly. I wrote [ 0:1] , [0:1] and the correct way is [0:0],[1:1]. The prediction was incorrect.

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.