Challenge

Week 4.1 - Genetic Algorithm

Aim:- To optimise the stalagmite function and find the global maxima of the function.

In order to optimise Stalagmite Function and to get Global Maxima, lets understand some topics first.

  • Optimization technique
  • Genetic Algorithim
  • how does genetic algorithim works
  • Syntax of Genetic Algorithim(ga)
  • Where to apply ga?

Optimization Technique:-

The classical optimization techniques are useful in finding the optimum solution or unconstrained maxima or minima of continuous and differentiable functions. These are analytical methods and make use of differential calculus in locating the optimum solution.

Genetic Algorithim:-

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.Genetic algorithim is a search based optimization technique based on the principle of genetic and natural selection.It is frequently used to find optimal solutions.Ga is an inbuilt matlab solver ,GA is an iterative optimization framework which involves selecting the initial population, producing a new generation, and then evaluating them for the next iteration. In GA, using both crossover and mutation operators can explore the solution space in multiple directions at once.

HOW DOES GENETIC ALGORITHIM WORKS:-

A genetic algorithm works by building a population of chromosomes which is a set of possible solutions to the optimization problem. Within a generation of a population, the chromosomes are randomly altered in hopes of creating new chromosomes that have better evaluation scores. The next generation population of chromosomes is randomly selected from the current generation with selection probability based on the evaluation score of each chromosome

FLOW CHART OF GENETIC ALGORITHIM

Syntax of Genetic Algorithim(ga):-

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,Aeq,beq,lb,ub,nonlcon,intcon)

x=ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)

fitnessfcn

Fitness functions

nvars

Number of design variables

Aineq

A matrix for linear inequality constraints

Bineq

b vector for linear inequality constraints

Aeq

Aeq matrix for linear equality constraints

Beq

beq vector for linear equality constraints

lb

Lower bound on x

ub

Upper bound on x

nonlcon

Nonlinear constraint functions

intcon Indices of integer variables
rngstate

Field to reset the state of the random number generator

solver

'ga'

options

Options created using optimoptions or an options structure, e.g population size ,generation size

NOTE:

X=ga(fun,nvars,A,b,Aeq,beq,lb,ub)

X=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);

as we dont have A,B inequality linear constrains and also dont have
Aand B linear equality thats why we put these square brackets as blank'[]'.

Where to apply ga?

You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.

lets write a code to solve the stalagmite function

first we have to make a function and the we can go for main programme

function f = stalagmite(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.0667)^2)/0.64);
f4=exp((-4*log(2)*(y-0.0667)^2)/0.64);
f=-(f1.*f2.*f3.*f4);
end

our main programme is written below;

clear all;
close all;
clc;

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

%creat a 2D mesh
[xx, yy]=meshgrid(x,y);
% lets evolvate 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
surfc(f)
xlabel('X axis')
ylabel('Y axis')
shading interp
[inputs,f_val]=ga(@stalagmite,2);
num_cases=50; %no ittrations=50
tic
% study 1 statical behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
toc %...tic toc command is used to measure time taken for 50 itteration
figure (1)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs study1')
subplot(2,1,2)
plot(fopt)
xlabel('ittrations')
ylabel('function minimum')
hold off
tic
%study 2 statistical behaviour/ with uper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end

toc % end this operation and to know how much time is to be taken
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 study 2')
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
hold off
%study 3 statistical behaviour/increase ga itterations
num_case=50;
options= gaoptimset('ga');
options=gaoptimset(options,'populationsize',170);
tic
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
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
plot3(xopt ,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded input/increased popolation size study3')
subplot(2,1,2)
plot(fopt)
xlabel('ittrations')
ylabel('function_minimum')
hold off
f=(fopt);
h=max(f);
disp(h)

the results of this programme are shown in the below fig

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.