Here in this chapter, we will learn MATLAB Code for Genetic Algorithms. MATLAB is a high-level programming language and environment designed for numerical computing and algorithm development. It provides a comprehensive set of tools and functions for data analysis, visualization, and mathematical operations, making it widely used in various fields such as engineering, science, and finance.
Genetic Algorithm (GA) is a population-based optimization technique inspired by the process of natural selection and evolution. It was proposed by John Holland in 1975. Since its origin, it has found many interesting applications in various branches of science and engineering. It is used to solve complex problems by mimicking the principles of biological genetics and survival of the fittest.
Let’s have a look, at how we implement MATLAB Code for Genetic Algorithms.
clc;
clear all;
% Genetic Algorithm Parameters
pop_size = 50;
num_vars = 30;
lb = -100;
ub = 100;
num_generations = 1000;
mutation_rate = 0.1;
crossover_rate = 0.7;
% Initialize population
population = lb + (ub - lb) * rand(pop_size, num_vars);
% Main loop
for gen = 1:num_generations
% Evaluate fitness, note that you have to define the objective function in seprate file
fitness = sphere_func(population);
best_fitness = min(fitness);
% Selection
tournament_size = 2;
tournament_winners = zeros(pop_size, num_vars);
for i = 1:pop_size
tournament_inds = randperm(pop_size, tournament_size);
tournament_fitness = fitness(tournament_inds);
[~, best_ind] = min(tournament_fitness);
tournament_winners(i, :) = population(tournament_inds(best_ind), :);
end
% Crossover
crossover_inds = rand(pop_size, num_vars) < crossover_rate;
offspring = zeros(pop_size, num_vars);
for i = 1:pop_size
parent1 = tournament_winners(i, :);
if i == pop_size
parent2 = tournament_winners(1, :);
else
parent2 = tournament_winners(i+1, :);
end
offspring(i, crossover_inds(i,:)) = parent1(crossover_inds(i,:));
offspring(i, ~crossover_inds(i,:)) = parent2(~crossover_inds(i,:));
end
% Mutation
mutation_inds = rand(pop_size, num_vars) < mutation_rate;
mutation_amounts = lb + (ub - lb) * rand(pop_size, num_vars);
offspring(mutation_inds) = mutation_amounts(mutation_inds);
% Update population
population = offspring;
% Display current best fitness
fprintf('Generation %d: Best Fitness = %f\n', gen, best_fitness);
end
% Final evaluation
final_fitness = sphere_func(population);
best_solution = population(final_fitness == min(final_fitness), :);
best_fitness = min(final_fitness);
fprintf('Final Best Fitness = %f\n', best_fitness);
disp('Best Solution:');
disp(best_solution);
In the code given above, you have to define a separate file for your objective function. for simplicity purposes, we have included the sphere function as the objective function.
% Define your objective function
function fitness = sphere_func(x)
fitness = sum(x.^2, 2);
end
Make sure to save the objective function file as “sphere_func.m” in the same directory as the Genetic Algorithm code. If you’re interested in the Python implementation of the Genetic Algorithm (Click here.) If you’re interested in knowing more about Genetic Algorithms, check this out. For any query or further assistance leave your comment below. Happy Learning!