Challenge

Week 4 - Genetic Algorithm

AIM:

Write a Matlab program to Optimize the stalagmite function and to find the global maxima of the function.

OBJECTIVE:

The objective of this report is to Optimize the stalagmite function by coding the program in Matlab and explain th concept of generic algorithm and also explain the concept of 'ga' syntax in MATLAB.

THEORY:

STALAGMITE FUNCTION:

Stalagmite function is a mathematical model that helps to generate maximas and minimas.Global maxima is a maximum peak value in entire function and global maxima is a minimum peak value in entire function. But local maxima and local minima are multiple number of values.

Optimization is finding of maxima and minima in a particular function but in generic algorithm , its only to find maxima.

here ,we have to optimize the stalagmite function using ga.

GENERIC ALGORITHM:

A Generic algorithm developed by charles darwins theory of natural evolution.This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to the next generation.

In MATLAB , generic algorithm generates a population of points at each iteration. The best point in the population approaches an optimal solution.selects the next population by computation which uses random number generators.

Genetic algorithm solves smooth or nonsmooth optimazation problems with any types of constraints, including integer constraints.It is a stochastic , population based algorithm that searches randomly by mutation and crossover among population members

Generic algorithm uses three main types of rules , at each step it creates the next generation for current population.

1. Selection rules which selects the individuals ,called parents, that contributes the next generation from the current population .

2. Crossover rules in which it combines two parents to form children for the next generation.

3. Mutation rules apply random changes to individual parents to form a children.

MATHEMATICAL EQUATION FOR STALAGMITE FUNCTION:

GA SYNTAX FROM HELPDESK:

Syntax
x = ga(fun,nvars)
x = ga(fun,nvars,A,b)
x = ga(fun,nvars,A,b,Aeq,beq)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)
x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)
x = ga(problem)
[x,fval] = ga(___)
[x,fval,exitflag,output] = ga(___)
[x,fval,exitflag,output,population,scores] = ga(___)

PROGRAM CODING :

clear all
close all
clc

x=linspace(0,0.7,200);
y=linspace(0,0.7,200);

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

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);% calculate the value of f for stalagmite;
 end
end

figure(1)
surfc(xx,yy,-f)
xlabel('xvalue')
ylabel('yvalue')

shading interp
[inputs,fval]=ga(@stalagmte,2);




num_case=70
tic
% study_case_1: statistical behaviour(unbounded condition);
for i=1:num_case
    [inputs,fopt(i)]= ga(@stalagmite,2)
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study_time_1=toc;

figure (1)
subplot(2,1,1)
hold on
surfc(x, y, -f) %create a 3 dim surface plot with a contour plot underneath.
shading interp %which removes the lines interim in image
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
%   PLOT3(x,y,z), where x, y and z are three vectors of the same length,
%   plots a line in 3-space through the points whose coordinates are the
%   elements of x, y and z.

title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('iteration')
ylabel('function maximum')


tic
% study_case_2: statistical behcaviour(upper and lower bounded condition)
for i=1:num_case
    [inputs,fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study_time_2=toc

figure (1)
subplot(2,1,1)
hold on
surfc(x,y,f) %create a 3 dim surface plot with a contour plot underneath.
xlabel('x value')
ylabel('y value')

shading interp %which removes the lines interim in image
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
%   PLOT3(x,y,z), where x, y and z are three vectors of the same length,
%   plots a line in 3-space through the points whose coordinates are the
%   elements of x, y and z.


subplot(2,1,2)
title('Bounded inputs')
plot(fopt)
xlabel('iteration')
ylabel('function minimum')


options=optimoptions('ga')
options=optimoptions(options,'populationsize',150);

tic
% study_case_3: statistical behcaviour(increasing the ga behaviour)

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

study_time_3=toc

figure (1)
subplot(2,1,1)
hold on
surfc(x,y,-f) %create a 3 dim surface plot with a contour plot underneath.
xlabel('x value')
ylabel('y value')

shading interp %which removes the lines interim in image
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','g')
%   PLOT3(x,y,z), where x, y and z are three vectors of the same length,
%   plots a line in 3-space through the points whose coordinates are the
%   elements of x, y and z.


subplot(2,1,2)
title('Bounded inputs with increased population size')
plot(fopt)
xlabel('iteration')
ylabel('function minimum')

PROGRAM:FUNCTION PROGRAM OF STALAGMITE:

function[f]=stalagmite (input);

x=input(1);
y=input(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

CODE EXPLANATION:

The above program is written to optimize the stalagmite function and finding the global maxima , by giving num case 70 generic algorith runs upto 70times and calculate the study time of 70 iterations. In this program we have calculated 3 study cases to compile it.

CASE STUDY 1:

In this case , we have calculated the statistical behaviour of stalagmite function by using for loop as ga(@stalagmite) and calculate study time using tik and toc command

CASE STUDY 2:

In this case , we have calculated the statistical behaviour of stalagmite function by setting upper and lower bounds, where lower bound(x1,y1) is (0,0) and upper boundary(x2,y2) is (1,1) with which we can notice the decrease in studytime because of boundaries.

CASE STUDY 3:

In this case , we have calculated the statistic behaviour of increasing the ga iterations. Population size has been increased. ga starts with random variables as we increaase the population it creates 70 set of 2 random variables, through which we get the global maxima.

OUTPUT:

1. PLOTS FOR UNBOUNDED INPUTS:

2. PLOTS FOR BOUNDED INPUTS:

3. PLOTS FOR BOUNDED INPUTS WITH INCREASED POPULATION SIZE:

VALUES:

STOPPING CRITERIA:

The generic algorithm uses the following criteria to determine when to stop:

1. GENERATIONS: The algorith stops when the number of count reached the value of generations.

2.TIME LIMIT: The algorithm stops after running for an amount of time in seconds.

3. FITNESS TIME: The algorithm stops when the value of the fitness func reaches the best point in the current population either less than or equal to.

the algorithm stops once it met with any one of the above criteria.

CONCLUSION:

When multiple number of iteration runs, generic algorith becomes optimum or linear. Where the number of cases is to check the over consistency of output values of ga, it gives the same output for all iterations, so that it can undergo optimum value.global maxima can be found.

where as running the program with lesser iterations, ga may r maynot find the global maxima.But no of iteration increases probability of finding global maxima will also increases to optimze global maxima.

RESULT:

Thus the global maxima of a stalagmite func is optimized using generic algorith 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.