Genetic Algorithm
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx yy]= meshgrid(x,y);
for i=1:length(xx)
for j=1:length(yy)
input(1)=xx(i,j);
input(2)=yy(i,j);
f(i,j)=stalagmit(input);
end
end
surfc(xx,yy,f)
shading interp
[inputs, fval]=ga(@stalagmit,2)
This program is about solving a global optimization problem. Optimization is all about maximizing or minimizing a function which governs the working of the particular engineering system. For this, stalagmite function is used, which gives local minimas and maximas. It is 3 dimensional function. Otimization in this case is finding the minimum or maximum value of the function without actually having the 3d plot. For this a function called genetic algorithm will be used.
First of all , one dimensional arrays x and y are created and then 2 dimensional arrays are formed using meshgrid. For each value of x and y an array named input is created.This created vector is a row vector with two values. These loops are basicallly creating the stalagmite 3d plot. Stalagmite is the user defined function which is defined based on given formulae as follows:
function [f]=stalagmit(input);
x=input(1);
y=input(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
PLot is created using surfc(). After this GA is finally performed. \"ga\" is in built function. Syntax of ga includes passing of two inputs. First is the function handle i.e the function that is needed o minimise. Since ga minimises the function,hence in the coding of stalagmite fuinction the value of f is negative. Therefore, ga will return the maximum value of the function as required(only with a negative sign). Second input is the number of variables in the function . Since x,y are the only variables, value passed is 2.
Finally two values will be shown as values of x,y at which the function value(i.e fval) is maximum.
Some more info on genetic algorithm:
Evolutionary algorithms are a family of optimization algorithms based on the principle of Darwinian natural selection. As part of natural selection, a given environment has a population of individuals that compete for survival and reproduction. The ability of each individual to achieve these goals determines their chance to have children, in other words to pass on their genes to the next generation of individuals, who for genetic reasons will have an increased chance of doing well, even better, in realizing these two objectives.
This principle of continuous improvement over the generations is taken by evolutionary algorithms to optimize solutions to a problem. In the initial generation, a population composed of different individuals is generated randomly or by other methods. An individual is a solution to the problem, more or less good: the quality of the individual in regards to the problem is called fitness, which reflects the adequacy of the solution to the problem to be solved. The higher the fitness of an individual, the higher it is likely to pass some or all of its genotype to the individuals of the next generation.
source:https://www.quora.com/What-is-a-genetic-algorithm
Optimization algorithms can roughly be divided into three classes: mathematical programming algorithms, heuristics, and metaheuristics. Mathematical programming algorithms have the most rigorous foundations, and it may be possible to prove that the algorithm actually converges, check that the proposed solution is close to at least a local optimum, and to estimate the rate of convergence.
However, it is not always possible or practical to apply a mathematical programming algorithm, and it may therefore be necessary instead to resort to heuristics and set up a set of rules that guides a selection algorithm. The perhaps best strategy is to use common sense, experience, and domain knowledge to set up the rules. Typically, it is not possible to prove much about the properties of the algorithm; it may not even be possible to write up a concise mathematical formulation of the problem that is actually being solved. Nevertheless, the heuristics may turn out to be useful. An example is the optimization of air-guiding systems done at Daimler by Moos, Klimetzek, and Rossmann some 10 years ago. They used the simple rule to add material at places in the current design where simulations indicate high levels of vorticity. This algorithm has apparently successfully been used to design air guiding systems in cars.
source:https://www.quora.com/What-should-every-programmer-know-about-genetic-algorithms