多旅行商问题的matlab程序
旅行商问题matlab源代码

function [pTS,fmin]=grTravSale(C)% Function [pTS,fmin]=grTravSale(C) solve the nonsymmetrical% traveling salesman problem.% Input parameter:% C(n,n) - matrix of distances between cities,% maybe, nonsymmetrical;% n - number of cities.% Output parameters:% pTS(n) - the order of cities;% fmin - length of way.% Uses the reduction to integer LP-problem:% Look: Miller C.E., Tucker A. W., Zemlin R. A.% Integer Programming Formulation of Traveling Salesman Problems. % J.ACM, 1960, Vol.7, p. 326-329.% Needed other products: MIQP.M.% This software may be free downloaded from site:% http://control.ee.ethz.ch/~hybrid/miqp/% Author: Sergiy Iglin% e-mail: siglin@yandex.ru% personal page: http://iglin.exponenta.ru% ============= Input data validation ==================if nargin<1,error('There are no input data!')endif ~isnumeric(C),error('The array C must be numeric!')endif ~isreal(C),error('The array C must be real!')ends=size(C); % size of array Cif length(s)~=2,error('The array C must be 2D!')endif s(1)~=s(2),error('Matrix C must be square!')endif s(1)<3,error('Must be not less than 3 cities!')end% ============ Size of problem ====================n=s(1); % number of vertexesm=n*(n-1); % number of arrows% ============ Parameters of integer LP problem ========Aeq=[]; % for the matrix of the boundary equationsfor k1=1:n,z1=zeros(n);z1(k1,:)=1;z2=[z1;eye(n)];Aeq=[Aeq z2([1:2*n-1],setdiff([1:n],k1))];endAeq=[Aeq zeros(2*n-1,n-1)];A=[]; % for the matrix of the boundary inequationsfor k1=2:n,z1=[];for k2=1:n,z2=eye(n)*(n-1)*(k2==k1);z1=[z1 z2(setdiff([2:n],k1),setdiff([1:n],k2))];endz2=-eye(n);z2(:,k1)=z2(:,k1)+1;A=[A;[z1 z2(setdiff([2:n],k1),2:n)]];endbeq=ones(2*n-1,1); % the right parts of the boundary equations b=ones((n-1)*(n-2),1)*(n-2); % the right parts of the boundary inequationsC1=C'+diag(ones(1,n)*NaN);C2=C1(:);c=[C2(~isnan(C2));zeros(n-1,1)]; % the factors for objective functionvlb=[zeros(m,1);-inf*ones(n-1,1)]; % the lower boundsvub=[ones(m,1);inf*ones(n-1,1)]; % the upper boundsH=zeros(n^2-1); % Hessian% ============= We solve the MIQP problem ========== [xmin,fmin]=MIQP(H,c,A,b,Aeq,beq,[1:m],vlb,vub);% ============= We return the results ==============eik=round(xmin(1:m)); % the arrows of the waye1=[zeros(1,n-1);reshape(eik,n,n-1)];e2=[e1(:);0]; % we add zero to a diagonale3=(reshape(e2,n,n))'; % the matrix of the waypTS=[1 find(e3(1,:))]; % we build the waywhile pTS(end)>1, % we add the city to the waypTS=[pTS find(e3(pTS(end),:))];endreturn。
多旅行商问题的matlab程序

