I’m certainly confused. I’m not able to find a way to decode the path by looking at the x property. For example, if you use this an input matrix:
p = [
[0, 21082, 65713, 88747, 94863, 94812, 118470, 85507, 54992, 52002, ],
[21082, 0, 47097, 70131, 76247, 76196, 92801, 66891, 36376, 35638, ],
[65713, 47097, 0, 39508, 45624, 45611, 69268, 62966, 49637, 53121, ],
[88747, 70131, 39508, 0, 7789, 21907, 43901, 48506, 69068, 72552, ],
[94863, 76247, 45624, 7789, 0, 25477, 36837, 59444, 75185, 78668, ],
[94812, 76196, 45611, 21907, 25477, 0, 24386, 26661, 55051, 62567, ],
[118470, 92801, 69268, 43901, 36837, 24386, 0, 38228, 68479, 73749, ],
[85507, 66891, 62966, 48506, 59444, 26661, 38228, 0, 37080, 46277, ],
[54992, 36376, 49637, 69068, 75185, 55051, 68479, 37080, 0, 7849, ],
[52002, 35638, 53121, 72552, 78668, 62567, 73749, 46277, 7849, 0, ],
]
You get a solution of:
status: OptimizationStatus.OPTIMAL route length 303767
The x matrix is much easier to understand visually.
I’ve outlined all 1.0 values in orange. I’ve been interpreting it by starting at row 0 (Ashuelot Bridge is the name of the location). The 1.0 value is in column 5, so I go to column and say that that’s the next node on my route, which is Berment Bridge. Berment has it’s 1.0 in column 4 so the next location is Railroad Bridge and so on.
This is not an optimal path, if you graph the 9 on a real map it is clearly wrong. But lets use our distances.
426,705 is the sum which is not the same as the 303767 we had output by the python.
So my method of interpreting the x value is clearly incorrect.
Here’s my python to print out the order as shown above.
def nextNodeIndex( node ):
"Returns the index of the first element in the node that has x != 0"
for index, value in enumerate (node):
if value.x > 0:
return index
return index
# the final path is in variable x, we need to walk x once, looking for
# the next node and walk it to see the subsequent node
solutionNodes = [0]
nextNode = 0
for nodeIndex in range(0, n-1):
nextNode = nextNodeIndex(x[nextNode])
solutionNodes.append(nextNode)
# the start node is also the end node
solutionNodes.append(0)
I also tried look at the y variable, but that isn’t making any sense to me. I see a single array of 10 elements, all of which name a node, but one of the nodes appears twice, so I’m not at all sure how to interpret that.
So my question remains, once I have a solution, how can I determine the path that makes up that solution?