Challenge

Week 4 - Genetic Algorithm

AIM : Write a code in MATLAB to:

1. Optimise the stalagmite function.

2.Find the global minima of the function.

3.Plot graphs for all 3 studies and for F maximum vs no. of iterations.

OBJECTIVE OF THE PROJECT:

Our main objective is to know about the concept of genetic algorithm so that we can implement it in our programs and to have infromation about the stalagmite function and how to optimise it.

STALAGMITE FUNCTION:

A stalagmite function is used to get local minima or maxima. For this particular problem we need to optimise it to get the maximum value. This is done with the help of genetic algorithm.

ABOUT GENETIC ALGORITHM:

A genetic algorithm is an algorithm that imitates the process of natural selection. They help solve optimization and search problems. Genetic algorithms are part of the bigger class of evolutionary algorithm. Genetic algorithms imitate natural biological processes, such as inherit, mutation, selection and crossover.

The concept of genetic algorithms is a search technique often used in computer science to find complex, non-obvious solutions to algorithmic optimisation and search problems. Genetic algorithms are global search heuristics.

Genetic algorithm involves the following process:

1.We start with an evolutionary phase with a population of randomly generated beings.We create some possible solutions and very often these have random values.

2.Then a fitness function scores each solution. The score will be a number that tells how well this solution solves the given problem.

3.In this step, the solutions for the next iteration are picked and they are combine alltogether. Again the fitness function is applied to it. If the algorithm has not reached the maximum numbers of generations again the solutions are picked, combined and fitness function is applied. The cycle repeats untill the required no of generations has not finished.

SYNTAX FOR GA:

  1. x = ga(fun,nvars,A,b)

Finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').

  1. x= ga(fun,nvars,A,b,Aeq,beq)

Finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').

  1. x= ga(fun,nvars,A,b,Aeq,beq,lb,ub)

Defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)

  1. x= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)

Subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)

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

Minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.

  1. x= ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

Requires that the variables listed in IntCon take integer values.

  1. x= ga(problem)

Finds the minimum for problem, where problem is a structure.

  1. [x,fval] = ga(___)

For any previous input arguments, also returns fval, the value of the fitness function at x.

  1. [x,fval,exitflag,output] = ga(___)

Also returns exitflag, an integer identifying the reason the algorithm terminated, and output, a structure that contains output from each generation and other information about the performance of the algorithm.

  1. [x,fval,exitflag,output,population,scores] = ga(___)

Also returns a matrix population, whose rows are the final population, and a vector scores, the scores of the final population.

STALAGMITE FUNCTION FOR THIS PROBLEM:

`f(x,y)=f1x xx f2x xx f1y xx f2y`

`f1x=[sin(5.1pix + 0.5)]^6`

`f2x= exp[-4ln(2)(x-0.0667)^2/0.64]`

`f1y= [sin(5.1piy+0.5)]^6`

`f2y=exp[-4ln(2)(y-0.0667)^2/0.64]`

CODES FOR THIS STALAGMITE FUNCTION :

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi.*x)+0.5)).^6;
f1y = (sin((5.1*pi.*y)+0.5)).^6;
f2x = exp(-4*log(2).*(((x-0.0667).^2)/0.64));
f2y = exp(-4*log(2).*(((y-0.0667).^2)/0.64));
f = -(f1x.*f1y.*f2x.*f2y);

end

FUNCTION CODE FOT THE STUDIES TO BE DONE:

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

for i=1:length(xx)
for j=1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)= stalagmite(input);
end
end

CODE FOR DIFFERENT OPTIMIZATION STUDIES:

close all
clear all
clc

% Defining Array
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 50
f =get_stalagmite(x,y);
F=-f


% 1st Study _Statistical Behavior
tic
for i = 1 : num_cases
    [inputs, fopt(i)] = ga(@ stalagmite,2);
        xopt(i) = inputs(1);
        yopt(i) = inputs(2);
        
end
study1_time=toc
fopt=-fopt;
 figure(1)
 subplot(2,1,1)
 hold on
 surfc(x,y,F)
 shading interp
 plot3( xopt, yopt , fopt, 'marker','o','markersize',6,'markerfacecolor','r')
 title('Unbounded Inputs')
 subplot(2,1,2)
 plot(fopt)
 xlabel('Iterations')
 ylabel('Function Maximum')

 %Study: 2 - Statistical Behavior - with Upper and Lower Bounds
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study2_time=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Bounded inputs')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function Maximum')

%Study: 3 - Increasing GA Iterations
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',170)

tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.5;0.5],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
fopt=-fopt;
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,F)
shading interp
title('Increased Iterations')
xlabel('x values')
ylabel('y values')
plot3(xopt,yopt,fopt,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function maximum')
max_value=fopt

RESULTS:

1.

2.

.

3.

4. The maximum value is calculated is 1.0000 .

ERRORS:

1. In stalagmite function code, I did not use the inverse function (negative sign) which gave the output for the minimum value.

2. Faced problem in defining the 2D aray in the get_stalagmite command.

CONCLUSION:

The global maxima for the given stalagmite funcrion has been carried out successfully with the help of genetic algorithm and the maximum value is found to be 1.0000

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.