Project

Genetic Algorithm

About genetic algorithm (GA):-

In a genetic algorithm, a population of candidate solutions(called individuals, creatures, or phenotypes) to an optimization problem is evolved toward better solutions. Each candidate solution has a set of properties (its chromosomes or genotype) which can be mutated and altered; traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible.

The evolution usually starts from a population of randomly generated individuals, and is an iterative processes, with the population in each iteration called a generation. In each generation, the fitness of every individual in the population is evaluated; the fitness is usually the value of the objective function in the optimization problem being solved. The more fit individuals are stochastically selected from the current population, and each individual's genome is modified (recombined and possibly randomly mutated) to form a new generation. The new generation of candidate solutions is then used in the next iteration of the algorithim. Commonly, the algorithm terminates when either a maximum number of generations has been produced, or a satisfactory fitness level has been reached for the population.

A typical genetic algorithm requires:

  1. a genetic repesentations of the solution domain,
  2. a fitness function to evaluate the solution domain.

A standard representation of each candidate solution is as an array of bits. Arrays of other types and structures can be used in essentially the same way. The main property that makes these genetic representations convenient is that their parts are easily aligned due to their fixed size, which facilitates simple crossover operations. Variable length representations may also be used, but crossover implementation is more complex in this case. Tree-like representations are explored in genetic programming and graph-form representations are explored in evloutionary programming; a mix of both linear chromosomes and trees is explored in gene expression programming.

Once the genetic representation and the fitness function are defined, a GA proceeds to initialize a population of solutions and then to improve it through repetitive application of the mutation, crossover, inversion and selection operators.

GA Flow chart :-

Stalagmitye function :-

Programe to calculate a stalagmite function :-

% programme to calculate the global maxima of a stalagmite function
clear all
close all
clc

% define our x and y in one dimensional 
x=linspace(0,0.6,150);
y=x;

% num of iterations to run i.e 50
num_cases=50;

% creating in 2Dimensional mesh
[xx, yy] = meshgrid(x,y);

% for loop to evaluating and calling the stalagmite function
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

tic
% case 1
% genetic algorithim
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study1_time=toc;

% plotting
figure(1)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maxium')

tic
%study 2 
%statistical behaviour with upper and lower bounds
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study2_time = toc;

%plotting
figure(2)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maxium')

%study 3
%GA with the higher population size
options = optimoptions ('ga');
options = optimoptions(options,'populationsize',200)

tic
for i=1:num_cases
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end
study3_time = toc;

%plotting
figure(3)
subplot(2,1,1)
hold on 
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maxium')


    

Stalagmite function code :-

% for calling the stalagmite funtion

function[f]= stalagmite(input_vector)
x1 = input_vector(1);
y1 = input_vector(2);

f1_x = (sin(5.1*pi*x1+0.5))^6;
f1_y = (sin(5.1*pi*y1+0.5))^6;

f2_x = exp((-4*log(2)*(x1-0.0667).^2)/0.64);
f2_y = exp((-4*log(2)*(y1-0.0667).^2)/0.64);

%-ve sign indicates the global maxima 
f = -(f1_x*f1_y*f2_x*f2_y);
end


Step wise explanation of code :-

Stalagmite function :

Stalagmite function is crestd under the name of 'Stalagmite' and it can accesed through call by reference method in this case , 'input_vector' will assign the value to the function.

x1 = input_vector(1);
y1 = input_vector(2;

The variable of x1 & y1 takes the value from input variables by using 'meshgrid command',then the value has been assigned in two dimesional array (2d).

  • First of all we have to use linspace command to create the 150 value from 0 to 0.6 .
  • By using the ' Meshgrid command ' function of two variables in two dimensional array.
  • For the iteration purpose we have to use num_cases, It runs 50 times.
  • we have to call the Stalagmite function .
  • After that you have use for loop comment.
  • Then you have create the tic & tac command,for study the table .
  • after that you have use the plottting command to plot the value the graph.

Error while facing the programe:-

No error

Results:-

STUDY-1

STUDY-2

STUDY-3

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.

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.