Project

Write a code in matlab to optimise the stalagmite functions and find the global maxima of the functions.

AIM: Programming to optimise the stalagmite finction and find the global maxima of the function.

THEORY:

1) Initial population

2) Fitness function

3) Selection

4) Crossover

5) Mutation

Genetic Algorithms are method that work based on Daewin's theory of natural selection:The fittest survives ,and has a greater chance of breeding and passing forward its genetic information.Let's say we have an equation unsolvable by anylitical methods,or an optimisation problem that takes too long to solve . first program randomly generates a population of solutions.Then,these solutions are evaluated by an function adequate to the problem ,determining how well they'fit'.The ones with best fit will have a greater chance of being selected to breed and generate the nesxt population.Finally the selected solution are recombined amongst themselves creating a new population.Random shifts,just like mutation ,also happen in order to introduce more variability.Whereas the first generation was completely random,the second is biased toward solution with better results.After several iterations,thet tend to converge to the optimum value.

In essence,it is very close to what evolution purpose .A population is evaluated by the environment; the fittest have a greater chance to breed and pass their 'good' genes along, which will them be recombined and mixed .The next generation will now have more genetic characterstics that represent a good fit in the current environment .GA's are especially attractive to rapidly find solution to diificult high-dimension problems ,with liitle information about the search space .As downside ,it does not always return the optimal value,as any heurstic .Instead ,it returns a value as good as the problem and the stopping criteria premit .

SYNTAX IN MATLAB

NOTE- : ga function only give minimum value (however we want to calculate maxima,hence refers my fitness function for maxima)

[x,fval]=

ga(function ,no of variables ,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)

1) x= optimum solution/variables

2) no of variables = Number of variables ,specified as a positive integer

3) A,B=Linear inequality constraints

For example ,to specify

2x1+2x2≤13

x1-x2≤20

x1+3x2≤30

give these constraints :

A=[2,2;1,-1;3]

b=[13;20;30]

4) Aeq,beq = Linear equality constraints

For example to specify

2x1+2x2=13

x1-x2=20

x1+3x2=30

give these constraints :

Aeq = [2,2;1,-1;1,3]

beq =[13;20;30]

5) lb,ub

lb = lower bounds

ub = Upper bounds

For example ,to specify

1 <=x(1)<=6

-3<=x(2)<=8

give these constraints

lb = [1 -3]

ub [ 6 8]

6) nonclon : Use for nonlinear constraints (we will see this in another program)

Intcon : to get solution in integer

option : Minimize with non default option ( Refere below program)

options = optimoptions('function','Constraints',limit);

PROGRAM

FITNESS FINCTION

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

In order to get maxima from, this fucntion we added negative sign in stalagmite function .We also add negative sign in the main function to get correct value.

Genetic Algorith Case 1

% STEP 1
%clearing workspace ,command window & closing all plots
 close all
 clear all
 clc

 % STEP 2
 % define our search space
 x = linspace(0,0.6,150);
 y = linspace(0,0.6,150);
% step 3
% calculating a 2D dimensional mesh
[xx, yy] = meshgrid(x,y);


% step 4
% evaluating the 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

tic
%step 5
%study1 -statical behaviour
num_cases=50;
for i = 1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,30);
    xopt(i)= inputs(1);
    yopt(i)=inputs(2);
end
study1_time = toc
F=-f;
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum');

Explanation

Step -1 Cleaaring workspace ,command window and closing al plots.

Steo 2 - Define our search space by using linspacae command

Step 3 - Calculating a 2 Dimentional mesh using meshgrid command.

Step 4 - Evaluating the stalagmite function for each value of x &y.

Step 5: using ga command with unbounded inputs means optimum value may be come from outside search space

RESULT GRAPH FOR THIS CASE :

GENETIC ALGORITH CASE 2

% STEP 1
%clearing workspace ,command window & closing all plots
 close all
 clear all
 clc

 % STEP 2
 % define our search space
 x = linspace(0,0.6,150);
 y = linspace(0,0.6,150);
% step 3
% calculating a 2D dimensional mesh
[xx, yy] = meshgrid(x,y);


% step 4
% evaluating the 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



%step 6
%study2-statical behaviour with upper and lover bounds
options = optimoptions('ga','populationsize',50);
tic
num_cases = 50
for i = 1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end
study2_time = toc
figure(2)
F=-f
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('functions maximum')

EXPLANATION STEP 6 :

using GA command wit bounded inputs means optimum value will be come from inside of search space,However there will be a huge variation in optimum value

RESULT GRAPH FOR THIS CASE

GENETIC ALGORITH CASE 3

% STEP 1
%clearing workspace ,command window & closing all plots
 close all
 clear all
 clc

 % STEP 2
 % define our search space
 x = linspace(0,0.6,150);
 y = linspace(0,0.6,150);
% step 3
% calculating a 2D dimensional mesh
[xx, yy] = meshgrid(x,y);


% step 4
% evaluating the 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


%step 7 
%study3 -increasing GA iteration
options=  optimoptions('ga','populationsize',200);
tic
num_cases = 50;
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

study3_time=toc
figure(3)
F =-f;
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population size ')
xlabel('x')
ylabel('y')
zlabel('f')
subplot(2,1,2)
plot(-fopt)
axis([0 50 0 1.5])
xlabel('iterations')
ylabel('function maximum')
FMAX = -min(fopt);
X= mean(xopt);
Y = mean(yopt);

EXPLANATION OF STEP 7

using GA command with bounded inputs means optimum value will be come from inside of search space . However

this time to avoid huge variation in optimum value,we have increased initial population size to 200 .( if we donot specify initial population in program ,MATLAB take 50 as initial population)

RESULT GRAPHY FOR THIS CASE

WORKSPACE VALUE

CONCLUSION -

Thus the global maxima of the stalagimate function is optimised using genetic algorithm in MATLAB .

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.