Challenge

Week 4 - Genetic Algorithm

AIM: To optimize a stalagmite mathematical model & to find the global maxima of the following model using Genetic Algorithm

Genetic algorithm: 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. 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, no differentiable, stochastic, or highly nonlinear.

ga in MATLAB:

The genetic algorithm at the command line, call the genetic algorithm function ga with the syntax

[x fval] = ga(@fitnessfun, nvars, options)

Where

  • @fitnessfun is a handle to the fitness function.
  • nvars is the number of independent variables for the fitness function.
  • options is a structure containing options for the genetic algorithm. If you do not pass in this argument, ‘ga’ uses its default options.

The results are given by

  • x— Point at which the final value is attained
  • fval—Final value of the fitness function
clear all
close all
clc
%defining our work space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50;
[xx yy] = meshgrid(x,y);  %2D mesh

%evaluating of 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(xx,yy,f)
xlabel('X axis');
ylabel('Y axis');
shading interp
[inputs,f_val] = ga(@stalagmite,2)


tic
%study 1 - Statistical behavior
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(xx,yy,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 minimum')
hold off
tic

%study 2 - Statistical behavior - with upper 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
study2_time = toc

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')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off

%study 3  increasing GA iterations
options = gaoptimset('ga');
options = gaoptimset(options,'populationSize', 150);

tic
for i = 1:num_cases
    [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 with increased population')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
hold off
g = max(fopt)

FUNCTION CODING :

function [fx_y] = stalagmite(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;
f2_x = exp(-4*log(2).*(((x-0.0667).^2)/0.64));
f2_y = exp(-4*log(2).*(((y-0.0667).^2)/0.64));

fx_y = -(f1_x.*f1_y.*f2_x.*f2_y);  %-ve sign will give the maximum of ga solved.
end

EXPLANATION :

At the starting of code we have written “clear all” , due to this previous data of the programme is erase . by “clc” the command window are clear

Then we assign value of x & y by usin linspace command.

Then we use command meshgrid and pass value of x & y and it is store in form of matrix.

Then we start code for stalagmite function .

We use two “for loop” , first i value is given from 1 to length of xx, second “I” value is given from 1 to length of yy. And the value are store in “ input_vector ” and after that we call stalagmite function.

After ending of loop we label x axis & y axis ,we pass command for “ shading interp ” ,due to this the output graph comes in proper way.

The genetic algorithm at the command line, call the genetic algorithm function ga with the syntax

[input,f_val] = ga(@stalagmite, 2, options)

Where

  • “@stalagmite” :n is a handle to the fitness function.
  • “2” : is the number of independent variables for the fitness function.
  • Options : is a structure containing options for the genetic algorithm. If you do not pass in this argument, ‘ga’ uses its default options.

The results are given by

  • input : Point at which the final value is attained
  • f_val : Final value of the fitness function

Now for study 1 – statistical behaviour

Before starting we add timer “ tic ”

We use two “for loop” , first i value is given from 1 to length of xx, second “I” value is given from 1 to length of yy. And the value are store in “ input_vector ” and after that we call stalagmite function.

After ending of loop we label x axis & y axis ,we pass command for “ shading interp ” ,due to this the output graph comes in proper way. After ending of loop we label x axis & y axis ,we pass command for “ shading interp ” ,due to this the output graph comes in proper way.

For study-2

In this one loop is rune from “i” 1 to num_case as 50,

We can specify any of the options that are available for “ga” by passing options as an input argument to ga using the syntax

[input, fopt(i)] = ga(@stalagmite, num ,[],[],[],[],[],[],[],options)

This syntax does not specify any linear equality, linear inequality, or nonlinear constraints.

You create options using the function Optimoptions

options = optimoptions (@ga) ;

This returns options with the default values for its fields. ga uses these default values if you do not pass in options as an input argument.

The value of each option is stored in a field of options, such as options.Population Size. You can display any of these values by entering options followed by a period and the name of the field. For example, to display the size of the population for the genetic algorithm, enter

options. Population Size

ans = '50 when number Of Variables <= 5, else 200'

To create options with a field value that is different from the default — for example to set Population size to 100 instead of its default value 50 — enter

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

This creates options with all values set to their defaults except for Population size, which is set to 100.

If you now enter,

ga(@stalagmite, num , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] , [ ] ,options)

ga runs the genetic algorithm with a population size of 150.

output :

1).

2).

3).

error :

After "shading interp " their was error in "ga" because i have not pass numerical value

i.e [inputs,f_val] = ga(@stalagmite) and correct method is [inputs,f_val] = ga(@stalagmite,2)

referance :

Taken help from matlab just by typing " help ga " >> enter > "in command window

https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html

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.