6 from itertools import *
7 from scipy import optimize as opt
8 from copy import deepcopy
14 distanceEmmissionMax = 30
23 # construction du graphe
25 lg = [(0, 1, 22.004323820359151), (0, 2, 28.750632705280324), (0, 3, 29.68069293796183), (0, 4, 8.547146256271331), (0, 5, 28.079672647730469), (0, 7, 23.017867703525138), (0, 8, 6.1268526078857208), (0, 9, 24.573433868296771), (1, 0, 22.004323820359151), (1, 2, 18.807277287689722), (1, 3, 18.982897767602783), (1, 4, 16.848855991756174), (1, 5, 17.042671653231526), (1, 6, 16.410544777532913), (1, 7, 25.598808236367063), (1, 8, 20.175759189503321), (1, 9, 12.843763853990932), (2, 0, 28.750632705280324), (2, 1, 18.807277287689722), (2, 3, 1.0957062702237066), (2, 4, 29.159997765424084), (2, 5, 1.8557839901886808), (2, 6, 23.122260476726876), (2, 9, 6.052562826627808), (3, 0, 29.68069293796183), (3, 1, 18.982897767602783), (3, 2, 1.0957062702237066), (3, 4, 29.884008054261855), (3, 5, 1.9922790489539697), (3, 6, 22.479228556182363), (3, 9, 6.4359869969688059), (4, 0, 8.547146256271331), (4, 1, 16.848855991756174), (4, 2, 29.159997765424084), (4, 3, 29.884008054261855), (4, 5, 28.006189408396626), (4, 7, 15.774839848636024), (4, 8, 3.6206480052249144), (4, 9, 23.804744370383144), (5, 0, 28.079672647730469), (5, 1, 17.042671653231526), (5, 2, 1.8557839901886808), (5, 3, 1.9922790489539697), (5, 4, 28.006189408396626), (5, 6, 21.492976178079076), (5, 8, 29.977996181215822), (5, 9, 4.4452006140146185), (6, 1, 16.410544777532913), (6, 2, 23.122260476726876), (6, 3, 22.479228556182363), (6, 5, 21.492976178079076), (6, 9, 20.04488615603487), (7, 0, 23.017867703525138), (7, 1, 25.598808236367063), (7, 4, 15.774839848636024), (7, 8, 16.915923579829141), (8, 0, 6.1268526078857208), (8, 1, 20.175759189503321), (8, 4, 3.6206480052249144), (8, 5, 29.977996181215822), (8, 7, 16.915923579829141), (8, 9, 25.962918470558208), (9, 0, 24.573433868296771), (9, 1, 12.843763853990932), (9, 2, 6.052562826627808), (9, 3, 6.4359869969688059), (9, 4, 23.804744370383144), (9, 5, 4.4452006140146185), (9, 6, 20.04488615603487), (9, 8, 25.962918470558208)]
28 #lg= [(0,1,23),(1,0,15),(1,2,45)]
36 l = [(random()*coteCarre, random()*coteCarre) for _ in range(n)]
37 for io in range(len(l)) :
38 for ie in range(len(l)) :
40 dist = mt.sqrt((l[io][0]-l[ie][0])**2 + (l[io][1]-l[ie][1])**2)
41 if dist <= distanceEmmissionMax :
42 G.add_edge(io,ie,weight=dist)
43 G.add_edge(ie,io,weight=dist)
44 test = not(any([ not(nx.has_path(G,o,sink)) for o in G.nodes() if sink in G.nodes() and o != sink]))
48 G.add_weighted_edges_from(lg)
49 #print nx.is_strongly_connected(G)
60 #print G.edges(data=True)
61 #TODO afficher le graphe et etre sur qu'il est connexe
66 #V = list(set(sample(N,int(len(N)*vrate)))-set([sink]))
67 V = list(set(N)-set([sink]))
74 L = range(len(G.edges()))
75 d = [di['weight'] for (_,_,di) in G.edges(data=True)]
82 assert l in L, " pb de dimennsion de l: "+str(l)+ " "+ str(L)
94 a = [[ail(i,l) for l in L ] for i in xrange(n)]
95 aplus = [[1 if ail(i,l) > 0 else 0 for l in L ] for i in xrange(n)]
96 amoins = [[1 if ail(i,l) < 0 else 0 for l in L ] for i in xrange(n)]
113 cs = [alpha + beta*(di**path_loss_exp) for di in d]
136 return cmp(x1[1],x2[1])
145 return mt.sqrt(sum([(d1[t]-d2[t])**2 for t in d1]))
148 def AfficheVariation (up,vp,lap,wp,thetap,etap,qp,Psp,Rhp,xp,valeurFonctionDualep):
149 global u, v, la, w, theta , q, Ps, Rh, eta, x,valeurFonctionDuale
151 print "du=",distance(u,up),
152 print "dv=",distance(v,vp),
153 print "dlambda=",distance(la,lap),
154 print "dw=",distance(w,wp),
155 print "dtheta=",abs(theta-thetap),
156 print "deta=",distance(eta,etap),
157 print "dq=",distance(q,qp),
158 print "dPs=",distance(Ps,Psp),
159 print "dRh=",distance(Rh,Rhp),
160 print "dx=",distance(x,xp),
161 print "dL=",abs(valeurFonctionDuale-valeurFonctionDualep),"\n"
164 valeurFonctionDuale = 0
167 r = x if (x >0 and x <= 1) else -1
168 #print "ds entre0et1 x et r", x,r
173 r = x if x >0 else -1
174 #print "ds xpos x et r", x,r
178 return x if x >= 0 else -1
181 def armin(f,xini,xr,args):
182 #xpos = lambda x : x if x > 0 else -1
185 #print "strictement pos"
186 #print "parametres passes a cobyla",xini,xpos,args,"consargs=(),rhoend=1E-5,iprint=0,maxfun=1000"
187 r= opt.fmin_cobyla(f,xini,cons=[xpos],args=args,consargs=(),rhoend=1E-3,iprint=0,maxfun=nbiter)
188 #print "le min str pos est",r
189 #print "le min pos est", r,"avec xini",xini
192 r = opt.fmin_cobyla(f,xini,[xposounul],args,consargs=(),rhoend=1E-3,iprint=0,maxfun=nbiter)
193 # print "le min pos est", r
194 #print "le min pos null est", r,"avec xini",xini
196 r = opt.fmin_cobyla(f,xini,[entre0et1],args,consargs=(),rhoend=1E-3,iprint=0,maxfun=nbiter)
197 #print "le min pos inf 1 est", r,"avec xini",xini
208 return omega/(mt.pow(k,0.5))
213 return omega/mt.sqrt(k)
218 def maj(k,maj_theta,mxg,idxexp):
219 # initialisation des operateurs lagrangiens
220 global u, v, la, w, theta , q, Ps, Rh, eta, x, valeurFonctionDuale
228 if not ASYNC or random() < taux_succes:
229 s = eta[(h,i)]-sum([a[i][l]*x[(h,l)] for l in L])
231 print "ds calcul u",abs(s),idxexp
233 smax = max(smax,abs(s))
234 up[(h,i)] = u[(h,i)]-theta*s
242 if not ASYNC or random() < taux_succes:
243 s = Rh[h]- mt.log(float(sigma2)/D)/(gamma*mt.pow(Ps[h],float(1)/3))
245 print "ds calcul v",abs(s),idxexp
247 smax = max(smax,abs(s))
248 vp[h] = max(0,v[h]-theta*s)
256 if not ASYNC or random() < taux_succes:
257 s = q[i]*Bi -sum([aplus[i][l]*cs[l]*sum([x[(h,l)] for h in V]) for l in L])-cr*sum([amoins[i][l]*sum([x[(h,l)] for h in V]) for l in L])-psi(Ps,i)
259 print "ds calcul la",abs(s),idxexp,i
261 smax = max(smax,abs(s))
262 resla = la[i]-theta*s
263 lap[i] = max(0,resla)
272 if not ASYNC or random() < taux_succes:
273 s = sum([a[i][l]*q[i] for i in N])
275 print "ds calcul w",abs(s),idxexp
277 smax = max(smax,abs(s))
278 wp[l] = w[l] + theta*s
284 thetap = maj_theta(k)
289 fa = sum([a[i][l]*w[l] for l in L]) - la[i]*Bi
293 if not ASYNC or random() < taux_succes:
294 c = -float(sum([a[i][l]*w[l] for l in L]) - la[i]*Bi)/(2*amplifieur)
295 rep = epsilon if c <= 0 else c
305 #print "maj des des Psh"
307 #print "ds f_ps",psh, v[h]* mt.log(float(sigma2)/D)/(gamma*((psh**2)**(float(1)/3))) +la[h]*psh
308 return v[h]* mt.log(float(sigma2)/D)/(gamma*mt.pow(float(2)/3)) +la[h]*psh
310 if not ASYNC or random() < taux_succes:
311 lah = 0.05 if la[h] == 0 else la[h]
312 rep = (float(2*v[h]*mt.log(float(sigma2)/D))/mt.pow(3*gamma*lah,float(3)/5))
313 Psp[h] = epsilon if rep <= 0 else rep
323 etap[(h,i)] = etahi(h,i,Rh)
329 return delta*rh*rh-v[h]*rh-sum([u[(h,i)]*eta[(h,i)] for i in N])
332 if not ASYNC or random() < taux_succes:
333 rep = float(v[h])/(2*delta)
334 Rhp[h] = 0 if rep < 0 else rep
340 r = delta*xhl*xhl + xhl*(cs[l]*sum([la[i]*aplus[i][l] for i in N]) +cr*sum([la[i]*amoins[i][l] for i in N])+sum([u[(h,i)]*a[i][l] for i in N]))
346 if not ASYNC or random() < taux_succes:
347 rep = -float(cs[l]*sum([la[i]*aplus[i][l] for i in N]) +cr*sum([la[i]*amoins[i][l] for i in N])+sum([u[(h,i)]*a[i][l] for i in N]))/(2*delta)
348 xp[(h,l)] = 0 if rep < 0 else rep
356 valeurFonctionDualep = 0
357 valeurFonctionDualep += sum([amplifieur*q[i]*q[i] for i in N])
358 valeurFonctionDualep += sum([sum([delta*(x[(h,l)]**2) for l in L]) for h in V])
359 valeurFonctionDualep += sum([delta*(Rh[h]**2) for h in V])
360 valeurFonctionDualep += sum([sum([u[(h,i)]*(sum([ a[i][l]*x[(h,l)] for l in L])- eta[(h,i)]) for i in N]) for h in V])
361 valeurFonctionDualep += sum([v[h]*(mt.log(float(sigma2)/D)/(gamma*mt.pow(Ps[h],float(2)/3)) - Rh[h]) for h in V])
362 valeurFonctionDualep += sum([la[i]*(psi(Ps,i) +sum([aplus[i][l]*cs[l]*sum([x[(h,l)] for h in V]) for l in L])+ cr*sum([ amoins[i][l]*sum([x[(h,l)] for h in V]) for l in L]) -q[i]*Bi) for i in N ])
363 valeurFonctionDualep += sum([w[l]*sum([a[i][l]*q[i] for i in N]) for l in L])
366 #AfficheVariation(up,vp,lap,wp,thetap,etap,qp,Psp,Rhp,xp,valeurFonctionDualep)
368 arret = abs(valeurFonctionDuale-valeurFonctionDualep) < error
370 return (up,vp,lap,wp,thetap,etap,qp,Psp,Rhp,xp,valeurFonctionDualep,arret,mxg,smax)
391 def initialisation():
392 global u, v, la, w, theta , q, Ps, Rh, eta, x,init
398 q[i] = 0.15 + random()*0.05
403 Ps[h] = 0.2+random()*0.3
407 Rh[vi] = 0.1 + random()*0.1
412 eta[(h,i)]= etahi(h,i,Rh)
422 # initialisation des operateurs lagrangiens
440 init = [deepcopy(q),deepcopy(Ps),deepcopy(Rh),deepcopy(eta),
441 deepcopy(x),deepcopy(u),deepcopy(v),deepcopy(la),deepcopy(w)]
445 def __evalue_maj_theta__():
446 global u, v, la, w, theta , q, Ps, Rh, eta, x, valeurFonctionDuale
453 om = omega/(mt.pow(k,0.75))
455 for idxexp in range(nbexp):
461 while k < itermax and not arret :
462 (u,v,la,w,theta,eta,q,Ps,Rh,x,valeurFonctionDuale,ar,mxg,smax)=maj(k,__maj_theta,mxg,idxexp)
463 errorq = (max(q.values()) - min(q.values()))/min(q.values())
464 arret = errorq < error
466 variation = "+" if smax > sm else "-"
469 print "k:",k,"erreur sur q", errorq, "et q:",q
472 print "variation trop grande"
479 print "nbre d'iteration trop grand"
484 print "###############"
486 print "###############"
489 print (min(m),max(m),float(sum(m))/nbexp,m),m
493 def __une_seule_exp__(fichier_donees):
494 global u, v, la, w, theta , q, Ps, Rh, eta, x, valeurFonctionDuale
498 fichier = open(fichier_donees, "r")
502 instructions[l[0]] = eval(l[1])
503 u, v, la, w, q, Ps, Rh, eta, x, = instructions['u'], instructions['v'], instructions['la'], instructions['w'], instructions['q'], instructions['Ps'], instructions['Rh'], instructions['eta'], instructions['x']
510 om = omega/(mt.pow(k,0.75))
512 for idxexp in range(nbexp):
517 while k < itermax and not arret :
518 (u,v,la,w,theta,eta,q,Ps,Rh,x,valeurFonctionDuale,ar,mxg,smax)=maj(k,__maj_theta,mxg,idxexp)
519 errorq = (max(q.values()) - min(q.values()))/min(q.values())
520 arret = errorq < error
522 variation = "+" if smax > sm else "-"
525 print "k:",k,"erreur sur q", errorq, "et q:",q
528 print "variation trop grande"
535 print "nbre d'iteration trop grand"
540 print "###############"
542 print "###############"
545 print (min(m),max(m),float(sum(m))/nbexp,m),m
551 __une_seule_exp__("config_initiale.py")
552 #__evalue_maj_theta__()
555 #__evalue_maj_theta__()
571 print "L",valeurFonctionDuale
575 # relation ente les variables primaires et secondaires ?