多旅行商问题的m a t l a b程序Document serial number【NL89WT-NY98YT-NC8CB-NNUUT-NUT108】%多旅行商问题的m a t l a b程序function varargout =mtspf_ga(xy,dmat,salesmen,min_tour,pop_size,num_iter,show_prog,show_r es)% 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 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 thefirst% point, but travels to a unique set of cities in between% 2. Except for the first, 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) 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 iterations 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 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 together, these represent the solution [1 5 6 9 1][1 4 2 8 1][1 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 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:% 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, mtspofs_ga, mtspv_ga, distmat%% Author: Joseph Kirk% Email% Release:% Release Date: 6/2/09% Process Inputs and Initialize Defaultsnargs = 8;for k = nargin:nargs-1switch kcase 0xy = 10*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 = 5;case 3min_tour = 2;case 4pop_size = 80;case 5num_iter = 5e3;case 6show_prog = 1;case 7show_res = 1;otherwiseendend% Verify Inputs[N,dims] = size(xy);[nr,nc] = size(dmat);if N ~= nr || N ~= ncerror('Invalid XY or DMAT inputs!')endn = N - 1; % Separate Start/End City% Sanity Checkssalesmen = 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 Selectionnum_brks = salesmen-1;dof = n - min_tour*salesmen; % degrees of freedomaddto = ones(1,dof+1);for k = 2:num_brksaddto = 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); % population of breaksfor k = 1:pop_sizepop_rte(k,:) = randperm(n)+1;pop_brk(k,:) = randbreaks();end% Select the Colors for the Plotted Routesclr = [1 0 0; 0 0 1; 0 1; 0 1 0; 1 0];if salesmen > 5clr = hsv(salesmen);end% Run the GAglobal_min = Inf;total_dist = zeros(1,pop_size);dist_history = zeros(1,num_iter);tmp_pop_rte = zeros(8,n);tmp_pop_brk = zeros(8,num_brks);new_pop_rte = zeros(pop_size,n);new_pop_brk = zeros(pop_size,num_brks);if show_progpfig = figure('Name','MTSPF_GA | Current BestSolution','Numbertitle','off');endfor iter = 1:num_iter% Evaluate Members of the Populationfor p = 1:pop_sized = 0;p_rte = pop_rte(p,:);p_brk = pop_brk(p,:);rng = [[1 p_brk+1];[p_brk n]]';for s = 1:salesmend = d + dmat(1,p_rte(rng(s,1))); % Add Start Distancefor k = rng(s,1):rng(s,2)-1d = d + dmat(p_rte(k),p_rte(k+1));endd = d + dmat(p_rte(rng(s,2)),1); % Add End Distanceendtotal_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;opt_rte = pop_rte(index,:);opt_brk = pop_brk(index,:);rng = [[1 opt_brk+1];[opt_brk n]]';if show_prog% Plot the Best Routefigure(pfig);for s = 1:salesmenrte = [1 opt_rte(rng(s,1):rng(s,2)) 1];if dims == 3, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));else plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:)); endtitle(sprintf('Total Distance = %, Iteration= %d',min_dist,iter));hold onendif dims == 3, plot3(xy(1,1),xy(1,2),xy(1,3),'ko');else plot(xy(1,1),xy(1,2),'ko'); endhold offendend% Genetic Algorithm Operatorsrand_grouping = randperm(pop_size);for p = 8:8:pop_sizertes = pop_rte(rand_grouping(p-7:p),:);brks = pop_brk(rand_grouping(p-7:p),:);dists = total_dist(rand_grouping(p-7:p));[ignore,idx] = min(dists);best_of_8_rte = rtes(idx,:);best_of_8_brk = brks(idx,:);rte_ins_pts = sort(ceil(n*rand(1,2)));I = rte_ins_pts(1);J = rte_ins_pts(2);for k = 1:8 % Generate New Solutionstmp_pop_rte(k,:) = best_of_8_rte;tmp_pop_brk(k,:) = best_of_8_brk;switch kcase 2 % Fliptmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J)); case 3 % Swaptmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);case 4 % Slidetmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]); case 5 % Modify Breakstmp_pop_brk(k,:) = randbreaks();case 6 % Flip, Modify Breakstmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J)); tmp_pop_brk(k,:) = randbreaks();case 7 % Swap, Modify Breakstmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);tmp_pop_brk(k,:) = randbreaks();case 8 % Slide, Modify Breakstmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]); tmp_pop_brk(k,:) = randbreaks();otherwise % Do Nothingendendnew_pop_rte(p-7:p,:) = tmp_pop_rte;new_pop_brk(p-7:p,:) = tmp_pop_brk;endpop_rte = new_pop_rte;pop_brk = new_pop_brk;endif show_res% Plotsfigure('Name','MTSPF_GA | Results','Numbertitle','off');subplot(2,2,1);if dims == 3, plot3(xy(:,1),xy(:,2),xy(:,3),'k.');else plot(xy(:,1),xy(:,2),'k.'); endtitle('City Locations');subplot(2,2,2);imagesc(dmat([1 opt_rte],[1 opt_rte]));title('Distance Matrix');subplot(2,2,3);rng = [[1 opt_brk+1];[opt_brk n]]';for s = 1:salesmenrte = [1 opt_rte(rng(s,1):rng(s,2)) 1];if dims == 3, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));else plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:)); endtitle(sprintf('Total Distance = %',min_dist));hold on;endif dims == 3, plot3(xy(1,1),xy(1,2),xy(1,3),'ko');else plot(xy(1,1),xy(1,2),'ko'); endsubplot(2,2,4);plot(dist_history,'b','LineWidth',2);title('Best Solution History');set(gca,'XLim',[0 num_iter+1],'YLim',[0 *max([1 dist_history])]); end% Return Outputsif nargoutvarargout{1} = opt_rte;varargout{2} = opt_brk;varargout{3} = min_dist;end% Generate Random Set of Break Pointsfunction breaks = randbreaks()if min_tour == 1 % No Constraints on Breakstmp_brks = randperm(n-1);breaks = sort(tmp_brks(1:num_brks));else % Force Breaks to be at Least the Minimum Tour Lengthnum_adjust = find(rand < cum_prob,1)-1;spaces = ceil(num_brks*rand(1,num_adjust));adjust = zeros(1,num_brks);for kk = 1:num_brksadjust(kk) = sum(spaces == kk);endbreaks = min_tour*(1:num_brks) + cumsum(adjust); endendend。
TSP旅行商问题matlab代码

