-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsynthNK.py
More file actions
183 lines (159 loc) · 4.28 KB
/
Copy pathsynthNK.py
File metadata and controls
183 lines (159 loc) · 4.28 KB
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
""" Similar methods as the ones in Uppaal """
from pprint import*
def getStrat(m, k):
""" returns the tab of mouvement given by the strategy m for each k robots """
tabStrat = [0]*k
for i in range(k):
tabStrat[i]= m % 5
m=int(m/5)
return tabStrat
def noMoves(k):
"""returns an interger representing the strategy where the is k robots and none of them is willing to move"""
noMove = 2;
for i in range(k):
noMove = noMove *5 +2;
return noMove
def getView(indice, conf, k):
"""returns the view of robot indice, in the configuration conf made of k robots"""
view1 = [0]*k
for i in range(k):
view1[i] = conf[(indice+i)%k]
return view1
def sameView(view1,view2, k):
""" this function compares two views. It returns
0 is the view are not equivalent
1 if they are the same
-1 if view1 is the opposite of view2"""
nb0start1 = 0
nb0start2 = 0
#booleens pour savoir si on est toujours dans le bloc de 0 du début
start1 = True
start2 = True
nb0end1 = 0
nb0end2 = 0
#booleens pour savoir si on est toujours à la fin
end1 = True
end2 = True
for i in range(k):
if (view1[i]==0 and start1):
nb0start1 +=1
else:
start1 = False
if (view2[i]==0 and start2):
nb0start2 +=1
else:
start2 = False
if (view1[((k-1-i)+k)%k]==0 and end1):
nb0end1 +=1
else:
end1 = False
if (view2[((k-1-i)+k)%k]==0 and end2):
nb0end2 +=1
else:
end2 = False
if (nb0start1+nb0end1) != (nb0start2+nb0end2):
return False
nbElt = k - (nb0start1 + nb0end1);
sens = -1 # vers la droite = 1 / antihoraire = 0
for i in range(nbElt):
if (view1[(i+nb0start1)%k] != view2[(i+nb0start2)%k]) and (view1[(i+nb0start1)%k] != view2[(k-1-i-nb0end2)%k] ):
return False
else: #dans le sens horaire
if(sens == 1):
if(view1[i+nb0start1] != view2[i+nb0start2]):
return False
#// dans le sens anti horaire
elif(sens == 0):
if(view1[i+nb0start1] != view2[k-1-i-nb0end2]):
return False
#//pas de sens encore obtenu
elif ((view1[i+nb0start1] == view2[i+nb0start2]) and (view1[i+nb0start1] != view2[k-1-i-nb0end2] )):
sens = 1
elif ((view1[i+nb0start1] != view2[i+nb0start2]) and (view1[i+nb0start1] == view2[k-1-i-nb0end2])):
sens = 0
#//autrement a droite on a la même chose qu'a gauche et du coup on continu sur pas de sens jusqu'a trouver de sens
return True
def isIn (view1, tabView):
""" boolean function that permits to search for the existence of a view in a tab of views"""
for i in range(len(tabView)):
if (sameView(view1, tabView[i],len(view1))):
return True
return False
def conf_to_confpos(tabConf,n,k):
"""return the tab representing a conf as tab of pos from a conf as d_1...d_k for a ring of size n and k robots """
j = 0
#raise ValueError()
tabpos = [0]*n
#pprint(tabConf)
l = [int(e) for e in tabConf]
for i in range(len(tabConf)):
#print(type(j))
var = tabConf[i]
#print("|",end="")
#print(var,end="|\n")
#print(type(tabConf))
j = (int(tabConf[i])+j)%n
#print(j)
tabpos[j] +=1
return tabpos
def toString(config):
newString = ""
for elem in config:
newString += "{0}".format(elem)
return newString
def getPos(confPos,n,k):
tabpos = [0]*k
rbt=0
for i in range(n):
for j in range(confPos[i]):
tabpos[rbt] = i
rbt+=1
return tabpos
def AllClasses(n,k):
dic = dict()
configs = AllConfs(n,k)
for c in configs:
k = equivalence(c)
newString = toString(k)
dic[newString]=1
StringList = list(dic.keys())
ret = []
for config in StringList:
ret1 = []
for elem in config:
ret1.append(elem)
ret.append(ret1)
return ret
def AllConfs(n,k):
ret =list()
if k==1 :
return [[n]]
for i in range(n):
c = AllConfs(n-i, k-1)
for j in c :
t = j
t.append(i)
ret.append(t)
return ret
def notHere(setOfConfig,n,k):
NotHereConfs=[]
AllConfigs = AllClasses(n,k)
#print("et il y a {0} classes d'equ de config".format(len(AllConfigs)))
for config in AllConfigs:
if config not in setOfConfig:
NotHereConfs.append(config)
return NotHereConfs
def equivalence(config):
S = []
cur = config
rcur = reverse(cur)
for i in range(len(config)) :
cur = next(cur)
rcur = reverse(cur)
p = min(cur,rcur)
S.append(p)
return (min(S))
def reverse(config):
return config[::-1]
def next(config):
return config[1::]+config[0:1]