-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
159 lines (154 loc) · 6.14 KB
/
graph.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
import matplotlib.pyplot as plt
from matplotlib.patheffects import Normal, Stroke
from typing import List, Tuple
from problem import State
from tree import Node, Path
class Graph:
def __init__(self):
self.graph: List[Tuple[float, float]] = []
self.coord: List[Tuple[float, float]] = []
self.treei = 0
self.gridi = 0
def grid(self, node: Node, m: int, n: int,
node_actions: List[Node] = [], rsize=40) -> None:
"""Graphs the path on a grid"""
# Getting path to state
path: Path = node.path_to(node)
path_states: List[State] = path['path']
path_transformed: List[State] = [[] for _ in range(len(path_states[0]))]
# Linear transformation of j substates (xj, yj)
for state in path_states:
j = 0
for sub_j in state:
xj, yj = sub_j[0], n + 1 - sub_j[1]
path_transformed[j].append((xj, yj))
j += 1
plt.clf() # Clear current figure
# Robot colors
color = ['#FF00FF', '#FF8000',
'#DD0000', '#FF69B4',
'#5E5EFF', '#454545']
# Plotting the path to state
j = 0
for sub_j in path_transformed:
coords = list(zip(*sub_j))
plt.plot(*coords, marker='o', color=color[j % 6],
alpha=0.5, linewidth=10, markersize=rsize)
j += 1
effect = [Stroke(linewidth=5, foreground='white'), Normal()]
# Plotting available actions
for node_f in node_actions:
j = 0
for sub_j in node_f.state:
parent = node.state[j]
xp, yp = parent[0], n + 1 - parent[1]
xc, yc = sub_j[0], n + 1 - sub_j[1]
plt.plot([xp, xc], [yp, yc], marker='o',
color='#02FF02', alpha=0.5,
linewidth=10, markersize=rsize)
j += 1
# Plotting each robot's location
j = 0
for sub_j in node.state:
xj, yj = sub_j[0], n + 1 - sub_j[1]
plt.plot(xj, yj, marker='o', color=color[j % 6],
markersize=rsize, path_effects=effect)
j += 1
self.gridi += 1
plt.grid(b=True, color='white', linewidth=5)
ax = plt.gca()
ax.set_xticks(
[i for i in range(1, m + 1)], minor=True)
ax.set_xticklabels(
[str(i) for i in range(1, m + 1)], minor=True)
ax.set_yticks(
[i for i in range(1, n + 1)], minor=True)
ax.set_yticklabels(
[str(i) for i in range(n, 0, -1)], minor=True)
ax.set_xticks(
[i + 0.5 for i in range(m + 1)], minor=False)
ax.set_yticks(
[i + 0.5 for i in range(n + 1)], minor=False)
ax.tick_params(color='none', labelcolor='#02FF02',
labelsize=20, labeltop=True,
labelbottom=False, which='minor')
ax.tick_params(colors='none', which='major')
ax.set_facecolor('none')
ax.set_aspect(1)
plt.savefig('grid{}.png'.format(self.gridi),
dpi='figure', orientation='landscape',
transparent=True)
return None
def tree(self, node: Node, expanded: List[Node] = []) -> None:
"""Graphs the tree"""
if node.parent:
# Retrieving parent coordinate
xp, yp = self.coord[node.parent.number]
# Generating child coordinates
lineage: int = node.lineage(node)
siblings: int = node.parent.children
yc = yp - 1
xc = node.sibling + 0.5 - siblings / 2 \
if siblings % 2 == 0 \
else node.sibling - siblings // 2
xc = xp + xc / lineage
else:
xc, yc = 0, 0
xp, yp = 0, 0
self.coord.append((xc, yc))
# Retrieving path coordinates
path: Path = node.path_to(node)
lbls = [str(list(state)) for state in path['path']]
path_coords = [self.coord[num] for num in path['number']]
pack = list(zip(*path_coords))
# Plotting the tree
effect = [Stroke(linewidth=5, foreground='white'), Normal()]
# Generating branches
for child in expanded:
# Retrieving parent coordinate
xp, yp = self.coord[child.parent.number]
yci = yp - 1
# Generating child coordinates
lineage: int = child.lineage(child)
siblings: int = child.parent.children
xci = child.sibling + 0.5 - siblings / 2 \
if siblings % 2 == 0 \
else child.sibling - siblings // 2
xci = xp + xci / lineage
branch = [(xp, yp), (xci, yci)]
branch = list(zip(*branch))
self.graph.extend(branch)
plt.clf() # Clear current figure
plt.plot(*self.graph,
color='#02FF02',
marker='o',
linewidth=2,
markersize=10,
path_effects=effect)
# Highlighting path
plt.plot(*pack,
color='#FF00FF',
marker='o',
linewidth=2,
markersize=10,
path_effects=effect)
# Generating text
for i in range(len(path_coords)):
xl, yl = path_coords[i]
plt.text(xl, yl + 0.5,
lbls[i],
color='white',
fontsize=15,
bbox=dict(alpha=0.9,
facecolor='#454545',
edgecolor='none',
boxstyle='round'),
verticalalignment='center',
horizontalalignment='center')
self.treei += 1
plt.axis('off')
plt.grid(b=None)
plt.savefig('tree{}.png'.format(self.treei),
dpi='figure', orientation='landscape',
transparent=True)
return None