def display_size_matching_vs_greedy(nmax,p,p2,inst,inter): plt.figure() ax = plt.subplot(111) width=5 stat=np.zeros(shape=(inter,inst)) stat2=np.zeros(shape=(inter,inst)) stat3=np.zeros(shape=(inter,inst)) stat4=np.zeros(shape=(inter,inst)) for i in range(1,inter+1): for j in range(1,inst+1): g=rndm(int(i*nmax/(inter+1)),p) m=len(algo_matching(g)) n=len(algo_greedy(g)) g2=rndm(int(i*nmax/(inter+1)),p2) q=len(algo_matching(g2)) r=len(algo_greedy(g2)) stat[i-1,j-1]= m stat2[i-1,j-1]= n stat3[i-1,j-1]= q stat4[i-1,j-1]= r stat,stat2,stat3,stat4=np.mean(stat,axis=1),np.mean(stat2,axis=1),np.mean(stat3,axis=1) ,np.mean(stat4,axis=1) ax.bar([int(i*nmax/(inter+1)) for i in range(1,inter+1)],list(stat2),width,label='Algo glouton, p='+str(p)) ax.bar([int(i*nmax/(inter+1))+width for i in range(1,inter+1)],list(stat3),width,label='Algo glouton, p='+str(p2)) ax.bar([int(i*nmax/(inter+1))+2*width for i in range(1,inter+1)],list(stat),width,label='Algo couplage, p='+str(p)) ax.bar([int(i*nmax/(inter+1))+3*width for i in range(1,inter+1)],list(stat4),width,label='Algo couplage, p='+str(p2)) plt.legend() plt.ylabel('Taille de la couverture renvoyée') plt.xlabel('Taille des instances de Graph (nb de sommets)') plt.show()
def find_couv_born(g,c_opt,c_curent): binf=g.graph_lower_bound()+len(c_curent) solrea=algo_greedy(g)+list(c_curent) bsup=len(solrea) if (bsup<len(c_opt)): c_opt.clear() c_opt.update(solrea) if binf<len(c_opt) and (binf<bsup): e=g.first_edge() if e is not None: i,j=e gi=g.copy() gi.delete_node(i) gj=g.copy() gj.delete_node(j) c_i=c_curent.copy() c_i.add(i) c_j=c_curent.copy() c_j.add(j) binfi=gi.graph_lower_bound()+len(c_i) binfj=gj.graph_lower_bound()+len(c_j) if binfi<binfj : find_couv_born(gi,c_opt,c_i) find_couv_born(gj,c_opt,c_j) else: find_couv_born(gj,c_opt,c_j) find_couv_born(gi,c_opt,c_i) else: if len(c_curent) < len(c_opt): c_opt.clear() c_opt.update(c_curent)
def iterative_branch_and_bound(g): global minactuel c_opt=np.linspace(0,1,len(g._out)+1) stack = deque([g]) if g is None: return while stack: gcurrent = stack.pop() #print("graphe actuel:") #print(gcurrent._out) e=gcurrent.first_edge() visites=gcurrent._visites solrea=algo_greedy(gcurrent)+visites #print("minactuel: "+str(minactuel)) #print("SOLREA --------") #print(solrea) bornesup=len(solrea) borneinf=gcurrent.graph_lower_bound()+len(visites) #print("Borne inf:") #print(borneinf) if (borneinf>=minactuel): #print(borneinf) #print(minactuel) continue #if (bornesup>minactuel): # continue if (bornesup<len(c_opt)): c_opt=set(solrea) minactuel=bornesup if (bornesup<borneinf): c_opt=set(solrea) minactuel=bornesup #print("Borne inf: "+str(borneinf)) if e is None: if (len(gcurrent._visites) < len(c_opt)): c_opt=set(gcurrent._visites) if (borneinf<minactuel): minactuel=borneinf else: i,j=e #print("(i,j): "+str((i,j))) gi,gj=gcurrent.copy(),gcurrent.copy() gi.delete_node(i) gj.delete_node(j) gi._visites.append(i) gj._visites.append(j) binfi=gi.graph_lower_bound() binfj=gj.graph_lower_bound() #print("binfi, binfj: "+str(binfi)+" "+str(binfj)) #print(gi._out) #print(gj._out) if (binfi<binfj): stack.extend([gj,gi]) else: stack.extend([gi,gj]) return c_opt
def display_time_matching_vs_greedy(nmax,p,p2,inst,inter): stat=np.zeros(shape=(inter,inst)) stat2=np.zeros(shape=(inter,inst)) stat3=np.zeros(shape=(inter,inst)) stat4=np.zeros(shape=(inter,inst)) for i in range(1,inter+1): for j in range(1,inst+1): g=rndm(int(i*nmax/(inter+1)),p) start_time = time.time() algo_matching(g) end_time = time.time() start_time2 = time.time() algo_greedy(g) end_time2 = time.time() g2=rndm(int(i*nmax/(inter+1)),p2) start_time3 = time.time() algo_matching(g2) end_time3 = time.time() start_time4 = time.time() algo_greedy(g2) end_time4 = time.time() stat[i-1,j-1]= end_time -start_time stat2[i-1,j-1]= end_time2 -start_time2 stat3[i-1,j-1]= end_time3 -start_time3 stat4[i-1,j-1]= end_time4 -start_time4 stat,stat2,stat3,stat4=np.mean(stat,axis=1),np.mean(stat2,axis=1),np.mean(stat3,axis=1) ,np.mean(stat4,axis=1) plt.plot([int(i*nmax/(inter+1)) for i in range(1,inter+1)],list(stat4),label='Algorithme glouton, p='+str(p2)) plt.plot([int(i*nmax/(inter+1)) for i in range(1,inter+1)],list(stat2),label='Algorithme glouton, p='+str(p)) plt.plot([int(i*nmax/(inter+1)) for i in range(1,inter+1)],list(stat3),label='Algorithme de couplage, p='+str(p2)) plt.plot([int(i*nmax/(inter+1)) for i in range(1,inter+1)],list(stat),label='Algorithme de couplage, p='+str(p)) plt.legend() plt.yscale("log") plt.ylabel('Moyenne de temps d\'exécution (s)') plt.xlabel('Taille des instances de graphe (nb de sommets)') plt.title("Durée d'exécution des algorithmes couplage et glouton ") plt.show()
def find_couv_born_imp2(g,c_opt,c_curent): binf=g.graph_lower_bound()+len(c_curent) solrea=algo_greedy(g)+list(c_curent) bsup=len(solrea) if (bsup<len(c_opt)): c_opt.clear() c_opt.update(solrea) if binf<len(c_opt) and (binf<bsup): e=g.first_edge() if e is not None: i,j=e degi=len(g._out[i]) degj=len(g._out[j]) gi=g.copy() gi.delete_node(i) gj=g.copy() gj.delete_node(j) c_i=c_curent.copy() c_i.add(i) c_j=c_curent.copy() c_j.add(j) binfi=gi.graph_lower_bound()+len(c_i) binfj=gj.graph_lower_bound()+len(c_j) if binfi<binfj or degi>degj: find_couv_born_imp2(gi,c_opt,c_i) c_j.update(set(gj._out[i])) gj.delete_nodes([i]+gj._out[i]) find_couv_born_imp2(gj,c_opt,c_j) else: if binfi>binfj or degi<degj: find_couv_born_imp2(gj,c_opt,c_j) c_i.update(set(gi._out[j])) gi.delete_nodes([j]+gi._out[j]) find_couv_born_imp2(gi,c_opt,c_i) else: if len(c_curent) < len(c_opt): c_opt.clear() c_opt.update(c_curent)