Challenge

Week 4.1 - Genetic Algorithm

Aim:

To optimise the given stalagmite function and Global maxima of the function.

Introduction of stalagmite and stalactites:

Stalagmite: It is upward growing mound of mineral deposite that have precipitated from water dripping onto the floor of a cave Most stalagmites have rounded or flattened tips. it was opposed from stalactites.

Stalactites: It is upward growing mound of mineral deposite that have precipitated from water dripping from caves. These formattion hang from the ceiling. it was opposed from stalagmite.

The formula for stalagmite functions

Introduction to the Genetic Algorithm(GA) is method for solving both constrained and unconstrained optimization problem that is based on natural selection that drives biological evolution.An intial set of solution is tested against the objective function

Genetic algorithm has 3 step

  1. Selectio rule: select the indiviuals called parent, that contribute to the population at the next generation
  2. Crossover: combain two parent to form children for the next genration
  3. Mutation: Rules apply random changes to indiviuals parent to form children

Syntax

x = ga(fun,nvars)

x = ga(fun,nvars) find a local unconstrained minimum,x,to the objective function,fun.nvars is the number of design variable of function,fun.

Code Explanation:

First a random array of a x,y value are created and corresponding 2d array was obtained using meshgrid function. A nested loop is used to create the array into array named as input_vector, the stalagmite function is written seperately. this function used to identify the genetic algorithms(ga)for global maxima.GA stands only to find global minima . tic toc command is used to get running time of each case.In the first cases the bound were not finished and plot were made. Bounding is given in next cases so that number of solution approaches more accuracy.The population is increased in the subsequent cases and plotting is done. the global maximum is find at the last case. the x and y values for which the function has which maximum value is displayed.The iteration were decrease to a such a value that the result didnt vary and the program were consumed minimum time to execute.

Stopping criteria:

  • Generation - The algorithms stops when the number generation reaches when the values generation
  • Time limit - The algorithms stops after running an amount of time is equal to time limit
  • Fitness limit - The algorithms stops when the value of the fitness function for the best point in the current population is less than or equal to fitness limit.

Main Program:

clear all
close all
clc

%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;


%creating 2dimensional mesh
[xx,yy] = meshgrid(x,y);

%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_function(input_vector);
end
end

%study 1- statitcal behaviour
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_function,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',6,'MarkerFaceColor','r')
title('unbounded')
subplot(2,1,2)
plot(fopt)
xlabel('iterations');
ylabel('Function minimum');

%study2 statistical behaviour-with upper and lower bounds
for i = 1:num_cases
[inputs, fopt(i)] = ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6,0.6]);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end

figure(2)
subplot(2,1,1)
hold on;
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iterations');
ylabel('Function minimum');

%study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);

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

figure(3)
subplot(2,1,1)
hold on;
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'Marker','o','MarkerSize',6,'MarkerFaceColor','r')
title('bounded inputs with modify population size')
subplot(2,1,2)
plot(fopt)
xlabel('iterations');
ylabel('Function minimum');

Function Code:

function [f] = stalagmite_function(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi).*input_vector(1)+0.5)).^6;
f1y = (sin((5.1*pi).*input_vector(2)+0.5)).^6;

f2x = exp((((input_vector(1)-0.667).^2)/0.64).*(-4*log(2)));
f2y = exp((((input_vector(2)-0.667).^2)/0.64).*(-4*log(2)));
f1 = f1x*f1y*f2x*f2y;
f=-f1;
end

Error:

The error that face in making of function program, i didnt place the x and y value so that is minor mistake of this program

And the error was corrected then i have the program successful.

Result:

case 1:Unbounded

Case 2:Bounded

Case 3: Bounded with more population size

value of x and y for maximum value function

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.