Challenge

Week 5 - Genetic Algorithm

AIM: To write a for stalagmite function and to calculate its global maxima value using MATLAB.

THEORY: Genetic Algorithm (GA) is a search-based optimization technique based on the principles of Genetics and Natural Selection. It is frequently used to find optimal or near-optimal solutions to difficult problems which otherwise would take a lifetime to solve. It is frequently used to solve optimization problems, in research, and in machine learning. The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that are based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them 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 a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non differentiable, stochastic, or highly non-linear. The genetic algorithm can address problems of mixed-integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses the main types of rules at each step to create the ne generation from the current population:

Selection rules: select the individuals, called parents that contribute to the population at the next generation.

Crossover rules: combine two parents to form children for the next generation

Mutation rules: apply random changes to individual parents to form the children.

FITNESS FUNCTION:

The fitness function takes the solution as input and produces the suitability of the solution as the output. This evaluates how close the solution is to the optimal solution of the desired problem.

The syntax for GA in Matlab:

x=ga(fun,nvars,A,B,Aeq,Beq, lb, ub,nonicon,Intcon.options)

This syntax is explained as follows:

‘X’ is the variable to store the output of the genetic algorithm.

‘fun’ is the function handle to call the function.

A and B are used to represent the linear inequalities of the function, if the linear inequalities do not exist, they can be set as empty[].

Aeq and Beq are used to represent the equality constraints similarly, if the linear equalities do not exist, they can be set as empty[].

‘ib' and 'ub are used to specify the upper and the lower bounds of the variables of the function.

‘nonlcon’ represents the non-linear constraints.

‘Intcon’ represents the integer constraint, such that the value of the function is an integer.

‘options’ used to provide the value of the default parameters used named parameters, if required to replace the default parameters. This is done by using the ‘optimoptions’.

SATALGMITE FUNCTION:

A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function.

The stalagmite function is given by:

PROCEDURE:

  • As MATLAB is a case sensitive software, make sure of that during the programming. And clear and close all the previous data before starting.

close all (closes all the previous plots )

clear all (clears all the variable data )

clc (clears the command window)

CODE:

% maximization stalagmite function

clear all

close all

clc

%Define inputs of search space

x=linspace(0,0.6,150);

y=linspace(0,0.6,150);

num_spaces=50;

%craeating a two dimensional array

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

% nested loop for array value

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

%plotting

figure(1)

surfc(xx,yy,-f);

title('stalagmite function');

shading interp

xlabel('x axis')

ylabel('y axis')

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

  • In this part of the code we use the 'clear all', 'close all', 'clc' to clear all the previous plots, command window and the previous data.
  • Defining two arrays x and y to store values from 0 to 0.6 storing 150 values in between them.
  • Defining num_spaces for the GA operations to perform.
  • Converting the two arrays into two 2-Dimensional arrays using 'meshgrid' command.
  • In this part of the code, start a nested for loop starting from 1 to the length of the array.
  • take each value of the array and store it an input_vector.
  • Use a function stalagmite to calculate using the input_vector as argument and store in a array f.
  • plot the stalagmite surface using surfc commandFor maximizing ,converting the values of the stalagmite function stored in the array f to their negative values. The minimum value of \'-f(x)\' is the maximum value of \'f(x).

% Case:1 Statistical Behaviour of the Genetic Algoritm (Unbounded)

tic

for i=1:num_spaces

[input1,fopt1(i)]=ga(@stalagmite,2);

xopt1(i)=input1(1);

yopt1(i)=input1(2);

end

Study_time1=toc;

%plotting

figure(2);

subplot(2,1,1);

hold on

surfc(xx,yy,-f);

xlabel('x-axis')

ylabel('y-axis')

zlabel('function')

shading interp;

plot3(xopt1,yopt1,-fopt1,'marker','o','markerfacecolor','r','markersize',5,'linewidth',1,'color','g');

title('Unbounded Points');

subplot(2,1,2);

plot(-fopt1);

xlabel('Number of Iteratons');

ylabel('Function');

  • In this part , tic and toc command is used to store the time taken for the code to evaluate.
  • num_spaces is used for the number of times the GA operations will be done.
  • for every input stalagmite function will be evaluated and the value will stored in xopt1 and yopt1 respectively.
  • The format of "ga" (genetic algorithm) used in this case is “[inputs, fopt] =ga (fun,nvars)”. Here inputs represent the values of the parameters x and y for which the function has the least value (fopt)”fun” is the function to be optimized, here it is a Stalagmite function, "nvars” the number of input variables, in this case its two “ input_vector(1)" and "input vector(2)”.
  • Now for plotting the graph figure command is used for a new figure window, "subplot" is used for dividing the window in parts, and here it divides the figure window in two parts in the first two graphs are plotted in a single graph using the "hold” on command “surfc” command is used which plots a surface graph and the other graph is 3 dimensional line plot for this the command used is "plot3" X and Y label is given along with the title in the second half a ID plot is generated of values stored in "fopt1" using the plot command and then label the x-axis and y-axis.

%Case 2: Statistical Behaviour of Genetic Algorithm using Lower and Upper Bound

tic

for i=1:num_spaces

[input2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

xopt2(i)=input2(1);

yopt2(i)=input2(2);

end

Study_time2=toc;

%plotting

figure(3)

subplot(2,1,1);

hold on

surfc(xx,yy,-f);

xlabel('x-axis')

ylabel('y-axis')

zlabel('function')

plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r','markersize',5,'linewidth',1,'color','g');

title('Bounded Points');

shading interp;

subplot(2,1,2);

plot(-fopt2);

xlabel('Number of Iteratons');

ylabel('Function');

% Case 3:Statistical behaviour of GA by increasing the number of iterations.

options=optimoptions('ga');

options=optimoptions(options,'PopulationSize',300);

tic

for i=1:num_spaces

[input3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);

xopt3(i)=input3(1);

yopt3(i)=input3(2);

end

study_time3= toc;

%plotting

figure(4)

subplot(2,1,1);

hold on

surfc(xx,yy,-f);

xlabel('x-axis')

ylabel('y-axis')

zlabel('function')

shading interp;

plot3(xopt3,yopt3,-fopt3,'marker','o','markerfacecolor','r','markersize',5,'Linewidth',2,'color','g');

title('Bounded with Increased Population Size');

subplot(2,1,2);

plot(-fopt3);

xlabel('Number of Iterations');

ylabel('Functions');

FUNCTION:

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

ERROR:

Fogot to write ga function

Solve-after giving this it solved.

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.