forked from GiadaPa/DODM-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_function.py
More file actions
306 lines (238 loc) · 12.8 KB
/
Copy pathimport_function.py
File metadata and controls
306 lines (238 loc) · 12.8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
@author: Leonora Frangu, Fabio James Greenwood, Giada Palma, Benedetta Pasqualetto
"""
### Libraries
from cmath import isnan
import pandas as pd
import numpy as np
import copy
## variables beginning in "input_request" denote the type of expected inputs, this is fed through methods XXX to dynamically read the input file
input_request_objects = {
"PEOPLE" : {
"PERSON_ID" : "ID",
"HOME_ID" : "Int",
"HOME_LAT" : "Float",
"HOME_LON" : "Float",
"BUDGET" : "Int",
"MAX_NB_CHANGES_TRANSPORT" : "Int"},
"PLACES" : {
"PLACE_ID" : "ID",
"PLACE_LAT" : "Float",
"PLACE_LON" : "Float",
"MAX_NB_PEOPLE" : "Int"},
"TASKS" : {
"TASK_ID" : "ID",
"PERSON_ID" : "Int",
"PLACE_ID" : "Int",
"COST" : "Int",
"SERVICE_TIME" : "Int",
"START_TIME" : "Int",
"END_TIME" : "Int",
"IS_SPECIAL" : "Int",
"EXTRA_SERVICE_TIME" : "Int",
"PENALTY" : "Int"},
"BIKE_STATIONS" : {
"BIKE_STATION_ID" : "ID",
"BIKE_STATION_LAT" : "Float",
"BIKE_STATION_LON" : "Float",
"NB_AVAILABLE_BIKES" : "Int",
"NB_FREE_SPOTS" : "Int"},
"BUS_LINES" : {
"LINE_ID" : "ID",
"START_TIME" : "Int",
"FREQUENCY" : "Int",
"MAX_NB_PEOPLE" : "Int"},
"BUS_STOPS" : {
"BUS_STOP_ID" : "ID",
"BUS_STOP_LAT" : "Float",
"BUS_STOP_LON" : "Float"}
}
input_request_links = {
"BUS_STOP_TO_LINE" : {
"BUS_STOP_ID" : "Int",
"BUS_LINE_ID" : "Int"},
"NODE_TRAVEL_INFO" : {
"NODE_ID" : "ID",
"NODE_ID" : "ID",
"MODE" : "String",
"DISTANCE" : "Float",
"TIME" : "Float",
"FITNESS" : "Float"}
}
#tuples are used here because they are ordered
input_request_global = {
"HORIZON" : [
("START" , "Int"),
("END" , "Int")],
"MODES_OF_TRANSPORTATION" : [
("ID" , "String"),
("Name", "String")],
"COST_BIKE_PER_MINUT" : "Float",
"COST_BUS_PER_RIDE" : "Float"
}
# Dictionary definitions
data_inputs = {}
input_objects = {}
input_links = {}
input_global = {}
#update_data_input(data_input_table, value, format)
#def update_data_input(data_input_table, value_name, value, format, object_name = "None", objects = input_objects.keys()):
# Function read all information in the input file
# Method reads first line of each paragraph to understand which class the input belongs to,
# then uses/references the input_request dictionaries to understand the expect data and format within that paragraph
#def import_inputs(input_objects = input_objects, input_links = input_links, input_global = input_global):
def import_inputs(explicit_input_file_location = 'inputs/instance_demo1_N10.txt'):
txtFile = open(explicit_input_file_location,'r')
input_objects = {}
input_links = {}
input_global = {}
input_request_names = list(input_request_objects.keys()) + list(input_request_global.keys())
with txtFile as f:
lines = f.readlines()
line_qty = len(lines)
currently_reading = True
current_line = 0
while currently_reading == True:
#terminate loop
if current_line >= line_qty:
currently_reading = False
break
#skip blank lines
if lines[current_line] == "\n":
current_line += 1
continue
#determine if paragraph is the start of a new date point and act accordingly
# if input in input_request_links
if lines[current_line].split()[0] == "BUS_STOP_ID" and lines[current_line].split()[1] == "BUS_LINE_ID":
current_class_name = "BUS_STOP_TO_LINE"
input_links[current_class_name] = {}
reading_current_class = True
current_line += 1
while reading_current_class == True:
#detect if the next class has arrived
if lines[current_line].split()[0] in input_request_names or (lines[current_line].split()[0] == "BUS_STOP_ID" and lines[current_line].split()[1] == "BUS_LINE_ID") or (lines[current_line].split()[0] == "NODE_ID" and lines[current_line].split()[1] == "NODE_ID"):
reading_current_class = False
continue
link_name = (lines[current_line].split()[0], lines[current_line].split()[1])
input_links[current_class_name][link_name] = 0
try:
if lines[current_line].split()[2] == "(DEPOSIT)":
input_links[current_class_name][link_name] = 1
except IndexError as err:
a = 1
del a
current_line += 1
if lines[current_line] == "\n":
reading_class_instance = False
current_line += 1
break
elif lines[current_line].split()[0] == "NODE_ID" and lines[current_line].split()[1] == "NODE_ID":
current_class_name = "NODE_TRAVEL_INFO"
input_links[current_class_name] = {}
reading_current_class = True
current_line += 1
while reading_current_class == True:
#detect if the next class has arrived
if lines[current_line].split()[0] in input_request_names or (lines[current_line].split()[0] == "BUS_STOP_ID" and lines[current_line].split()[1] == "BUS_LINE_ID") or (lines[current_line].split()[0] == "NODE_ID" and lines[current_line].split()[1] == "NODE_ID"):
reading_current_class = False
continue
link_ID = (int(lines[current_line].split()[0]),int(lines[current_line].split()[1]),lines[current_line].split()[2])
input_dict = { "DISTANCE" : float(lines[current_line].split()[3]), "TIME" : float(lines[current_line].split()[4]), "FITNESS" : float(lines[current_line].split()[5])}
input_links[current_class_name][link_ID] = input_dict
current_line += 1
if current_line >= line_qty:
reading_current_class = False
currently_reading = False
break
if lines[current_line] == "\n":
reading_class_instance = False
currently_reading == False
current_line += 1
break
elif lines[current_line].split()[0] == "MODES_OF_TRANSPORTATION":
current_class_name = copy.deepcopy(lines[current_line].split()[0])
input_global[current_class_name] = {}
reading_current_class = True
ID = 1
current_line += 1
while reading_current_class == True:
#detect if the next class has arrived
if lines[current_line].split()[0] in input_request_names or (lines[current_line].split()[0] == "BUS_STOP_ID" and lines[current_line].split()[1] == "BUS_LINE_ID") or (lines[current_line].split()[0] == "NODE_ID" and lines[current_line].split()[1] == "NODE_ID"):
reading_current_class = False
continue
input_global[current_class_name][ID] = lines[current_line].split()[1]
ID += 1
current_line += 1
if lines[current_line] == "\n":
reading_class_instance = False
current_line += 1
break
ID = np.nan
print("Hello")
#if not in input_request_links
elif lines[current_line].split()[0] in input_request_names:
# if input in input_request_objects
if lines[current_line].split()[0] in input_request_objects.keys():
current_class_name = copy.deepcopy(lines[current_line].split()[0])
input_class = input_request_objects[lines[current_line].split()[0]]
reading_current_class = True
input_objects[lines[current_line].split()[0]] = {}
current_line += 1
#loop for reading all values of class X
while reading_current_class == True:
#detect if the next class has arrived
if lines[current_line].split()[0] in input_request_names or (lines[current_line].split()[0] == "BUS_STOP_ID" and lines[current_line].split()[1] == "BUS_LINE_ID") or (lines[current_line].split()[0] == "NODE_ID" and lines[current_line].split()[1] == "NODE_ID"):
reading_current_class = False
continue
#detect if there is an ID for class
try:
if input_class[lines[current_line].split()[0]] == "ID":
ID = int(lines[current_line].split()[1])
else:
ID = np.nan
except IndexError as err:
ID = np.nan
#read instance of class
reading_class_instance = True
while reading_class_instance == True:
#check for end of class instance
if lines[current_line] == "\n":
reading_class_instance = False
current_line += 1
continue
input_objects = update_input_table(input_objects, input_value=lines[current_line].split()[1], input_name=lines[current_line].split()[0], input_format = input_class[lines[current_line].split()[0]], object_class_name=current_class_name , ID = ID)
current_line += 1
# if input in input_request_global
elif lines[current_line].split()[0] in input_request_global.keys():
input_class = input_request_global[lines[current_line].split()[0]]
if not isinstance(input_class, list):
#input_global[lines[current_line].split()[0]] = lines[current_line].split()[1]
input_global = update_input_table(input_global, input_value=lines[current_line].split()[1], input_name=lines[current_line].split()[0], input_format = input_class)
else:
for key, i in zip(input_class, range(1, len(input_class)+1)):
#input_global[key] = lines[current_line].split()[i]
input_global = update_input_table(input_global, input_value=lines[current_line].split()[i], input_name=key[0], input_format = key[1])
current_line += 1
else:
current_line += 1
return input_objects, input_links, input_global
def update_input_table(input_table, input_value, input_name, input_format, object_class_name = np.nan, ID = np.nan):
#create new object instance if required for object
if not pd.isnull(ID) and not ID in input_table[object_class_name]:
input_table[object_class_name][ID] = {}
if input_format == "Int":
input_var = int(input_value)
elif input_format == "ID":
input_var = int(input_value)
elif input_format == "Float":
input_var = float(input_value)
elif input_format == "String":
input_var = input_value
else:
raise Exception("FG NOTE - Error: format ~" + input_format + "~ not found")
if pd.isnull(ID):
input_table[input_name] = input_var
else:
input_table[object_class_name][int(ID)][input_name] = input_var
return input_table
#input_objects, input_links, input_global = import_inputs()