-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize.py
64 lines (49 loc) · 1.98 KB
/
visualize.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
from abc import abstractmethod, ABC
from model import Before, Last, Shunt
class Visualize(ABC):
@abstractmethod
def visualize(self, model):
"""Visualize the solution to the hump-shunting problem given by <model>."""
class VisualizeBasic(Visualize):
def visualize(self, model):
solution = model.facts(atoms=True)
befores = solution.query(Before).all()
print(list(befores))
class VisualizeText(Visualize):
def visualize(self, model):
solution = model.facts(atoms=True)
befores = solution.query(Before).all()
lasts = solution.query(Last).all()
shunts = solution.query(Shunt).all()
steps = {}
for before in befores:
if before.move not in steps:
steps[before.move] = {}
step_tracks = steps[before.move]
if before.track not in step_tracks:
step_tracks[before.track] = {}
track = step_tracks[before.track]
track[before.second_car] = before.first_car
for last in lasts:
step_tracks = steps[last.move]
if last.track not in step_tracks:
step_tracks[last.track] = {}
track = step_tracks[last.track]
track["LAST"] = last.car
step_shunt = {}
for shunt in shunts:
step_shunt[shunt.move] = shunt.track
for step in sorted(steps.keys())[1:]:
print(f"Step {step}")
for track in sorted(steps[step].keys()):
track_order = steps[step][track]
car = track_order["LAST"]
sorted_track = [car]
while car in track_order:
car = track_order[car]
sorted_track.insert(0, car)
track_str = f"{track}: {sorted_track}"
if step_shunt[step] == track:
track_str += " -->"
print(track_str)
print()