def test_build_matrice_of_subgraph(graphes_GR_LG): dico = dict(); print("TEST build_matrice_of_subgraph => debut") for graphe_GR_LG in graphes_GR_LG: mat_LG = graphe_GR_LG[1]; prob = graphe_GR_LG[5]; num_graph = graphe_GR_LG[8]+"_p_"+str(prob); sommets = creat_gr.sommets_mat_LG(mat_LG, etat=0); id_nom_som, nom_sommet_alea = random.choice(list( enumerate(sommets.keys()))) mat_subgraph = algo_couv.build_matrice_of_subgraph( nom_sommet=nom_sommet_alea, sommets_k_alpha=sommets) aretes_LG = fct_aux.aretes(mat_GR=mat_LG, orientation=False, val_0_1=1) aretes_subgraph = fct_aux.aretes(mat_GR=mat_subgraph, orientation=False, val_0_1=1) aretes_int = aretes_LG.intersection(aretes_subgraph); res="" if aretes_int == aretes_subgraph: res = "OK" else: res = "NOK" dico[num_graph]={"res":res} print("TEST build_matrice_of_subgraph => FIN") return pd.DataFrame.from_dict(dico).T;
def test_grouped_cliques_by_node(graphes_GR_LG): etat = 2; dico_df = dict() for graphe_GR_LG in graphes_GR_LG: num_graph = graphe_GR_LG[8]+"_p_"+str(graphe_GR_LG[5]); mat_LG = graphe_GR_LG[1]; #mat_GR = graphe_GR_LG[0]; aretes_LG = fct_aux.aretes(mat_LG); sommets_LG = creat_gr.sommets_mat_LG(mat_LG) #test cliques_covers cliqs_couverts, aretes, sommets = \ algo_couv.clique_covers(mat_LG, aretes_LG, sommets_LG,True); noms_sommets = fct_aux.node_names_by_state(sommets=sommets, etat_1=etat) dico_cliques = fct_aux.grouped_cliques_by_node( cliques=cliqs_couverts, noms_sommets_1=noms_sommets) dico_df[num_graph]={"noms_sommets":noms_sommets} for nom, liste_cliques in dico_cliques.items(): dico_df[num_graph][nom] = liste_cliques; return pd.DataFrame.from_dict(dico_df).T
def test_sommets_mat_LG(nbre_sommets_GR = 5, nbre_moyen_liens = (2,5), chemin_matrice = "tests/matrices/") : mat_LG, mat_GR = creat_gr.generer_reseau(nbre_sommets_GR, nbre_moyen_liens, chemin_matrice) sommets = creat_gr.sommets_mat_LG(mat_LG); sommet = np.random.choice(mat_LG.columns) voisins = set(sommets[sommet].voisins); test = True while len(voisins) != 0: voisin = voisins.pop() if mat_LG.loc[sommet, voisin] != 1 : test = False; if test : print("TEST : sommets_mat_LG = OK") else : print("TEST : sommets_mat_LG = NOK")
def execute_algos(mat_GR, mat_LG, chemin_matrice, mode, critere, prob, k_erreur, nbre_graphe, num_graph_G_k, alpha, number_items_pi1_pi2, DBG): """ executer les algorithmes de couverture et de correction selon les parametres. """ print("num_graph_G_k={} <=== debut ===>".format(num_graph_G_k)) start_G_k = time.time() results_k_alpha = [] moy_dh, moy_dc = 0, 0 sum_dh, sum_dc = np.inf, np.inf aretes_LG = fct_aux.aretes(mat_LG, orientation=False, val_0_1=1) for alpha_ in range(0, alpha): result_k_alpha = None mat_LG_k_alpha, aretes_LG_k_alpha, aretes_modifiees = \ add_remove_edges( mat_LG, aretes_LG, k_erreur, prob) mat_LG_k_alpha.to_csv(chemin_matrice + \ NOM_MATE_LG_k_alpha + str(alpha_) + EXTENSION, index_label = INDEX_COL_MATE_LG) sommets_k_alpha = creat_gr.sommets_mat_LG(mat_LG_k_alpha) # algo couverture cliques_couvertures, aretes_LG_k_alpha_res, sommets_k_alpha_res = \ algoCouverture.clique_covers( mat_LG_k_alpha, aretes_LG_k_alpha, sommets_k_alpha, DBG) sommets_trouves_couv = [] sommets_absents_couv = set() etat0_couv, etat1_couv, etat_1_couv, etat2_couv, etat3_couv, cliqs_couv = \ set(), set(), set(), set(), set(), set() sommets_trouves_couv, sommets_absents_couv, \ etat0_couv, etat1_couv, etat_1_couv, etat2_couv, etat3_couv = \ analyse_resultat(cliques_couvertures, sommets_k_alpha_res, set(mat_GR.columns)) # algo de correction sommets_trouves_cor = [] sommets_absents_cor = set() etat0_cor, etat1_cor, etat_1_cor, etat2_cor, etat3_cor, cliqs_cor = \ set(), set(), set(), set(), set(), set() aretes_LG_k_alpha_cor = [] if fct_aux.is_exists_sommet(sommets=sommets_k_alpha, etat_1=-1): aretes_LG_k_alpha_cor = aretes_LG_k_alpha_res.copy() cliques_couvertures_1 = list(cliques_couvertures.copy()) aretes_res_non_effacees = list( map(frozenset, aretes_LG_k_alpha_res)) cliques_couvertures_1.extend(aretes_res_non_effacees) sommets_tmp = creat_gr.sommets_mat_LG(mat_LG_k_alpha) sommets_k_alpha_1 = fct_aux.modify_state_sommets_mat_LG( sommets=sommets_tmp, sommets_res=sommets_k_alpha_res) cliques_couvertures_cor, \ aretes_LG_k_alpha_cor, \ sommets_k_alpha_cor, \ cliques_par_nom_sommets_k_alpha_cor, \ dico_sommets_corriges = \ algoCorrection.correction_algo( cliques_couvertures=set(cliques_couvertures_1), aretes_LG_k_alpha=aretes_LG_k_alpha, sommets_LG=sommets_k_alpha_1, mode_correction=mode, critere_correction=critere, number_items_pi1_pi2=number_items_pi1_pi2, DBG=DBG ) sommets_trouves_cor, sommets_absents_cor, \ etat0_cor, etat1_cor, etat_1_cor, etat2_cor, etat3_cor = \ analyse_resultat(cliques_couvertures_cor, sommets_k_alpha_cor, set(mat_GR.columns)) # calcul distance dc_alpha = len( calculate_hamming_distance(mat_LG=aretes_LG_k_alpha, mat_LG_k=aretes_LG_k_alpha_cor) ) # aretes_LG_k_alpha_ est celle apres correction. On peut ajouter aussi aretes_LG_k_alpha dh_alpha = len( calculate_hamming_distance(mat_LG=aretes_LG, mat_LG_k=aretes_LG_k_alpha_cor)) #resultat d'un execution k_alpha result_k_alpha = ( num_graph_G_k, k_erreur, alpha_, mode, critere, prob, len(sommets_trouves_couv), len(sommets_absents_couv), len(etat0_couv), len(etat1_couv), len(etat_1_couv), len(etat2_couv), len(etat3_couv), len(sommets_trouves_cor), len(sommets_absents_cor), len(etat0_cor), len(etat1_cor), len(etat_1_cor), len(etat2_cor), len(etat3_cor), len(cliqs_couv), len(cliqs_cor), dc_alpha, dh_alpha, ) results_k_alpha.append(result_k_alpha) sum_dc = dc_alpha if sum_dc == np.inf else sum_dc + dc_alpha sum_dh = dh_alpha if sum_dh == np.inf else sum_dh + dh_alpha pass # for alpha_ moy_dc = sum_dc / alpha moy_dh = sum_dh / alpha if moy_dh == 0 and moy_dc == 0: correl_dc_dh = 1 else: correl_dc_dh = abs(moy_dh - moy_dc) / max(moy_dh, moy_dc) # ecrire dans un fichier pouvant etre lu pendant qu'il continue d'etre ecrit nbre_sommets_LG = len(mat_LG.columns) chemin_dist = chemin_matrice + "../.." + "/" + "distribution" + "/" path = Path(chemin_dist) path.mkdir(parents=True, exist_ok=True) if not path.is_dir() else None f = open(chemin_dist + \ "distribution_moyDistLine_moyHamming_k_" + \ str(k_erreur) + \ ".txt","a") f.write(str(num_graph_G_k) + ";" \ + str(k_erreur) + ";" \ + str( round(moy_dc,2) ) + ";" \ + str( round(moy_dh,2) ) + ";" \ # str( len() ) + ";" + \ + str(nbre_sommets_LG) + ";" \ + str(len(aretes_LG)) + ";" \ + str(correl_dc_dh) + ";" \ + str( time.time()-start_G_k ) \ + "\n" ) print("results_k_alpha={}".format(len(results_k_alpha))) print("num_graph_G_k={} <=== Termine :runtime={} ===>".format( num_graph_G_k, round(time.time() - start_G_k, 4))) if DBG: return results_k_alpha else: return [] pass # execute_algo
def test_update_sommets_LG(graphes_GR_LG): """ faire un test pour verifier si les modifications d'un graphe sont repercutees dans sommets_LG. on fera ainsi: - on construit sommets_LG avec mat_LG - on supprime/ajoute k aretes de mat_LG => on obtient mat_LG_k - on applique update_sommets_LG => sommets_LG_new - on verifie que le voisinage des sommets sommets_LG_new a change en comparaison a sommets_LG. """ dico_df = dict(); for graphe_GR_LG in graphes_GR_LG: print("TEST is_state ET selected_node") mat_LG = graphe_GR_LG[1]; prob = graphe_GR_LG[5]; k_erreur = graphe_GR_LG[6]; sommets_LG = creat_gr.sommets_mat_LG(mat_LG, etat=2); aretes_LG = fct_aux.aretes(mat_GR=mat_LG, orientation=False, val_0_1=1) mat_LG_k_alpha, aretes_LG_k_alpha, aretes_modifiees = \ gr_disco_simi.add_remove_edges( mat_LG, aretes_LG, k_erreur, prob) sommets_k_alpha = creat_gr.sommets_mat_LG(mat_LG_k_alpha, etat=2) cliques_couvertures_res, aretes_LG_k_alpha_res, sommets_k_alpha_res = \ algo_couv.clique_covers( mat_LG_k_alpha, aretes_LG_k_alpha, sommets_k_alpha, DBG) if aretes_LG_k_alpha_res: cliques_couvertures_res = cliques_couvertures_res.union( aretes_LG_k_alpha_res) cliques_par_nom_sommets_k = fct_aux.grouped_cliques_by_node( cliques_couvertures_res, sommets_LG.keys()) sommets_new = algo_corr.update_sommets_LG( sommets_LG, cliques_couvertures_res, cliques_par_nom_sommets_k) res_supp, res_ajout = 'OK', 'OK'; for arete_modif in aretes_modifiees['aretes_supprimees']: som_0, som_1 = list(arete_modif)[0], list(arete_modif)[1] if som_0 in sommets_new[som_1].voisins or \ som_1 in sommets_new[som_0].voisins: res_supp = "NOK"; for arete_modif in aretes_modifiees['aretes_ajoutees']: som_0, som_1 = list(arete_modif)[0], list(arete_modif)[1] if som_0 in sommets_LG[som_1].voisins or \ som_1 in sommets_LG[som_0].voisins: res_ajout = "NOK"; # ########33 esprit tordu # res_supp, res_ajout = 'NOK', 'NOK'; # for arete_modif in aretes_modifiees['aretes_supprimees']: # som_0, som_1 = list(arete_modif)[0], list(arete_modif)[1] # if som_0 not in sommets_new[som_1].voisins and \ # som_1 not in sommets_new[som_0].voisins: # res_supp = "OK_s" # # for arete_modif in aretes_modifiees['aretes_ajoutees']: # som_0, som_1 = list(arete_modif)[0], list(arete_modif)[1] # if som_0 not in sommets_LG[som_1].voisins and \ # som_1 not in sommets_LG[som_0].voisins: # res_ajout = "OK_a"; # ########33 num_graph = graphe_GR_LG[8]+"_p_"+str(prob); dico_df[num_graph] = { "nbre_sommets": len(mat_LG.columns), "res_ajout": res_ajout, "res_supp": res_supp } return pd.DataFrame.from_dict(dico_df).T;
def test_modify_state_sommets_mat_LG(graphes_GR_LG): results_k_alpha = [] cols = ['num_graph', 'sommets_trouves_couv','sommets_absents_couv', 'etat0_couv','etat1_couv','etat_1_couv', 'etat2_couv','etat3_couv', 'sommets_trouves_cor','sommets_absents_cor', 'etat0_cor','etat1_cor','etat_1_cor', 'etat2_cor','etat3_cor','res'] for graphe_GR_LG in graphes_GR_LG: num_graph = graphe_GR_LG[8]+"_p_"+str(graphe_GR_LG[5]); mat_LG = graphe_GR_LG[1]; mat_GR = graphe_GR_LG[0]; aretes_LG = fct_aux.aretes(mat_LG); sommets_LG = creat_gr.sommets_mat_LG(mat_LG) #test cliques_covers cliqs_couverts, aretes, sommets = \ algo_couv.clique_covers(mat_LG, aretes_LG, sommets_LG,True); # verif l'etat des sommets apres couverture sommets_trouves_couv=[]; sommets_absents_couv=set(); etat0_couv, etat1_couv, etat_1_couv, etat2_couv, etat3_couv = \ set(), set(), set(), set(), set(); sommets_trouves_couv, sommets_absents_couv, \ etat0_couv, etat1_couv, etat_1_couv, etat2_couv, etat3_couv = \ gr_disco_simi.analyse_resultat(cliqs_couverts, sommets, set(mat_GR.columns)) # test correction sommets_tmp = creat_gr.sommets_mat_LG(mat_LG) sommets_k_alpha_1 = fct_aux.modify_state_sommets_mat_LG( sommets=sommets_tmp, sommets_res=sommets) cliques_couvertures_cor, \ aretes_LG_k_alpha_cor,\ sommets_k_alpha_cor = \ algo_corr.correction_algo( cliques_couverture=set(cliqs_couverts), aretes_LG_k_alpha=aretes_LG, sommets_LG=sommets_k_alpha_1 ) # verif l'etat des sommets apres correction sommets_trouves_cor, sommets_absents_cor, \ etat0_cor, etat1_cor, etat_1_cor, etat2_cor, etat3_cor = \ gr_disco_simi.analyse_resultat(cliques_couvertures_cor, sommets_k_alpha_cor, set(mat_GR.columns)) if etat0_couv == etat0_cor and etat1_couv == etat1_cor and \ etat_1_couv == etat_1_cor and etat2_couv == etat2_cor and \ etat3_couv == etat3_cor : res = "OK" else: res = "NOK" result_k_alpha = (num_graph, len(sommets_trouves_couv),len(sommets_absents_couv), len(etat0_couv),len(etat1_couv),len(etat_1_couv), len(etat2_couv),len(etat3_couv), len(sommets_trouves_cor),len(sommets_absents_cor), len(etat0_cor),len(etat1_cor),len(etat_1_cor), len(etat2_cor),len(etat3_cor), res ) results_k_alpha.append(result_k_alpha); df = pd.DataFrame(results_k_alpha, columns=cols) return df; pass
def test_algo_covers(graphes_GR_LG) : """ test la fonction algo_cover et aussi is_exists_sommet """ f=lambda x: set(x.split("_")) etat_recherche_1 = -1#0,1,2,3,-1; dico_df = dict() for graphe_GR_LG in graphes_GR_LG: prob = graphe_GR_LG[5]; num_graph = graphe_GR_LG[8]+"_p_"+str(prob); mat_LG = graphe_GR_LG[1]; aretes_LG = fct_aux.aretes(mat_LG); sommets_LG = creat_gr.sommets_mat_LG(mat_LG) start = time.time() #test cliques_covers cliqs_couverts, aretes, sommets = \ algo_couv.clique_covers(mat_LG, aretes_LG, sommets_LG,True); # test is_exists_sommet exist_som_1 = None; exist_som_1 = fct_aux.is_exists_sommet(sommets=sommets, etat_1=etat_recherche_1) # test modify_state_sommets_mat_LG => pas fait car je ne sais pas ce que je dois comparer # sommets_tmp = creat_gr.sommets_mat_LG(mat_LG); # sommets_LG_after = fct_aux.modify_state_sommets_mat_LG( # sommets=sommets_tmp, # sommets_res=sommets) runtime = round(time.time() - start, 2); som_trouves=[] for cliq in cliqs_couverts: aretes = list(map(f, cliq)) sommet_commun = None; sommet_commun = set.intersection(*aretes); if sommet_commun != None and len(sommet_commun) == 1: som_trouves.append(sommet_commun.pop()) # calculer le nombre de sommets ayant un etat specifique etat0, etat1, etat_1, etat2, etat3 = set(), set(), set(), set(), set(); for nom_som, sommet in sommets.items(): if sommet.etat == 0: etat0.add(nom_som); elif sommet.etat == 1: etat1.add(nom_som); elif sommet.etat == 2: etat2.add(nom_som); elif sommet.etat == 3: etat3.add(nom_som); elif sommet.etat == -1: etat_1.add(nom_som); mat_GR = graphe_GR_LG[0] som_GR = set(mat_GR.columns) som_absents = som_GR.union(som_trouves) - set(som_trouves) res = "" if som_GR == set(som_trouves): res = 'OK' else: res = 'NOK' print("TEST cliques_cover num_graphe={} runtime={}, ==>res={}, exist_som={}".format( num_graph,runtime,res, exist_som_1)) dico_df[num_graph] = {"res":res, "nbre_som_GR":len(som_GR), "nbre_som_trouves":len(som_trouves), "som_absents":som_absents, "aretes_res":aretes, "etat0": len(etat0), "etat1": len(etat1), "etat2": len(etat2), "etat3": len(etat3), "etat_1": len(etat_1), "exist_som_1": exist_som_1, "runtime":runtime } return pd.DataFrame.from_dict(dico_df).T;
def test_update_edges_neighbor(graphes_GR_LG): dico_df = dict(); for graphe_GR_LG in graphes_GR_LG: mat_LG = graphe_GR_LG[1]; prob = graphe_GR_LG[5]; num_graph = graphe_GR_LG[8]+"_p_"+str(prob); sommets = creat_gr.sommets_mat_LG(mat_LG, etat=0); id_nom_som, nom_sommet_alea = random.choice(list( enumerate(sommets.keys()))) aretes_LG = fct_aux.aretes(mat_GR=mat_LG, orientation=False, val_0_1=1) # print("TEST update_edge num_graph={}".format(num_graph)); cliques = algo_couv.partitionner( sommet = sommets[nom_sommet_alea], sommets_k_alpha = sommets, aretes_LG_k_alpha = aretes_LG, DBG= True ) cliques_coh = [] bool_clique, bool_coherent, cliques_coh = \ algo_couv.verify_cliques( cliques = cliques, nom_sommet = nom_sommet_alea) C1, C2 = set(), set(); if len(cliques_coh) == 1: C1 = cliques_coh[0]; elif len(cliques_coh) == 2: C1, C2 = cliques_coh[0], cliques_coh[1]; aretes_LG_res, sommets = algo_couv.update_edges_neighbor( C1 = C1, C2 = C2, aretes = aretes_LG, sommets = sommets) aretes_supps_res = aretes_LG.union(aretes_LG_res) - aretes_LG_res; # transform sommets to dataframe puis calculer aretes_restantes et # comparer aretes_restantes avec aretes_LG aretes_supps_cal = set(); mat_res = fct_aux.convert_sommet_to_df(sommets_k_alpha=sommets); aretes_restantes = fct_aux.aretes(mat_GR=mat_res, orientation=False, val_0_1=1) aretes_supps_cal = aretes_LG.union(aretes_restantes) - aretes_restantes; res = "" if aretes_supps_cal == aretes_supps_res: res = 'OK'; else: res = 'NOK'; dico_df[num_graph] = {"nom_sommet":nom_sommet_alea, "voisins":set(sommets[nom_sommet_alea].voisins), "cliques":cliques, "cliques_coh":cliques_coh, "aretes_supps_res": aretes_supps_res, "aretes_supps_cal": aretes_supps_cal, "res":res } print("TEST update_edge, num_graph={}, res={}".format(num_graph,res)); return pd.DataFrame.from_dict(dico_df).T;
def test_is_state_selected_node(graphes_GR_LG): """ faire un test pour savoir l etat a attribuer a ce graphe ensuite modifier a 0 et 3 enfin tester si un sommet est a 0 ou 3 """ dico_df = dict(); nbre_etats_3 = 3; nbre_etats_0 = 3; etats_possibles = [0,2,3] print("TEST is_state ET selected_node => debut") for graphe_GR_LG in graphes_GR_LG: mat_LG = graphe_GR_LG[1]; prob = graphe_GR_LG[5]; etat = random.choice(etats_possibles); sommets = creat_gr.sommets_mat_LG(mat_LG, etat=2); nom_sommets = list(sommets.keys()) noms = [] if etat == 0: for nbre_0 in range(nbre_etats_0): id_nom_som, nom_sommet = random.choice(list( enumerate(nom_sommets))) nom_sommets.pop(id_nom_som); noms.append(nom_sommet); sommet = sommets[nom_sommet] sommet.etat = 0; if etat == 3: for nbre_3 in range(nbre_etats_3): id_nom_som, nom_sommet = random.choice(list( enumerate(nom_sommets))) nom_sommets.pop(id_nom_som); noms.append(nom_sommet); sommet = sommets[nom_sommet]; sommet.etat = 3; bool_state = algo_couv.is_state(sommets_k_alpha=sommets, critere0=0, critere3=3); nbre_sommet_0_2_3 = 0; for nom_sommet, sommet in sommets.items(): if sommet.etat == etat: nbre_sommet_0_2_3 += 1; # sommet selectionne selected_sommet = algo_couv.selected_node(sommets_k_alpha= sommets, critere0=0, critere3=3) is_som_etat_modif = False; if selected_sommet != None and selected_sommet.nom in noms: is_som_etat_modif = True; selected_nom_som = selected_sommet.nom if selected_sommet != None else None; num_graph = graphe_GR_LG[8]+"_p_"+str(prob); dico_df[num_graph] = { "nbre_sommets":len(mat_LG.columns), "etat_choisi": etat, "nbre_sommet_0_2_3":nbre_sommet_0_2_3, "bool_state":bool_state, "noms_etat_modifs":noms, "selected_sommet":selected_nom_som, "is_som_etat_modif":is_som_etat_modif } pass # for print("TEST is_state ET selected_node => FIN") return pd.DataFrame.from_dict(dico_df).T;