clc;
clear;
close all;
%% Problem Definition
model = CreateModel(); % Create TSP Model
CostFunction=@(tour) TourLength(tour, model); % Cost Function
ActionList=CreatePermActionList(model.n); % Action List
nAction=numel(ActionList); % Number of Actions
%% Tabu Search Parameters
MaxIt=50; % Maximum Number of Iterations
TL=round(0.5*nAction); % Tabu Length
%% Initialization
% Create Empty Individual Structure
empty_individual.Position=[];
empty_individual.Cost=[];
% Create Initial Solution
sol=empty_individual;
sol.Position=randperm(model.n);
sol.Cost=CostFunction(sol.Position);
% Initialize Best Solution Ever Found
BestSol=sol;
% Array to Hold Best Costs
BestCost=zeros(MaxIt,1);
% Initialize Action Tabu Counters
TC=zeros(nAction,1);
%% Tabu Search Main Loop
for it=1:MaxIt
bestnewsol.Cost=inf;
% Apply Actions
for i=1:nAction
if TC(i)==0
newsol.Position=DoAction(sol.Position,ActionList{i});
newsol.Cost=CostFunction(newsol.Position);
newsol.ActionIndex=i;
if newsol.Cost<=bestnewsol.Cost
bestnewsol=newsol;
end
end
end
% Update Current Solution
sol=bestnewsol;
% Update Tabu List
for i=1:nAction
if i==bestnewsol.ActionIndex
TC(i)=TL; % Add To Tabu List
else
TC(i)=max(TC(i)-1,0); % Reduce Tabu Counter
end
end
% Update Best Solution Ever Found
if sol.Cost<=BestSol.Cost
BestSol=sol;
end
% Save Best Cost Ever Found
BestCost(it)=BestSol.Cost;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
% Plot Best Solution
figure(1);
PlotSolution(BestSol, model);
pause(0.01);
% If Global Minimum is Reached
if BestCost(it)==0
break;
end
end
BestCost=BestCost(1:it);
%% Results
figure;
plot(BestCost,'LineWidth',2);
xlabel('Iteration');
ylabel('Best Cost');
grid on;
【路径规划-TSP问题】基于禁忌搜索算法求解旅行商问题Matlab源码2 上传.zip
版权申诉
51 浏览量
2023-01-07
20:36:58
上传
评论
收藏 48KB ZIP 举报
天天Matlab科研工作室
- 粉丝: 3w+
- 资源: 7258
最新资源
- 设计1.ms14
- vscode-1.64.1.tar源码文件
- vscode-1.64.0.tar源码文件
- vscode-1.52.0.tar源码文件
- Music-Player +PlayerActivity+ rockplayer+ SeeJoPlayer 播放器JAVA源码
- vscode-1.46.0.tar源码文件
- 最近很火植物大战僵尸杂交版2.08苹果+安卓+PC+防闪退工具V2+修改工具+高清工具+通关存档整合包更新
- 超级好用的截图工具PixPin,可录制Gif图
- Screenshot_2024-05-21-17-06-42-64_2332cb9b27b851b548ba47a91682926c.jpg
- 毕业设计参考 - 基于树莓派、OpenCV及Python的人脸识别
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