Просмотр исходного кода

Modification of contraction methods in order to choice the number of contracted edges.

Christopher Herbez 10 лет назад
Родитель
Сommit
767ee7cae1

+ 1 - 0
src/tests/boost_graph/graph_builder.hpp

@@ -132,6 +132,7 @@ public:
         boost::add_edge(v29, v27, 1., graph);
         boost::add_edge(v29, v28, 1., graph);
         boost::add_edge(v30, v27, 1., graph);
+        boost::add_edge(v30, v28, 1., graph);
         boost::add_edge(v31, v32, 1., graph);
         boost::add_edge(v31, v10, 1., graph);
         boost::add_edge(v32, v33, 1., graph);

+ 5 - 9
src/tests/boost_graph/graph_partitioning.hpp

@@ -44,7 +44,8 @@ public:
                OutputEdgeList& output_edges,
                Connections& parent_connections)
     {
-        srand(7266);
+        srand((unsigned)time(NULL));
+    	//srand(7266);
 
         UnorientedGraph* g = new UnorientedGraph();
         OrientedGraph* go = new OrientedGraph();
@@ -56,25 +57,20 @@ public:
 
         delete gop;
 
-        int nbr_parties = 8;
+        int nbr_parties = 12;
         Edges edge_partie;
         Connections connections;
 
-        /*
-         * teste sur la fonction gggp_pond amélioration + validation complete
-         * sur un nombre de parties supérieur à 6. Recherche de l'origine de
-         * l'erreur de segmentation et la corriger
-         */
-
         output_edges = OutputEdgeList(nbr_parties);
 
-        graphs = Multiniveau(num_vertices(*g)/4, g, graph_origin, go, nbr_parties,"rand", "gggp_pond",
+        graphs = Multiniveau(num_vertices(*g)/2, g, graph_origin, go, nbr_parties,"rand", "gggp_pond",
                              "cut_norm", "norm", edge_partie ,
                              output_edges, input_edges,
                              parent_connections);
         //std::cout<<"Sorti ! "<<std::endl;
 
         delete graph_origin;
+        delete go;
 
     }
 };

+ 52 - 10
src/tests/boost_graph/partitioning/gggp.cpp

@@ -469,12 +469,16 @@ OrientedGraphs Multiniveau(uint niveau_contraction,
     baseg.push_back(g);
     ListEntiersEntiers liste_corr;
     uint cpt =0;
-    while(num_vertices(*baseg.at(cpt))>niveau_contraction)
+
+    int val_cpt = num_vertices(*g);
+    bool stop = false;
+
+    while(stop != true)
     {
     	if(contraction == "HEM")
-    		contraction_HEM(baseg.at(cpt),baseg,liste_corr);
+    		stop = contraction_HEM(baseg.at(cpt),baseg,liste_corr,niveau_contraction,val_cpt);
     	else
-    		contraction_Random_Maching(baseg.at(cpt),baseg,liste_corr);
+    		stop = contraction_Random_Maching(baseg.at(cpt),baseg,liste_corr,niveau_contraction,val_cpt);
         cpt++;
         std::cout<<"passage"<<std::endl;
     }
@@ -494,15 +498,53 @@ OrientedGraphs Multiniveau(uint niveau_contraction,
         }
         std::cout << std::endl;
     }
