Challenge

Week 5 - Genetic Algorithm

 Aim: Writing a program in matlab using genetic algorithm for global optimization of stalagmite function

Objective:

  • To generate a code in matlab for stalagmite function.
  • To plot a stalagmite function and find maxima.
  • To optimize a local maxima to global maxima using genetic algorithm in matlab.
  • Plot a stalagmite function for each study and find a number of iteration required to converge towards global maxima.

Theory:

Genetic algorithm is technique by which process is solved using biological evolution, but in domain of numbers.

Many numerical problems can not be solved using normal optimization techniques, this is where a genetic algorithm comes into picture its predicts the answer of any complex function using evolution theory proposed by charles darwin.

Structure of evolutionary algorithm:

Review general structure of genetic algorithm:

  • Initialization
  • Select parents and perform cross-over
  • Mutation
  • Merge main population and off-springs
  • Evaluate, sort, and select
  • Terminate

Cross-over:

In cross-over the parents are cut at specific length, and the end parts are swapped, the cross-over is divided into three types:

  • single-point cross-over
  • multi-point cross-over
  • Uniform cross-over

Mutation:

In this process the errors are added in genetic algorithm, which make possible to create innovative and revolutionary solution for a problem. 

Process diagram for genetic algorithm:

Steps to write program or logic:

  • Providing a input in form of 1-dimensional array for x and y variables.
  • Converting 1-dimensional array to 2-dimensional array using meshgrid.
  • Creating for loop to generate input vectors.
  • Writing a function for calculation of stalagmite.
  • Calling and function inside a for loop.
  • Creating one more for loop for no of iteration.
  • Using genetic algorithm syntax to call function, and calculate global maxima of stalagmite. 

Syntax for genetic algorithm

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

Where,

fun=@function

nvars= no of variables

lb=lower bounds

ub=upper bounds

options=this can be used to increase values of ga 

Syntax:

options = optimoptions(SolverName,Name,Value)

  • Plot a result of 2-d values and function,also plot the output values for x , y, and function.
  • Conduct different study to get optimal value of function
  • Terminate
  • Matlab program to calculate maxima of stalagmite function:
%program to predict global maxima of a stalagmite function
close all
 clear all
  clc
  %inputs
  x=linspace(0,0.6,100); %1-dimensional array of x
  y=linspace(0,0.6,100);
  no_of_iter = 50;
  [xx, yy]=meshgrid(x,y); %2-dimensional array of x and y
  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_func(input_vector); �lling atalagmite function
      end
  end
  tic     �lculation of time for no of iterations
  for i=1:no_of_iter
      [input,fval(i)]= ga(@stalagmite_func , 2); %genetic algorithm syntax to calculate %maxima 
      x_output(i) = input(1); %output values
      y_output(i) = input(2);
  end
  study_one = toc;
%PLOTTING
  figure(1)
  subplot(2,1,1)
  surfc(xx,yy,-f)
  shading interp
  xlabel('xaxis')
  ylabel('yaxis')
  title('study_one unbounded')
  hold on
  plot3(x_output,y_output,-fval,'marker','o','markersize',5,'markerfacecolor','m')
  subplot(2,1,2)
  plot(-fval)
  xlabel('no of iteration')
  ylabel('F_maximum')



  %Study 2 using bounds
  %since there are mny maxima's to reduce it we should go with different
  �ses
  %let us set a upper and lower bounds to the problem
  LB=[0;0];
  UB=[0.6;0.6];
  nvars=2;
  tic
  for i=1:no_of_iter
      [input,fval(i)]=ga(@stalagmite_func,nvars,[],[],[],[],LB,UB);
      x_output(i)=input(1);
      y_output(i)=input(2);
  end
  study_two = toc;
  figure(2)
  subplot(2,1,1)
  surfc(xx,yy,-f)
  shading interp
  xlabel('xaxis');
  ylabel('yaxis');
  title('study-two bounded with LB and UB');
  hold on
  plot3(x_output,y_output,-fval,'marker','o','markersize',5,'markerfacecolor','m')
  subplot(2,1,2)
  plot(-fval)
  xlabel('no of iteration');
  ylabel('F_maximum');



%study-3 increasing value of 'ga'
  %increaing the value of ga
  options=optimoptions('ga');
  options=optimoptions(options,'populationsize',200);
  tic
  for i=1:no_of_iter
      [input,fval(i)]=ga(@stalagmite_func,nvars,[],[],[],[],LB,UB,[],[],options);
      x_output(i)=input(1);
      y_output(i)=input(2);
  end
  study_three = toc;
  figure(3)
  subplot(2,1,1)
  surfc(xx,yy,-f)
  shading interp
  xlabel('xaxis');
  ylabel('yaxis');
  title('study-three bounded with incremeting ga');
  hold on
  plot3(x_output,y_output,-fval,'marker','o','markersize',5,'markerfacecolor','m')
  subplot(2,1,2)
  plot(-fval)
  xlabel('no of iteration');
  ylabel('F_maximum');

Matlab program for stalagmite function:

function f = stalagmite_func(input_vector)
f1x=(sin((5.1*pi).*input_vector(1)+0.5)).^6;
f1y=(sin((5.1*pi).*input_vector(2)+0.5)).^6;
f2x= exp(((-4)*log(2)).*((input_vector(1)-0.0667).^2/0.64));
f2y= exp(((-4)*log(2)).*((input_vector(2)-0.0667).^2/0.64));
fxy=f1x*f1y*f2x*f2y;
f=-fxy;
end

Study-1: Using genetic algorithm syntax, unbounded

In solution we get many maxima's which is uncertain, so to get better results we should assign bounds.

Time taken:

Study-2: Using genetic algorithm syntax, bounded

Time taken:

In solution we get few points which is combination of global and local maxima points, to get further better results we can increase a value of ga.

Study-3: Increasing a value of 'ga' using options in 'ga' syntax.

In solution we get global maxima that is single value for a function.

Time taken:

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.