Matlab代码演示
【示例】旅行商问题(TSP问题)。假设有一个旅行商人要拜访全国31个省会城市,他需要选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择要求是:所选路径的路程为所有路径之中的最小值。
全国31个省会城市的坐标为 [1304 2312;3639 1315;4177 2244;3712 1399;3488 1535;3326 1556;3238 1229;4196 1004;4312 790;4386 570;3007 1970;2562 1756;2788 1491;2381 1676;1332 695;3715 1678;3918 2179;4061 2370;3780 2212;3676 2578;4029 2838;4263 2931;3429 1908;3507 2367;3394 2643;3439 3201;2935 3240;3140 3550;2545 2357;2778 2826;2370 2975]。
%%################################ 1. 主要函数:模拟退火算法实现 ###################################################
function [obj,order,city] = SA(TIn,TdIn,LIn,city,order,k)
%% Input parameters
%SA Simulated Annealing
%TIn: Current Temperature
%TdIn:Final Temperature
%LIn: Number of initial iterations of the inner loop
%city: Current City-coordinate
%order: Current city-order
%k:Temperature Attenuation Coefficient
%% Output parameters
%obj: Objective function value(total distance between 31 cities)
%order: New city-order
%city: New City-coordinate
%% Data preparation
iter = 1;%Number of outer loop iterations
T = TIn;
Td = TdIn;
distcur = dist(city);
obj(iter) = distcur;
L = LIn;
%% Main code
% ##################Outer loop############################################
while T>Td
%% Setting cycle times for inner cycle
if T<100
L = 100;
elseif T<10
L = 200;
end
%% inner cycle
for i = 1:L
[tmp_city,order] = new_solution(city,order);
distnew = dist(tmp_city);
delta = distnew - distcur;
if delta < 0
city = tmp_city;
else
p = exp(-delta/T);
if p>rand
city = tmp_city;
end
end
distcur = dist(city);
end
%% Update the information of the outer loop
iter = iter + 1;%Oteration number updating
obj(iter) = distcur;%Objective function value updating
T = k*T;%Decay temperature
end
end
%%################################ 2. 新解产生函数 ##############################################################
function [tmp_Codnt,order] = new_solution(Codnt,Order)
%% input parameter
%Codnt:a struct defines current city coordinate
%Order:the order of the current solution
%% output parameter
%tmp_Codnt:a struct defines new city coordinate
%order:the new city order
%%
n = size(Codnt,2);
tmp_Codnt = Codnt;
order = Order;
%%
%###################随机产生两个城市编号#######################
p1 = unidrnd(n);
p2 = unidrnd(n);
while p1==p2
p1 = unidrnd(n);
p2 = unidrnd(n);
end
%%
%###################对换两个城市位置形成新解#######################
tmp = tmp_Codnt(p1);
tmp_Codnt(p1) = tmp_Codnt(p2);
tmp_Codnt(p2) = tmp;
tmp = order(p1);
order(p1)=order(p2);
order(p2)=tmp;
end
%%######################## 3. 目标函数计算函数 ###############################################################
function distvalue = dist(Codnt)
%% input parameter
%Codnt: a struct defines city coordinate
%% output parameter
% distvalue:Euclidean distance based on current solution
%%
n = size(Codnt,2);
distvalue = 0;
for i=1:n-1
distvalue = distvalue + sqrt((Codnt(i).x-Codnt(i+1).x)^2+(Codnt(i).y-Codnt(i+1).y)^2);
end
distvalue = distvalue + sqrt((Codnt(n).x-Codnt(1).x)^2+(Codnt(n).y-Codnt(1).y)^2);
end
%%############################ 4. 图形绘制函数 ################################################################
function Fgplot(city,order)
%% iuput parameters
%city: city coordinate
%order: city order of the current solution
n = size(city,2);
for i=1:n-1
plot([city(i).x,city(i+1).x],[city(i).y,city(i+1).y],'bo-');
hold on;
text(city(i).x+30,city(i).y+10,num2str(order(i)));
end
plot([city(n).x,city(1).x],[city(n).y,city(1).y],'bo-');
text(city(n).x+10,city(n).y+10,num2str(order(n)));
xlabel('城市位置横坐标');
ylabel('城市位置纵坐标');
end
%%##################################### main.m (参数输入) #######################################################
clear all;
close all;
clc;
T = 300;%Initial Temperature
Td = 0.005;%Terminal Temperature
L = 50;%Number of initial iterations of the inner loop
k = 0.995;%Temperature attenuation coefficient
CityCoordinate = [1304 2312;3639 1315;4177 2244;3712 1399;3488 1535;3326 1556;3238 1229;4196 1004;4312 790;4386 570;3007 1970;2562 1756;2788 1491;2381 1676;1332 695;3715 1678;3918 2179;4061 2370;3780 2212;3676 2578;4029 2838;4263 2931;3429 1908;3507 2367;3394 2643;3439 3201;2935 3240;3140 3550;2545 2357;2778 2826;2370 2975];
nCity = size(CityCoordinate,1);
city = struct([]);
for i = 1:nCity
city(i).x = CityCoordinate(i,1);
city(i).y = CityCoordinate(i,2);
end
tic
figure(1)
order = 1:nCity;
Fgplot(city,order);
title('初始解路径')
[obj,order,City] = SA(T,Td,L,city,order,k);
figure(2)
m = length(obj);
plot(obj)
xlabel('迭代次数');
ylabel('城市间总距离');
title('总距离演变曲线')
figure(3)
Fgplot(City,order);
title('最优解路径')
disp('1.新的路径顺序为:')
disp(order)
disp('2.初始目标距离为:')
disp(obj(1))
disp('3.最优目标距离值为:')
disp(obj(m))
toc
图形展示
参考文献
[1] http://cighao.com/2015/12/03/introduction-of-SA/
[2] https://www.cnblogs.com/ranjiewen/p/6084052.html
[3] 公众号----交通与优化之模拟退火
[4] S. Kirkpatrick, C. D. Gelatt, Jr., M. P. Vecchi, 1983. Optimization by Simulated Annealing. Science, 220, No.4598.
[5] 包子阳,余继周. 智能优化算法及其MATLAB实例[M]. 电子工业出版社,2016.
[6] python 优化包scikit-opt
————————————————
本文转载自CSDN平台博主:Sunshing15
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_52721512/article/details/126497778