-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphSystem.py
311 lines (263 loc) · 12 KB
/
GraphSystem.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
import copy
from manim import *
import codecs
import random
from numpy import cos
# from manimlib import *
RANDOM_POSITINAL_ENABLE = True
POSITIONAL_RANDOMNESS = 0.1
RANDOM_PRECESION = 1000
y_bias = 0.4
top_y_pos = 3.4
class Node:
def __init__(self, name,scale,width = 5,node_color=WHITE):
self.parent = None
self.left = None
self.right = None
self.name = name
self.right_edge = None
self.left_edge = None
self.visual_shape = Circle()
self.scale = scale
self.depth = 0
self.order = 0
self.random_index = 0
self.dad = None
self.arrow = None
self.visual_shape.set_stroke(width=width,color=BLUE)
self.visual_shape.set_color(node_color)
self.H = 0
self.visual_H = None
self.visual_H_name = None
self.calculated_cost = 0
self.visual_calculated_cost = None
self.seen = False
def set_pos(self,x,y):
self.x = x
self.y = y
self.visual_name = Text(str(self.name))
self.visual_name.move_to([x, y, 0])
self.visual_shape.move_to([x, y, 0])
self.graphics = VGroup(self.visual_name,self.visual_shape)
self.visual_name.scale(self.scale)
self.visual_shape.scale(self.scale)
def set_dad(self,node,arrow):
self.dad = node
self.arrow = arrow
def set_huristic(self,cost):
self.H = cost
def set_calculated_cost(self,cost):
self.calculated_cost = cost
class Graph:
def __init__(self,scale,root = None):
self.root = root
self.scale = scale
self.map = {}
self.nodes = {}
self.edges = {} # exp: {(A,B):2}
self.visual_edges = {}
self.branching_factors = []
self.has_cost = False
if root is not None:
self.map[root] = []
def creat_new_node(self,name):
node = Node(name,self.scale)
self.nodes[name] = node
self.nodes[name].random_index = random.randint(10,2000)
return node
def insert(self,new_node,old_node,No_duplicate = False):
if not self.map.keys().__contains__(old_node):
self.map[old_node] = []
if not (self.map[old_node]).__contains__(new_node):
(self.map[old_node]).append(new_node)
if new_node.depth == 0 and new_node is not self.root:
new_node.depth = old_node.depth+1
new_node.order = self.branching_factors[new_node.depth]
self.branching_factors[new_node.depth]+=1
elif No_duplicate:
new_node = copy.deepcopy(new_node)
new_node.depth = old_node.depth+1
new_node.order = self.branching_factors[new_node.depth]
self.branching_factors[new_node.depth]+=1
new_node.parent = old_node
def add_edge_cost(self,from_node,to_node,cost):
self.edges[from_node,to_node] = cost
return
def read_from_file(self,path):
with codecs.open(path, 'r') as f:
Lines = f.readlines()
max_depth = int(Lines[0].split(" ")[0])
self.branching_factors = []
for i in range(max_depth*2+1):
self.branching_factors.append(0)
all_nodes = Lines[1].split(" ")
all_nodes[len(all_nodes) -1] = all_nodes[len(all_nodes) -1].replace("\n","")
for j in range(0,len(all_nodes)):
self.creat_new_node(all_nodes[j])
self.root = self.nodes[all_nodes[0]]
# adding links between nodes
index = 0
for i in range(2,len(Lines)):
links = Lines[i].split(" ")
links[len(links) -1] = links[len(links) -1].replace("\n","")
for j in range(1,len(links)):
self.insert(self.nodes[links[j]],self.nodes[links[0]])
def read_from_file_with_edge_costs(self,path):
self.has_cost = True
with codecs.open(path, 'r') as f:
Lines = f.readlines()
max_depth = int(Lines[0].split(" ")[0])
self.branching_factors = []
for i in range(max_depth*2+1):
self.branching_factors.append(0)
# adding nodes with hiuristic
all_nodes = Lines[1].split(" ")
Hiuristics = Lines[2].split(" ")
Hiuristics[len(Hiuristics) -1] = Hiuristics[len(Hiuristics) -1].replace("\n","")
all_nodes[len(all_nodes) -1] = all_nodes[len(all_nodes) -1].replace("\n","")
for j in range(0,len(all_nodes)):
self.creat_new_node(all_nodes[j])
self.nodes[all_nodes[j]].set_huristic(int(Hiuristics[j]))
self.root = self.nodes[all_nodes[0]]
# adding links between nodes with edge values
index = 0
for i in range(3,len(Lines),2):
links = Lines[i].split(" ")
edges = Lines[i + 1].split(" ")
edges[len(edges) -1] = edges[len(edges) -1].replace("\n","")
links[len(links) -1] = links[len(links) -1].replace("\n","")
for j in range(1,len(links)):
self.insert(self.nodes[links[j]],self.nodes[links[0]])
self.add_edge_cost(self.nodes[links[0]],self.nodes[links[j]],int(edges[j-1]))
def set_edge_visual_tex(self,from_node,to_node,from_node_pos,to_node_pos,cost_str):
cost = Tex(cost_str)
cost.scale(self.scale)
from_node_x = from_node_pos[0]
from_node_y = from_node_pos[1]
to_node_x = to_node_pos[0]
to_node_y = to_node_pos[1]
a = ( float(from_node_y - to_node_y) / float(from_node_x - to_node_x) )
if a == 0:
a = 0.000000000001
vertical_perpendicular = -1.0 / a
center = (np.array([from_node_x,from_node_y,0]) + np.array([to_node_x,to_node_y,0])) / 2
target_point = np.array([center[0]+1,(center[0]+1.0)*vertical_perpendicular + (center[1] - (center[0]*vertical_perpendicular)),0])
vector = np.array(target_point - center)
vector = normalize(vector)
# temp = Line(center,target_point,stroke_width=1)
cost.move_to(center)
cost.shift(vector*0.2)
self.visual_edges[from_node,to_node] = cost
return cost
def add_edge_visual_tex(self,from_node,to_node):
if self.has_cost:
return(self.set_edge_visual_tex(from_node,to_node,[from_node.x,from_node.y,0],[to_node.x,to_node.y,0],str(self.edges[from_node,to_node])))
return None
def show_complete_graph(self,scene,RIGHT_X_AREA,LEFT_X_AREA):
# top = 3
group = VGroup()
# index = 0
# adding nodes
for i in self.nodes:
node = self.nodes[i]
current_y_point = float(top_y_pos - ((self.scale*2 + y_bias) * node.depth))
# if index == 0:
# current_y_point = top - (self.scale*2 + self.scale) * node.depth
# index = 1
current_x_point = (RIGHT_X_AREA + LEFT_X_AREA)/2
if self.branching_factors[node.depth] > 1:
current_x_point = LEFT_X_AREA + ((RIGHT_X_AREA - LEFT_X_AREA) / (self.branching_factors[node.depth]-1))*node.order
if RANDOM_POSITINAL_ENABLE:
# random.seed(node.random_index)
current_x_point += (float(random.randint(-RANDOM_PRECESION*POSITIONAL_RANDOMNESS,RANDOM_PRECESION*POSITIONAL_RANDOMNESS))) / (RANDOM_PRECESION)
node.set_pos(current_x_point,current_y_point)
group.add(node.graphics)
# scene.add(node.graphics)
# scene.add(node.graphics)
# adding edges
for i in self.map:
nodes = self.map[i]
for j in nodes:
arrow = Line([i.x,i.y,0],[j.x,j.y,0],stroke_width=1,buff= self.scale)
group.add(arrow)
cost = self.add_edge_visual_tex(i,j)
if cost is not None:
group.add(cost)
# scene.add(arrow)
return group
def make_nodes_connected_bi_directional(self):
# repeat = True
# map = self.map
# while(repeat):
# try:
# repeat = False
# for i in map:
# dad = i
# for j in map[i]:
# son = j
# for d in map:
# for q in map[d]:
# if q == son and d.name != dad.name:
# clone = copy.deepcopy(q)
# clone.depth = 0
# map[d].remove(q)
# self.insert(clone,d)
# repeat = True
# for x,z in self.edges:
# if x.name == d.name and z.name == q.name:
# self.add_edge_cost(d,clone,self.edges[x,z])
# break
# except:
# print("cleaning")
map = copy.deepcopy(self.map)
for i in map:
for j in map[i]:
check = True
if map.keys().__contains__(j) and (map[j]).__contains__(i):
check = False
if check:
original = self.nodes[j.name]
# node = copy.deepcopy(i)
node = self.nodes[i.name]
# node.depth = 0
# self.nodes[node.name + str(index)] = node
# index+=1
# node.graphics = j.graphics
# node.visual_shape = j.visual_shape
# node.visual_name = j.visual_name
self.insert(node,original)
print(" " + i.name+" " + original.name)
if self.has_cost:
for x,z in self.edges:
if x.name == node.name and z.name == original.name:
print("we did it" + " " + original.name+" " + node.name)
self.add_edge_cost(original,node,self.edges[x,z])
self.visual_edges[original,node] = self.visual_edges[node,original]
break
self.graph_cleaner()
def graph_cleaner(self):
map = self.map
for i in map:
for j in map[i]:
order = j.order
depth = j.depth
parent_order = j.parent.order
for k in map:
for l in map[k]:
if l.depth == depth:
if l.parent is not None and l.parent.order > parent_order and l.order < order:
j.order -= 1
l.order += 1
def get_node_relative_pos(self,node,RIGHT_X_AREA,LEFT_X_AREA):
return self.get_node_relative_pos2(self.branching_factors[node.depth],node.order,node.depth,RIGHT_X_AREA,LEFT_X_AREA)
def get_node_relative_pos2(self,branching_factor,order,depth,RIGHT_X_AREA,LEFT_X_AREA):
current_y_point = float(top_y_pos - ((self.scale*2 + y_bias) * depth))
current_x_point = (RIGHT_X_AREA + LEFT_X_AREA)/2
if branching_factor > 1:
current_x_point = float(LEFT_X_AREA + ((RIGHT_X_AREA - LEFT_X_AREA) / (branching_factor-1))*order)
if RANDOM_POSITINAL_ENABLE:
# random.seed(node.random_index)
current_x_point += (float(random.randint(-RANDOM_PRECESION*POSITIONAL_RANDOMNESS,RANDOM_PRECESION*POSITIONAL_RANDOMNESS))) / (RANDOM_PRECESION)
return [current_x_point,current_y_point,0]
def set_node_cost_tex(self,cost_tex):
self.cost_tex = cost_tex