使用分支定界算法在Matlab中求解TSP

时间:2018-04-07 19:21:36

标签: matlab

我自己制作了一个Matlab脚本,用于解决应用分支定界算法的旅行商问题。

1)在我的例子中,我有4个城市(A,B,C,D)。我直接定义了距离矩阵。

2)在另一个文件(generate_distance_matrix)中,我编写了一些用于加载excel文件的代码(Test)。在那个excel文件中,我随机选择一些坐标(x1,x2):

如何将这两个脚本组合起来?我如何以迭代的方式调整脚本?因为目前我只为4个城市编写了代码(TSP_test)。

我的脚本的工作步骤是:减少初始成本矩阵(行和列缩减)

评估下一个节点。例如(A => B)

设置M(A,B)= 0

将行A和列B设置为inf

设置M(B,A)= inf

评估成本值

对下一个节点重复这些步骤。

我希望有人能帮我解决问题。

如果我的剧本中的内容不明确,请询问。

在这里,您可以看到分支定界算法的脚本:

    % Script for solving the TSP by using branch and bound
% Step 1: Write the initial cost matrix and reduce it (Row reduction and
% Column reduction

% Step 2: From the reduced matrix from step 1, M(A,B) = 0
% Set row A and column B to inf
% Set M(B,A) to inf
clear all
clc
% Rows from the inital cost matrix
% There are 4 cities (city A...city D)
A = [inf, 4, 12, 7];
B = [5, inf, inf, 18];
C = [11, inf, inf, 6];
D = [10, 2, 3, inf];

% Initial cost matrix representing the distances between the cities
% for example: the distance between city A and B is 4
M_initial_cost_matrix = [A; B; C; D];

% Reduce the initial cost matrix
row_reduction = min(M_initial_cost_matrix, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_initial_cost_matrix - row_reduction;

column_reduction = min(M_row_reduced);

M_reduced_1 = M_row_reduced - column_reduction;

M_reduced_1(isnan(M_reduced_1)) = inf;

% Evaluate the cost value
cost_1 = sum(row_reduction) + sum(column_reduction)

% Distance between city A and B (node 2)
M_reduced = M_reduced_1

M_reduced(1, :) = inf;
M_reduced(:, 2) = inf;
M_reduced(2, 1) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_2 = M_row_reduced - column_reduction;

M_reduced_2(isnan(M_reduced_2)) = inf

cost_2 = cost_1 + sum(row_reduction) + sum(column_reduction) + M_reduced_1(1, 2)

% Distance between city A and C (node 3)
M_reduced = M_reduced_1

M_reduced(1, :) = inf;
M_reduced(:, 3) = inf;
M_reduced(3, 1) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_3 = M_row_reduced - column_reduction;

M_reduced_3(isnan(M_reduced_3)) = inf;

cost_3 = cost_1 + sum(row_reduction) + sum(column_reduction) + M_reduced_1(1, 3)

% Distance between city A and D (node 4)
M_reduced = M_reduced_1

M_reduced(1, :) = inf;
M_reduced(:, 4) = inf;
M_reduced(4, 1) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_4 = M_row_reduced - column_reduction;

M_reduced_4(isnan(M_reduced_4)) = inf;

cost_4 = cost_1 + sum(row_reduction) + sum(column_reduction) + M_reduced_1(1, 4)

% Lowest cost
lowest_cost = min([cost_2, cost_3, cost_4])

% Distance between city A, C, B (node 5)
M_reduced = M_reduced_3

M_reduced(3, :) = inf;
M_reduced(:, 2) = inf;
M_reduced(2, 3) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_5 = M_row_reduced - column_reduction;

M_reduced_5(isnan(M_reduced_5)) = inf;

cost_5 = cost_3 + sum(row_reduction) + sum(column_reduction) + M_reduced_3(3, 2)

% Distance between city A, C, D (node 6)
M_reduced = M_reduced_3

M_reduced(3, :) = inf;
M_reduced(:, 4) = inf;
M_reduced(4, 3) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_6 = M_row_reduced - column_reduction;

M_reduced_6(isnan(M_reduced_6)) = inf;

cost_6 = cost_3 + sum(row_reduction) + sum(column_reduction) + M_reduced_3(3, 4)

% Distance between city A, C, D, B (node 7)
M_reduced = M_reduced_3

M_reduced(4, :) = inf;
M_reduced(:, 2) = inf;
M_reduced(2, 4) = inf;

row_reduction = min(M_reduced, [], 2);

row_reduction(isinf(row_reduction)) = 0;

M_row_reduced = M_reduced - row_reduction;

column_reduction = min(M_row_reduced);

column_reduction(isinf(column_reduction)) = 0;

M_reduced_7 = M_row_reduced - column_reduction;

M_reduced_7(isnan(M_reduced_7)) = inf;

cost_7 = cost_3 + sum(row_reduction) + sum(column_reduction) + M_reduced_3(4, 2)
% Optimal path is: A, C, D, B, A with a cost of 25

这里是用于从名为“test”的excel文件生成距离矩阵的脚本。

% column 1: ID for city
% column 2: X coordinate
% column 3: Y coordinate

data = xlsread('Test.xlsx',1)
Dist = dist(data(:,2:3)')
Dist(Dist==0) = inf

最佳

0 个答案:

没有答案