Week 4.1 - Genetic Algorithm
AIM: To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
OBJECTIVES:
1) Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
2) Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
CODE:
clear all
close all
clc
x = linspace(0,0.6,150); %x array from 0 to 0.6.
y = linspace(0,0.6,150); %y array from 0 to 0.6.
[xx, yy] = meshgrid(x,y); %used to create an array size of 150x150.
for i =1:length(xx)
for j = 1:length(yy)
input_vector(1) = xx(i,j); %Putting each value of 'xx(i,j)' to 'input_vector(1)'.
input_vector(2) = yy(i,j); %Putting each value of 'yy(i,j)' to 'input_vector(2)'.
f(i,j) = stalagmite_fun1(input_vector); %Calling function 'stalagmite_fun1(input_vector)' which returns the value and stored in an array of 150x150.
end
end
%Genetic Algorithm Case 1
tic %Start stopwatch Timer
iteration = 100; %Intializing
for i = 1:iteration
[inputs_1, fopt_1(i)] = ga(@stalagmite_fun1,2); %Genetic Algorithm initializing syntax = ga(@fun_name,nvar,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options). Here we use only function name and number of variables since it is unbounded.
xopt_1(i) = inputs_1(1); %Putting each value of 'inputs_1(1)' to 'xopt_1(i)' making an array of size 1x50.
yopt_1(i) = inputs_1(2); %Putting each value of 'inputs_1(2)' to 'yopt_1(i)' making an array of size 1x50.
end
study_time1 = toc; %Read elapsed time from stopwatch and put it to 'study_time1'.
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f); %Makes a Surface.
hold on
shading interp %Makes smooth curve on surface.
plot3(xopt_1,yopt_1,-fopt_1,'marker','o','markersize',5,'markerfacecolor','r') %'plot3' makes a 3D plot.
title('UNBOUNDED INPUTS') %Title of the plot
xlabel('x-value')
ylabel('y-value')
zlabel('f-value')
axis([0 .7 0 .5 0 1])
subplot(2,1,2)
plot(-fopt_1) %Plot between 'Iterations' and 'Function Maximum'.
xlabel('Iterations')
ylabel('Function Maximum')
%Genetic Algorithm Case 2:
tic %Start stopwatch Timer
for i=1:iteration
[inputs_2, fopt_2(i)] = ga(@stalagmite_fun1,2,[],[],[],[],[0;0],[0.6;0.6]); %Genetic Algorithm initializing syntax = ga(@fun_name,nvar,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options). Here we use only function name, number of variables, lower bound and upper bound since it is bounded.
xopt_2(i) = inputs_2(1); %Putting each value of 'inputs_2(1)' to 'xopt_2(i)' making an array of size 1x50.
yopt_2(i) = inputs_2(2); %Putting each value of 'inputs_2(2)' to 'yopt_2(i)' making an array of size 1x50.
end
study_time2 = toc; %Read elapsed time from stopwatch and put it to 'study_time2'.
figure (2)
subplot(2,1,1)
surfc(xx,yy,-f); %Makes a Surface.
hold on
shading interp %Makes smooth curve on surface.
plot3(xopt_2,yopt_2,-fopt_2,'marker','o','markersize',5,'markerfacecolor','r') %'plot3' makes a 3D plot.
title('BOUNDED INPUTS') %Title of the plot
xlabel('x-value')
ylabel('y-value')
zlabel('f-value')
axis([0 .7 0 .5 0 1])
subplot(2,1,2)
plot(-fopt_2) %Plot between 'Iterations' and 'Function Maximum'.
xlabel('Iterations')
ylabel('Function Maximum')
%Genetic Algorithm case 3:
options = optimoptions('ga','populationsize',250); %'options = optimoptions' returns options with specified parameters set using one or more name-value pair arguments.
tic %Start stopwatch Timer
for i = 1:iteration
[inputs_3, fopt_3(i)] = ga(@stalagmite_fun1,2,[],[],[],[],[0;0],[0.1;0.1],[],[],options); %Genetic Algorithm initializing syntax = ga(@fun_name,nvar,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options). Here we use only function name, number of variables, lower bound, upper bound and options since it is bounded with optimization options.
xopt_3(i) = inputs_3(1); %Putting each value of 'inputs_3(1)' to 'xopt_3(i)' making an array of size 1x50.
yopt_3(i) = inputs_3(2); %Putting each value of 'inputs_3(2)' to 'yopt_3(i)' making an array of size 1x50.
end
study_time3 = toc; %Read elapsed time from stopwatch and put it to 'study_time3'.
figure(3)
subplot(2,1,1)
surfc(xx,yy,-f); %Makes a Surface.
hold on
shading interp %Makes smooth curve on surface.
plot3(xopt_3,yopt_3,-fopt_2,'marker','o','markersize',5,'markerfacecolor','r') %'plot3' makes a 3D plot.
title('BOUNDED INPUTS WITH OPTIMIZED OPTIONS') %Title of the plot
xlabel('x-value')
ylabel('y-value')
zlabel('f-value')
axis([0 .7 0 .5 0 1])
subplot(2,1,2)
plot(-fopt_3) %Plot between 'Iterations' and 'Function Maximum'.
xlabel('Iterations')
ylabel('Function Maximum')
Total_time = study_time1 + study_time2 + study_time3 %Total time taken for the function to excute.
FUNCTION CODE:
function [result] = stalagmite_fun1(input_vector)
x = input_vector(1); %Giving the value of 'input_vector(1)' to 'x'.
y = input_vector(2); %Giving the value of 'input_vector(1)' to 'x'.
%function definition
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));
[result] = -(f1_x.*f1_y.*f2_x.*f2_y); %Combining the calculate value to the 'result'.
end
ERRORS FACED WHILE CODING:
1) Unrecognized funtion or variable 'fopt1' Line: 37.
2) Invalid Expression Line: 52.
3) Error using 'plot' Line: 95.
STEPS TAKEN TO OVERCOME THOSE ERRORS:
1) Replaced 'fopt1' to 'fopt_1'.
2) Corrected the expression as 'ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
3) Replaced 'plot' to 'plot3'.
SCREENSHOTS SHOWING ERRORS THROWN IN COMMNAD WINDOW:
1)

