Challenge

Week 5 - Genetic Algorithm

CODE:

Main code:

clear variables

close all

clc

% Defining our search space
x = linspace(0, 0.6, 150);
y = linspace(0,0.6,150);
% Creating a 2Dimensional mesh
[xx , yy] = meshgrid(x , y);
% Evaluating the stalagmite function(Study 1)
% for value of i is 1 to length of matrix xx
for i = 1:length(xx)
%for value of j is 1 to length of matrix yy
for j = 1:length(yy)
input_vector(1) = xx(i,j);%x-coordinate
input_vector(2) = yy(i,j);%y-cordinate
%evaluating function
f(i,j) = stalagmite(input_vector);
end
end
% Finding the maxima and corresponding coordinates
%getting that value to a linear index and converting it to matrix indeces
[max_val, idx] = max(f(:));
[max_row, max_col] = ind2sub(size(f), idx);
%x & y co_ordinates
max_x = xx(max_row, max_col);
max_y = yy(max_row, max_col);
% Display results
disp('Maximum value of stalagmite function in Study 1:');
disp(max_val);
disp('Coordinates of the maximum value (x, y):');
disp([max_x, max_y]);
% Plotting the surface
figure;
surfc(xx, yy, f);
shading interp;
title('Stalagmite Function Surface (Study 1)');
xlabel('X axis');
ylabel('Y axis');
zlabel('Function Value');
% Mark the maxima on the plot
hold on;
plot3(max_x, max_y, max_val, 'ro', 'MarkerSize', 10, 'LineWidth', 2);
legend('Surface', 'Maximum Point');
hold off;
% Define the lower and upper bounds
lb = [0, 0]; % lower
ub = [0.6, 0.6]; % upper
% Increased Population and Mutation Rate(Study 2)
%display 100 iterations
%apply adaptive feasible mutation
options2 = optimoptions('ga','Display','iter','MaxGenerations', 100, ...
'PopulationSize', 100, 'MutationFcn', @mutationadaptfeasible, 'OutputFcn', @ga_output);
% Run the GA for Study 2 settings (Increased Population and Mutation Rate)
[x2, fval2] = ga(@stalagmite, 2, [], [], [], [], lb, ub, [], options2);
disp('Optimal solution for Study 2 (Increased Population and Mutation Rate):');
disp(x2);
disp('Maximum fitness value for Study 2:');
disp(fval2);
% Crossover Fraction and Tournament Selection(Study 3)
%display 100 iterations
%apply crossover farction of 0.8
options3 = optimoptions('ga', 'Display', 'iter', 'MaxGenerations', 100, ...
'PopulationSize', 100, 'CrossoverFraction', 0.8, ...
'SelectionFcn', @selectiontournament, 'OutputFcn', @ga_output);
% Run the GA for Study 3 settings (Crossover Fraction and Tournament Selection)
[x3, fval3] = ga(@stalagmite, 2, [], [], [], [], lb, ub, [], options3);
disp('Optimal solution for Study 3 (Crossover Fraction and Tournament Selection):');
disp(x3);%optimal value
disp('Maximum fitness value for Study 3:');
disp(fval3);%fitness value
% Output function to track maximum fitness value over iterations
function [state, options, optOutput] = ga_output(options, state, flag)
persistent maxFitnessValues
if isempty(maxFitnessValues)
maxFitnessValues = [];
end
% Store the best fitness value for the current iteration
maxFitnessValues = [maxFitnessValues, -state.Score]; % Track fitness using state.Score
if strcmp(flag, 'done')
% Plot maximum fitness value vs iterations
figure
plot(maxFitnessValues, 'LineWidth', 2);
title('Maximum Fitness Value vs Iterations');
xlabel('Iteration');
ylabel('Maximum Fitness Value (F)');
grid on;
end
optOutput = []; % Provide the missing output argument
end

Function Code:

%stalagmite
function f = stalagmite (input)
x = input(1);
y = input(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.6667).^2)*1.5625;
f2_y = exp(-4*log(2))*((y-0.6667).^2)*1.5625;
f = f1_x .* f2_x .* f1_y .* f2_y;
end

Plot:

STUDY 1

maxima:

STUDY 2

STUDY 3


Errors:

":" was not added in "f()"

if "is"empty. "is" should be used inbetween

number should be specified for function vector [x3 fval3]

@mutationdapt is wrong utilization "mutationadaptfeasible" is correct.

"many more errors are there but, not able to add"


OUTPUT:

Maximum value of stalagmite function in Study 1:

0.0012

Coordinates of the maximum value (x, y):

0.0644 0.0644

Single objective optimization:

2 Variables

Options:

CreationFcn: @gacreationuniform

CrossoverFcn: @crossoverscattered

SelectionFcn: @selectionstochunif

MutationFcn: @mutationadaptfeasible

Best Mean Stall

Generation Func-count f(x) f(x) Generations

1 200 1.109e-22 1.395e-05 0

2 295 4.564e-32 8.479e-06 0

3 390 3.529e-32 5.816e-06 0

4 485 3.529e-32 2.424e-07 1

5 580 3.529e-32 2.563e-07 2

6 675 3.529e-32 5.167e-07 3

7 770 3.529e-32 1.583e-06 4

8 865 3.529e-32 7.709e-07 5

9 960 3.529e-32 3.69e-07 6

10 1055 3.529e-32 3.194e-10 7

11 1150 3.529e-32 5.128e-10 8

12 1245 3.529e-32 1.362e-17 9

13 1340 9.209e-33 2.309e-19 0

14 1435 2.957e-34 2.729e-19 0

15 1530 2.957e-34 3.974e-18 1

16 1625 2.957e-34 7.701e-18 2

17 1720 2.957e-34 1.862e-17 3

