Challenge

Week 4.1 - Genetic Algorithm

Challenge Title:- By using Genatic Algorithm find the Maxmium value of stalagmite

Genetic algorithms are commonly used to generate high-quality solutions to optimization and search

problems by relying on biologically inspired operators such as mutation, crossover and selection.

1.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 repeatedly

modifies a population of individual solutions.

2.At each step, the genetic algorithm selects individuals at random from the current population to be parents

and uses them to produce the children for the next generation. Over successive generations, the population

"evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimization

problems that are not well suited for standard optimization algorithms, including problems in which the

objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.

3.The genetic algorithm can address problems of mixed integer programming, where some components are

restricted to be integer-valued.

4.The genetic algorithm uses three main types of rules at each step to create the next generation from the

current population:

a)Selection rules select the individuals, called parents, that contribute to the population at the next

generation.

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

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

5.Generates a population of points at each iteration. The best point in the population approaches an optimal

solution.

6.elects the next population by computation which uses random number generators.

funtion for the stalagmite:-

function dxy= stalagamite_function(input_vector)
x=input_vector(1);
y=input_vector(2);
f1 = (sin(5.1*pi*x+0.5))^6;
f2 = (sin(5.1*pi*y+0.5))^6;
f3 = exp(-4*log(2)*((x-0.667)^2)/0.64);
f4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
dxy = -(f1*f2*f3*f4); % to get the stalagmite peak on the positive 
end

in the above code explans about the stalagmite function formulae

f1,f2,f3,f4 are the function formulae to get F maximum and minimum

dxy is the product of all the formulae to get plot results

x,y are the two function variables or inputs for the function

stalagmite function mathematical model to know the peak using genetic algorithm

clear all 
close all
clc
%define the search spacing
x=linspace(0,0.6,150); %linespace is a command to get numbers
y=linspace(0,0.6,150);  %linespace is a command to get numbers
%creating the 2dimensional mesh
[xx yy] = meshgrid(x,y);  %meshgrid is command which pairs the x and y inputs respectivly rows and columns
%Evaluating the stalgmite funciton
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) = stalagamite_function(input_vector);
    end
end
surfc(xx,yy,f); %creates a 3d-dimensional surface plot with a contour plot underneath
shading interp % to hide the mesh on the stalagmite 3D plot
[inputs,fval]= ga(@stalagamite_function,2);
%GA is in build commmand to determine the F max and min by using input
% function, here we need to give input variable count and in options
% populate number

linespace is a command to get number from given input co ordinate upper bound to lower bound and between how many number should me there

surfc(X,Y,Z) :- creates a three-dimensional surface plot with a contour plot underneath. A surface plot is a three-dimensional surface that has solid edge colors and solid face colors. The function plots the values in matrix Z as heights above a grid in the x-y plane defined by X and Y.

mesh grid:- mesh grid is a command which pairs the x,y input co ordinates respectivly to form a matrix rows and columns

Code:- using genatic algorthm finding the maximum peak value using population optioin, we are doning coding for the four different cases studies( CASE1,CASE2,CAE3,CASE4)

clear all;
close all;
clc;
%define the search spacing
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
numcase = 30;
%creating 2D aray using meshgrid function
[xx yy] = meshgrid(x,y);
%nested for loop  array valueinput 
%Evaluating the stalgmite funciton
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) = stalagamite_function(input_vector); % function command
    end
end
tic  % to start command with time intervals
for i= 1:numcase
    [input,fval(i)]=ga(@stalagamite_function,2);
    %in case1 ga using two input variable x,y
    x_out(i)=input(1);
    y_out(i)=input(2);
end
case_time_1=toc
figure(1)
subplot(2,1,1) %subplot is used to show the out put in single window different outputs
surfc(xx,yy,-f) 
xlabel('x')
ylabel('y')
title('case1(unbounded') % title to display the heading 
shading interp %it used shade to stalagmite 3D-graph
hold on
plot3(x_out,y_out,-fval, 'marker','o', 'MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2) %subplot is used to show the out put in single window different outputs
plot(-fval)
xlabel('Iteration')
ylabel('Function minuimum')

%case 2 intersecting bonding box
tic
for i= 1:numcase
    [input,fval(i)]=ga(@stalagamite_function,2,[],[],[],[],[0;0],[0.6;0.6]);
%case2 ga we have given the upper and lower bound condition like a box
%by using boundary condition the points calculate the stalagmite peaks
    x_out(i)=input(1);
    y_out(i)=input(2);
end
case_time_2=toc
figure(2)
subplot(2,1,1)
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('case2 bounded','Color','k')
shading interp
hold on
plot3(x_out,y_out,-fval, 'marker','o', 'MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iteration')
ylabel('max Function minuimum')

%case 3 optimzatioin
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',80)
tic
for i= 1:numcase
    [input,fval(i)]=ga(@stalagamite_function,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
%case 3 ga function have options and population size so that the points may
%create opporximatly plot the peaks of stalagmite
    x_out(i)=input(1);
    y_out(i)=input(2);
end
case_time_3=toc
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('case3 bounded','Color','k')
shading interp
hold on
plot3(x_out,y_out,-fval, 'marker','o', 'MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iteration')
ylabel('max Function minuimum')

%case 4 optimzatioin
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',150)
tic
for i= 1:numcase
    [input,fval(i)]=ga(@stalagamite_function,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    %case 4 ga function have options and population size so that the points may
%create opporximatly plot the peaks of stalagmite
    x_out(i)=input(1);
    y_out(i)=input(2);
end
case_time_4=toc
figure(4)
subplot(2,1,1)
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('case4 bounded','Color','b')
shading interp
hold on
plot3(x_out,y_out,-fval, 'marker','o', 'MarkerSize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iteration')
ylabel('max Function minuimum')

case1:-

ga using two input variable x,y

case2:-

case2 ga we have given the upper and lower bound condition like a box ..,

by using boundary condition the points calculate the stalagmite peaks

case3:-

function have options and population size 100 so that the points may

create opporximatly plot the peaks of stalagmite

case4:-

ga function have options and population size 200 so that the points may

create opporximatly plot the peaks of stalagmite

Output:-

all the commands,plots are attached below

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.