class Tour():
def __init__(self, size):
self.size = size
self.xs = np.random.random_integers(1, 99, size)
self.ys = np.random.random_integers(1, 99, size)
def clone(self):
c = Tour(len(self.xs))
c.xs = np.copy(self.xs)
c.ys = np.copy(self.ys)
return c
def distance(self):
d = 0
for i in range(self.size):
d += np.sqrt((self.xs[i] - self.xs[(i + 1) % self.size]) ** 2 +
(self.ys[i] - self.ys[(i + 1) % self.size]) ** 2)
return d
def swap(self, i, j):
t = self.xs[i]
self.xs[i] = self.xs[j]
self.xs[j] = t
t = self.ys[i]
self.ys[i] = self.ys[j]
self.ys[j] = t
def plot(self):
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
ax.set_xlim(0, 100)
ax.set_ylim(0, 100)
ax.plot(self.xs, self.ys)
ax.scatter(self.xs, self.ys)