if n==50
city50=[31 32;32 39;40 30;37 69;27 68;37 52;38 46;31 62;30 48;21 47;25 55;16 57;
17 63;42 41;17 33;25 32;5 64;8 52;12 42;7 38;5 25; 10 77;45 35;42 57;32 22;
end
s=smnew; %产生了新的种群
[f,p]=objf(s,dislist); %计算新种群的适应度
%记录当前代最好和平均的适应度
[fmax,nmax]=max(f);
ymean(gn)=1000/mean(f);
ymax(gn)=1000/fmax;
end
seln(i)=j; %选中个体的序号
if i==2&&j==seln(i-1) %%若相同就再选一次
r=rand; %产生一个随机数
prand=p-r;
j=1;
while prand(j)<0
j=j+1;
ymean=zeros(gn,1);
ymax=zeros(gn,1);
xmax=zeros(inn,CityNum);
scnew=zeros(inn,CityNum);
smnew=zeros(inn,CityNum);
while gn<gnmax+1
for j=1:2:inn
seln=sel(p); %选择操作
zhi=logical(scro(1,1:chb2)==scro(1,i));
y=scro(2,zhi);
scro(1,i)=y;
运输多染色体遗传的多旅行商问题matlab程序代码

运输多染色体遗传的多旅行商问题matlab程序代码篇一:多染色体遗传的多旅行商问题(Multi-迁徙 Problem in Multi-染色体 Gene Expression System)是一个复杂的生物信息学问题,通常需要通过数学建模来解决。
在这个问题中,个体在生殖过程中需要从不同的地区携带不同的基因型进行迁徙,以期望得到最大的基因型多样性。
在多旅行商问题中,每个个体需要在不同的染色体上携带不同的基因型,并且每个染色体上的基因型数量不同。
此外,每个地区需要适应不同的环境,因此每个地区的基因型变异率也有所不同。
因此,每个个体携带的基因型组合必须适应这个地区的环境,并且尽可能地增加个体的基因型多样性。
为了解决这个问题,可以使用数学建模的方法,将多旅行商问题转化为一个优化问题。
在这个优化问题中,个体的基因型多样性可以通过最大化一个函数来确定。
这个函数通常是一个组合优化问题,需要考虑到每个地区适应不同的环境以及每个染色体上的基因型数量的影响。
下面是一个用MATLAB求解多旅行商问题的示例代码,该代码使用了遗传算法来优化基因型多样性:```matlab% 多旅行商问题示例代码% 假设有n个染色体,每个染色体上有多个基因型% 定义基因型组合优化问题的目标函数function z = objective(a, b, c)z = a * (1 - b) * (1 - c) - b * a * c - a * (1 - b) * (1 - c) - c *a *b - b *c * a - c * b * c;end% 定义遗传算法的类classdef Multi迁徙Prob < handleversion 3.0% 定义个体的构造函数function this = Multi迁徙Prob(n, m) this.n = n;this.m = m;% 初始化种群if this.n < 2this.p = [1 1; 1 1];elsethis.p = randperm(this.n);end% 初始化随机变异源this.s = rand(this.n, 1);% 初始化个体this.i = 0;this.j = 0;this.k = 0;% 定义状态转移函数function this.next_state = stateif this.i == this.j && this.k == 0state = this.p;elsestate = rand();endend% 定义适应函数function this.适应 =适应(state)% 计算适应度if this.n == 2适应度 = [0.5 * this.s(1) * this.s(2); 0.5 * this.s(1) * this.s(2)]; else适应度 = [适应度; 0];end% 计算适应度矩阵this.a = [state(1); state(2)];this.b = [适应度; 0];this.c = [适应度; 0];% 计算适应度向量this.a = 适应度 * this.a;this.b = 适应度 * this.b;this.c = 适应度 * this.c;end% 初始化遗传算法的主循环this.m = 1;this.p = this.s;this.i = 0;this.j = 0;this.k = 0;% 计算初始种群中个体的适应度this.a = [this.a; this.b; this.c]; this.p[this.i] = this.a;this.p[this.j] = this.a;this.p[this.k] = this.a;% 迭代种群中的个体for this.l = 1:this.m% 随机选择一个个体this.l = randperm(this.n);% 计算当前个体的适应度this.a = [this.a; this.b; this.c];this.p[this.i + 1] = this.a;this.p[this.j + 1] = this.a;this.p[this.k + 1] = this.a;% 计算当前个体的基因型this.i = this.i + 1;this.j = this.j + 1;this.k = this.k + 1;this.j = j;this.k = k;% 计算当前个体的适应度向量this.b = [适应度; this.s(j) * this.s(k)];this.c = [适应度; this.s(j) * this.s(k)]; end% 计算种群中个体的适应度this.a = [this.a; this.b; this.c];this.p = this.p + this.a;% 计算适应度矩阵this.a = 适应度 * this.a;this.b = 适应度 * this.b;this.c = 适应度 * this.c;% 计算种群中个体的基因型this.i = this.i + 1;this.j = this.j + 1;this.k = this.k + 1;this.j = j;this.k = k;% 计算种群中个体的适应度向量this.a = [适应度; this.s(j) * this.s(k)]; this.p = this.p + this.a;% 输出最终适应度this.a = [适应度; this.s(j) * this.s(k)];% 输出最终基因型多样性z = objective(this.p, this.a, this.b, this.c);endend```该代码使用遗传算法来解决多旅行商问题,通过优化个体的基因型多样性来实现最大的基因型多样性。
蚁群算法TSP(旅行商问题)通用matlab程序

蚁群算法TSP(旅行商问题)通用matlab程序function [R_best,L_best,L_ave,Shortest_Route,Shortest_Length]=ACAT SP(C,NC_max,m,Alpha,Beta,Rho,Q)%%================================================= ========================%% ACATSP.m%% Ant Colony Algorithm for Traveling Salesman Problem%%shown wang%% Email:mycareer@%%-------------------------------------------------------------------------%% 主要符号说明%% C n个城市的坐标,n×2的矩阵%% NC_max 最大迭代次数%% m 蚂蚁个数%% Alpha 表征信息素重要程度的参数%% Beta 表征启发式因子重要程度的参数%% Rho 信息素蒸发系数%% Q 信息素增加强度系数%% R_best 各代(步)最佳路线%% L_best 各代(步)最佳路线的长度%%================================================= ========================%%第一步:变量初始化n=size(C,1); %n表示问题的规模(城市个数)n个城市Size 矩阵的尺寸D=zeros(n,n); %D表示完全图的赋权邻接矩阵Zeros 零矩阵for i=1:nfor j=1:nif i~=jD(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5;elseD(i,j)=eps;Eps 相对浮点精度endD(j,i)=D(i,j);endendEta=1./D; %Eta为启发因子,这里设为距离的倒数Tau=ones(n,n); %Tau为信息素矩阵Ones 全“1”矩阵Tabu=zeros(m,n); %存储并记录路径的生成:用以记录蚂蚁m当前所走过的城市NC=1; %迭代计数器,迭代步数或搜索次数R_best=zeros(NC_max,n); %各代(步)最佳路线L_best=inf.*ones(NC_max,1); %各代(步)最佳路线的长度Inf 无穷大Ones 全“1”矩阵L_ave=zeros(NC_max,1); %各代(步)路线的平均长度while NC<=NC_max %停止条件之一:达到最大迭代次数%%第二步:将m只蚂蚁放到n个城市上Randpos=[ ];for i=1:(ceil(m/n)) %Ceil 朝正无穷大方向取整Randpos=[Randpos,randperm(n)];endTabu(:,1)=(Randpos(1,1:m))';%%第三步:m只蚂蚁按概率函数选择下一座城市,完成各自的周游for j=2:nfor i=1:mvisited=Tabu(i,1:(j-1)); %已访问的城市J=zeros(1,(n-j+1)); %待访问的城市P=J; %待访问城市的选择概率分布Jc=1;for k=1:nif length(find(visited==k))==0J(Jc)=k;Jc=Jc+1;endend%下面计算待选城市的概率分布for k=1:length(J)P(k)=(Tau(visited(end),J(k))^Alpha)*(Eta(visited(end),J(k))^Beta); endP=P/(sum(P));%按概率原则选取下一个城市Pcum=cumsum(P);Select=find(Pcum>=rand);to_visit=J(Select(1));Tabu(i,j)=to_visit;endendif NC>=2Tabu(1,:)=R_best(NC-1,:);end%%第四步:记录本次迭代最佳路线L=zeros(m,1);for i=1:mR=Tabu(i,:);for j=1:(n-1)L(i)=L(i)+D(R(j),R(j+1));endL(i)=L(i)+D(R(1),R(n));endL_best(NC)=min(L);pos=find(L==L_best(NC));R_best(NC,:)=Tabu(pos(1),:);L_ave(NC)=mean(L);NC=NC+1%%第五步:更新信息素Delta_Tau=zeros(n,n);for i=1:mfor j=1:(n-1)Delta_Tau(Tabu(i,j),Tabu(i,j+1))=Delta_Tau(Tabu(i,j),Tabu(i,j+1))+Q/L(i );endDelta_Tau(Tabu(i,n),Tabu(i,1))=Delta_Tau(Tabu(i,n),Tabu(i,1))+Q/L(i); endTau=(1-Rho).*Tau+Delta_Tau;%%第六步:禁忌表清零Tabu=zeros(m,n);end%%第七步:输出结果Pos=find(L_best==min(L_best));Shortest_Route=R_best(Pos(1),:)Shortest_Length=L_best(Pos(1))subplot(1,2,1)DrawRoute(C,Shortest_Route)subplot(1,2,2)plot(L_best)hold onplot(L_ave)function DrawRoute(C,R)%%================================================= ========================%% DrawRoute.m%% 画路线图的子函数%%-------------------------------------------------------------------------%% C Coordinate 节点坐标,由一个N×2的矩阵存储%% R Route 路线%%================================================= ========================N=length(R);scatter(C(:,1),C(:,2));hold onplot([C(R(1),1),C(R(N),1)],[C(R(1),2),C(R(N),2)])hold onfor ii=2:Nplot([C(R(ii-1),1),C(R(ii),1)],[C(R(ii-1),2),C(R(ii),2)]) hold onend设置初始参数如下:m=31;Alpha=1;Beta=5;Rho=0.1;NC_max=200;Q=100; 31城市坐标为:1304 23123639 13154177 22443712 13993488 15353326 15563238 12294196 10044312 7904386 5703007 19702562 17562788 14912381 16761332 6953715 16783918 21794061 23703780 22123676 25784029 28384263 29313429 19083507 23673394 26433439 32012935 32403140 35502545 23572778 28262370 2975运行后得到15602的巡游路径,路线图和收敛曲线如下:。
多旅行商问题遗传算法

function varargout = mtspf_ga(dmat,salesmen,min_tour,pop_size,num_iter,show_prog,show_res) %dmat 任意两城市间的最短路径矩阵通过floyed算法求得结果。
%salesmen 旅行商个数%min_tour 每个旅行商最少访问的城市数%pop_size 种群个体数%num_iter 迭代的代数%show_prog,show_res 显示的参数设定nargs = 7; %处理输入参数,用来给定一些默认的参数;for k = nargin:nargs-1switch kcase 0dmat = 10*rand(20,20);case 1salesmen = 5;case 2min_tour = 2;case 3pop_size = 80;case 4num_iter = 5e3;case 5show_prog = 1;case 6show_res = 1;otherwiseendend% 检查输入矩阵[nr,nc] = size(dmat);if nr ~= ncerror('Invalid XY or DMA T inputs!')endn = nr - 1; % 除去起始的城市后剩余的城市的数% 输入参数的检查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));% 初始化路线、断点的选择num_brks = salesmen-1;dof = n - min_tour*salesmen; % 可以自由访问的城市数addto = ones(1,dof+1);for k = 2:num_brksaddto = cumsum(addto);endcum_prob = cumsum(addto)/sum(addto);% 初始化种群pop_rte = zeros(pop_size,n); % 路径集合的种群pop_brk = zeros(pop_size,num_brks); % 断点集合的种群for k = 1:pop_sizepop_rte(k,:) = randperm(n)+1;pop_brk(k,:) = randbreaks();end% 选择绘图时的个商人的颜色可删去;clr = [1 0 0; 0 0 1; 0.67 0 1; 0 1 0; 1 0.5 0];if salesmen > 5clr = hsv(salesmen);end% 开始运行遗传算法过程global_min = Inf; %初始化最短路径total_dist = zeros(1,pop_size);dist_history = zeros(1,num_iter);tmp_pop_rte = zeros(8,n); %当前的路径设置tmp_pop_brk = zeros(8,num_brks); %当前的断点设置new_pop_rte = zeros(pop_size,n); %更新的路径设置new_pop_brk = zeros(pop_size,num_brks);%更新的断点设置if show_progpfig = figure('Name','MTSPF_GA | Current Best Solution','Numbertitle','off'); endfor iter = 1:num_iter% 评价每一代的种群适应情况并作出选择。
基于遗传算法解决旅行商问题的MATLAB程序

