Challenge

Week 4.1 - Genetic Algorithm

%% GLOBAL OPTIMIZATION ::

>> is an area of study which deals with how to minimize and maximize an function by using various inputs..

>> The genetic algorithm is a technique for solving both constrained and unconstrained optimization problems

that are based on natural selection, the process that drives biological evolution..

%% STALAGMITE FUNCTION ::

>> is a mathematical function,that helps to generate local maximas and global maximas of an function..

>> to generate the maximas we are using the GENETIC ALGORITHAM ..

% STALAGMITE FUNCTION AND THE GLOBAL MAXIMA FUNCTION;;;

close all

clear all

%% Defining our search space-1 dimensional array;;

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%% creating 2dimensional mesh;;

>> meshgrid transforms the domain specified by vectors x and y into arrays X and Y

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

%% evaluating the stalagmite function;;

>> Before evaluating we have to fing the stalagmite function based on the formula;;

>> stalagmite_code is an function name and carries by 'input_vector' as input to the function,

>> 'f' is an output of the function,

function [f] = stalagmite_code(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;

>> dividing the equation into parts for easy multiplication


t1 = ((x-0.0667).^2)/(0.64);

t2 = ((y-0.0667).^2)/(0.64);

f2_x = exp(-4.*log(2)*t1);

f2_y = exp(-4.*log(2)*t2);

>>By using '-' (-ve) sign the plot will be reversed so that we can find the maxima of the given function easly by using ga.


f = -(f1_x.*f2_x.*f1_y.*f2_y);

end

>> INSERTING the stalagmite function into the main function

for i=1:length(xx)


for k=1:length(yy)

input_vector(1)=xx(i,k);
input_vector(2)=yy(i,k);


f(i,k)=stalagmite_code(input_vector);

end
end

%%creating its surface through ‘surfc’ command

>>‘surfc’ command provide the contour of the generated surface.

>> applying 'surf' command by using'-ve' sign whcih shows the surface upside down,gets an better global maxima of the function;;

>>'-f' for global maxima of the function;;

surfc(x,y,-f)

shading interp ;;; To remove the lines produced by the mesh grid

xlabel('x')

ylabel('y')

[inputs,fval]=ga(@stalagmite_code,2)

>>[inuts,fval] is an optimum value of the function

>> 'fval' is the value of the objective function at the current value of 'x'

>> 'ga' is an matlab in bulit function ,,used here for only maxima of the function

>>'stalagmite_code is the function handle with an order of '2'

>> error by entering an 'f' +ve sign value the plot get into the minima of the function

%%STATISTICAL STUDY UNBOUNDED::

>>calculating the study time through tic and toc command;;

% study 1st-statistical behaviour

>> 'opt' optimum value of ga

>> using Number of itreation for which the function will evaluate the fitness

num_cases=50;

tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite_code,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);

end


study1_time=toc


%ploting the graph 1

figure(1)


subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

>>'plot3' creats an plot of x,y, and the optimum function value that 'ga' preditcs;

>> 'marker' used for highlight the patricular data points;


plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximam value')

%% STATISTICAL BEHAVIOUR (BOUNDED)::


tic
>> study 2-statistical bhevaiour-with upper end and lower bounds

>> GA SYNTAX:: syntax x=ga(fun,nravs,A,b,Aeq,beq,lb,ub,nonlcon,intcon)

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


study2_time=toc;

%ploting the graph 2


figure(2)


subplot(2,1,1)
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r');
subplot(2,1,2)
plot(-fopt)
title('bounded inputs')
xlabel('iterations')
ylabel('function maximam value')

%%STATISTICAL STUDY WITH INCREASE IN SIZE OF POPULATION::


>>The drawback is that the time for evaluation will increase.

>> increasing the population size by using

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

%optimoptions function;;

>> GA SYNTAX:: syntax x =ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options);


tic
for k=1:num_cases
[inputs,fopt(k)]=ga(@stalagmite_code,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(k)=inputs(1);
yopt(k)=inputs(2);
end


study3_time=toc;

%ploting the graph 3


figure(3)


subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r');
subplot(2,1,2)
plot(-fopt)
title('bounded inputs with modified population data')
xlabel('iterations')
ylabel('function maximum value')

%% output are 3 different statistical plots

>>> GA SYNTAX ;;;

>> syntax x=ga(fun,nravs,A,b,Aeq,beq,lb,ub,nonlcon,intcon) or

syntax x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options),requires that the variables listed in intcon take integer values.

  • fun:: function handle/objective function

Is to Write the functionobjective to accept a row vector of length nvars and return a scalar value.

  • nvars:: number of variable

is an +ve integer which passes row vector of length nvars to fun

  • A:: linear inequality constrains

is specified as real matrix,,A is M-by-nvars matrix,,'M' is number of inequalities

  • b :: linear inequality constrain

is specified as real matrix, b is M-element vector related to the A matrix

  • Aeq :: linear equality constrains

is specified as real matrix, Aeq is Me-by-nvars matrix,where 'Me' is number of equalities

  • beq :: linear equality constrain

is specified as real matrix,beq is Me-element vectorvto the Aeq matrix

  • lb :: lower bounds

specified as real vector or array of doubles, 'lb' represents the lower bound elements - wise lb<x<ub

  • ub :: upper bounds

specified as real vector or array of doubles, 'ub' represents the upper bound elements - wise lb<x<ub

  • nonlcon :: nonlinear constrains

specified as function handle that accepts a array or vector 'x' and returns two arrays are c(x) and ceq(x)

c(x) ,ceq(x) is the array of nonlinear equality constrains at x.ga attempt to satisfy

  • intcon :: integer variable

specified as a vector of +ve integers taking value from 1 to 10 nvras,, each value of intcon represents the x- component that integer-valued

  • options :: optimization options

specified as output of optimoptions or structure

for size of population : Positive integer ; {50} when numberofvariables=<5 , {100} otherwise {min(max(10* nvras,40)100)} for mixed-integer problems

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.