Project

Optimization of a function using inbuilt function 'ga' (genetic algorithm)

Aim : To find the maxime value of a function using matlab.

Objective :

1. Knowing how to use ga inbuilt function , and syntax of ga.

2. Information about genetic algorithm.

Theory :

Genetic algorithm is to find the optimum value .

1. At , first let's about some basic information about genetic algorithm.

2. Genetic algorithm also called evolutionary algorithm , as the idea of this algorithm was from darwin

Evolutionary theory.

3. Genetic algorithm steps :

1. Initialization.

2. Selection.

3. Crossover.

4. Mutation.

5.Sorting and selecting best solution.

4. Solutions of a fitness function called chromosomes.

5. Fitness function , the function to find the optimum value.

Steps explanation in gentic algorithm :

1. Initialization : At first , a random solution is created and caluculated it's fitness value .

2. Selected best solution , using different types of selection algorithms like Tournament selection.

3. chromosomes undergoes crosssover for to generate offsprings (advanced over Parents).

4 . Undergoes mutation if required , to modify it's nature.

5 . Sorting and selected best solutions from it .

6. The steps repeated.

Chart descrinig the Genetic algorithm:

Syntax of 'ga' :

[opt_co,fval,exitflag,output,population,score] = ga(fit_fun,nvar,Aeq,beq,A,B,lb,ub,nonlcon,options)

outputs:

1.opt_co = co-ordinates of the optimum soluitons.

2.fval = optimum soluiton value.

3.exitflag = tells what is the stopping criteria.

4.Outputs - tells information about ga number of iterations , population size and stopping criteria.....

5.population - final population.

6.score - final popualtion fitiness value.

Inputs:

1.fit_fun - fitness function , always must be a handle.

2.nvar - Number of decision variables.

3.Aeq - L.H.S side , containing the coefficients of the linear equality constraints.

4.beq - R.H.S side , containing the numeric value of linear equality constraints.

5.A - Similar to Aeq , but A contains linear inequality constraints.

6. B - similar to B,contains for linear inequality constraints.

7 . lb - Lower bound of decision variables.

8 . ub - upper bound of decision variables.

9 . nonlcon - function handle , contains information non-linear equalities and inequalities constraints.

10. optoins - options used to change default values of ga , if required.

Fitness function:

`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 = -1*f1*f2*f3*f4)`

Code snippet for fitness function:

function output_func_value = stalgamite(input_vec)

% this function used to get the value of the function at given inputs
% storing the first value in input_vec in x
x = input_vec(1)  ;   

% storing the second value in input_vec in y
y = input_vec(2) ;

% splitting total function into parts for simplicity

    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);
  
% Final equation 
% output_func_value , stores the function value at given inputs
% -ve sign for to find the maximum output of the fitness function.
    output_func_value = (-1*f1*f2*f3*f4);
   
end

This snippet for defining the fitness function.

-ve sign used to find the maximum value of the fitness function , as ga only finds the optimum minimum value.

Code snippet:

close all           % to clear the figures at present.
clear all           % to clear the data in worksapce.
clc                 % to clear the command window.

% creating the range of x and y axis values
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% Creating a 2-D grid for to make the size of x and y equal
[xx,yy] = meshgrid(x,y);

% storing the length of x in variable n
n = 50;

% creating an array to store the function values at all inputs
%f_out = NaN(n,n);

% creating an input array to store the x and y value at the current iterations
%input = NaN(1,2);

% for loop to calculate the function value of the objective function.
for i = 1:length(x)
    for j = 1:length(x)
        inputs(1) = xx(i,j);
        inputs(2) = yy(i,j);
        f_out(i,j) = stalgamite(inputs);
    end
end
% rng used to create a same random numbers over executions.
% study 1
% this ga gives the global maxima of the objective function , considered no
% bound constraints , non linear/linear inequality and equality
% constraints.
for i = 1:n
      [inpu,fval(i)] = ga(@stalgamite,2);
      xopt(i) = inpu(1);
      yopt(i) = inpu(2);
end
figure(1)
subplot(2,1,1)
surfc(xx,yy,f_out)
hold on
plot3(xopt,yopt,fval,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('x values')
ylabel('y values')
zlabel('f_out values')
title('function values for given inputs')
shading interp
subplot(2,1,2)
plot(-fval)
xlabel('No. of iterations')
ylabel('Function maximum')
title('No.of iterations vs Function maximum')

%% Study 2
% Now , considering the bound constraints of decision variable.
% Lower bound = [0 0]
% Upper bound = [1 1]
% Considering no.of iterations as 50
tic
for j = 1:n
[inpu1,fval1(j)] = ga(@stalgamite,2,[],[],[],[],[0 0],[1 1]);
optimum_coordinate1(j) = inpu1(1);
optimum_coordinate4(j) = inpu1(2);
end
stop_time2 = toc;

figure(2)
subplot(2,1,1)
surfc(xx,yy,f_out)
hold on
plot3(optimum_coordinate1,optimum_coordinate4,fval1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('x values')
ylabel('y values')
zlabel('f_out values')
title('function values for given inputs')
shading interp
subplot(2,1,2)
plot(-fval1)
xlabel('No. of iterations')
ylabel('Function maximum')
title('No.of iterations vs Function maximum')

%% Study 3 
%Changing the population size , using optimoptions
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',300);
for j = 1:n
[inpu1,fval2(j)] = ga(@stalgamite,2,[],[],[],[],[0 0],[1 1],[],[],options);
optimum_coordinate3(j) = inpu1(1);
optimum_coordinate4(j) = inpu1(2);
end
stop_time3 = toc

figure(3)
subplot(2,1,1)
surfc(xx,yy,f_out)
hold on
plot3(optimum_coordinate3,optimum_coordinate4,fval2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('x values')
ylabel('y values')
zlabel('f_out values')
title('function values for given inputs')
shading interp
subplot(2,1,2)
plot(-fval2)
xlabel('No. of iterations')
ylabel('Function maximum')
title('No.of iterations vs Function maximum')

Step explanation of code:

1. At first , commands to clean the data on present interface.

2. defining the range of x and y values.

3. using meshgrid , to create a 2-D coordinate or grid of x and y values.

4. used for loop , to find function values for all values of x and y combination values.

5. ga used to find optimum values and returns optimum coordinated and optimum function value.

6. plotted the surf plot , and optimum values in the same plot using subplot command , iterations vs function optimum values in next subplot.

7. This code for without any constraints.

8. Shading interp used for to remove the gridlines on the surf plot.

Output :

8. In second study , considered bound constraints.

9 . Lower bound as [ 0 0 ] , Upper bound as [ 1 1 ] , means decrease the range for matlab to work.

10 . code same as with no constraints.

Output :

as can observe that , acuuracy increased to find optimum value.

11. Now , study by increasing the population size.

12. code same.

Output:

as you can onserve the red point over the maximum peak , indicating the maximum value.

Outputs of code:

Conclusion :

1. Accuracy/precesion increased on by increaing the populaiton size , and totally depends on the constraints.

2. Got a single global maximum value over the defined range.

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.