问题:已知n个城市之间的相互距离,现有一个推销员必须遍访这n个城市,并且每个城市只能访问一次,最后又必须返回出发城市。
如何安排他对这些城市的访问次序,可使其旅行路线的总长度最短?分析:用离散数学或图论的术语来说,假设有一个图g=(v,e),其中v是顶点集,e 是边集,设d=(dij)是由顶点i和顶点j之间的距离所组成的距离矩阵,旅行商问题就是求出一条通过所有顶点且每个顶点只通过一次的具有最短距离的回路。
这个问题可分为对称旅行商问题(dij=dji,,任意i,j=1,2,3,…,n)和非对称旅行商问题(dij≠dji,,任意i,j=1,2,3,…,n)。
若对于城市v={v1,v2,v3,…,vn}的一个访问顺序为t=(t1,t2,t3,…,ti,…,tn),其中ti∈v(i=1,2,3,…,n),且记tn+1= t1,则旅行商问题的数学模型为:min l=σd(t(i),t(i+1)) (i=1,…,n)旅行商问题是一个典型的组合优化问题,并且是一个np难问题,其可能的路径数目与城市数目n是成指数型增长的,所以一般很难精确地求出其最优解,本文采用遗传算法求其近似解。
遗传算法:初始化过程:用v1,v2,v3,…,vn代表所选n个城市。
定义整数pop-size作为染色体的个数,并且随机产生pop-size个初始染色体,每个染色体为1到18的整数组成的随机序列。
适应度f的计算:对种群中的每个染色体vi,计算其适应度,f=σd(t(i),t(i+1)).评价函数eval(vi):用来对种群中的每个染色体vi设定一个概率,以使该染色体被选中的可能性与其种群中其它染色体的适应性成比例,既通过轮盘赌,适应性强的染色体被选择产生后台的机会要大,设alpha∈(0,1),本文定义基于序的评价函数为eval(vi)=alpha*(1-alpha).^(i-1) 。
[随机规划与模糊规划]选择过程:选择过程是以旋转赌轮pop-size次为基础,每次旋转都为新的种群选择一个染色体。
多旅行商问题的matlab程序

