how to write simulated annealing code and compare it with genetic algorithm code in matlab

Here is an example of how to write a simulated annealing algorithm and a genetic algorithm in MATLAB, and then compare them:

  1. Simulated Annealing Algorithm:
main.m
% Define the objective function
objective = @(x) x.^2;

% Define the initial solution and the corresponding objective value
x0 = 5;
obj0 = objective(x0);

% Define the cooling schedule parameters
initialTemperature = 100;
coolingRate = 0.9;

% Simulated Annealing algorithm
x = x0;
obj = obj0;
temperature = initialTemperature;
while temperature > 1e-6
    % Generate a new solution using a random neighborhood operator
    xNew = x + randn()*temperature;
    objNew = objective(xNew);
    
    % Accept the new solution with a certain probability
    if objNew < obj || rand() < exp((obj - objNew)/temperature)
        x = xNew;
        obj = objNew;
    end
    
    % Cool down the temperature
    temperature = temperature * coolingRate;
end

% Print the final solution and objective value
disp("Simulated Annealing Algorithm:");
disp("Solution: " + x);
disp("Objective Value: " + obj);
891 chars
35 lines
  1. Genetic Algorithm:
main.m
% Define the objective function
objective = @(x) x.^2;

% Define the genetic algorithm parameters
populationSize = 100;
numberOfVariables = 1;
tournamentSize = 2;
mutationRate = 0.1;
generations = 100;

% Create the initial population
population = rand(populationSize, numberOfVariables);

% Genetic Algorithm
for generation = 1:generations
    % Evaluate the fitness for each individual in the population
    fitness = objective(population);
    
    % Select the parents for reproduction using tournament selection
    parents = zeros(populationSize, numberOfVariables);
    for i = 1:populationSize
        tournamentIndices = randperm(populationSize, tournamentSize);
        tournamentFitness = fitness(tournamentIndices);
        [~, index] = min(tournamentFitness);
        parents(i,:) = population(tournamentIndices(index),:);
    end
    
    % Apply crossover to generate the offspring
    offspring = zeros(populationSize, numberOfVariables);
    for i = 1:2:populationSize
        crossoverPoint = randi(numberOfVariables);
        offspring(i,:) = [parents(i,1:crossoverPoint), parents(i+1,crossoverPoint+1:end)];
        offspring(i+1,:) = [parents(i+1,1:crossoverPoint), parents(i,crossoverPoint+1:end)];
    end
    
    % Apply mutation to the offspring
    for i = 1:populationSize
        if rand() < mutationRate
            mutationPoint = randi(numberOfVariables);
            offspring(i,mutationPoint) = offspring(i,mutationPoint) + randn();
        end
    end
    
    % Replace the old population with the new offspring
    population = offspring;
end

% Evaluate the fitness for the final population
fitness = objective(population);

% Find the best solution from the final population
[~, index] = min(fitness);
bestSolution = population(index,:);

% Print the final solution and objective value
disp("Genetic Algorithm:");
disp("Solution: " + bestSolution);
disp("Objective Value: " + objective(bestSolution));
1941 chars
59 lines

To compare these two algorithms, you can run them with the same objective function and compare the final solutions and objective values they produce. Keep in mind that simulated annealing is a stochastic algorithm, so the results may vary between runs.

Note: This is a basic implementation of simulated annealing and genetic algorithm. Depending on your specific problem, you may need to adapt the algorithm for your needs.

gistlibby LogSnag