main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. * @file tests/boost_graph/partitioning/main.cpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #include <tests/boost_graph/partitioning/gggp.hpp>
  26. #include <tests/boost_graph/partitioning/graph_build.hpp>
  27. #include <boost/graph/copy.hpp>
  28. #include <boost/timer.hpp>
  29. #include <fstream>
  30. #include <iostream>
  31. using namespace paradevs::tests::boost_graph;
  32. UnorientedGraph::vertex_iterator vertexIt, vertexEnd;
  33. UnorientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  34. OrientedGraph::vertex_iterator vertexIto, vertexEndo;
  35. OrientedGraph::adjacency_iterator neighbourIto, neighbourEndo;
  36. int main()
  37. {
  38. boost::timer t;
  39. srand((unsigned)time(NULL));
  40. /*std::vector<std::pair < double, int >> tab_diff;
  41. for(int i =0; i<5; i++){
  42. std::pair<double, int> diff(rand_fini(0,15)+0.426,i);
  43. tab_diff.push_back(diff);
  44. std::cout<<tab_diff.at(i).first<<" "<<tab_diff.at(i).second<<std::endl;
  45. }
  46. std::cout<<std::endl;
  47. sort(tab_diff.begin(),tab_diff.end());
  48. for(int i =0; i<5; i++){
  49. std::cout<<tab_diff.at(i).first<<" "<<tab_diff.at(i).second<<std::endl;
  50. }
  51. std::vector<int> Diff;
  52. for(int i =0; i<tab_diff.size(); i++){
  53. Diff.push_back(tab_diff.at(i).second);
  54. }
  55. for(int i =0; i<tab_diff.size(); i++){
  56. std::cout<<Diff.at(i)<<std::endl;
  57. }*/
  58. OrientedGraph* go = new OrientedGraph();
  59. /*UnorientedGraph graph_origin;
  60. build_graph(*go, 11);
  61. UnorientedGraph *g = new UnorientedGraph();
  62. make_unoriented_graph(*go, *g);
  63. EntiersEntiers Partition;
  64. Entiers *part1 = new Entiers();
  65. Entiers *part2 = new Entiers();
  66. for(uint i = 0; i<11; i ++){
  67. part1->push_back(i);
  68. }
  69. Partition.push_back(part1);
  70. int node = 1;
  71. int index = 0;
  72. gggp_pond(g, part1, part2, Partition, node, index, "cut", 2);
  73. Partition.push_back(part2);
  74. double cut = Cut_cluster(Partition, *g, "cut");
  75. for(int x =0; x<Partition.size(); x++){
  76. std::cout<<"** ";
  77. for(int y =0; y<Partition.at(x)->size();y++){
  78. std::cout<<Partition.at(x)->at(y)<<" ";
  79. }
  80. std::cout<<std::endl;
  81. }
  82. std::cout<<std::endl;
  83. std::cout<<"cut : "<<cut<<std::endl;
  84. for(EntiersEntiers::iterator it = Partition.begin(); it != Partition.end(); it++)
  85. {
  86. delete *it;
  87. *it = NULL;
  88. }
  89. EntiersEntiers Partition2;
  90. Entiers *part3 = new Entiers();
  91. Entiers *part4 = new Entiers();
  92. for(uint i = 0; i<11; i ++){
  93. part3->push_back(i);
  94. }
  95. Partition2.push_back(part3);
  96. gggp_pond(g, part3, part4, Partition2, node, index, "ratio", 2);
  97. Partition2.push_back(part4);
  98. cut = Cut_cluster(Partition2, *g, "ratio");
  99. for(int x =0; x<Partition2.size(); x++){
  100. std::cout<<"** ";
  101. for(int y =0; y<Partition2.at(x)->size();y++){
  102. std::cout<<Partition2.at(x)->at(y)<<" ";
  103. }
  104. std::cout<<std::endl;
  105. }
  106. std::cout<<std::endl;
  107. std::cout<<"cut : "<<cut<<std::endl;
  108. for(EntiersEntiers::iterator it = Partition2.begin(); it != Partition2.end(); it++)
  109. {
  110. delete *it;
  111. *it = NULL;
  112. }*/
  113. std::vector<std::string> color;
  114. color.push_back("node [color=lightblue2, style=filled];");
  115. color.push_back("node [color=red, style=filled];");
  116. color.push_back("node [color=limegreen, style=filled];");
  117. color.push_back("node [color=plum, style=filled];");
  118. color.push_back("node [color=orange, style=filled];");
  119. color.push_back("node [color=yellow2, style=filled];");
  120. color.push_back("node [color=slateblue1, style=filled];");
  121. color.push_back("node [color=plum, style=filled];");
  122. color.push_back("node [color=black, style=filled];");
  123. color.push_back("node [color=dimgrey, style=filled];");
  124. color.push_back("node [color=gold, style=filled];");
  125. color.push_back("node [color=indigo, style=filled];");
  126. color.push_back("node [color=turquoise, style=filled];");
  127. color.push_back("node [color=olive, style=filled];");
  128. color.push_back("node [color=cyan, style=filled];");
  129. color.push_back("node [color=crimson, style=filled];");
  130. color.push_back("node [color=lightsalmon, style=filled];");
  131. int nbr_sommets = 10000;
  132. int nbr_sources = nbr_sommets/100*1; // 1% des sommets sont des sources
  133. Entiers niveau = {5,4,3,2};
  134. //build_graph(*go, 38);
  135. //build_generator_graph(go, nbr_sommets, nbr_sources , 2 , 3 ,niveau);
  136. build_generator_graph_linked(go, nbr_sommets, 100 , 2, 3);
  137. //build_graph_cyclique(*go);
  138. //const char *texte = new const char();
  139. //texte = "../../sortie_graphe/enregistrement_100.txt";
  140. //Text_generator_graph(texte,go);
  141. //Graph_constructor_txt(texte, go);
  142. std::ofstream fichier ("../../sortie_graphe/Tests/Graphes/txt/graph_linked_200_30.txt", std::ios::out);
  143. fichier<<"digraph G {"<<std::endl;
  144. tie(vertexIto, vertexEndo) = vertices(*go);
  145. for (; vertexIto != vertexEndo; ++vertexIto) {
  146. fichier<<(*go)[*vertexIto]._index<<"-> {";
  147. //std::cout<<(*go)[*vertexIto]._index<<" -> ";
  148. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  149. *go);
  150. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  151. fichier<<(*go)[*neighbourIto]._index<<";";
  152. //std::cout<<(*go)[*neighbourIto]._index<<" ";
  153. }
  154. fichier<<"}"<<std::endl;
  155. //std::cout<<std::endl;
  156. }
  157. fichier<<"}";
  158. fichier.close();
  159. //std::cout<<std::endl;
  160. int nbr_parties = 50;
  161. int niveau_contraction = num_vertices(*go)/50;
  162. std::vector<double> Cut;
  163. int nbr_tirage = 20;
  164. for(uint i =0; i<nbr_tirage; i++){
  165. Edges edge_partie;
  166. OutputEdgeList outputedgeslist(nbr_parties);
  167. InputEdgeList inputedgelist;
  168. Connections connections;
  169. UnorientedGraph *g = new UnorientedGraph();
  170. UnorientedGraph graph_origin;
  171. make_unoriented_graph(*go, *g);
  172. boost::copy_graph(*g, graph_origin);
  173. OrientedGraphs graphs = Multiniveau(niveau_contraction, g, &graph_origin, go, nbr_parties, 10,"HEM", "gggp",
  174. "diff", "ratio", edge_partie ,
  175. outputedgeslist, inputedgelist,
  176. connections,false,Cut,2);
  177. //std::cout<<std::endl;
  178. //std::cout<<std::endl;
  179. }
  180. //std::ofstream fichier_cut ("../../sortie_graphe/Tests/Cut/cut_s200_nc10_d0_c1_t10_p8_gggp.txt", std::ios::out);
  181. double moy = 0.;
  182. for(int cpt = 0; cpt <Cut.size(); cpt ++){
  183. //if(cpt!=Cut.size()-1)
  184. //fichier_cut<<Cut.at(cpt)<<";";
  185. //else
  186. //fichier_cut<<Cut.at(cpt);
  187. moy += Cut.at(cpt);
  188. //std::cout<<Cut.at(cpt)<<std::endl;
  189. }
  190. //fichier_cut.close();
  191. moy/= Cut.size();
  192. std::cout<<moy<<std::endl;
  193. /*int nbr_parties = 20;
  194. int nbr_sommets = 200;
  195. int nbr_sources = nbr_sommets/100*1; // 1% des sommets sont des sources
  196. const char *texte = new const char();
  197. texte = "../../sortie_graphe/enregistrement_200.txt";
  198. //build_graph(*go, 38);
  199. Entiers niveau = {2,2};
  200. //build_generator_graph(go, nbr_sommets, nbr_sources , 2 , 3 ,niveau);
  201. //Text_generator_graph(texte,go);
  202. Graph_constructor_txt(texte, go);
  203. //make_unoriented_graph(*go, *g);
  204. std::ofstream fichier ("../../sortie_graphe/Tests/arbre_s200_n22_tests.txt", std::ios::out);
  205. fichier<<"digraph G {"<<std::endl;
  206. tie(vertexIto, vertexEndo) = vertices(*go);
  207. for (; vertexIto != vertexEndo; ++vertexIto) {
  208. fichier<<(*go)[*vertexIto]._index<<"-> {";
  209. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  210. *go);
  211. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  212. fichier<<(*go)[*neighbourIto]._index<<";";
  213. }
  214. fichier<<"}"<<std::endl;
  215. }
  216. fichier<<"}";
  217. fichier.close();
  218. std::vector<double> Cut;
  219. int niveau_contraction = num_vertices(*go)/1;
  220. for(uint i =0; i<500; i++){
  221. Edges edge_partie;
  222. OutputEdgeList outputedgeslist(nbr_parties);
  223. InputEdgeList inputedgelist;
  224. Connections connections;
  225. UnorientedGraph *g = new UnorientedGraph();
  226. make_unoriented_graph(*go, *g);
  227. boost::copy_graph(*g, graph_origin);
  228. OrientedGraphs graphs = Multiniveau(niveau_contraction, g, &graph_origin, go, nbr_parties, 20,"HEM", "ggp",
  229. "cut", "ratio", edge_partie ,
  230. outputedgeslist, inputedgelist,
  231. connections,Cut,4);
  232. }
  233. std::ofstream fichier_cut ("../../sortie_graphe/Tests/Cut/cut_s200_n22_d4-20_c1_t500_p20_ggp.txt", std::ios::out);
  234. double moy = 0.;
  235. for(int cpt = 0; cpt <Cut.size(); cpt ++){
  236. if(cpt!=Cut.size()-1)
  237. fichier_cut<<Cut.at(cpt)<<";";
  238. else
  239. fichier_cut<<Cut.at(cpt);
  240. moy += Cut.at(cpt);
  241. //std::cout<<Cut.at(cpt)<<std::endl;
  242. }
  243. fichier_cut.close();
  244. moy/= Cut.size();
  245. double etendu = *max_element(Cut.begin(),Cut.end()) - *min_element(Cut.begin(),Cut.end());
  246. std::cout<<std::endl;
  247. std::cout<<" *** Partitionnement en "<<nbr_parties<<" parties d'un graphe de taille "<<2000<<" *** "<<std::endl;
  248. std::cout<<std::endl;
  249. std::cout<<" === 1 tirage === "<<std::endl;
  250. std::cout<<"Cout de coupe min : "<<*min_element(Cut.begin(),Cut.end())<<" "<<"Cout de coupe max : "<<*max_element(Cut.begin(),Cut.end())<<std::endl;
  251. std::cout<<"Cout de coupe normalisé moyen : "<<moy<<std::endl;
  252. std::cout<<"Étendu du coût de coupe : "<<etendu<<std::endl;
  253. std::cout << "Duration : " << t.elapsed() << " seconds" << std::endl;
  254. std::cout << std::endl;*/
  255. /*for(int i =0; i<graphs.size(); i++){
  256. fichier2<<color.at(i)<<std::endl; C
  257. tie(vertexIto, vertexEndo) = vertices(graphs.at(i));
  258. for (; vertexIto != vertexEndo; ++vertexIto) {
  259. fichier2<<(graphs.at(i))[*vertexIto]._index<<"-> {";
  260. //std::cout<<(graphs.at(i))[*vertexIto]._index<<" est connecté avec : ";
  261. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  262. graphs.at(i));
  263. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  264. fichier2<<(graphs.at(i))[*neighbourIto]._index<<";";
  265. //std::cout<<(graphs.at(i))[*neighbourIto]._index<<" ";
  266. }
  267. fichier2<<"}"<<std::endl;
  268. //std::cout<<std::endl;
  269. }
  270. fichier2<<std::endl;
  271. //std::cout<<std::endl;
  272. }
  273. fichier2<<"}"<<std::endl;
  274. fichier2.close();
  275. delete go;*/
  276. std::cout << "Duration : " << t.elapsed()/nbr_tirage << " seconds" << std::endl;
  277. return 0;
  278. }