%多旅行商问题的matlab程序function varargout = mtspf_gaxy,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 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 first, 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 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 iterations 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 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 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 to 7 and back to 1%% 2D Example:% n = 35;% xy = 10randn,2;% salesmen = 5;% min_tour = 3;% pop_size = 80;% num_iter = 5e3;% a = meshgrid1:n;% dmat = reshapesqrtsumxya,:-xya',:.^2,2,n,n;% opt_rte,opt_brk,min_dist = mtspf_gaxy,dmat,salesmen,min_tour, ... % pop_size,num_iter,1,1;%% 3D Example:% n = 35;% xyz = 10randn,3;% salesmen = 5;% min_tour = 3;% pop_size = 80;% num_iter = 5e3;% a = meshgrid1:n;% dmat = reshapesqrtsumxyza,:-xyza',:.^2,2,n,n;% opt_rte,opt_brk,min_dist = mtspf_gaxyz,dmat,salesmen,min_tour, ... % pop_size,num_iter,1,1;%% See also: mtsp_ga, mtspo_ga, mtspof_ga, mtspofs_ga, mtspv_ga, distmat %% Author: Joseph Kirk% Email: jdkirk630gmail% Release: 1.3% Release Date: 6/2/09% Process Inputs and Initialize Defaultsnargs = 8;for k = nargin:nargs-1switch kcase 0xy = 10rand40,2;case 1N = sizexy,1;a = meshgrid1:N;dmat = reshapesqrtsumxya,:-xya',:.^2,2,N,N;case 2salesmen = 5;case 3min_tour = 2;case 4pop_size = 80;case 5num_iter = 5e3;case 6show_prog = 1;case 7show_res = 1;otherwiseendend% Verify InputsN,dims = sizexy;nr,nc = sizedmat;if N ~= nr || N ~= ncerror'Invalid XY or DMAT inputs'endn = N - 1; % Separate Start/End City% Sanity Checkssalesmen = max1,minn,roundrealsalesmen1;min_tour = max1,minfloorn/salesmen,roundrealmin_tour1; pop_size = max8,8ceilpop_size1/8;num_iter = max1,roundrealnum_iter1;show_prog = logicalshow_prog1;show_res = logicalshow_res1;% Initializations for Route Break Point Selectionnum_brks = salesmen-1;dof = n - min_toursalesmen; % degrees of freedom addto = ones1,dof+1;for k = 2:num_brksaddto = cumsumaddto;endcum_prob = cumsumaddto/sumaddto;% Initialize the Populationspop_rte = zerospop_size,n; % population of routes pop_brk = zerospop_size,num_brks; % population of breaks for k = 1:pop_sizepop_rtek,: = randpermn+1;pop_brkk,: = 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 > 5clr = hsvsalesmen;end% Run the GAglobal_min = Inf;total_dist = zeros1,pop_size;dist_history = zeros1,num_iter;tmp_pop_rte = zeros8,n;tmp_pop_brk = zeros8,num_brks;new_pop_rte = zerospop_size,n;new_pop_brk = zerospop_size,num_brks;if show_progpfig = figure'Name','MTSPF_GA | Current Best Solution','Numbertitle','off'; endfor iter = 1:num_iter% Evaluate Members of the Populationfor p = 1:pop_sized = 0;p_rte = pop_rtep,:;p_brk = pop_brkp,:;rng = 1 p_brk+1;p_brk n';for s = 1:salesmend = d + dmat1,p_rterngs,1; % Add Start Distancefor k = rngs,1:rngs,2-1d = d + dmatp_rtek,p_rtek+1;endd = d + dmatp_rterngs,2,1; % Add End Distanceendtotal_distp = d;end% Find the Best Route in the Populationmin_dist,index = mintotal_dist;dist_historyiter = min_dist;if min_dist < global_minglobal_min = min_dist;opt_rte = pop_rteindex,:;opt_brk = pop_brkindex,:;rng = 1 opt_brk+1;opt_brk n';if show_prog% Plot the Best Routefigurepfig;for s = 1:salesmenrte = 1 opt_rterngs,1:rngs,2 1;if dims == 3, plot3xyrte,1,xyrte,2,xyrte,3,'.-','Color',clrs,:;else plotxyrte,1,xyrte,2,'.-','Color',clrs,:; endtitlesprintf'Total Distance = %1.4f, Iteration = %d',min_dist,iter;hold onendif dims == 3, plot3xy1,1,xy1,2,xy1,3,'ko';else plotxy1,1,xy1,2,'ko'; endhold offendend% Genetic Algorithm Operatorsrand_grouping = randpermpop_size;for p = 8:8:pop_sizertes = pop_rterand_groupingp-7:p,:;brks = pop_brkrand_groupingp-7:p,:;dists = total_distrand_groupingp-7:p;ignore,idx = mindists;best_of_8_rte = rtesidx,:;best_of_8_brk = brksidx,:;rte_ins_pts = sortceilnrand1,2;I = rte_ins_pts1;J = rte_ins_pts2;for k = 1:8 % Generate New Solutionstmp_pop_rtek,: = best_of_8_rte;tmp_pop_brkk,: = best_of_8_brk;switch kcase 2 % Fliptmp_pop_rtek,I:J = fliplrtmp_pop_rtek,I:J;case 3 % Swaptmp_pop_rtek,I J = tmp_pop_rtek,J I;case 4 % Slidetmp_pop_rtek,I:J = tmp_pop_rtek,I+1:J I;case 5 % Modify Breakstmp_pop_brkk,: = randbreaks;case 6 % Flip, Modify Breakstmp_pop_rtek,I:J = fliplrtmp_pop_rtek,I:J;tmp_pop_brkk,: = randbreaks;case 7 % Swap, Modify Breakstmp_pop_rtek,I J = tmp_pop_rtek,J I;tmp_pop_brkk,: = randbreaks;case 8 % Slide, Modify Breakstmp_pop_rtek,I:J = tmp_pop_rtek,I+1:J I;tmp_pop_brkk,: = randbreaks;otherwise % Do Nothingendendnew_pop_rtep-7:p,: = tmp_pop_rte;new_pop_brkp-7:p,: = tmp_pop_brk;endpop_rte = new_pop_rte;pop_brk = new_pop_brk;endif show_res% Plotsfigure'Name','MTSPF_GA | Results','Numbertitle','off';subplot2,2,1;if dims == 3, plot3xy:,1,xy:,2,xy:,3,'k.';else plotxy:,1,xy:,2,'k.'; endtitle'City Locations';subplot2,2,2;imagescdmat1 opt_rte,1 opt_rte;title'Distance Matrix';subplot2,2,3;rng = 1 opt_brk+1;opt_brk n';for s = 1:salesmenrte = 1 opt_rterngs,1:rngs,2 1;if dims == 3, plot3xyrte,1,xyrte,2,xyrte,3,'.-','Color',clrs,:;else plotxyrte,1,xyrte,2,'.-','Color',clrs,:; endtitlesprintf'Total Distance = %1.4f',min_dist;hold on;endif dims == 3, plot3xy1,1,xy1,2,xy1,3,'ko';else plotxy1,1,xy1,2,'ko'; endsubplot2,2,4;plotdist_history,'b','LineWidth',2;title'Best Solution History';setgca,'XLim',0 num_iter+1,'YLim',0 1.1max1 dist_history; end% Return Outputsif nargoutvarargout{1} = opt_rte;varargout{2} = opt_brk;varargout{3} = min_dist;end% Generate Random Set of Break Pointsfunction breaks = randbreaksif min_tour == 1 % No Constraints on Breakstmp_brks = randpermn-1;breaks = sorttmp_brks1:num_brks;else % Force Breaks to be at Least the Minimum Tour Length num_adjust = findrand < cum_prob,1-1;spaces = ceilnum_brksrand1,num_adjust;adjust = zeros1,num_brks;for kk = 1:num_brksadjustkk = sumspaces == kk;endbreaks = min_tour1:num_brks + cumsumadjust;endendend。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
%多旅行商问题的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 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 first, 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) 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 iterations 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 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 together, these represent the solution [1 5 6 9 1][1 4 2 8 1][1 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 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:% 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, mtspofs_ga, mtspv_ga, distmat%% Author: Joseph Kirk% Email: jdkirk630@% Release: 1.3% Release Date: 6/2/09% Process Inputs and Initialize Defaultsnargs = 8;for k = nargin:nargs-1switch kcase 0xy = 10*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 = 5;case 3min_tour = 2;case 4pop_size = 80;case 5num_iter = 5e3;case 6show_prog = 1;case 7show_res = 1;otherwiseendend% Verify Inputs[N,dims] = size(xy);[nr,nc] = size(dmat);if N ~= nr || N ~= ncerror('Invalid XY or DMAT inputs!')endn = N - 1; % Separate Start/End City% Sanity Checkssalesmen = 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 Selectionnum_brks = salesmen-1;dof = n - min_tour*salesmen; % degrees of freedom addto = ones(1,dof+1);for k = 2:num_brksaddto = cumsum(addto);endcum_prob = cumsum(addto)/sum(addto);% Initialize the Populationspop_rte = zeros(pop_size,n); % population of routes pop_brk = zeros(pop_size,num_brks); % population of breaksfor k = 1:pop_sizepop_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 > 5clr = hsv(salesmen);end% Run the GAglobal_min = Inf;total_dist = zeros(1,pop_size);dist_history = zeros(1,num_iter);tmp_pop_rte = zeros(8,n);tmp_pop_brk = zeros(8,num_brks);new_pop_rte = zeros(pop_size,n);new_pop_brk = zeros(pop_size,num_brks);if show_progpfig = figure('Name','MTSPF_GA | Current Best Solution','Numbertitle','off'); endfor iter = 1:num_iter% Evaluate Members of the Populationfor p = 1:pop_sized = 0;p_rte = pop_rte(p,:);p_brk = pop_brk(p,:);rng = [[1 p_brk+1];[p_brk n]]';for s = 1:salesmend = d + dmat(1,p_rte(rng(s,1))); % Add Start Distancefor k = rng(s,1):rng(s,2)-1d = d + dmat(p_rte(k),p_rte(k+1));endd = d + dmat(p_rte(rng(s,2)),1); % Add End Distanceendtotal_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;opt_rte = pop_rte(index,:);opt_brk = pop_brk(index,:);rng = [[1 opt_brk+1];[opt_brk n]]';if show_prog% Plot the Best Routefigure(pfig);for s = 1:salesmenrte = [1 opt_rte(rng(s,1):rng(s,2)) 1];if dims == 3, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));else plot(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');else plot(xy(1,1),xy(1,2),'ko'); endhold offendend% Genetic Algorithm Operatorsrand_grouping = randperm(pop_size);for p = 8:8:pop_sizertes = pop_rte(rand_grouping(p-7:p),:);brks = pop_brk(rand_grouping(p-7:p),:);dists = total_dist(rand_grouping(p-7:p));[ignore,idx] = min(dists);best_of_8_rte = rtes(idx,:);best_of_8_brk = brks(idx,:);rte_ins_pts = sort(ceil(n*rand(1,2)));I = rte_ins_pts(1);J = rte_ins_pts(2);for k = 1:8 % Generate New Solutionstmp_pop_rte(k,:) = best_of_8_rte;tmp_pop_brk(k,:) = best_of_8_brk;switch kcase 2 % Fliptmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));case 3 % Swaptmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);case 4 % Slidetmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);case 5 % Modify Breakstmp_pop_brk(k,:) = randbreaks();case 6 % Flip, Modify Breakstmp_pop_rte(k,I:J) = fliplr(tmp_pop_rte(k,I:J));tmp_pop_brk(k,:) = randbreaks();case 7 % Swap, Modify Breakstmp_pop_rte(k,[I J]) = tmp_pop_rte(k,[J I]);tmp_pop_brk(k,:) = randbreaks();case 8 % Slide, Modify Breakstmp_pop_rte(k,I:J) = tmp_pop_rte(k,[I+1:J I]);tmp_pop_brk(k,:) = randbreaks();otherwise % Do Nothingendendnew_pop_rte(p-7:p,:) = tmp_pop_rte;new_pop_brk(p-7:p,:) = tmp_pop_brk;endpop_rte = new_pop_rte;pop_brk = new_pop_brk;endif show_res% Plotsfigure('Name','MTSPF_GA | Results','Numbertitle','off');subplot(2,2,1);if dims == 3, plot3(xy(:,1),xy(:,2),xy(:,3),'k.');else plot(xy(:,1),xy(:,2),'k.'); endtitle('City Locations');subplot(2,2,2);imagesc(dmat([1 opt_rte],[1 opt_rte]));title('Distance Matrix');subplot(2,2,3);rng = [[1 opt_brk+1];[opt_brk n]]';for s = 1:salesmenrte = [1 opt_rte(rng(s,1):rng(s,2)) 1];if dims == 3, plot3(xy(rte,1),xy(rte,2),xy(rte,3),'.-','Color',clr(s,:));else plot(xy(rte,1),xy(rte,2),'.-','Color',clr(s,:)); endtitle(sprintf('Total Distance = %1.4f',min_dist));hold on;endif dims == 3, plot3(xy(1,1),xy(1,2),xy(1,3),'ko');else plot(xy(1,1),xy(1,2),'ko'); endsubplot(2,2,4);plot(dist_history,'b','LineWidth',2);title('Best Solution History');set(gca,'XLim',[0 num_iter+1],'YLim',[0 1.1*max([1 dist_history])]); end% Return Outputsif nargoutvarargout{1} = opt_rte;varargout{2} = opt_brk;varargout{3} = min_dist;end% Generate Random Set of Break Pointsfunction breaks = randbreaks()if min_tour == 1 % No Constraints on Breakstmp_brks = randperm(n-1);breaks = sort(tmp_brks(1:num_brks));else % Force Breaks to be at Least the Minimum Tour Length num_adjust = find(rand < cum_prob,1)-1;spaces = ceil(num_brks*rand(1,num_adjust));adjust = zeros(1,num_brks);for kk = 1:num_brksadjust(kk) = sum(spaces == kk);endbreaks = min_tour*(1:num_brks) + cumsum(adjust);endendend。