Week 4.1 - Genetic Algorithm
REPORT--
GA
A genetic algorithm is a method of optimization. The algorithm repeatedly modifies a population of individual solutions. It uses
- selection
- crossover
- mutation
- termination.
in the selection, it has to select ga has to select the two individuals for the process
Crossover-used to combine the genetic information of two parents to generate new offspring.
Mutation-used to maintain the genetic diversity of the offspring from parents.
termination-it is the most important part for ga. as the name suggests it is to stop the whole process.
Stopping criteria.
it is nothing but terminating the genetic process once we reached the requirements. Or maybe until it is a waste of time in case we didn't meet the requirements, maybe solutions are more than enough.
To stop the ga, we can use several generations up to which it has to process. And it can be terminated after several iterations if there is no change in the output.
SYNTAX FOR GA
x= ga(fun,var,A,b,D,c,lb,ub,options)
this is the syntax for ga
where,
fun-objective function name
var-number of variables.
A and b - refer to the linear inequalities of function. written as [A b]
D and c -refer to linear equalities of function.[D c]
lb and ub- refers to lower bound and upper bounds.
options-it refers to the number of populations for the ga.
Steps
- first stalagmite function is defined. And the function is given with a set of arguments and a formula for solving.
- Another code is written as the main code.
- In the main code x and y inputs are given.
- Mesh is created using a mesh grid for further calculations.
- Using for loop for all values of x and y, the stalagmite function is solved and stored in f(i,j).
- Later to find the global maxima we used ga for several generations by using stalagmite for the second order.
- graph is plotted for obtained values to find the global maxima.
- step 6 is repeated for study 2 for upper and lower bounds. And also for study 3 by mentioning the size of the population.
Errors-
- vectors must be the same length.
- surface z must contain more than one row or column.
- Some errors due to syntax.
Steps were taken to overcome errors-
- error was due to incorrect order of matrix f_opt. It was corrected by making some changes to it like f_opt(i).
- Error was due to an inappropriate set of elements in the matrix. It was corrected by giving correct instructions.
- syntax errors are managed by writing the code with correct syntax.
%stalagmite function. function [st] = stalagmite(input_vector) %function stalagmite %arguments x=input_vector(1); y=input_vector(2); a=(sind(5.1*pi*x+0.5))^6; b=(sind(5.1*pi*y+0.5))^6; c=exp(-4*log(2)*(x-0.067)^2/0.64); d=exp(-4*log(2)*(y-0.067)^2/0.64); st=-(a*b*c*d); %final formula end %main function clear all; close all; clc; x=linspace(-1,2,150); %inputs for variable x y=linspace(-1,2,150); %%inputs for variable y [xx, yy]=meshgrid(x,y); %creatinga mesh grid for i=1:length(x) %for loop for soving the stalagmite for j=1:length(y) input_vector(1)=xx(i,j); input_vector(2)=yy(i,j); f(i,j)=stalagmite(input_vector); end end %study one num_cases=80; %number of generations %for loop for solving the problem using genetic algorithm for i=1:num_cases [inputs, f_opt(i)]=ga(@stalagmite,2); %ga syntax x_opt(i)=inputs(1); y_opt(i)=inputs(2); end %plotting figure(1) subplot(2,1,1) hold on surfc(xx,yy,-f) plot3(x_opt,y_opt,f_opt,'marker','o','markersize',5,'markerfacecolor','r') title('global maxima') xlabel('x'); ylabel('y'); subplot(2,1,2) plot(f_opt) xlabel('iterations') ylabel('function max') title('study1 global maxima') %study two num_cases=80; tic %for loop for solving the problem using genetic algorithm for i=1:num_cases [inputs, f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);%ga syntax x_opt(i)=inputs(1); y_opt(i)=inputs(2); end figure(1) subplot(2,1,1) hold on surfc(x,y,-f) plot3(x_opt,y_opt,f_opt,'marker','o','markersize',5,'markerfacecolor','r') title('global maxima') xlabel('x'); ylabel('y'); subplot(2,1,2) plot(f_opt) xlabel('iterations') ylabel('function max') title('study 2 global maxima') %study three num_cases=80; optins=optimoptions('ga'); %creating population options=optimoptions(optins,'PopulationSize',150); %population size tic %for loop for solving the problem using genetic algorithm for i=1:num_cases [inputs, f_opt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);%ga syntax x_opt(i)=inputs(1); y_opt(i)=inputs(2); end figure(1) subplot(2,1,1) hold on surfc(xx,yy,-f) plot3(x_opt,y_opt,f_opt,'marker','o','markersize',5,'markerfacecolor','r') title('global maxima') xlabel('x'); ylabel('y'); subplot(2,1,2) plot(f_opt) xlabel('iterations') ylabel('function max') title('study 3 global maxima')
References-
https://skill-lync.com/knowledgebase/genetic-algorithm-explained