% Define parameters
pc = 0.3; % Crossover probability
pm = 0.2; % Mutation probability
num_iters = 50; % Number of iterations
pop_size = 10; % Population size
% Initial population (each row is a chromosome)
population = [265 395 139;
294 274 273;
41 42 479;
313 414 71;
51 210 211;
315 347 458;
257 367 372;
312 404 196;
213 294 328;
12 328 86];
% Random values for selection and crossover
initial_random_values = rand(pop_size, 1);
parent_selection_values = rand(pop_size, 1);
% Mutation data (indices in 1D format)
mutated_tiles = [1, 25, 28, 20, 23, 22];
mutated_values = [196, 328, 86, 353, 16, 138];
% Fitness function (Max-Z function)
max_z_function = @(x1, x2, x3) (10*x1 + 9*x2 + 8*x3) ...
- 5 * (max(0, 4*x1 + 3*x2 + 2*x3 - 1300))^2 ...
- 5 * (max(0, 3*x1 + 2*x2 + 2*x3 - 1000))^2;
for iter = 1:num_iters
fprintf('Iteration %d\n', iter);
% Compute fitness values for population
num_values = size(population, 1);
Z_values = zeros(num_values, 1);
for i = 1:num_values
x1 = population(i, 1);
x2 = population(i, 2);
x3 = population(i, 3);
Z_values(i) = max_z_function(x1, x2, x3);
end
% Shift fitness values to ensure non-negative probabilities
Z_min = min(Z_values);
Z_shifted = Z_values + abs(Z_min);
total_Z = sum(Z_shifted);
probabilities = Z_shifted / total_Z;
cumulative_probabilities = cumsum(probabilities);
% Selection Process (Roulette Wheel Selection)
new_population = zeros(size(population));
for i = 1:length(initial_random_values)
rand_val = initial_random_values(i);
selected_idx = find(cumulative_probabilities >= rand_val, 1, 'first');
new_population(i, :) = population(selected_idx, :);
end
% Parent Selection
parent_indices = find(parent_selection_values <= pc);
selected_parents = new_population(parent_indices, :);
% Crossover Process
num_parents = size(selected_parents, 1);
if num_parents >= 2
new_offspring = zeros(num_parents, size(selected_parents, 2));
for i = 1:num_parents
parent1 = selected_parents(i, :);
parent2 = selected_parents(mod(i, num_parents) + 1, :);
offspring1 = [parent1(1), parent2(2), parent2(3)];
offspring2 = [parent2(1), parent1(2), parent1(3)];
new_offspring(i, :) = offspring1;
if i < num_parents
new_offspring(i + 1, :) = offspring2;
else
new_offspring(1, :) = offspring2;
end
end
for i = 1:num_parents
if i <= length(parent_indices)
new_population(parent_indices(i), :) = new_offspring(i, :);
end
end
end
% Mutation Process
[num_rows, num_cols] = size(new_population);
for i = 1:length(mutated_tiles)
tile_idx = mutated_tiles(i);
row = floor((tile_idx - 1) / num_cols) + 1;
col = mod(tile_idx - 1, num_cols) + 1;
if row <= num_rows && col <= num_cols
new_population(row, col) = mutated_values(i);
end
end
% Compute fitness for mutated population
Z_values = zeros(num_values, 1);
for i = 1:num_values
x1 = new_population(i, 1);
x2 = new_population(i, 2);
x3 = new_population(i, 3);
Z_values(i) = max_z_function(x1, x2, x3);
end
% Compute standard deviation
mean_Z = mean(Z_values);
variance = sum((Z_values - mean_Z).^2) / num_values;
fitness_std_dev = sqrt(variance);
% Display iteration results
fprintf('Standard Deviation of Fitness: %.4f\n', fitness_std_dev);
% Update population for next iteration
population = new_population;
end