Sei sulla pagina 1di 4

CE191: Civil and Environmental Engineering Systems Analysis

Lab 2
Integer Programming: Solving the Traveling Salesman Problem

Professor A. Bayen Fall 06

You are managing a construction site. The road network at the site is as shown in figure 1. The
rectangles in the graph are sites, and the lines are roads between sites. The numbers on the lines
represent the estimated travel times between the two sites connected by the road. The entry/exit
point to the site is represented by rectangle 1.

2 5
2
6 5
3
1 3 5
6
1
5
3
4

Fig. 1: Road network of the construction site.

You are required to do a daily tour of the sites. The objective of this laboratory assignment is to
determine the tour with the minimum travel time. Your tour should start at the entry/exit point
(rectangle 1) and end there as well.

1. Transform the problem to one on a fully connected graph in which all the vertices are the
entry/exit point and the sites you must inspect. You can do this by solving a shortest-path
problem (by hand or using Matlab).
Your report should include the 6-by-6 matrix containing the minimum path costs.

1
CE191 – Fall 05 Lab 2 2

BONUS question:
You will receive 5 additional points on the course total if you provide the code of a Matlab
function that returns the minimum path cost matrix defined above. This function must
take as input a cost matrix on a graph that is not necessarily fully connected. The cost
between 2 nodes that are not linked is defined to be -1. The cost between a node and itself
is defined to be -1. In the case of this assignment, the cost matrix that the function would
receive as input is the following

−1 2 −1 5 −1 −1
 
 2 −1 3 −1 −1 −1
 
−1 3 −1 1 6 5 
 
 5 −1 1 −1 −1 3 
 
−1 −1 6 −1 −1 5 
−1 −1 5 3 5 −1

The function should be written to work in the general case, for any number of nodes. In
order to receive full credit for this question, the algorithm must be explained and the code
must be commented correctly. No GSI help will be provided for this question.
Please note that the credit received for this question will be added to your course total, and
that the maximum course total remains 100.

2. Use a branch and bound algorithm together with an asssignment relaxation to compute the
optimal tour. Your algorithm is to be applied on fully-connected graphs such as the on
created in the previous part, without any consideration for the physical road network of the
construction site. You should:

(a) Understand how the Matlab file assignment.m works1 . This file takes as input a square
cost matrix and solves the assignment problem for that cost matrix. It constructs a linear
vector of objective costs f, matrices of constraints A and Aeq, and vectors of constraints
b and beq, in order to solve the following LP:

min f 0 · x
x

subject to

Ax ≤ b
Aeq x = beq

The code of this function is included below, and can be download from the assigment
page of the course web site.
1
Note: you should use Matlab 5.0 or higher, and have the Optimization package (it is the case in 345 Davis). The
Student Version does not have the Optimization package by default
CE191 – Fall 05 Lab 2 3

In assignment.m, the following variables are used: Cost, f, X1, Xmat, and Fval. Your
report should answer the following questions (in a few words or sentences per question):
• What does the element on the ith row and jth column of Cost represent?
• What does the ith element of f represent?
• What does the ith element of X1 represent?
• What does the element on the ith row and jth column of Xmat represent?
• What does Fval represent?
• What does the constraint Ax ≤ b represent?
• What does the constraint Aeq x = beq represent? (note that two types of constraints
are represented by Aeq )

(b) Write a Matlab function tsp branch and bound that solves the Traveling Salesman
Problem, taking as input the matrix of costs between each pair of nodes. The func-
tion should compute the tour using the assignment relaxation (i.e. every time you have
to solve the assignment problem you can call the function assignment.m). The function
tsp branch and bound must work for any cost matrix. Your report should:
• specify clearly the branch and bound strategy that was used
• present the general structure of the program solving the TSP (using pseudo-code
for example)
• include the optimal tour and its length.
One possible way of structuring the branch and bound algorithm is to use a cell array
as a stack to keep track of the sub-problems. For your convenience, some MATLAB files
have been provided to help you develop a cell array stack.
• cellArray.m shows examples of basic cell array operations
• pop.m and push.m are skeletons of stack functions; you can use these as starting
points for your implementation
• teststack.m constains a suite of tests that will check if your pop.m and push.m
implementations are working correctly
Using these files is completely optional; you can come up with your own cell array stack
from scratch, or you could find some completely different way to do the branch and
bound, if you like. Include your versions of pop.m and push.m in your report if you
decide to use them.

Please submit your m-files in a ZIP file, and include a few sentences in the report,
explaining how to run your programs and describing their inputs and their outputs.
CE191 – Fall 05 Lab 2 4

Code of assignment.m

function [Xmat,Fval] = assignment(Cost);


% Solves the assignment relaxation of a Travelling Salesman Problem.
% Part of the lab is to figure out the specifics of what this code does,
% so the comments are sparse.

if (size(Cost,1)~=size(Cost,2))
error(’Cost Matrix not square’);
end
n = size(Cost,1);

f = [];% Vector of cost


r = [];% Vector to generate second set of constraints in Aeq matrix
runit = zeros(1,n);
runit(1) = 1;

for i=1:n
f = [f Cost(i,:)];
r = [r runit];
end

%Constraint matrix
clear A
A = -1*eye(length(f));
b = zeros(1,length(f));
Aeq = zeros(2*n,n*n);
for i=1:n
Aeq(i,(i-1)*(n)+1:i*(n)) = 1;
Aeq(i,(i-1)*(n)+ i) = 0; %a_i,i = 0;
Aeq(i+n,i:n*n) = r(1:length(r) - i + 1);
Aeq(i+n, i + (i-1)*n) = 0; %a_j,j= 0
end
beq = ones(1,2*n);

%Solving Binary Integer LP assignment problem


[X1 Fval] = bintprog(f’,A,b’,Aeq,beq’);

X=X1’;
%Recovering Matrix solution from vector solution
Xmat = zeros(size(Cost));
for i=1:n
Xmat(i,:) = X((i-1)*n+1:i*n);
end

Potrebbero piacerti anche