-
+    UnorientedGraph *gtmp = new UnorientedGraph();
+    *gtmp = *baseg.at(baseg.size() - 1);
     std::cout<<"Partitionnement "<<std::endl;
     if(type_methode == "gggp_pond" || type_methode == "gggp"){
-        for(uint i = 0;i < num_vertices(*baseg.at(baseg.size() - 1)); i++)
-        {
-            part->push_back(i);
-        }
-        Partition.push_back(part);
-    	bissectionRec(baseg.at(baseg.size()-1),Partition,nbr_parties,type_methode);
+		for(uint i = 0;i < num_vertices(*baseg.at(baseg.size() - 1)); i++)
+		{
+			part->push_back(i);
+		}
+		Partition.push_back(part);
+		bissectionRec(baseg.at(baseg.size()-1),Partition,nbr_parties,type_methode);
+		double cut_norm = Cut_cluster(Partition,*gtmp,"norm");
+		std::cout<<"Cout de coupe normalisé initial : "<<cut_norm<<std::endl;
+		int cpt_part = 0;
+		while (cpt_part!=3){
+			EntiersEntiers new_Partition;
+			Entiers *new_part = new Entiers();
+			for(uint i = 0;i < num_vertices(*baseg.at(baseg.size() - 1)); i++)
+			{
+				new_part->push_back(i);
+			}
+			new_Partition.push_back(new_part);
+			bissectionRec(baseg.at(baseg.size()-1),new_Partition,nbr_parties,type_methode);
+			double new_cut_norm = Cut_cluster(new_Partition,*gtmp,"norm");
+			std::cout<<"Nouveau cout de coupe normalisé : "<<new_cut_norm<<std::endl;
+
+			if(new_cut_norm<cut_norm){
+				std::cout<<"Changement !!!"<<std::endl;
+				for(EntiersEntiers::iterator it = Partition.begin(); it != Partition.end(); it++)
+				{
+					delete *it;
+					*it = NULL;
+				}
+				Partition = new_Partition;
+				cut_norm = new_cut_norm;
+			}
+			else{
+				for(EntiersEntiers::iterator it = new_Partition.begin(); it != new_Partition.end(); it++)
+				{
+					delete *it;
+					*it = NULL;
+				}
+			}
+			cpt_part++;
+		}
+		std::cout<<std::endl;
+		std::cout<<"Cout de coupe normalisé conservé : "<<cut_norm<<std::endl;
+		delete gtmp;
     }
     else
     	Partition = Random_partitioning(baseg.at(baseg.size()-1),nbr_parties);

+ 1 - 1
src/tests/boost_graph/partitioning/graph_build.cpp

@@ -125,6 +125,7 @@ void build_graph(UnorientedGraph& ug, OrientedGraph& og)
     add_edge(v29, v27, EdgeProperties(1.), og);
     add_edge(v29, v28, EdgeProperties(1.), og);
     add_edge(v30, v27, EdgeProperties(1.), og);
+    add_edge(v30, v28, EdgeProperties(1.), og);
     add_edge(v31, v32, EdgeProperties(1.), og);
     add_edge(v31, v10, EdgeProperties(1.), og);
     add_edge(v32, v33, EdgeProperties(1.), og);
@@ -264,7 +265,6 @@ void build_graph(UnorientedGraph& ug, OrientedGraph& og)
     boost::add_edge(v151, v161, EdgeProperties(1.), ug);
     boost::add_edge(v161, v171, EdgeProperties(1.), ug);
     boost::add_edge(v81, v111, EdgeProperties(1.), ug);
-    boost::add_edge(v121, v131, EdgeProperties(1.), ug);
     boost::add_edge(v81, v181, EdgeProperties(1.), ug);
     boost::add_edge(v81, v191, EdgeProperties(1.), ug);
     boost::add_edge(v81, v201, EdgeProperties(1.), ug);

+ 6 - 3
src/tests/boost_graph/partitioning/main.cpp

