Challenge

Week 4.1 - Genetic Algorithm

Main code:

clear all
close all
clc


x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_cases= 150;
[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);
    end
end
surfc(x,y,f)


tic
% Study 1 - Statistical behaviour
for i= 1:num_cases
    [inputs, fopt(i)]= ga(@stalagmite,2);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end


study1_time= toc

figure (1)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')

tic
% Study 2-Statistical behaviour with upper & lower bounds
for i= 1:num_cases
    [inputs, fopt(i)]= ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end


study2_time= toc

figure (2)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')


tic
% Study 3 increaing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);


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


study3_time=toc

figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')


Function code:

function[f]= stalagmite(input_vector)
    

fx= (sin (5.1*pi*input_vector(1)+0.5))^6;
fy= (sin (5.1*pi*input_vector(2)+0.5))^6;


fxx=exp((-4*log(2))*(((input_vector(1)-0.0667)^2/0.64)));
fyy=exp((-4*log(2))*(((input_vector(2)-0.0667)^2/0.64)));

f= -(fx*fy*fxx*fyy);
end

Stalagmite function:

Stalagmite:

A stalagmite is an upward-growing mound of mineral deposits that have precipitated from water dripping onto the floor of a cave.

The equation of stalagmite function,

Genetic algorithm (ga):

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. To create next generation, it uses three methods,

A. Selection B. Mutation C. Crossover

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.

Working of genetic algorithm:

1. It begins with creating a initial population randomly

2. After initial population is created, it creates a new population. At each step, algorithm uses individuals in current generation to create next generation. To create a new population values, it performs the following steps,

A. Calculates raw fitness scores using the fitness values computing from the current population

B. Raw fitness scores are converted into range of values called as expextation values

C. Select members called members based on their expectations

D. Individuals in current population with low fitness values are passed into the next population

E. Produces children from the parents i.e by making random changes to the single parent (mutation) or by combination of vector entries of pair (crossover)

F. Replaces current generation with children to form next generation

3. Algorithm stops when one of the stopping criteria is met

Genetic Algorithm (ga) syntax used in the program:

1. x=ga(fun,nvars) where fun=function name, nvars= no. of variables

2. x=ga(fun,nvars,A,B,Aeq,Beq) - A set of lower & upper bounds so that the values will be lying within that range

3. x=ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,IntCon,options) where,

A, B = linear inequality constraints

Aeq, Beq = linear equality constraints

lb = lower bounds

ub = upper bounds

nonlcon = non linear constraints

IntCon = vector of positive integers

options = optimisation options

Explanation of the program:

Using linspace command, assume set of x and y values of about 150 values at an inteval of 0.6. Assume the no. of iterations. Create an mesh grid for 2D arrays for x and y. Calculate the stalagmite function and store the value in f using for loop and introduce -ve sign to find global maxima. For study 1, use tic toc command to calculate global maxima by taking some random values from the function. Similarly for study 2. For study 3, use the population size to find.

Output:

Study 1:

Study 2:

Study 3:

The point on the highest peak is called Global maxima.

Conclusion: Using generic algorithm, we are able to calculate global maxima. We are adding -ve sign to fopt and f to make the default global minima to maxima. By increasing the population size, we can get more enhanced results.

Errors:

Use of extra parenthesis in function code

Remedy:

Try to look closely the function parameters whether it is correctly enclosing the function or not and try to read the error in command window

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.