% PFSP_AOA.m
% 设置问题参数
numJobs = 10; % 作业数量
numMachines = 3; % 机器数量
numIterations = 100; % 迭代次数
numIndividuals = 50; % 种群中个体数量
alpha = 0.5; % 阿基米德常量
% 生成随机初始解
initialSolution = randperm(numJobs);
% 初始化种群
population = repmat(initialSolution, numIndividuals, 1);
% 计算初始种群适应度
fitness = zeros(numIndividuals, 1);
for i = 1:numIndividuals
fitness(i) = calculateFitness(population(i, 😃);
end
% 迭代优化
for iteration = 1:numIterations
% 计算种群中每个个体的适应度得分
scores = alpha * fitness + (1 - alpha) * max(fitness);
% 计算选择概率
probabilities = scores / sum(scores);
% 选择父代个体
parents = zeros(numIndividuals, numJobs);
for i = 1:numIndividuals
index = rouletteWheelSelection(probabilities);
parents(i, :) = population(index, :);
end
% 生成子代个体
offspring = zeros(numIndividuals, numJobs);
for i = 1:numIndividuals
% 交叉操作:单点交叉
parent1 = parents(i, :);
parent2 = parents(mod(i, numIndividuals) + 1, :);
offspring(i, :) = crossover(parent1, parent2);
% 变异操作:交换两个位置的作业
if rand < 0.1
offspring(i, :) = mutate(offspring(i, :));
end
end
% 计算子代个体适应度
offspringFitness = zeros(numIndividuals, 1);
for i = 1:numIndividuals
offspringFitness(i) = calculateFitness(offspring(i, :));
end
% 更新种群
population = offspring;
fitness = offspringFitness;
% 显示当前迭代结果
bestFitness = min(fitness);
disp(['Iteration: ' num2str(iteration) ', Best Fitness: ' num2str(bestFitness)]);
end
% 输出最优解
bestSolution = population(fitness == min(fitness), 😃;
disp(‘Best Solution:’);
disp(bestSolution);
% 计算适应度函数
function fitness = calculateFitness(solution)
% 计算每个作业在每个机器上的完成时间
completionTimes = zeros(numMachines, numJobs);
for i = 1:numMachines
for j = 1:numJobs
if i == 1 && j == 1
completionTimes(i, j) = solution(j);
elseif i == 1
completionTimes(i, j) = completionTimes(i, j-1) + solution(j);
elseif j == 1
completionTimes(i, j) = completionTimes(i-1, j) + solution(j);
else
completionTimes(i, j) = max(completionTimes(i-1, j), completionTimes(i, j-1)) + solution(j);
end
end
end
% 计算完成时间矩阵中最后一个元素的值
fitness = completionTimes(numMachines, numJobs);
end
% 轮盘赌选择
function index = rouletteWheelSelection(probabilities)
r = rand;
cumulativeProbs = cumsum(probabilities);
index = find(r <= cumulativeProbs, 1, ‘first’);
end
% 单点交叉
function child = crossover(parent1, parent2)
point = randi([2, numJobs-1]);
child = [parent1(1:point) parent2(point+1:end)];
end
% 变异
function mutatedSolution = mutate(solution)
positions = randperm(numJobs, 2);
mutatedSolution = solution;
mutatedSolution(positions) = solution(fliplr(positions));
end