@@ -43,7 +43,7 @@ int main()
 {
     boost::timer t;
 
-    srand(7266);
+    srand((unsigned)time(NULL));
 
     UnorientedGraph* g = new UnorientedGraph();
     OrientedGraph* go = new OrientedGraph();
@@ -55,7 +55,7 @@ int main()
 
     delete gop;
 
-    int nbr_parties = 38;
+    int nbr_parties = 4;
 
     /*EntiersEntiers Partition = Random_partitioning(g,nbr_parties);
 
@@ -73,7 +73,9 @@ int main()
     InputEdgeList inputedgelist;
     Connections connections;
 
-    OrientedGraphs graphs = Multiniveau(38, g, graph_origin, go, nbr_parties,"HEM", "gggp_pond",
+    std::cout<<num_vertices(*g)/4<<std::endl;
+
+    OrientedGraphs graphs = Multiniveau(num_vertices(*g)/2, g, graph_origin, go, nbr_parties,"rand", "gggp_pond",
                                         "cut_norm", "norm", edge_partie ,
                                         outputedgeslist, inputedgelist,
                                         connections);
@@ -123,6 +125,7 @@ int main()
     }
 
 	delete graph_origin;
+	delete go;
 
     std::cout << "Duration : " << t.elapsed() << " seconds" << std::endl;
 

+ 59 - 7
src/tests/boost_graph/partitioning/utils.cpp

@@ -463,7 +463,7 @@ void Affinage_recherche_locale(UnorientedGraph *g, EntiersEntiers &Partition, do
                 }
                 std::cout<<"\n"<<std::endl;*/
                 double cut_min = *min_element(tmp_cut.begin(),tmp_cut.end());
-                std::cout<<"cout de coupe minimum de la liste : "<<cut_min<<std::endl;
+                //std::cout<<"cout de coupe minimum de la liste : "<<cut_min<<std::endl;
                 if(cut_min<cut){
                     std::clog<<"Changement ! "<<std::endl;
                     int tmp = recherche_val_double(tmp_cut,cut_min);
@@ -659,7 +659,7 @@ void Modif_fonction_Gain_Cut(EntiersEntiers &Partition,UnorientedGraph *g, Entie
 	}
 }
 
-void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr){
+bool contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
 	UnorientedGraph *gtmp = new UnorientedGraph();
 	*gtmp=*g;
 	Entiers Random_list_vertices; // Initialisation du tableau de sommets rangés aléatoirements
@@ -686,6 +686,7 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 	 */
 	for(uint i=0; i<nbr_vertex; i++){
 		int vertexs = Random_list_vertices[i];
+		//std::cout<<"Le sommet tiré est : "<<vertexs<<std::endl;
 		if(vertexs!=-1){
 			Entiers liste_voisin = Liste_adjacence(*gtmp,vertexs,Random_list_vertices); // Recherche des sommets adjacents au sommets  tiré
 			if(liste_voisin.size()!=0){
@@ -721,7 +722,7 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 
 				remove_edge(vertex_save,vertex_delete,*gtmp); // Suppression de l'arc reliant le couple de sommets
 
-				Entiers neigh_vertex_save; // Initialisation du vecteur contenant les somemts adjacents au "sommet sauvegardé"
+				Entiers neigh_vertex_save; // Initialisation du vecteur contenant les sommets adjacents au "sommet sauvegardé"
 				Entiers neigh_vertex_delete; // Initialisation du vecteur contenant les somemts adjacents au "sommet à détruire"
 				tie(neighbourIt, neighbourEnd) = adjacent_vertices(vertex_save,*gtmp);
 
@@ -765,6 +766,8 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 				 */
 				Random_list_vertices[i]=-1;
 				Random_list_vertices[recherche_val(Random_list_vertices,best_vertexs)]=-1;
+				val_cpt--;
+				std::cout<<val_cpt<<std::endl;
 			}
 			else{
 				/*
@@ -777,11 +780,36 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 				tableau_de_correspondance->push_back(couple);
 				Random_list_vertices[i]=-1;
 			}
+
+
+
+			/*std::cout<<"Graphe contracté : "<<std::endl;
+			tie(vertexIt, vertexEnd) = vertices(*gtmp);
+			for (; vertexIt != vertexEnd; ++vertexIt) {
+				std::cout << (*gtmp)[*vertexIt]._index
+						<< " est connecté avec ";
+				tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,
+						*gtmp);
+				for (; neighbourIt != neighbourEnd; ++neighbourIt)
+					std::cout << (*gtmp)[*neighbourIt]._index << " ";
+				std::cout << " et son poids est de "
+						<< (*gtmp)[*vertexIt]._weight<<std::endl;
+			}
+			std::cout << std::endl;*/
+		}
+		if(val_cpt == val_reduc){
+			for(uint j=i+1; j <nbr_vertex; j++){
+				if(Random_list_vertices[j] !=-1){
+				Entiers *couple = new Entiers();
+				couple->push_back(Random_list_vertices.at(j));
+				tableau_de_correspondance->push_back(couple);}
+			}
+			break;
 		}
 	}
 
 	std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
-	//std::cout<<"\n"<<std::endl;
+	std::cout<<"\n"<<std::endl;
 	/*
 	 * Suppression des sommets de la liste "sommets à détruire". Cette suppression est
 	 * effectuée dans l'ordre décroissant afin à maintenir à jour la renumérotation
@@ -792,13 +820,13 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 		remove_vertex(sommets_a_detruire[j],*gtmp);
 	}
 
-	/**std::clog<<"Affichage avant tri "<<std::endl;
+	std::clog<<"Affichage avant tri "<<std::endl;
 	for(uint k = 0;k<tableau_de_correspondance->size();k++){
 		for(uint v = 0; v<tableau_de_correspondance->at(k)->size();v++){
 			std::cout<<tableau_de_correspondance->at(k)->at(v)<<" ";
 		}
 		std::cout<<"\n"<<std::endl;
-	}*/
+	}
 	std::sort(tableau_de_correspondance->begin(),tableau_de_correspondance->end(),myobject); // Trie dans l'ordre croissant des couples de sommets de la liste de correspondance
 
 	std::clog<<"Tableau de correspondance "<<std::endl;
@@ -812,9 +840,15 @@ void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &
 	liste_corr.push_back(tableau_de_correspondance);
 	std::cout<<"\n"<<std::endl;
 	baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
+
+	if(val_cpt == val_reduc)
+		return true;
+	else
+		return false;
+
 }
 
