2 from networkx.algorithms import isomorphism
5 from optparse import OptionParser
14 """Convertit un nombre en binaire"""
16 res =[0 for i in range(n)]
31 acc = acc + 2**(n-i-1)
35 def calculetmix(epsilon):
40 treatedlist =get_treatedlist(rf)
41 #print "taillle de la liste ",len(treatedlist)
42 n= int(log(len(treatedlist[0]))/log(2))
44 l2n = range(int(pow(2,n)))
45 pi0 = [pow(2,-n) for _ in range(int(pow(2,n)))]
46 #treatedlist = [treatedlist[11]]
48 for perm in treatedlist:
49 M = np.zeros((pow(2,n),pow(2,n)))
55 M[conf-1][dec(confbp,n)-1] = 1/float(n)
57 #print "Markov chain",M
58 eigs = np.linalg.eigvals(M)
61 modulusofegs = [abs(l) for l in eigs]
62 modulusofegs = [l if (1-l)>error else 0 for l in modulusofegs]
63 #print "modulusofegs",modulusofegs
64 lambdastar = max(modulusofegs)
65 #print "lambdastar",lambdastar
66 trel = 1.0/(1-lambdastar)
67 def tmixSup(epsilon) :
68 pimin = 1.0/int(pow(2,n))
70 return np.log(1.0/(epsilon*pimin))*trel
71 def tmixInf(epsilon) :
72 pimin = 1.0/int(pow(2,n))
74 return (trel-1)*np.log(1.0/(2*epsilon))
77 print "F_"+str(cpt+1), perm, int(tmixInf(epsilon)), int(tmixSup(epsilon))+1
78 resTMIX +=[(perm, int(tmixInf(epsilon)), int(tmixSup(epsilon))+1)]
83 resTMIX.sort(cmp=compare)
88 def is_double_stoc(M):
90 return all( [abs (sum([M[i,j] for i in range(nbl)])-1.0)<1E-6 for j in range(nbc)])
94 def matriceAdjacenceDe(G):
95 l2n = range(int(pow(2,n)))
96 P = np.zeros((2**n,2**n))
105 P[i][i]=(n-k)/float(n)
110 M = matriceAdjacenceDe(G)
112 r = r and is_double_stoc(M)
113 r = r and nx.is_strongly_connected(G)
117 def graphe_de_liste(l):
119 l2n = range(int(pow(2,n)))
122 # r est l'image binaire de conf en parallele
124 # on construit poentiellement les n image en unitaire
126 # confbp est conf sauf en cp ou c'est r[cp]
130 G.add_edge(conf,dec(confbp,n))
135 def genereGrapheHamiltonien(G):
137 def largeur(profondeur, effectues):
138 #print "profondeur",profondeur
141 for (c,vi) in effectues:
143 for v in G.neighbors(d):
145 effectues_p += [([_ for _ in c]+[(d,v)],set([_ for _ in vi]+[v]))]
146 acc[profondeur] = effectues_p
147 if profondeur <= pow(2,n) :
148 largeur(profondeur+1,effectues_p)
149 largeur(4,[([(0,1)],set([0,1]))])
150 res = acc[pow(2,n)+1]
152 for (c,vertices) in res :
153 assert len(vertices) == pow(2,n)
161 def graphSansListe(G,l):
163 Gp.add_edges_from(G.edges())
164 Gp.remove_edges_from(l)
167 def genereTousLesGraphesSansCycleHamiltonien(G):
168 r = [graphSansListe(G,l) for l in genereGrapheHamiltonien(G)]
171 if all([not(isomorphic(g,g2)) for g2 in res]):
173 return [grapheToList(G) for G in res]
175 def graphe_de_edges(l):
177 l2n = range(int(pow(2,n)))
184 # s'il y a des doublons ce n'est pas une perm
186 return len(set(L)) == len(L)
190 l2n = range(int(pow(2,n)))
192 images = range(int(pow(2,n)))
199 if (o,dec(epb,n)) in edges:
204 def isomorphic(g1,g2):
205 GM = isomorphism.DiGraphMatcher(g1,g2)
206 return GM.is_isomorphic()
208 def parcours_p(visited,resG,l):
213 G = graphe_de_edges(lp)
214 if nx.is_strongly_connected(G) :
215 if all([not(isomorphic(G,g2)) for g2 in visited]):
217 M = matriceAdjacenceDe(G)
218 if is_double_stoc(M):
220 gtl = grapheToList(G)
222 parcours_p(visited,resG,G.edges())
225 def parcours_l_dual(visited,resG,GGedges):
227 print "\n en entrant",int(prof),"a visiter",len(visited)
230 for i in range(len(visited)) :
232 print "ds calcul",i,len(nvisited)
233 ld = visited[i].edges()
234 lr = list(GGedges-set(ld))
238 lp = [x for x in ld]+[lr[j]]
239 G= graphe_de_edges(list(GGedges-set(lp)))
240 if nx.is_strongly_connected(G) :
241 Gd= graphe_de_edges(lp)
242 if all([not(isomorphic(Gd,g2)) for g2 in nvisited]):
244 M = matriceAdjacenceDe(G)
245 if is_double_stoc(M):
247 gtl = grapheToList(G)
250 parcours_l_dual(nvisited,resG,GGedges)
253 def parcours_l(visited,resG):
255 if prof < profM - nbmaxiter :
257 print "\n en entrant",int(prof),"a vistier",len(visited)
260 for i in range(len(visited)) :
262 #print "ds calcul",i,len(nvisited)
263 l = visited[i].edges()
267 G = graphe_de_edges(lp)
268 if nx.is_strongly_connected(G) :
269 if all([not(isomorphic(G,g2)) for g2 in nvisited]):
271 M = matriceAdjacenceDe(G)
272 if is_double_stoc(M):
274 gtl = grapheToList(G)
277 parcours_l(nvisited,resG)
285 l = range(int(pow(2,n)))
287 G = graphe_de_liste(l)
288 treatedlist = genereTousLesGraphesSansCycleHamiltonien(G)
290 #Ginit = nx.DiGraph()
293 # parcours en profondeur : deux graphes sont voinsin si l'un contient
294 # un arc de plus que l'autre
296 #parcours_p(visited,resG,G.edges())
297 #parcours_l([G],resG)
298 #parcours_l_dual([Ginit],resG,set(G.edges()))
300 #print [grapheToList(X) for X in resG]
310 treatedlist =get_treatedlist(rf)
312 treatedlist = [ perm for perm in permute_in_place(range(8))]
313 print "taillle de la liste ",len(treatedlist)
317 n= int(log(len(treatedlist[0]))/log(2))
322 l2n = range(int(pow(2,n)))
323 # pour chaque permutation de l ensemble [0,...,2^[n-1}}
325 #for perm in permute_in_place(range(4)):
326 D=[0 for x in range(len(treatedlist))]
328 for perm in treatedlist:
333 # si elle contient un element stable on l enleve (GTPIC n est pas SCC)
334 if all([ ident!=perm[ident] for ident in l2n]):
335 # on construit le GTPIC
341 # r est l'image binaire de conf en parallele
343 # on construit poentiellement les n image en unitaire
345 # confbp est conf sauf en cp ou c'est r[cp]
349 G.add_edge(conf,dec(confbp))
351 if nx.is_strongly_connected(G):
357 def isomorphic(g1,g2):
358 GM = isomorphism.DiGraphMatcher(g1,g2)
359 return GM.is_isomorphic()
360 if all([not(isomorphic(G,g2)) for g2 in resG]):
365 # print "pas scc",perm
369 P = np.zeros((2**n,2**n))
378 P[i][i]=(n-k)/float(n)
381 if is_double_stoc(P):
384 unif = 1/float(pow(2,n))
386 while (d >0.00001 and t <10000):
389 T =[ sum ([T[j]*P[j][i] for j in l2n]) for i in l2n]
391 dp = sum([abs(x-unif) for x in T])*(pow(2,n))
397 print perm, t, d, D[count]
405 parser = OptionParser()
406 parser.add_option("-i", "--input", dest="i",
407 help="file of sequences")
408 (options, args) = parser.parse_args()
409 if (options.i != None):
413 if __name__ == "__main__":