Matlab 如何从组件旅行的无序列表中提取TSP旅行
以下是初始代码:Matlab 如何从组件旅行的无序列表中提取TSP旅行,matlab,combinatorics,discrete-mathematics,Matlab,Combinatorics,Discrete Mathematics,以下是初始代码: n = 5; trips = nchoosek(1:n,2); x_tsp=[0;1;1;0;1;0;1;0;0;1]; 因此,一些旅行团的组成部分的无序列表被提供给了我们。现在我们必须将其转换为实际的旅行,这意味着以TSP方式有序地遍历城市。下面的代码实现了这一点。但它似乎对循环使用,并且是重复的。这可以用更少的行进行矢量化吗 x_tsp = logical(round(x_tsp)); relevant_trips=trips(x_tsp,:); tour=zeros(n
n = 5;
trips = nchoosek(1:n,2);
x_tsp=[0;1;1;0;1;0;1;0;0;1];
因此,一些旅行团的组成部分的无序列表被提供给了我们。现在我们必须将其转换为实际的旅行,这意味着以TSP方式有序地遍历城市。下面的代码实现了这一点。但它似乎对循环使用,并且是重复的。这可以用更少的行进行矢量化吗
x_tsp = logical(round(x_tsp));
relevant_trips=trips(x_tsp,:);
tour=zeros(n+1,1);
r=randi(n);
tour(1)=relevant_trips(r,1);
tour(2)=relevant_trips(r,2);
tour(end)=tour(1);
for i=3:n
relevant_trips(r,:)=[];
[~,next_ind]=ismember(tour(i-1),relevant_trips);
[r,c] = ind2sub(size(relevant_trips),next_ind);
if c==2
tour(i)=relevant_trips(r,1);
else
tour(i)=relevant_trips(r,2);
end
end