2)

3)

RESULTS:
Successfully wrote the code in Matlab to optimise the stalagmite function and found the GLOBAL MAXIMA of the function and Global Maxima is at x = 0.0668315, y = 0.0668201, z = 1.
INFERENCE:
Genetic Algorithm determines the minima value of the function. We formulated the code to derive the Maxima of the function usinng GA.
PLOTS:
1) GENETIC ALGORITHM CASE 1:

2) GENETIC ALGORITHM CASE 2:

3) GENETIC ALGORITHM CASE 3:

CONCEPT OF GENETIC ALGORITHM:
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.
Genetic Algorithms (GAs) are search based algorithms based on the concepts of natural selection and genetics. GAs are a subset of a much larger branch of computation known as Evolutionary Computation.
GAs were developed by John Holland and his students and colleagues at the University of Michigan, most notably David E. Goldberg and has since been tried on various optimization problems with a high degree of success.
In GAs, we have a pool or a population of possible solutions to the given problem. These solutions then undergo recombination and mutation (like in natural genetics), producing new children, and the process is repeated over various generations. Each individual (or candidate solution) is assigned a fitness value (based on its objective function value) and the fitter individuals are given a higher chance to mate and yield more “fitter” individuals. This is in line with the Darwinian Theory of “Survival of the Fittest”.
In this way we keep “evolving” better individuals or solutions over generations, till we reach a stopping criterion.
I)Advantages of GAs
GAs have various advantages which have made them immensely popular. These include −
-
Does not require any derivative information (which may not be available for many real-world problems).
-
Is faster and more efficient as compared to the traditional methods.
-
Has very good parallel capabilities.
-
Optimizes both continuous and discrete functions and also multi-objective problems.
-
Provides a list of “good” solutions and not just a single solution.
-
Always gets an answer to the problem, which gets better over the time.
-
Useful when the search space is very large and there are a large number of parameters involved.
II)Limitations of GAs
Like any technique, GAs also suffer from a few limitations. These include −
-
GAs are not suited for all problems, especially problems which are simple and for which derivative information is available.
-
Fitness value is calculated repeatedly which might be computationally expensive for some problems.
-
Being stochastic, there are no guarantees on the optimality or the quality of the solution.
-
If not implemented properly, the GA may not converge to the optimal solution.
SYNTAX OF ga IN MATLAB:
x = ga(fun,nvars): finds a local unconstrained minimum, x, to the objective function where 'fun' is the function and 'nvars' is the number of variables.
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').
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').
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.)
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.)
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.) and optimoptions can be greated using options.