Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement def k _ tsp _ mtz _ encoding ( n , k , cost _ matrix ) : The input ` n ` denotes

Implement def k_tsp_mtz_encoding(n, k, cost_matrix): The input `n` denotes the size of the graph with vertices labeled `0`,..,`n-1`,`k` is the number of salespeople, and `cost_matrix` is a list of lists wherein `cost_matrix[i][j]` is the edge cost to go from `i` to `j` for `i != j`. Your code must avoid accessing `cost_matrix[i][i]` to avoid bugs. These entries will be supplied as `None` in the test cases.
use the following setup:
- Decision variables $x_{i,j}$ for $i
ot= j$ denoting that the tour traverses the edge from $i$ to $j$.
- Time stamps $t_1,\ldots, t_{n-1}$. The start/end vertex $0$ does not get a time stamp.
the code must obey the following degree constraints:
- Vertex 0: $k$ edges leave and $k$ edges enter.
- Vertex 1,..., n-1: 1 edge leaves and 1 edge enters (same as TSP).
Formulate the timestamp constraints
Your code must return a list `lst` that has exactly $k$ lists in it, wherein `lst[j]` represents the locations visited by the $j^{th}$ salesperson. The tour must start with vertex 0
For the example above, for $k=2$, your code must return
~~~
[[0,2,1,4],[0,3]]
~~~
For the example above, for $k=3$, your code must return
~~~
[[0,1,4],[0,2],[0,3]]
from pulp import *
def k_tsp_mtz_encoding(n, k, cost_matrix):
# check inputs are OK
assert 1<= k < n
assert len(cost_matrix)== n, f'Cost matrix is not {n}x{n}'
assert all(len(cj)== n for cj in cost_matrix), f'Cost matrix is not {n}x{n}'
prob = LpProblem('kTSP', LpMinimize)
# finish your implementation here
# your code must return a list of k-lists [[0, i1,..., il],[0, j1,...,jl],...] wherein
# the ith entry in our list of lists represents the
# tour undertaken by the ith salesperson
# your code here
raise NotImplementedError
Must pass the following test cases:
cost_matrix=[[None,3,4,3,5],
[1, None, 2,4,1],
[2,1, None, 5,4],
[1,1,5, None, 4],
[2,1,3,5, None]]
n=5
k=2
all_tours = k_tsp_mtz_encoding(n, k, cost_matrix)
print(f'Your code returned tours: {all_tours}')
assert len(all_tours)== k, f'k={k} must yield two tours -- your code returns {len(all_tours)} tours instead'
tour_cost =0
for tour in all_tours:
assert tour[0]==0, 'Each salesperson tour must start from vertex 0'
i =0
for j in tour[1:]:
tour_cost += cost_matrix[i][j]
i = j
tour_cost += cost_matrix[i][0]
print(f'Tour cost obtained by your code: {tour_cost}')
assert abs(tour_cost -12)<=0.001, f'Expected tour cost is 12, your code returned {tour_cost}'
for i in range(1, n):
is_in_tour =[1 if i in tour else 0 for tour in all_tours]
assert sum(is_in_tour)==1, f' vertex {i} is in {sum(is_in_tour)} tours -- this is incorrect'
print('test passed')
cost_matrix=[[None,3,4,3,5],
[1, None, 2,4,1],
[2,1, None, 5,4],
[1,1,5, None, 4],
[2,1,3,5, None]]
n=5
k=3
all_tours = k_tsp_mtz_encoding(n, k, cost_matrix)
print(f'Your code returned tours: {all_tours}')
assert len(all_tours)== k, f'k={k} must yield two tours -- your code returns {len(all_tours)} tours instead'
tour_cost =0
for tour in all_tours:
assert tour[0]==0, 'Each salesperson tour must start from vertex 0'
i =0
for j in tour[1:]:
tour_cost += cost_matrix[i][j]
i = j
tour_cost += cost_matrix[i][0]
print(f'Tour cost obtained by your code: {tour_cost}')
assert abs(tour_cost -16)<=0.001, f'Expected tour cost is 16, your code returned {tour_cost}'
for i in range(1, n):
is_in_tour =[1 if i in tour else 0 for tour in all_tours]
assert sum(is_in_tour)==1, f' vertex {i} is in {sum(is_in_tour)} tours -- this is incorrect'
print('test passed')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Lakehouse

Authors: Bill Inmon ,Mary Levins ,Ranjeet Srivastava

1st Edition

1634629663, 978-1634629669

More Books

Students also viewed these Databases questions

Question

=+1. How visible is your service/product?

Answered: 1 week ago