目录
模拟退火算法
主要代码
Mutate
Sphere
模拟退火算法
主要代码
repmat 重复数组副本
B = repmat(A,n)
返回一个数组,该数组在其行维度和列维度包含A
的n
个副本。A
为矩阵时,B
大小为size(A)*n
。unifrnd 生成连续统一的随机数
sort 对数组进行排序
semilogy 半对数图,y 轴有对数刻度
semilogy(X,Y)
在 x 轴上使用线性刻度、在 y 轴上使用以 10 为底的对数刻度来绘制 x 和 y 坐标。
要绘制由线段连接的一组坐标,请将
X
和Y
指定为相同长度的向量。要在同一组坐标轴上绘制多组坐标,请将
X
或Y
中的至少一个指定为矩阵。
semilogy(X,Y,LineSpec)
使用指定的线型、标记和颜色创建绘图。
semilogy(X1,Y1,...,Xn,Yn)
在同一组坐标轴上绘制多对 x 和 y 坐标。此语法可替代将坐标指定为矩阵的形式。
clc;
clear;
close all;
%% Problem Definition
CostFunction = @(x) Sphere(x); % Cost Function 成本函数
nVar = 5; % Number of Decision (Unknwon) Variables决策数
VarSize = [1 nVar]; % Decision Variables Matrix Size
VarMin = -10; % Lower Bound of Decision Variables
VarMax = 10; % Upper Bound of Decision Variables
%% SA Parameters
MaxIt = 1000; % Maximum Number of Iterations最大迭代次数
MaxSubIt = 20; % Maximum Number of Sub-iterations最大次迭代次数
T0 = 0.1; % Initial Temp.初始温度
alpha = 0.99; % Temp. Reduction Rate温度降低速率
nPop = 10; % Population Size人口规模
nMove = 5; % Number of Neighbors per Individual
mu = 0.5; % Mutation Rate变异率
sigma = 0.1*(VarMax-VarMin); % Mutation Range (Standard Deviation)突变范围(标准偏差)
%% Initialization
% Create Empty Structure for Individuals
empty_individual.Position = [];
empty_individual.Cost = [];
% Create Population Array
pop = repmat(empty_individual, nPop, 1);
% Initialize Best Solution
BestSol.Cost = inf;
% Initialize Population
for i = 1:nPop
% Initialize Position
pop(i).Position = unifrnd(VarMin, VarMax, VarSize);
% Evaluation
pop(i).Cost = CostFunction(pop(i).Position); % Cost Function 成本函数
% Update Best Solution
if pop(i).Cost <= BestSol.Cost
BestSol = pop(i);
end
end
% Array to Hold Best Cost Values
BestCost = zeros(MaxIt, 1); % Maximum Number of Iterations最大迭代次数
% Intialize Temp.
T = T0;
%% SA Main Loop
for it = 1:MaxIt %MaxIt最大迭代次数
for subit = 1:MaxSubIt %MaxSubIt最大次迭代次数
% Create and Evaluate New Solutions
newpop = repmat(empty_individual, nPop, nMove);
for i = 1:nPop
for j = 1:nMove
% Create Neighbor
newpop(i, j).Position = Mutate(pop(i).Position, mu, sigma, VarMin, VarMax);
% Evaluation
newpop(i, j).Cost = CostFunction(newpop(i, j).Position);
end
end
newpop = newpop(:);
% Sort Neighbors
[~, SortOrder] = sort([newpop.Cost]);
newpop = newpop(SortOrder);
for i = 1:nPop
if newpop(i).Cost <= pop(i).Cost
pop(i) = newpop(i);
else
DELTA = (newpop(i).Cost-pop(i).Cost)/pop(i).Cost;
P = exp(-DELTA/T);
if rand <= P
pop(i) = newpop(i);
end
end
% Update Best Solution Ever Found
if pop(i).Cost <= BestSol.Cost
BestSol = pop(i);
end
end
end
% Store Best Cost Ever Found
BestCost(it) = BestSol.Cost;
% Display Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
% Update Temp.
T = alpha*T;
sigma = 0.98*sigma;
end
%% Results
figure;
%plot(BestCost, 'LineWidth', 2);
semilogy(BestCost, 'LineWidth', 2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
Mutate
rand 生成均匀分布的随机数
randn 生成正态分布的随机数
function y = Mutate(x, mu, sigma, VarMin, VarMax)
A = (rand(size(x)) <= mu);
J = find(A == 1);
y = x;
y(J) = x(J)+sigma*randn(size(J));
% Clipping
y = max(y, VarMin);
y = min(y, VarMax);
end
Sphere
function z = Sphere(x)
z = sum(x.^2);
end