-void contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr){
+bool contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt){
 	UnorientedGraph *gtmp = new UnorientedGraph();
 	*gtmp=*g;
 	Entiers Random_list_vertices; // Initialisation du tableau de sommets rangés aléatoirements
@@ -917,6 +951,8 @@ void contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntie
 				 */
 				Random_list_vertices[i]=-1;
 				Random_list_vertices[recherche_val(Random_list_vertices,best_vertexs)]=-1;
+				val_cpt--;
+				std::cout<<val_cpt<<std::endl;
 			}
 			else{
 				/*
@@ -930,6 +966,15 @@ void contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntie
 				Random_list_vertices[i]=-1;
 			}
 		}
+		if(val_cpt == val_reduc){
+			for(uint j=i+1; j <nbr_vertex; j++){
+				if(Random_list_vertices[j] !=-1){
+				Entiers *couple = new Entiers();
+				couple->push_back(Random_list_vertices.at(j));
+				tableau_de_correspondance->push_back(couple);}
+			}
+			break;
+		}
 	}
 
 	std::sort(sommets_a_detruire.begin(), sommets_a_detruire.end()); // Trie dans l'ordre croissant des "sommets à détruire"
@@ -964,6 +1009,13 @@ void contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg, ListEntie
 	liste_corr.push_back(tableau_de_correspondance);
 	std::cout<<"\n"<<std::endl;
 	baseg.push_back(gtmp); // Ajout du graphe modifié à la "base des graphe"
+
+
+	if(val_cpt == val_reduc)
+		return true;
+	else
+		return false;
+
 }
 
 Entiers Liste_adjacence(UnorientedGraph &g, int vertexs,const Entiers &random_vertices){ // a revoir !!!!

+ 4 - 4
src/tests/boost_graph/partitioning/utils.hpp

@@ -73,10 +73,10 @@ void Affinage_recherche_locale(UnorientedGraph *g, EntiersEntiers &Partition,
 
 void projection(EntiersEntiers &Partition,ListEntiersEntiers::iterator lit);
 
-void contraction_HEM(UnorientedGraph *g, Base_Graph &baseg,
-                     ListEntiersEntiers &liste_corr);
-void contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg,
-								ListEntiersEntiers &liste_corr);
+bool contraction_HEM(UnorientedGraph *g, Base_Graph &baseg,
+                     ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt);
+bool contraction_Random_Maching(UnorientedGraph *g, Base_Graph &baseg,
+								ListEntiersEntiers &liste_corr, int val_reduc, int &val_cpt);
 
 Entiers Liste_adjacence(UnorientedGraph &g, int vertexs,
                         const Entiers &random_vertices);