Week 4.1 - Genetic Algorithm
clear all
close all
clc
% inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% storing values of x & y between 0 to 0.6
[xx yy]=meshgrid(x,y);
% running ga for 50 times
num_case=50;
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)=stalagmitefunction(input_vector);
end
end
% case study 1 - statistical behaviour of GA
% without providing boundries
tic % starting stopwatch timer
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmitefunction,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_1=toc % reading the time taken by stopwatch
% plotting for case study 1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
% control the color shading of surface by interpolating the colormap index
title('unbound input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
% case study 2 - with bounded conditions( upper & lower bound)
tic
for i=1:num_case
[inputs2,fopt2(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0,0],[1,1])
xopt2(i)=inputs2(1);
yopt2(i)=inputs2(2);
end
study_time_2=toc
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')
% case study 3 - increasing the ga iteration
% with bounded conditions ( upper & lower bound)
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500);
% create a set of options with each options set to its default value
% population size is set to 200.
tic
for i=1:num_case
[inputs3,fopt3(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt3(i)=inputs3(1);
yopt3(i)=inputs3(2);
end
study_time_3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'MarkerFaceColor','r')
title('bounded inputs with increase population size')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('Function maximum')
main function :
function [f]=stalagmitefunction(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));
f=-(f1_x*f1_y*f2_x*f2_y);
end
To Construct a Program for Genetic Alogrithm
GENETIC ALGORITHM - 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. At each step, the genetic algorithm selects individuals 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, nondifferentiable. stochastic or highly nonlinear. The genetic algorithm can address problems of mixed integer programming where some components are restricted to be integer-valued.
SELECTION Intuits - the genetic algorithm uses three main types of rules at each step to create the next generation iron, the current population' Selection rules select the individuals, called parents, that contribute to the population at the next generation. the selection is generally stochastic, and can depend on the indivtduals'scores.
CROSSOVER RULES - Crossover is the most significant phase in a genetic algorithm. or each pair of parents to be mated a crossover point is chosen at random from within the genes.
MUTATION RULES - Mutation is a genetic operator used to maintain genetic diversity from one generation of a population of genetic algorithm chromosomes to the next. it is analogous to biological mutation
OPTIMIZATION -
Optimization refers to finding the values ol inputs in such a my that we per the 'best' output values. The definition of 'best' varies from problem to problem, but in mathematical terms. it refers to maximizing or minimizing one or more objective functions, by varying the input parameters.
The set of all possible solutions or values which the inputs can take make up the search space. In this search space. lies a point or a set of points which gives the optimal solution. the aim of optimization is to find that point or set of points in the search space.
ECONOMIES - It has been used to characterize a variety of models including the cobweb model the overlapping generations model game theory schedule optimization and asset pricing.
NEURAL NETWORKS - A neural network consists of a series of algorithms that endeavor to determine and identify patterns. it works similarly to howa human brain's neural network works. in algorithms a neural network refers to a network of neurons, iihere a neuron is a mathematical function used to collect as well as to classify data from a given model.
IMAGE PROCESSING - Genetic Algorithms (0,45) are increasingly being explored in many areas of image analysis to solve compex optimization problems.
FITNESS FUNCTION - The fitness function simply defined is a function which takes a candidate solution to the problem as input and produces as output how 'fir our how 'good' the solution is with respect to the problem in consideration
THE SYNTAX OF GA IN MATLAB x=ga(tun,nyars.A,B,Aeq.Beq,.1b,ub.nonlcon.intcon,opt)
- x variable to store the output of the genetic algorithm
- 'fun to can the
- 'A rad 'B' are used to represent the linear inequalities of the function! the linear inequalities do not exist they can be set as empty( I.1Cond 'uti are used to specify the upper and lower bounds of the variable of the function.
- 'nonclorirepresents the integers constraints such that the value of trhe function is an integer.
- 'options used to provide the value of the default parameter if it is required to replace the parameter .this is done by using the 'optirnoptionf.
STALAGMITE FUNCTION - R creates local maxima and local minimas for data and also helps in finding best design characteristics for a product. And to solve the Stalagmite function we need Genetic Algorithms as it gives us the global mimos for the data and even for a parameter declared.
Main program :
clear all
close all
clc
% inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% storing values of x & y between 0 to 0.6
[xx yy]=meshgrid(x,y);
% running ga for 50 times
num_case=50;
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)=stalagmitefunction(input_vector);
end
end
% case study 1 - statistical behaviour of GA
% without providing boundries
tic % starting stopwatch timer
for i=1:num_case
[inputs,fopt(i)]=ga(@stalagmitefunction,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time_1=toc % reading the time taken by stopwatch
% plotting for case study 1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'markerfacecolor','r')
% control the color shading of surface by interpolating the colormap index
title('unbound input')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
- Here we use num_case=50 that indicates the time need to repeat & it calculates value.
- Tic command is used in order to find out the time taken.
- For loop command is used length of its sets as the number case.
- Ga command is used before the function & variable in program.
- Calculated x & y values are stored in xopt & y opt.
- Subplot command is used to get more than one plot in single file.
- We use surface command to get 3D graph.
- The negative sign in function(f) plotting we invert & we need to revert it back.
- Plot calculated maximum value as a dot structure by using marker & marker size.
- The title & labeling we use title & label commands.
- Here we get random values & we did not get an exact global maxima value.
% case study 2 - with bounded conditions( upper & lower bound)
tic
for i=1:num_case
[inputs2,fopt2(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0,0],[1,1])
xopt2(i)=inputs2(1);
yopt2(i)=inputs2(2);
end
study_time_2=toc
figure(2)
subplot(2,1,1);
hold on
surfc(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','MarkerSize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')
- In previous study we inferred that the search space of the ga is not within the search space so to overcome this error we used the bounded script method.
- As the name suggest here we create a boundary & within the boundary the function will execute.
- All the commands are same as previous study.
- Here as a limit function we call stalagmite function.
- Here our lower boundary limit are 0 0 & upper boundary limit are 1 1.
- This studay is better than the previous one.
% case study 3 - increasing the ga iteration
% with bounded conditions ( upper & lower bound)
options=optimoptions('ga');
options=optimoptions(options,'populationsize',500);
% create a set of options with each options set to its default value
% population size is set to 200.
tic
for i=1:num_case
[inputs3,fopt3(i)]=ga(@stalagmitefunction,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt3(i)=inputs3(1);
yopt3(i)=inputs3(2);
end
study_time_3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'MarkerFaceColor','r')
title('bounded inputs with increase population size')
subplot(2,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('Function maximum')
- here all the commands are same as previous.
- Here we introduces initial population size that ga needs to perform within boundary limit.
- We used iptimization command we mention the options population size.
- We get the plot.
- Population value is randomly increased up to which a single exact global maxima value is found.
- We get a single exact point at which at the population size. We stop the program.
Function program :
function [f]=stalagmitefunction(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));
f=-(f1_x*f1_y*f2_x*f2_y);
end
Output:-