-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput_reader.py
More file actions
160 lines (128 loc) · 4.63 KB
/
Input_reader.py
File metadata and controls
160 lines (128 loc) · 4.63 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
# Function that returns two python dictionnaries : evac_paths & arcs
def parse_instance(path):
evac_paths = {} # {node_id : {'pop', 'max_rate', 'route_length', 'route_nodes':[node1,node2,..]} }
arcs = {} # arcs = {(node1,node2):{'duedate', 'length', 'capacity'}}
myfile = open(path, 'r')
ins = myfile.readlines()
i = 0
state = 0
while i < len(ins):
l = ins[i]
l = l.split(" ")
if l[0] == 'c':
i = i + 1
elif state == 0:
num_evac_nodes = int(l[0])
# safe_node = int(l[1]) UNUSED RIGHT NOW, MAY BE USEFUL LATER
for j in range(0, num_evac_nodes):
i = i + 1
l = ins[i]
l = l.split(" ")
node_id = int(l[0]) # id of node
pop = int(l[1]) # population
max_rate = int(l[2]) # max rate
route_length = int(l[3]) # k : length of route
route_nodes = [] # escape route nodes init as empty set
for k in range(0, int(l[3])):
route_nodes.append(int(l[4 + k]))
evac_paths[node_id] = {'pop': pop, 'max_rate': max_rate, 'route_length': route_length,
'route_nodes': route_nodes}
i = i + 1
state = 1
elif state == 1:
# num_nodes = int(l[0]) UNUSED RIGHT NOW, MAY BE USEFUL LATER
num_arcs = int(l[1])
for j in range(0, num_arcs):
i = i + 1
l = ins[i]
l = l.split(" ")
node1 = int(l[0])
node2 = int(l[1])
duedate = int(l[2])
length = int(l[3])
capacity = int(l[4])
if node1 < node2:
arcs[(node1, node2)] = {'due_date': duedate, 'length': length, 'capacity': capacity}
else:
arcs[(node2, node1)] = {'due_date': duedate, 'length': length, 'capacity': capacity}
state = -1
else:
i = i + 1
return evac_paths, arcs
# Function that takes as input the two dictionnaries (evac_paths + arcs) and prints them
def print_input_data(evac_paths, arcs):
print("========================")
print("INFORMATION D'EVACUATION")
print("========================")
print("Nombre de noeuds à évacuer : " + str(len(evac_paths)))
print("----------")
for keys, values in evac_paths.items():
print(keys)
print(values)
print("")
print("==============")
print("ARCS DU GRAPHE")
print("==============")
print("Nombre d'arcs : " + str(len(arcs)))
print("-----------------")
for keys, values in arcs.items():
print(keys)
print(values)
def reverse_arcs(arcs):
res = arcs
for a in arcs:
if a[0] > a[1]:
tmp = (a[1], a[0])
vtmp = arcs[a]
del res[a]
res[tmp] = vtmp
return res
# Parses solution data
def parse_solution(path):
solution = {} # {'instance_name', 'nb_evac_nodes', 'node_data', 'validity', 'aim_function', 'time', 'method'}
myfile = open(path, 'r')
l = myfile.readline()
solution['instance_name'] = l.rstrip('\n')
l = myfile.readline()
solution['nb_evac_nodes'] = int(l)
node_data = {} # {'id', evac_rate, 'start'}
for i in range(solution['nb_evac_nodes']):
l = myfile.readline()
ins = l.split()
node_data[int(ins[0])] = (int(ins[1]), int(ins[2]))
solution['node_data'] = node_data
l = myfile.readline()
solution['validity'] = l.rstrip('\n')
l = myfile.readline()
solution['aim_function'] = int(l)
l = myfile.readline()
solution['time'] = int(l)
l = myfile.readline()
solution['method'] = l.rstrip('\n')
return solution
def create_solution_file(solution_dico):
f1 = open('./outfile', 'w+')
print(solution_dico)
f1.write(solution_dico['instance_name'])
f1.write('\n')
f1.write(str(solution_dico['nb_evac_nodes']))
f1.write('\n')
soda = solution_dico['node_data']
for key in soda:
f1.write(str(key))
f1.write(" ")
f1.write(str(soda[key][0]))
f1.write(" ")
f1.write(str(soda[key][1]))
f1.write('\n')
f1.write(solution_dico['validity'])
f1.write('\n')
f1.write(str(solution_dico['aim_function']))
f1.write('\n')
f1.write(str(solution_dico['time']))
f1.close()
def __main__():
evac_path, arc = parse_instance('./Exemple/graphe-TD-sans-DL-data.txt')
print_input_data(evac_path, arc)
sol = parse_solution('./Exemple/graphe-TD-sans-DL-sol.txt')
print(sol)