Project

Genetic Algorithm

Aim:

To calculate the global maxima of a stalagmite function

Introduction:

A genetic algorithm is a search heuristic that is inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

Genetic Algorithms are primarily used in optimization problems of various kinds, but they are frequently used in other application areas as well.


Code:

clear all
close all
clc

%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

[xx yy]=meshgrid(x,y);
num_case=150;

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

%study 1
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

Study_time_1=toc

figure(1)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('unbounded inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

%study 2
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end

study_time_2=toc

figure(2)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('bounded inputs');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

%study 3
options=optimoptions('ga');
options=optimoptions(options,'populationsize',450);
tic
for i=1:num_case
  [inputs,Fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
  xopt(i)=inputs(1);
  yopt(i)=inputs(2);
end
study_time_3=toc

figure(3)
subplot(2,1,1);
hold on
surfc(xx,yy,F);
shading interp;
ylabel('y data');
xlabel('x data');
plot3(xopt,yopt,Fopt,'marker','o','markersize',5,'markerfacecolor','k');
title('bounded inputs with increase population size');
subplot(2,1,2);
plot(Fopt);
xlabel('iterations');
ylabel('Function maximum');

max_value=Fopt


Code for stalagmite function:

function y=stalagmite(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));
  y=-[f1_x*f1_y*f2_x*f2_y];
end

Output:

Mistakes:

  1. y=-[f1_x*f1_y*f2_x*f2_y]; was first written by me as y=-[f1_x f1_y f2_x f2_y]; so this error occured:




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.