模拟退火(SA)算法

目录

模拟退火算法

主要代码

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值