Challenge

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:


- Description: This case uses unbounded inputs, meaning the genetic algorithm samples across a broad range without constraints.


- Observation: The values for ‘fopt’ are scattered across the entire search space, making it difficult to draw any meaningful conclusions. The output is too coarse, indicating that the search space is too large and needs refinement.


- Output:


Conclusion:

Scattered and inconsistent values for ‘fopt`.

2. Case 2:
- Description: To improve the numerical analysis, we set upper and lower bounds for the inputs, restricting the search space to a bounding box from 0 to 0.6.


- Observation: Most iterations yield an optimum value of f = 1.0 , with some fluctuations around f = 0.871 in a few iterations. This suggests that while the bounded search space improves results, further refinements are needed.


- Output:


Conclusion: More consistent ‘fopt` values with occasional fluctuations, indicating improved but not perfect optimization.

3. Case 3:
- Description: The initial population size is increased to 100 to further refine the output.


- Observation: This approach is more effective, with the optimum value f = 1.0 achieved in nearly all iterations. Only a couple of cases show slight fluctuations around f = 0.8471. The results indicate better convergence towards the global maximum.


- Output:


Conclusion: Consistent ‘fopt` values near 1.0, with minor deviations, showing improved convergence.

4. Case 4:


- Description: This case combines the restricted bounding box and increased population size approaches. The bounding box is further reduced to 0–0.1, and the population size is increased from 100 to 300.


- Observation: The scattering of ‘fopt` values is insignificant, and the maximum value of the function shows negligible variation across iterations. Every iteration results in an optimum value f = 1.0 , indicating that the solution has converged perfectly.


- Output:


Conclusion: Extremely consistent ‘fopt` values at 1.0, demonstrating excellent convergence.

Conclusion


The genetic algorithm's performance in optimizing the stalagmite function improves significantly with strategic adjustments to the search space and population size.

- Case 1: The unbounded approach resulted in scattered and inconsistent values, highlighting the need for a more constrained search space.


- Case 2: Introducing bounds improved the results but still showed some fluctuations.


- Case 3: Increasing the population size led to more consistent and reliable convergence towards the global maximum.


- Case 4: Combining a smaller bounding box with a larger population size produced the best results, with all iterations converging to the global maximum of f = 1.0.

These observations suggest that refining the search space and increasing the population size are effective strategies for improving the performance of genetic algorithms. This approach can be crucial in practical engineering applications where finding the global optimum is essential. The results demonstrate that the genetic algorithm can effectively optimize complex functions when appropriately configured.



References

1. Goldberg, D. E. (1989). Genetic Algorithms in Search, Optimization, and Machine Learning. Addison-Wesley.


2. MATLAB Documentation: Genetic Algorithm. Retrieved from [MathWorks](https://www.mathworks.com/help/gads/genetic-algorithm.html).


3. Michalewicz, Z. (1996). Genetic Algorithms + Data Structures = Evolution Programs. Springer-Verlag.

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.