MATLAB多旅行商问题源代码

上传人:我*** 文档编号:135963842 上传时间:2020-06-21 格式:DOC 页数:8 大小:88KB
返回 下载 相关 举报
MATLAB多旅行商问题源代码_第1页
第1页 / 共8页
MATLAB多旅行商问题源代码_第2页
第2页 / 共8页
MATLAB多旅行商问题源代码_第3页
第3页 / 共8页
MATLAB多旅行商问题源代码_第4页
第4页 / 共8页
MATLAB多旅行商问题源代码_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《MATLAB多旅行商问题源代码》由会员分享,可在线阅读,更多相关《MATLAB多旅行商问题源代码(8页珍藏版)》请在金锄头文库上搜索。

1、MATLAB多旅行商问题源代码function varargout = mtspf_ga(xy,dmat,salesmen,min_tour,pop_size,num_iter,show_prog,show_res)% MTSPF_GA Fixed Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA)% Finds a (near) optimal solution to a variation of the M-TSP by setting% up a GA to search for the shortest

2、route (least distance needed for% each salesman to travel from the start location to individual cities% and back to the original starting place)% Summary:% 1. Each salesman starts at the first point, and ends at the first% point, but travels to a unique set of cities in between% 2. Except for the fi

3、rst, each city is visited by exactly one salesman% Note: The Fixed Start/End location is taken to be the first XY point% Input:% 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)

4、 is the number of salesmen to visit the cities% MIN_TOUR (scalar integer) is the minimum tour length for any of the% salesmen, NOT including the start/end point% POP_SIZE (scalar integer) is the size of the population (should be divisible by 8)% NUM_ITER (scalar integer) is the number of desired ite

5、rations for the algorithm to run% SHOW_PROG (scalar logical) shows the GA progress if true% SHOW_RES (scalar logical) shows the GA results if true% Output:% OPT_RTE (integer array) is the best route found by the algorithm% OPT_BRK (integer array) is the list of route break points (these specify the

6、indices% into the route used to obtain the individual salesman routes)% MIN_DIST (scalar float) is the total distance traveled by the salesmen% Route/Breakpoint Details:% If there are 10 cities and 3 salesmen, a possible route/break% combination might be: rte = 5 6 9 4 2 8 10 3 7, brks = 3 7% Taken

7、together, these represent the solution 1 5 6 9 11 4 2 8 11 10 3 7 1,% which designates the routes for the 3 salesmen as follows:% . Salesman 1 travels from city 1 to 5 to 6 to 9 and back to 1% . Salesman 2 travels from city 1 to 4 to 2 to 8 and back to 1% . Salesman 3 travels from city 1 to 10 to 3

8、to 7 and back to 1% 2D Example:% n = 35;% xy = 10*rand(n,2);% salesmen = 5;% min_tour = 3;% pop_size = 80;% num_iter = 5e3;% a = meshgrid(1:n);% dmat = reshape(sqrt(sum(xy(a,:)-xy(a,:).2,2),n,n);% opt_rte,opt_brk,min_dist = mtspf_ga(xy,dmat,salesmen,min_tour, .% pop_size,num_iter,1,1);% 3D Example:%

9、 n = 35;% xyz = 10*rand(n,3);% salesmen = 5;% min_tour = 3;% pop_size = 80;% num_iter = 5e3;% a = meshgrid(1:n);% dmat = reshape(sqrt(sum(xyz(a,:)-xyz(a,:).2,2),n,n);% opt_rte,opt_brk,min_dist = mtspf_ga(xyz,dmat,salesmen,min_tour, .% pop_size,num_iter,1,1);% See also: mtsp_ga, mtspo_ga, mtspof_ga,

10、mtspofs_ga, mtspv_ga, distmat% Author: Joseph Kirk% Email: % Release: 1.3% Release Date: 6/2/09% Process Inputs and Initialize Defaultsnargs = 8;for k = nargin:nargs-1 switch k case 0 xy = 10*rand(40,2); case 1 N = size(xy,1); a = meshgrid(1:N); dmat = reshape(sqrt(sum(xy(a,:)-xy(a,:).2,2),N,N); cas

11、e 2 salesmen = 5; case 3 min_tour = 2; case 4 pop_size = 80; case 5 num_iter = 5e3; case 6 show_prog = 1; case 7 show_res = 1; otherwise endend% Verify InputsN,dims = size(xy);nr,nc = size(dmat);if N = nr | N = nc error(Invalid XY or DMAT inputs!)endn = N - 1; % Separate Start/End City% Sanity Check

12、ssalesmen = 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 Selectio

13、nnum_brks = salesmen-1;dof = n - min_tour*salesmen; % degrees of freedomaddto = ones(1,dof+1);for k = 2:num_brks addto = cumsum(addto);endcum_prob = cumsum(addto)/sum(addto);% Initialize the Populationspop_rte = zeros(pop_size,n); % population of routespop_brk = zeros(pop_size,num_brks); % populatio

14、n of breaksfor k = 1:pop_size pop_rte(k,:) = randperm(n)+1; pop_brk(k,:) = randbreaks();end% Select the Colors for the Plotted Routesclr = 1 0 0; 0 0 1; 0.67 0 1; 0 1 0; 1 0.5 0;if salesmen 5 clr = hsv(salesmen);end% Run the GAglobal_min = Inf;total_dist = zeros(1,pop_size);dist_history = zeros(1,num

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号