18 1815 9.305e-37 1.862e-17 0

19 1910 9.305e-37 2.238e-24 1

20 2005 3.23e-40 3.637e-21 0

21 2100 1.016e-42 3.659e-21 0

22 2195 1.016e-42 5.291e-21 1

23 2290 1.016e-42 1.109e-19 2

24 2385 1.016e-42 1.569e-19 3

25 2480 2.847e-43 1.539e-19 0

26 2575 2.847e-43 3.926e-20 1

27 2670 2.847e-43 3.419e-27 2

28 2765 7.421e-49 3.113e-27 0

29 2860 7.421e-49 2.58e-29 1

30 2955 7.421e-49 6.413e-29 2

Best Mean Stall

Generation Func-count f(x) f(x) Generations

31 3050 9.217e-50 1.644e-31 0

32 3145 9.217e-50 2.874e-33 1

33 3240 1.346e-56 4.932e-36 0

34 3335 1.346e-56 1.985e-35 1

35 3430 1.346e-56 1.141e-36 2

36 3525 1.346e-56 7.898e-38 3

37 3620 1.346e-56 1.082e-37 4

38 3715 1.346e-56 7.564e-37 5

39 3810 1.346e-56 7.963e-35 6

40 3905 1.346e-56 5.27e-34 7

41 4000 1.346e-56 5.27e-34 8

42 4095 1.346e-56 5.27e-34 9

43 4190 1.346e-56 1.284e-36 10

44 4285 1.346e-56 5.86e-38 11

45 4380 1.346e-56 6.239e-38 12

46 4475 1.346e-56 1.206e-39 13

47 4570 1.346e-56 1.573e-37 14

48 4665 1.346e-56 1.998e-37 15

49 4760 1.346e-56 4.323e-39 16

50 4855 1.346e-56 2.422e-38 17

51 4950 1.346e-56 2.111e-38 18

ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Optimal solution for Study 2 (Increased Population and Mutation Rate):

0.5570 0.1649

Maximum fitness value for Study 2:

1.3460e-56

Single objective optimization:

2 Variables

Options:

CreationFcn: @gacreationuniform

CrossoverFcn: @crossoverscattered

SelectionFcn: @selectiontournament

MutationFcn: @mutationadaptfeasible

Best Mean Stall

Generation Func-count f(x) f(x) Generations

1 200 1.384e-27 1.92e-06 0

2 295 1.384e-27 5.838e-10 1

3 390 1.384e-27 3.529e-07 2

4 485 1.384e-27 3.281e-13 3

5 580 1.384e-27 3.133e-07 4

6 675 1.384e-27 5.188e-10 5

7 770 1.384e-27 2.307e-12 6

8 865 1.384e-27 1.007e-15 7

9 960 1.384e-27 1.41e-18 8

10 1055 1.384e-27 1.213e-21 9

11 1150 7.6e-37 2.42e-24 0

12 1245 7.6e-37 1.764e-21 1

13 1340 6.448e-38 8.491e-26 0

14 1435 6.448e-38 1.016e-22 1

15 1530 6.448e-38 1.252e-26 2

16 1625 4.575e-38 4.795e-29 0

17 1720 2.437e-41 1.71e-27 0

18 1815 6.484e-46 1.185e-22 0

19 1910 6.484e-46 5.739e-19 1

20 2005 6.484e-46 1.124e-22 2

21 2100 6.484e-46 4.229e-26 3

22 2195 6.484e-46 4.197e-30 4

23 2290 6.484e-46 1.649e-33 5

24 2385 1.708e-49 9.592e-36 0

25 2480 1.708e-49 6.367e-33 1

26 2575 1.708e-49 7.264e-36 2

27 2670 8.443e-53 1.14e-40 0

28 2765 4.133e-58 1.064e-37 0

29 2860 4.133e-58 8.898e-34 1

30 2955 1.978e-62 2.42e-37 0

Best Mean Stall

Generation Func-count f(x) f(x) Generations

31 3050 1.192e-62 6.617e-34 0

32 3145 5.704e-67 3.468e-30 0

33 3240 5.704e-67 2.155e-26 1

34 3335 5.704e-67 5.83e-30 2

35 3430 5.704e-67 1.321e-33 3

36 3525 5.704e-67 3.346e-37 4

37 3620 5.704e-67 2.671e-41 5

38 3715 5.704e-67 6.333e-41 6

39 3810 5.704e-67 1.635e-42 7

40 3905 5.704e-67 1.08e-40 8

41 4000 5.704e-67 3.693e-41 9

42 4095 5.704e-67 6.221e-41 10

43 4190 5.704e-67 6.798e-41 11

44 4285 5.704e-67 8.969e-42 12

45 4380 5.704e-67 2.491e-41 13

46 4475 5.704e-67 3.254e-41 14

47 4570 5.704e-67 9.15e-41 15

48 4665 5.704e-67 3.456e-41 16

49 4760 5.704e-67 1.58e-41 17

50 4855 5.704e-67 4.288e-41 18

51 4950 5.704e-67 1.039e-41 19

ga stopped because the average change in the fitness value is less than options.FunctionTolerance.

Optimal solution for Study 3 (Crossover Fraction and Tournament Selection):

0.5570 0.5570

Maximum fitness value for Study 3:

5.7045e-67

---------------------------------------------OUTPUT END------------------------------------------------

GA (Genetic Algorithm):

* It is a technique for optimization which uses natural selection principles to find an optimal solution

* It uses inheritance, mutation, selection, and crossover for evolution of future generations


ga SYNTAX:

[x, fval] = ga(@function, nvars, [] [] lb,ub)

x - optimised value of variables

fval - fuction value

nvars - no. of variables

[] - place for constraints (sometimes not needed)

lb - lower boundary

ub - upper boundary

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.