I'm trying to figure out how to use scipy's dual_annealing
function. My problem is the classic travelling salesperson problem: Given a list of coordinates of cities to visit, find the shortest distance when visiting all cities. I've already solved it with a brute force method. Now I'd like to solve it with scipy's dual_annealing
.
This is my attempt:
import numpy as np
from scipy import optimize
from scipy import spatial
def total_distance(a):
prev = None
total_distance = 0
for curr in a:
if prev is None:
prev = curr
continue
else:
total_distance += spatial.distance.euclidean(prev, curr)
return total_distance
# List of coordinates with cities to visit.
inputs = [(485, 475), (1150, 750), (1008, 480), (1562, 134), (1155, 523)]
a = np.array(inputs)
min_distance = optimize.dual_annealing(total_distance, a)
The last line gives this error:
Exception has occurred: ValueError Bounds are not consistent min < max
The dual_annealing
function takes the required argument bounds
. From the documentation:
bounds : sequence or Bounds Bounds for variables. There are two ways to specify the bounds: Instance of Bounds class. Sequence of (min, max) pairs for each element in x.
Given the array of input coordinates to choose from, what do I need to do to fit this requirement? I don't understand what the documentation means when it mentions pairs.
Link to the dual_annealing documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.dual_annealing.html#scipy.optimize.dual_annealing