【MTSP】基于matlab遗传算法求解多旅行商问题【含Matlab源码 1338期】
一、获取代码方式
获取代码方式1:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
获取代码方式2:
完整代码已上传我的资源:【MTSP】基于matlab遗传算法求解多旅行商问题【含Matlab源码 1338期】
备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
二、TSP简介
旅行商问题,即TSP问题(Traveling Salesman Problem)又译为旅行推销员问题、货郎担问题,是数学领域中著名问题之一。假设有一个旅行商人要拜访n个城市,他必须选择所要走的路径,路径的限制是每个城市只能拜访一次,而且最后要回到原来出发的城市。路径的选择目标是要求得的路径路程为所有路径之中的最小值。
TSP的数学模型
三、遗传算法简介
1 引言
2 遗传算法理论
2.1 遗传算法的生物学基础
2.2 遗传算法的理论基础
2.3 遗传算法的基本概念
2.4 标准的遗传算法
2.5 遗传算法的特点
2.6 遗传算法的改进方向
3 遗传算法流程
4 关键参数说明
四、部分源代码
function varargout = mtsp_ga_multi_ch(xy,dmat,salesmen,min_tour,max_tour,tw,pop_size,num_iter,use_complex,show_prog,show_res)
% MTSP_GA_MULTI_CH Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA) using multi-chromosome representation
% Finds a (near) optimal solution to a variation of the M-TSP by setting
% up a GA to search for the shortest route, taking into account
% additional constraints, and minimizing the number of salesmen.
%
% Summary:
% 1. Each salesman starts at the first location, and ends at the first
% location, but travels to a unique set of cities in between
% 2. Except for the first, each city is visited by exactly one salesman
% 3. The algorithm uses a special, so-called multi-chromosome genetic
% representation to code solutions into individuals.
% 4. Special genetic operators (even complex ones) are used.
% 5. The number of salesmen used is minimized during the algorithm
% 6. Additional constraints have to be satisfied
% - minimum number of locations, what each salesmen visit
% - maximum distane travelled by each salesmen
% 7. Time windows can be defined for each locations (e.g. packing/loading times).
%
% Note: The Fixed Start/End location is taken to be the first XY point
%
% Inputs:
% XY (float) is an Nx2 matrix of city locations, where N is the number of cities
% DMAT (float) is an NxN matrix of city-to-city distances or costs
% SALESMEN (scalar integer) is the number of salesmen to visit the cities
% MIN_TOUR (scalar integer) is the minimum number of cities for each
% salesmen, NOT including the start/end point
% MAX_TOUR (scalar integer) is the maximum tour length for each salesmen
% TW (scalar_integer) is the time window for each location
% POP_SIZE (scalar integer) is the size of the population (should be divisible by 8)
% NUM_ITER (scalar integer) is the number of desired iterations for the algorithm to run
% USE_COMPLEX (scalar boolen 0/1) is the flag wether to use complex mutation operators or not
% SHOW_PROG (scalar logical) shows the GA progress if true
% SHOW_RES (scalar logical) shows the GA results if true
%
% Outputs:
% OPT_RTE (integer array) is the best route found by the algorithm
% MIN_DIST (scalar float) is the total distance traveled by the salesmen
% OPT_ITER (scalar int) is the number of iterations until the optimal solution has been found
% OPT_TIME (scalar float) is the time in milliseconds until the optimal solution has been found
% DIST_HISTORY (float array) is the history of distances of best found solutions
%
% Authors: Andras Kiraly, Janos Abonyi
% Email: kiralya@fmt.uni-pannon.hu
% Release Date: 16/10/2014
% The implementation is based on the work of Joseph Kirk: mtspf_ga
%
% *************************************************************************% Process Inputs and Initialize Defaults
nargs = 11;
for k = nargin:nargs-1switch kcase 0xy = 40*rand(40,2);case 1N = size(xy,1);a = meshgrid(1:N);dmat = reshape(sqrt(sum((xy(a,:)-xy(a',:)).^2,2)),N,N);case 2salesmen = 8;case 3min_tour = 5;case 4max_tour = 100;case 5tw = 0;case 6pop_size = 80;case 7num_iter = 500;case 8use_complex = 0;case 9show_prog = 1;case 10show_res = 1;otherwiseend
endmerging_prob = 0.3;% Verify Inputs
[N,dims] = size(xy);
[nr,nc] = size(dmat);
if N ~= nr || N ~= ncerror('Invalid XY or DMAT inputs!')
end
n = N - 1; % Separate Start/End City% Sanity Checks
salesmen = max(1,min(n,round(real(salesmen(1)))));
min_tour = max(1,min(floor(n/salesmen),round(real(min_tour(1)))));
pop_size = max(8,8*ceil(pop_size(1)/8));
num_iter = max(1,round(real(num_iter(1))));
show_prog = logical(show_prog(1));
show_res = logical(show_res(1));% Initializations for Route Break Point Selection
num_brks = salesmen-1;
dof = n - min_tour*salesmen; % degrees of freedom
addto = ones(1,dof+1);
for k = 2:num_brksaddto = cumsum(addto);
end
cum_prob = cumsum(addto)/sum(addto);% Initialize the Populations
pop_rte = zeros(pop_size,n); % population of routes
pop_brk = zeros(pop_size,num_brks); % population of breaks
for k = 1:pop_sizepop_rte(k,:) = randperm(n)+1;pop_brk(k,:) = randbreaks();
end% Select the Colors for the Plotted Routes
clr = [1 0 0; 0 0 1; 0.67 0 1; 0 1 0; 1 0.5 0];
if salesmen > 5clr = hsv(salesmen);
end% Run the GA
global_min = Inf;
tmp_pop_8 = cell(1,8);
new_pop = cell(1,pop_size);
total_dist = zeros(1,pop_size);
dist_history = zeros(1,num_iter);
if show_progpfig = figure('Name','MTSPF_GA | Current Best Solution','Numbertitle','off');
end% ----=== TARNSFORMATION --> multiple chromosome [BEGIN] ===----
pop = cell(1,pop_size);
for k = 1: pop_sizepop{k}.ch{1} = pop_rte(k, 1:pop_brk(k,1));for j=2:salesmen-1pop{k}.ch{j} = pop_rte(k, pop_brk(k,j-1)+1:pop_brk(k,j));endpop{k}.ch{salesmen} = pop_rte(k, pop_brk(k,end)+1:n);
end
% ----=== TARNSFORMATION --> multiple chromosome [END] ===----penalty_rate = 100;
start_time = cputime; % get actual time for performance measure
for iter = 1:num_iter% Evaluate Members of the Populationfor p = 1:pop_sized = 0;for s = 1:length(pop{p}.ch)sman = pop{p}.ch{s};d2 = 0;if ~isempty(sman)d2 = d2 + dmat(1,sman(1)) + tw; % Add Start Distancefor k = 1:length(sman)-1d2 = d2 + dmat(sman(k),sman(k+1)) + tw;endd2 = d2 + dmat(sman(end),1); % Add End Distanceif (d2 > max_tour)d2 = d2 + (d2 - max_tour) * penalty_rate;endendd = d + d2;endtotal_dist(p) = d;end% Find the Best Route in the Population[min_dist,index] = min(total_dist);dist_history(iter) = min_dist;if min_dist < global_minglobal_min = min_dist; % the optimal solution so faropt_rte = pop{index}; % the best solution so faropt_time = cputime - start_time; % compute the elapsed timeopt_iter = iter; % store the iteration numbersalesmen = sum(cellfun(@(x) length(x), opt_rte.ch) > 0);if show_prog% Plot the Best Routefigure(pfig);for s = 1:salesmenrte = [1 opt_rte.ch{s} 1];if dims == 3, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));elseplot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:));endtitle(sprintf('Total Distance = %1.4f, Iteration = %d',min_dist,iter));hold onendif dims == 3,plot3(xy(1,1),xy(1,2),xy(1,3),'ko');elseplot(xy(1,1),xy(1,2),'ko'); endhold offendend
五、运行结果
六、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016.
[2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.
【MTSP】基于matlab遗传算法求解多旅行商问题【含Matlab源码 1338期】相关推荐
- 【GA MTSP】基于matlab遗传算法求解多旅行商问题(多且同始终点)【含Matlab源码 1339期】
一.获取代码方式 获取代码方式1: 通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码. 获取代码方式2: 完整代码已上传我的资源:[MTSP]基于matlab遗传算法求解多旅行商问题[ ...
- 【Matlab验证码识别】遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别【含GUI源码 1694期】
一.代码运行视频(哔哩哔哩) [Matlab验证码识别]遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别[含GUI源码 1694期] 二.matlab版本及参考文献 1 matlab ...
- 【Matlab路径规划】改进的遗传算法机器人避障路径规划【含GUI源码 703期】
一.代码运行视频(哔哩哔哩) [Matlab路径规划]改进的遗传算法机器人避障路径规划[含GUI源码 703期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] ...
- 【Matlab生物电信号】生物电信号仿真【含GUI源码 684期】
一.代码运行视频(哔哩哔哩) [Matlab生物电信号]生物电信号仿真[含GUI源码 684期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]董兵,超于毅,李 ...
- 【Matlab语音分析】语音信号分析【含GUI源码 1718期】
一.代码运行视频(哔哩哔哩) [Matlab语音分析]语音信号分析[含GUI源码 1718期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]韩纪庆,张磊,郑铁 ...
- 【Matlab人脸识别】BP神经网络人脸识别(含识别率)【含GUI源码 891期】
一.代码运行视频(哔哩哔哩) [Matlab人脸识别]BP神经网络人脸识别(含识别率)[含GUI源码 891期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] ...
- 【Matlab人脸识别】形态学教室人数统计(带面板)【含GUI源码 1703期】
一.代码运行视频(哔哩哔哩) [Matlab人脸识别]形态学教室人数统计(带面板)[含GUI源码 1703期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]孟 ...
- 【Matlab人脸识别】人脸实时检测与跟踪【含GUI源码 673期】
一.代码运行视频(哔哩哔哩) [Matlab人脸识别]人脸实时检测与跟踪[含GUI源码 673期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]孟逸凡,柳益君 ...
- 【Matlab图像融合】小波变换遥感图像融合【含GUI源码 744期】
一.代码运行视频(哔哩哔哩) [Matlab图像融合]小波变换遥感图像融合[含GUI源码 744期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 包子阳,余 ...
- 【Matlab语音加密】语音信号加密解密(带面板)【含GUI源码 181期】
一.代码运行视频(哔哩哔哩) [Matlab语音加密]语音信号加密解密(带面板)[含GUI源码 181期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]韩纪庆 ...
最新文章
- Android 访问WebService
- 转图像偏微分方程不适定问题
- HDU 6015 Colorful Tree(2017多校)
- 作业二/Git的安装以及使用
- UE3 使用光源函数
- Docker Windows 安装
- 6 个步骤,搞定 AI 车牌识别器!(附详细分析)
- 基于jQuery8款超赞的评分插件
- java学习笔记——IO流
- 网线百兆与千兆的接法
- 如何使用deeptools处理BAM数据
- 微服务实施笔记(一)
- 利用Kalibr标定Camera-IMU外参
- JS实现国家省市三级无刷新联动
- 基于Python命令行的NBA文字直播小工具
- 《C++ Primer》习题参考答案:第6章 - C++模块设计——函数
- 奔跑吧ansible.pdf 免费下载
- php 如何计算上网流量,手机流量是怎么计算上网流量的?
- MTK WIFImac地址
- 永续合约_杠杆合约_合约交易基础知识