-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrega1.py
352 lines (290 loc) · 11.9 KB
/
entrega1.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from collections import defaultdict
from itertools import combinations
from simpleai.search import (
SearchProblem,
breadth_first,
depth_first,
uniform_cost,
iterative_limited_depth_first,
greedy,
astar,
)
from simpleai.search.viewers import BaseViewer, ConsoleViewer, WebViewer
RAFAELA = "rafaela"
SUNCHALES = "sunchales"
LEHMANN = "lehmann"
SUSANA = "susana"
SC_DE_SAGUIER = "sc_de_saguier"
ESPERANZA = "esperanza"
RECREO = "recreo"
SANTA_FE = "santa_fe"
SAN_VICENTE = "san_vicente"
SANTO_TOME = "santo_tome"
ANGELICA = "angelica"
SAUCE_VIEJO = "sauce_viejo"
CONNECTIONS = [
(SUNCHALES, LEHMANN, 32),
(LEHMANN, RAFAELA, 8),
(RAFAELA, SUSANA, 10),
(SUSANA, ANGELICA, 25),
(ANGELICA, SAN_VICENTE, 18),
(SC_DE_SAGUIER, ANGELICA, 60),
(ANGELICA, SANTO_TOME, 85),
(RAFAELA, ESPERANZA, 70),
(ESPERANZA, RECREO, 20),
(RECREO, SANTA_FE, 10),
(SANTO_TOME, SANTA_FE, 5),
(SANTO_TOME, SAUCE_VIEJO, 15),
]
CONNECTIONS_DICT = defaultdict(dict)
for connection in CONNECTIONS:
city_a, city_b, liters = connection
CONNECTIONS_DICT[city_a][city_b] = liters
CONNECTIONS_DICT[city_b][city_a] = liters
KM_FOR_OIL_LITER = 100
SEDES = [RAFAELA, SANTA_FE]
TRUCKS_EXAMPLE = [
# id, ciudad de origen, y capacidad de combustible máxima (litros)
("c1", RAFAELA, 1.5),
("c2", RAFAELA, 2),
("c3", SANTA_FE, 2),
]
PACKAGES_EXAMPLE = [
# id, ciudad de origen, y ciudad de destino
('p1', RAFAELA, ANGELICA),
('p2', RAFAELA, SANTA_FE),
('p3', ESPERANZA, SUSANA),
('p4', RECREO, SAN_VICENTE),
]
class MercadoArtificalProblem(SearchProblem):
def __init__(self, trucks_liters, packages_goal=PACKAGES_EXAMPLE, *args, **kwargs):
self.packages_goal = packages_goal
self.trucks_liters = trucks_liters
super().__init__(*args, **kwargs)
def is_goal(self, state):
# print("-"*50)
# print("IN IS GOAL")
# print("state:", state)
# print("-"*50)
trucks_info, packages_info = state
if packages_info != self.packages_goal:
# los paquetes tienen que estar en la ciudad de destino
return False
for truck in trucks_info:
# los camiones deben estar en alguna de las sedes
if truck[1] not in SEDES:
return False
# print("IS GOAL TRUE")
return True
def actions(self, state):
"""
each action is defined by a tuple of four elements, for example:
('c1', 'esperanza', 0.7, ('p1', 'p2')),
- c1: truck_id
- 'esperanza': city where the truck will travel
- 0.7: liters of oil spended in the trip
- ('p1', 'p2'): packages to transport in the trip
"""
# print("-"*50)
# print("IN ACTIONS")
# print("state:", state)
trucks_info, packages_info = state
posible_actions = []
for truck in trucks_info:
truck_id, truck_city, oil_in_truck = truck
posible_trips = []
for connected_city, km in CONNECTIONS_DICT[truck_city].items():
oil_for_trip = km / KM_FOR_OIL_LITER
if oil_for_trip < oil_in_truck:
posible_trips.append((connected_city, oil_for_trip))
posible_packages_to_take = [
package_id
for package_id, package_city in packages_info
if package_city == truck_city
]
packages_combinations = []
# any combination of packages, from 0 to all.
for comb_qty in range(len(posible_packages_to_take) + 1):
qty_combinations = combinations(posible_packages_to_take, comb_qty)
packages_combinations.extend(qty_combinations)
posible_actions.extend(
(truck_id, city_to_go, oil_for_trip, packages)
for city_to_go, oil_for_trip in posible_trips
for packages in packages_combinations
)
# print("RETURNES ACTIONS:", posible_actions)
# print("-"*50)
return posible_actions
def result(self, state, action):
# print("-"*50)
# print("IN RESULT")
# print("state:", state)
# print("action:", action)
trucks_info, packages_info = state
trucks_dict = {
truck_id: (city, oil)
for truck_id, city, oil in trucks_info
}
truck_id, city_to_go, oil_for_trip, packages_to_move = action
# Update the state of the moved truck
if city_to_go in SEDES:
oil_in_moved_truck = self.trucks_liters[truck_id]
else:
oil_in_moved_truck = trucks_dict[truck_id][1] - oil_for_trip
moved_truck_new_state = (truck_id, city_to_go, oil_in_moved_truck)
trucks_new_state = [
truck_state for truck_state in trucks_info
if truck_state[0] != truck_id
]
trucks_new_state.append(moved_truck_new_state)
# Update the state for the moved packages
packages_new_state = []
for package_id, package_city in packages_info:
if package_id in packages_to_move:
package_city = city_to_go
packages_new_state.append((package_id, package_city))
result_to_return = tuple(trucks_new_state), tuple(packages_new_state)
# print("RETURNED RESULT:", result_to_return)
# print("-"*50)
return result_to_return
def cost(self, state, action, state2):
"""
The cost of an action is equal to the oil spended in the trip.
"""
# print("-"*50)
# print("IN COST")
# print("state:", state)
# print("action:", action)
# print("RETURNED COST:", action[2])
# print("-"*50)
return action[2]
def heuristic(self, state):
print("-"*50)
print("IN HEURISTIC")
print("state:", state)
trucks_info, packages_info = state
trucks_locations = [truck[1] for truck in trucks_info]
packages_goal_dict = {
package_id: city
for package_id, city in self.packages_goal
}
packages_not_in_destination = [
# package_id, current_city, destination_city
(package_id, current_city, packages_goal_dict[package_id])
for package_id, current_city in packages_info
if current_city != packages_goal_dict[package_id]
]
cities_with_pkg_to_move = [x[1] for x in packages_not_in_destination]
estimated_km = 0
for package_id, current_city, destination in packages_not_in_destination:
pkg_city_shortest_conn = min(CONNECTIONS_DICT[current_city].values())
pkg_estimated_cost = 0
if destination in CONNECTIONS_DICT[current_city]:
pkg_estimated_cost += CONNECTIONS_DICT[current_city][destination]
else:
destination_shortest_conn = min(CONNECTIONS_DICT[destination].values())
print(f"parcial heuristic for [MOVE PKG] {package_id}: {pkg_city_shortest_conn} + {destination_shortest_conn}")
pkg_estimated_cost += pkg_city_shortest_conn + destination_shortest_conn
print("current_city:", current_city)
print("trucks_locations:", trucks_locations)
if not current_city in trucks_locations:
nearest_pkg_cities = [
adjacent_city
for adjacent_city, distance in CONNECTIONS_DICT[current_city].items()
if distance == pkg_city_shortest_conn
]
for near_city in nearest_pkg_cities:
if near_city in trucks_locations:
print(f"parcial heuristic for [TRUCK ARRIVING] {package_id}: {pkg_city_shortest_conn}")
pkg_estimated_cost += pkg_city_shortest_conn
break
else:
trucks_shortest_conn = min(
min(CONNECTIONS_DICT[truck_city].values())
for truck_city in trucks_locations
)
print(f"parcial heuristic for [TRUCK ARRIVING] {package_id}: {pkg_city_shortest_conn} + {trucks_shortest_conn}")
pkg_estimated_cost += pkg_city_shortest_conn + trucks_shortest_conn
pkgs_to_move_in_current_city = cities_with_pkg_to_move.count(current_city)
print(f"heuristic for {package_id}: {pkg_estimated_cost} / {pkgs_to_move_in_current_city}")
print(f"heuristic for {package_id}: {pkg_estimated_cost / pkgs_to_move_in_current_city}")
estimated_km += pkg_estimated_cost / pkgs_to_move_in_current_city
trucks_not_in_sede = [
location for location in trucks_locations
if location not in SEDES
]
cities_set = set(cities_with_pkg_to_move)
extra_trucks_to_move = len(trucks_not_in_sede) - len(cities_set)
if extra_trucks_to_move > 0:
shortest_sede_connection = min(
min(conn for conn in CONNECTIONS_DICT[sede_city].values())
for sede_city in SEDES
)
shortest_con_per_city = [
min(CONNECTIONS_DICT[truck_city].values())
for truck_city in trucks_not_in_sede
]
print("shortest per city", shortest_con_per_city)
shortest_city_trucks_connection = min(
shortest_con_per_city
)
print(f"heuristic for EXTRA TRUCKS: {extra_trucks_to_move} * max({shortest_sede_connection}, {shortest_city_trucks_connection})")
print(f"heuristic for EXTRA TRUCKS: {extra_trucks_to_move * max(shortest_sede_connection, shortest_city_trucks_connection)}")
estimated_km += extra_trucks_to_move * max(
shortest_sede_connection, shortest_city_trucks_connection
)
print("estimated km in heuristic:", estimated_km)
print("RETURNED HEURISTIC:", estimated_km / KM_FOR_OIL_LITER)
print("-"*50)
return estimated_km / KM_FOR_OIL_LITER
def planear_camiones(metodo, camiones=TRUCKS_EXAMPLE, paquetes=PACKAGES_EXAMPLE, debug=False):
# a state is defined by a tuple of two elements:
# - trucks with info wich is a tuple of (id, city, oil_liters)
# - packages info wich is a tuple of (id, city)
initial_state = (tuple(camiones), tuple(x[:-1] for x in paquetes))
packages_goal = tuple((x[0], x[2]) for x in paquetes)
trucks_liters = {x[0]: x[-1] for x in camiones}
problem = MercadoArtificalProblem(trucks_liters, packages_goal, initial_state)
search_methods = {
"astar": astar,
"breadth_first": breadth_first,
"depth_first": depth_first,
"uniform_cost": uniform_cost,
"greedy": greedy,
}
method = search_methods[metodo]
if debug:
visor = ConsoleViewer()
# visor = WebViewer()
else:
visor = BaseViewer()
result = method(problem, graph_search=True, viewer=visor)
if debug:
print('estadísticas:')
print('cantidad de acciones hasta la meta:', len(result.path()))
print(visor.stats)
print("FINAL STATE:")
print(result.state)
for i, step in enumerate(result.path()):
cont = input("print step by step? Y/n")
if cont.upper() == "N":
break
print(f"----------- STEP {i} ----------")
action, state = step
print("ACTION")
print(action)
print("STATE")
print(state)
return [action for action, _ in result.path() if action is not None]
if __name__ == "__main__":
camiones = [
("c1", RAFAELA, 40),
]
paquetes = [
("p1", RAFAELA, ESPERANZA),
("p2", RAFAELA, SANTA_FE),
("p3", SANTA_FE, ESPERANZA),
]
camiones = [('c1', 'rafaela', 1.5), ('c2', 'santa_fe', 9999)]
paquetes = [('p1', 'sunchales', 'sauce_viejo')]
planear_camiones("astar", camiones, paquetes, debug=True)