-
Notifications
You must be signed in to change notification settings - Fork 0
/
labyrinthe.sage
executable file
·95 lines (85 loc) · 2.37 KB
/
labyrinthe.sage
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
'''
p-q labyrinth
i
-------------------------
p p p p p p p
| q S 0 0 0 0 0 0
j | q . . [0] 0 0 0 0
| q . 0 0 . . . .
| q . . 0 0 0 0 0
| q 0 0 0 . 0 . E
'''
#
# place at coordinates i,j a summit of weight w and link it to close summits
def place_weight(G,w,i,j,p,q):
n=i+j*p
if i<p-1: # right
if G.has_edge(n,n+1) : G.delete_edge(n,n+1)
if G.has_edge(n+1,n) : G.delete_edge(n+1,n)
G.add_edges( [(n,n+1,w) ,(n+1,n,w)] )
if i>0: # left
if G.has_edge(n,n-1) : G.delete_edge( n,n-1 )
if G.has_edge(n-1,n) : G.delete_edge( n-1,n )
G.add_edges( [(n,n-1,w), (n-1,n,w)] )
if j<q-1 : # down
if G.has_edge(n,n+p) : G.delete_edge( (n+p,n) )
if G.has_edge(n+p,n) : G.delete_edge( (n,n+p) )
G.add_edges( [(n,n+p,w) ,(n+p,n,w)] )
if j>0 : # up
if G.has_edge(n,n-p) : G.delete_edge( (n,n-p) )
if G.has_edge(n-p,n) : G.delete_edge( (n-p,n) )
G.add_edges( [(n,n-p,w) ,(n-p,n,w)] )
#
# generate graph G of random p-q labyrinth
def labyrinth(p, q):
# Init empty square graph
G = DiGraph(sparse=True, weighted=True)
for j in [0..q-1]:
for i in [0..p-1]:
place_weight(G,1,i,j,p,q)
# place obstacle
n=floor(p*q/7)
for j in [0..n-1]:
r_p=randrange(0,p-1)
r_q=randrange(0,q-1)
place_weight(G,10000,r_p,r_q,p,q)
return G
#
# create graph G of random p-q labyrinth from file
def labyrinth_from_file(file_path):
#file_path = "/home/epsi/Desktop/RO/graphes/map.txt"
with open(file_path,"r") as file:
lines = file.read().splitlines()
q = len( lines[0] )
p = len( lines )
file.close()
G = Graph(sparse=True, weighted=True) # Init emtpy square graph
cpt_line = 0
for line in lines:
cpt_char = 0
for char in line:
if char == "0":
place_weight(G,1,cpt_line,cpt_char,p,q)
cpt_char += 1
cpt_line += 1
cpt_line = 0
for line in lines:
cpt_char = 0
for char in line:
if char == ".":
place_weight(G,10000,cpt_line,cpt_char,p,q)
cpt_char += 1
cpt_line += 1
return G
#
# Modify a graph to highlight a shortest path
# dictionary path from i_0
# node from which return to i_0
def draw_path(G, path, i_0, i_1):
i=i_1
while i != i_0:
if G.has_edge(path[i],i): G.delete_edge(path[i],i)
G.add_edges([(path[i],i,777),(path[i],i,777)])
i=path[i]
def sub(G,w):
return G.subgraph(edge_property=(lambda e: e[2] == w)).to_simple()