- Program Overview
This article presents a MATLAB-based comparison between binary-coded genetic algorithm (GA) and real-coded genetic algorithm. The comparison focuses on three key metrics: optimal fitness value, average fitness value, and computational efficiency.
- Test Environment and Results
The implementation was executed using MATLAB R2022a. The results demonstrate the performance differences between the two encoding schemes across multiple test functions.
- Core Implementation
The following code implements the genetic algorithm framework with configurable encoding schemes:
% Initialize population within bounds [-3, 3]
population = 3 * rand(populationSize, problemDim) - 3;
% Initialize best fitness value
globalBestFitness = -inf;
% Evaluate initial population fitness
for i = 1:populationSize
fitnessValues(i) = objectiveFunction(population(i, :));
if fitnessValues(i) > globalBestFitness
globalBestFitness = fitnessValues(i);
bestIndividual = population(i, :);
end
end
% Main evolutionary loop
for iteration = 1:maxIterations
iteration
% Roulette wheel selection
normalizedFitness = fitnessValues / sum(fitnessValues);
cumulativeFitness = cumsum(normalizedFitness);
% Create mating pool
for i = 1:populationSize
randomValue = rand(1);
selectedIndices = find(cumulativeFitness >= randomValue);
matingPool(i, :) = population(selectedIndices(1), :);
end
% Crossover operation
for i = 1:populationSize
if rand(1) < crossoverRate
parentA = randi([1, populationSize]);
parentB = randi([1, populationSize]);
while parentA == parentB
parentA = randi([1, populationSize]);
parentB = randi([1, populationSize]);
end
crossoverPoint = randi([1, problemDim]);
tempGene = matingPool(parentA, crossoverPoint+1:end);
matingPool(parentA, crossoverPoint+1:end) = matingPool(parentB, crossoverPoint+1:end);
matingPool(parentB, crossoverPoint+1:end) = tempGene;
end
end
% Mutation operation
mutationMask = rand(populationSize, problemDim) >= mutationRate;
randomGenes = 3 * rand(populationSize, problemDim) - 3;
offspring = mutationMask .* matingPool + (~mutationMask) .* randomGenes;
% Update population and fitness values
population = offspring;
for i = 1:populationSize
fitnessValues(i) = objectiveFunction(population(i, :));
if fitnessValues(i) > globalBestFitness
globalBestFitness = fitnessValues(i);
bestIndividual = population(i, :);
end
end
% Record fitness history
bestFitnessHistory(iteration) = globalBestFitness;
avgFitnessHistory(iteration) = mean(fitnessValues);
end
% Visualization
figure;
plot(1:maxIterations, bestFitnessHistory, 'b-', 'LineWidth', 2);
hold on;
plot(1:maxIterations, avgFitnessHistory, 'r--', 'LineWidth', 2);
legend('Optimal Fitness', 'Average Fitness');
xlabel('Iteration Number');
ylabel('Fitness Value');
grid on;
elapsedTime = toc;
save('results.mat', 'bestFitnessHistory', 'avgFitnessHistory', 'elapsedTime');
- Algorithm Principles
Genetic algorithms are optimization techniques inspired by biological evolution, widely applied to function optimization, machine learning, and pattern recognition. The encoding scheme is a critical component that determines problem representation and search space characteristics.
4.1 Binary-Coded Genetic Algorithm
Binary-coded GA represents solutions as strings of binary digits (0s and 1s). Each decition variable is encoded as a binary sequence, making this approach particularly effective for discrete optimization problems.
Algorithm Steps:
- Initialization: Generate random binary strings as the initial population. String length equals the number of decision variables.
- Fitness Evaluation: Calculate fitness values based on the objective function for each individual.
- Selection: Choose parent individuals for reproduction based on fitness, using methods such as roulette wheel or tournament selection.
- Crossover: Randomly pair individuals and exchange genetic material at crossover points to create offspring.
- Mutation: Randomly flip bits in some individuals to maintain population diversity.
- Termination: Stop when maximum iterations are reached or satisfactory solution is found.
4.2 Real-Coded Genetic Algorithm
Real-coded GA represents solutions as vectors of real numbers. This encoding is particularly suitable for continuous optimization problems and mixed discrete-continuous problems with continuous decision variables.
Algorithm Steps:
- Initialization: Generate random real-valued vectors within variable bounds as the initial population.
- Fitness Evaluation: Compute fitness values using the objective function for each solution.
- Selection: Select parent solutions based on fitness values using selection operators.
- Crossover: Create offspring by combining genes of parent solutions through linear recombination.
- Mutation: Modify genes by adding small random perturbations to maintain diversity.
- Termination: Stop when convergence criteria are satisfied.
4.3 Key Differences
The fundamental distinction lies in solution representation. Binary encoding operates on discrete search spaces through bit manipulation, while real encoding directly searches continuous spaces using floating-point values. Real-coded approaches offer more natural representation for continuous problems but may require more sophisticated strategies to avoid local optima. The choice between encoding schemes depends on problem characteristics, with binary encoding preferred for discrete problems and real encoding for continuous optimization tasks.