-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataGenerator.py
94 lines (70 loc) · 2.91 KB
/
DataGenerator.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
83
84
85
86
87
88
89
90
91
92
93
94
import csv
import random
import pickle
# CSV Parsing positions
POS_NAME = 4
POS_LATUTUDE = 6
POS_LONGITUDE = 7
class DataGenerator:
# price range per flight
PRICE_MIN = 100
PRICE_MAX = 2000
# destinations count range
DESTINATIONS_MIN = 2
DESTINATIONS_MAX = 6
test_airports = []
test_graph = {}
def __init__(self):
pass
def load_saved_graph(self, file_name):
try:
data = pickle.load(open(file_name, "rb"))
self.test_airports = data[0]
self.test_graph = data[1]
except:
print("Error while loading file...")
def save_graph(self, file_name):
if self.test_airports != [] and self.test_graph != {}:
data_to_save = [self.test_airports, self.test_graph]
pickle.dump(data_to_save, open(file_name, "wb"))
def load_new_data(self, limit):
airports = []
with open('airports.dat', 'r', encoding="utf8") as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
# iterate over CSV file
for row in reader:
# airport name shall not be empty
if "" != row[POS_NAME]:
# [0] position for further id
airports.append([None, float(row[POS_LATUTUDE]), float(row[POS_LONGITUDE]), row[POS_NAME]])
self.test_airports = random.sample(airports, limit)
self.assign_id()
def assign_id(self):
id = 0
for current_airport in self.test_airports:
current_airport[0] = id
id += 1
pass
def create_graph(self):
for current_airport in self.test_airports:
# count of destinations in current airport
current_dest_count = random.randint(DataGenerator.DESTINATIONS_MIN, DataGenerator.DESTINATIONS_MAX)
# selected target airports for current airport
current_dest_airports = random.sample(self.test_airports, current_dest_count)
# create graph edges
current_nodes = []
for current_dest_airport in current_dest_airports:
# prevent selection destination airport same as start airport
if current_airport[0] != current_dest_airport[0]:
# rand price
current_price = random.randint(DataGenerator.PRICE_MIN, DataGenerator.PRICE_MAX)
current_departure_time = round(random.uniform(0, 24), 2)
current_nodes.append([current_dest_airport[0], current_departure_time, current_price])
# assign edges to current node
self.test_graph[current_airport[0]] = current_nodes
# print(str(current_airport[0]) + ":" + str(self.test_graph[current_airport[0]]) + '\n')
# print(self.test_graph)
def get_airports(self):
return self.test_airports
def get_graph(self):
return self.test_graph