Week 5 - Genetic Algorithm
Objective: Optimization of the Stalagmite Function Using Genetic Algorithm
Introduction
In this
report, we explore the concept of genetic algorithms (GAs) and their
application in optimizing the stalagmite function. Genetic algorithms are
search heuristics that mimic the process of natural evolution, providing
efficient solutions to optimization and search problems. The stalagmite
function, known for its complex landscape, serves as an ideal candidate to
demonstrate the capabilities of GAs in finding global maxima.
Genetic
Algorithm Overview
A genetic
algorithm is an optimization technique based on the principles of natural
selection and genetics. The core idea is to evolve a population of candidate
solutions towards better solutions over successive generations. Key components
of GAs include:
1. Population:
A set of potential solutions.
2. Chromosomes:
Representation of solutions.
3. Fitness
Function: Evaluates how close a given solution is to the optimal solution.
4. Selection:
Process of choosing the best solutions for reproduction.
5. Crossover:
Combining two parent solutions to produce offspring.
6. Mutation:
Introducing random changes to offspring to maintain diversity.
7. Iteration:
Repeated application of selection, crossover, and mutation to evolve the
population.
Application
in Engineering
Genetic
algorithms are widely used in engineering for optimization problems where the
solution space is large and complex. Applications include:
- Structural
Optimization: Designing structures with optimal strength-to-weight ratios.
- Control
Systems: Tuning parameters of controllers for optimal performance.
- Signal
Processing: Enhancing signal quality by optimizing filters.
- Robotics:
Path planning and motion optimization for robots.
- Aerospace
Engineering: Optimizing flight paths and aerodynamic shapes.
Approach and
Steps Taken
To optimize
the stalagmite function using a genetic algorithm, we follow these steps:
1. Function
Definition: Define the stalagmite function.
2. Parameter
Initialization: Initialize parameters such as population size, number of
generations, and bounds for variables.
3. Genetic
Algorithm Execution: Run the genetic algorithm to find the optimal solution.
4. Result
Analysis: Plot the results and analyze the performance of the genetic algorithm
under different scenarios.
Code
Implementation
The
following MATLAB code demonstrates the application of a genetic algorithm to
optimize the stalagmite function. The code includes three different cases:
unbounded inputs, bounded inputs, and increased population size.
% Start Of the Program
% Close all open figures, clear the workspace, and clear the command window
close all;
clear all;
clc;
% Create a vector of 150 points from 0 to 0.6 for x and y
x = linspace(0, 0.6, 150);
y = linspace(0, 0.6, 150);
limit = 30; % Set the number of iterations for the genetic algorithm
% Create a 2D grid of x and y values
[xx, yy] = meshgrid(x, y);
% Initialize a 2D matrix to store function values
f = zeros(length(x), length(y));
% Populate the matrix f with values from the stalagmite function
for i = 1:length(xx)
for j = 1:length(yy)
input_vector = [xx(i, j), yy(i, j)]; % Create the input vector
f(i, j) = stalagmite_fun(input_vector); % Evaluate the function
end
end
% Start timer for Case 1
tic;
% Case 1: Unbounded Inputs
for i = 1:limit
[input, fval(i)] = ga(@stalagmite_fun, 2); % Perform genetic algorithm with 2 variables
x_out(i) = input(1); % Store the x-coordinate of the optimal input
y_out(i) = input(2); % Store the y-coordinate of the optimal input
end
case_time_1 = toc; % Stop timer
% Plot the results of Case 1
figure(1);
subplot(2, 1, 1);
surfc(xx, yy, -f); % Surface plot of the function
xlabel('x');
ylabel('y');
title('Case 1 (Unbounded Inputs)', 'Color', 'k');
shading interp; % Smooth color transition for better visualization
hold on;
plot3(x_out, y_out, -fval, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r'); % Plot optimal points
subplot(2, 1, 2);
plot(-fval);
xlabel('Number of Iterations');
ylabel('Maximum Function Value');
% Case 2: Bounded Inputs
tic; % Start timer for Case 2
for i = 1:limit
[input, fval(i)] = ga(@stalagmite_fun, 2, [], [], [], [], [0; 0], [0.6; 0.6]); % GA with bounds
x_out(i) = input(1);
y_out(i) = input(2);
end
case_time_2 = toc;
% Plot the results of Case 2
figure(2);
subplot(2, 1, 1);
hold on;
surfc(xx, yy, -f);
xlabel('x');
ylabel('y');
title('Case 2 (Bounded Inputs)', 'Color', 'k');
shading interp;
plot3(x_out, y_out, -fval, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
subplot(2, 1, 2);
plot(-fval);
xlabel('Number of Iterations');
ylabel('Maximum Function Value');
% Case 3: Increased Population Size
options = optimoptions('ga', 'PopulationSize', 100);
tic; % Start timer for Case 3
for i = 1:limit
[input, fval(i)] = ga(@stalagmite_fun, 2, [], [], [], [], [0; 0], [0.6; 0.6], [], options);
x_out(i) = input(1);
y_out(i) = input(2);
end
case_time_3 = toc;
% Plot the results of Case 3
figure(3);
subplot(2, 1, 1);
hold on;
surfc(xx, yy, -f);
shading interp;
xlabel('x');
ylabel('y');
title('Case 3 (Increased Population Size)', 'Color', 'k');
plot3(x_out, y_out, -fval, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
subplot(2, 1, 2);
plot(-fval);
xlabel('Number of Iterations');
ylabel('Maximum Function Value');
% Case 4: Different Bounding Box and Larger Population
options = optimoptions('ga', 'PopulationSize', 300);
tic; % Start timer for Case 4
for i = 1:limit
[input, fval(i)] = ga(@stalagmite_fun, 2, [], [], [], [], [0; 0], [0.1; 0.1], [], options);
x_out(i) = input(1);
y_out(i) = input(2);
end
case_time_4 = toc;
% Plot the results of Case 4
figure(4);
subplot(2, 1, 1);
hold on;
surfc(xx, yy, -f);
shading interp;
xlabel('x');
ylabel('y');
title('Case 4 (Different Bounding Box)', 'Color', 'k');
plot3(x_out, y_out, -fval, 'o', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
subplot(2, 1, 2);
plot(-fval);
xlabel('Number of Iterations');
ylabel('Maximum Function Value');
% Display the maximum function value and corresponding x and y values
[max_val, idx] = max(-fval);
max_x = x_out(idx);
max_y = y_out(idx);
fprintf('The global maximum of the function is at x = %.4f, y = %.4f with a value of %.4f\n', max_x, max_y, max_val);
% End of Script
% Definition of the stalagmite function
function f = stalagmite_fun(input_vector)
% Calculate individual components of the function
f1x = (sin((5.1 * pi) * input_vector(1) + 0.5)).^6;
f1y = (sin((5.1 * pi) * input_vector(2) + 0.5)).^6;
f2x = exp((((input_vector(1) - 0.0667).^2) / 0.64) * (-4 * log(2)));
f2y = exp((((input_vector(2) - 0.0667).^2) / 0.64) * (-4 * log(2)));
% Combine the components
f1 = f1x * f1y * f2x * f2y;
% Return the negative of the function for maximization
f = -f1;
end
Results and Observations
1. Case 1: