Homework 32 — Traveling Salesperson Problem heuristic
Due Sunday December 5
Files
- Module heuristic.py
- Tester test_heuristic.py
Your task
- Implement a stragegy that
Function consider( cities, tour, i1, i2 )
- Parameter
cities
is a list of points.
- Parameter
tour
is an arrangement of indices intocities
.
- Parameters
i1
andi2
are indices totour
- The function performs the following actions
- Computes the cost
c1
of the currenttour
.
- Performs a swap of the of
tour
elements at indicesi1
andi2
.
- Computes the cost
c2
of the newtour
.
- If
c2
is less thanc1
, the function returnsTrue
. Otherwise, the function reswapstour
elements at indicesi1
andi2
(thus, restoring the better solution) and returnsFalse
.
- The built-in tester should produce the following output
testing consider()
cities: [(281, 468), (515, 643), (738, 88), (530, 120), (410, 754)]
starting tour: [1, 0, 4, 3, 2]
i1: 2
i2: 4
consider( cities, tour, i1, i2 ): True
current tour: [1, 0, 2, 3, 4]
cities: [(281, 468), (515, 643), (738, 88), (530, 120), (410, 754)]
starting tour: [1, 0, 4, 3, 2]
i1: 1
i2: 3
consider( cities, tour, i1, i2 ): False
current tour: [1, 0, 4, 3, 2]