How to do a Population and crossover on matrix ?

3 vues (au cours des 30 derniers jours)
Firas Al-Kharabsheh
Firas Al-Kharabsheh le 4 Mai 2016
How to generate a 20 population of matrix and then calculate fitness funtion to eaach one using sum (sum (matrix)) then perform a random selection of parents among the 10 min value of matrix then do the crossover on them ??? And after that a randomly mutation
To achieve the minimum fitness Using matlab

Réponses (1)

Shishir Reddy
Shishir Reddy le 30 Mai 2025
As per my understanding, you would like to Implement a genetic algorithm in MATLAB to minimize the sum of matrix elements by evolving a population of random matrices using selection, crossover, and mutation.
Kindly refer to the following steps for the same -
1. Set Parameters and Generate Initial Population
clc;
clear;
% Parameters
pop_size = 20; % Total number of matrices
matrix_size = [5, 5]; % Size of each matrix
num_generations = 50; % Number of generations to evolve
mutation_rate = 0.1; % Probability of mutation (10%)
% Initialize population with random integers from 0 to 10
population = cell(pop_size, 1);
for i = 1:pop_size
population{i} = randi([0, 10], matrix_size);
end
2. Evaluate Fitness for Each Matrix and Select the Top 10 Fittest Matrices
for gen = 1:num_generations
% Compute fitness (lower is better)
fitness = zeros(pop_size, 1);
for i = 1:pop_size
fitness(i) = sum(sum(population{i}));
end
% Select 10 matrices with the lowest fitness
[~, idx] = sort(fitness); % Sort indices by fitness
top10 = population(idx(1:10)); % Select best 10
3. Generate New Population via Crossover and Mutation
new_population = cell(pop_size, 1);
for i = 1:2:pop_size
% Randomly select two parents from top 10
p1 = top10{randi([1, 10])};
p2 = top10{randi([1, 10])};
% Crossover: combine rows from both parents
crossover_point = randi([1, matrix_size(1)-1]);
child1 = [p1(1:crossover_point, :); p2(crossover_point+1:end, :)];
child2 = [p2(1:crossover_point, :); p1(crossover_point+1:end, :)];
% Mutation: randomly change one element in each child with a small chance
if rand < mutation_rate
row = randi([1, matrix_size(1)]);
col = randi([1, matrix_size(2)]);
child1(row, col) = randi([0, 10]);
end
if rand < mutation_rate
row = randi([1, matrix_size(1)]);
col = randi([1, matrix_size(2)]);
child2(row, col) = randi([0, 10]);
end
% Add children to new population
new_population{i} = child1;
if i+1 <= pop_size
new_population{i+1} = child2;
end
end
4. Update Population and Print Progress
% Replace the old population with the new one
population = new_population;
% Print best fitness in this generation
best_fit = min(cellfun(@(m) sum(sum(m)), population));
fprintf('Generation %d - Best Fitness: %d\n', gen, best_fit);
end
Generation 1 - Best Fitness: 81 Generation 2 - Best Fitness: 83 Generation 3 - Best Fitness: 75 Generation 4 - Best Fitness: 75 Generation 5 - Best Fitness: 74 Generation 6 - Best Fitness: 75 Generation 7 - Best Fitness: 70 Generation 8 - Best Fitness: 70 Generation 9 - Best Fitness: 69 Generation 10 - Best Fitness: 69 Generation 11 - Best Fitness: 68 Generation 12 - Best Fitness: 68 Generation 13 - Best Fitness: 68 Generation 14 - Best Fitness: 65 Generation 15 - Best Fitness: 65 Generation 16 - Best Fitness: 65 Generation 17 - Best Fitness: 65 Generation 18 - Best Fitness: 65 Generation 19 - Best Fitness: 65 Generation 20 - Best Fitness: 65 Generation 21 - Best Fitness: 65 Generation 22 - Best Fitness: 65 Generation 23 - Best Fitness: 61 Generation 24 - Best Fitness: 61 Generation 25 - Best Fitness: 61 Generation 26 - Best Fitness: 63 Generation 27 - Best Fitness: 63 Generation 28 - Best Fitness: 63 Generation 29 - Best Fitness: 63 Generation 30 - Best Fitness: 61 Generation 31 - Best Fitness: 61 Generation 32 - Best Fitness: 61 Generation 33 - Best Fitness: 60 Generation 34 - Best Fitness: 61 Generation 35 - Best Fitness: 60 Generation 36 - Best Fitness: 60 Generation 37 - Best Fitness: 57 Generation 38 - Best Fitness: 59 Generation 39 - Best Fitness: 59 Generation 40 - Best Fitness: 59 Generation 41 - Best Fitness: 59 Generation 42 - Best Fitness: 58 Generation 43 - Best Fitness: 56 Generation 44 - Best Fitness: 56 Generation 45 - Best Fitness: 54 Generation 46 - Best Fitness: 54 Generation 47 - Best Fitness: 54 Generation 48 - Best Fitness: 52 Generation 49 - Best Fitness: 53 Generation 50 - Best Fitness: 52
I hope this helps.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by