-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tour.py
83 lines (63 loc) · 2.06 KB
/
Tour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# This class encodes our routes, referred to as tours
from random import shuffle
class Tour:
# Construct a blank tour if not specified
def __init__(self, tm, tour=None):
self.tm = tm
# Holds tour of cities
self.__tour = []
# Cache
self.__fitness = 0
self.__distance = 0
if tour == None:
for i in range(self.tm.number_of_cities()):
self.__tour.append(None)
else:
self.__tour = tour
# Creates a random individual
def generate_individual(self):
for city_index in range(self.tm.number_of_cities()):
self.set_city(city_index, self.tm.get_city(city_index))
shuffle(self.__tour)
# Get city from tour
def get_city(self, tour_position):
return self.__tour[tour_position]
# Set city in certain position within a tour
def set_city(self, tour_position, city):
self.__tour[tour_position] = city
self.__fitness = 0
self.__distance = 0
# Get tours fitness
def get_fitness(self):
if self.__fitness == 0:
self.__fitness = 1/self.get_distance()
return self.__fitness
# Get total distance of tour
def get_distance(self):
if self.__distance == 0:
tour_distance = 0
for city_index in range(self.tour_size()):
# City we're travelling from
from_city = self.get_city(city_index)
# City we're travelling to
if (city_index+1 < self.tour_size()):
destination_city = self.get_city(city_index+1)
else:
destination_city = self.get_city(0)
# Get distance b/t two cities
tour_distance += from_city.distance_to(destination_city)
distance = tour_distance
return distance
# Get number of cities in our tour
def tour_size(self):
return len(self.__tour)
# Check if tour contains a specific city
def contains_city(self, city):
return city in self.__tour
def to_string(self):
gene_string = ''
gene_string = '(' + self.get_city(0).to_string() + ')'
for i in range(1, self.tour_size()):
gene_string += '->' + str(self.get_city(i).distance_to(self.get_city(i-1))) + '->' + '(' + self.get_city(i).to_string() + ')'
#return gene_string
return str(self.get_city(2).distance_to(self.get_city(1)))