gggp.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. /**
  2. * @file tests/boost_graph/partitioning/gggp.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 <algorithm>
  27. #include <iostream>
  28. #include <fstream>
  29. namespace paradevs { namespace tests { namespace boost_graph {
  30. extern UnorientedGraph::vertex_iterator vertexIt, vertexEnd;
  31. extern UnorientedGraph::adjacency_iterator neighbourIt, neighbourEnd;
  32. extern OrientedGraph::vertex_iterator vertexIto, vertexEndo;
  33. extern OrientedGraph::adjacency_iterator neighbourIto, neighbourEndo;
  34. void ggp(UnorientedGraph *g, Entiers *sommetsSource,
  35. Entiers *sommetsDestination, EntiersEntiers &Partition, int rand, int distance)
  36. {
  37. //std::cout<<""<<std::endl;
  38. //int val;
  39. Entiers sommets_adj;
  40. if(sommetsSource->size()==1){
  41. //val=0;
  42. //std::cout<<"Entré dans le debug ! "<<std::endl;
  43. Entiers tailles;
  44. for(uint i=0;i<Partition.size();i++){
  45. tailles.push_back(Partition.at(i)->size());
  46. }
  47. uint tmp=*max_element(tailles.begin(),tailles.end());
  48. for(uint i=0; i<Partition.size();i++){
  49. if(Partition.at(i)->size()==tmp)
  50. {
  51. ggp(g,Partition[i],sommetsDestination,Partition,rand_fini(0,Partition.at(i)->size()), distance);
  52. return;
  53. }
  54. }
  55. }
  56. // else
  57. // val=rand_fini(0,sommetsSource->size()-1);//Tirage aléatoire de l'indice du premier sommet entre 0 et taille du tableau -1
  58. //std::cout<<"val : "<<sommetsSource->at(val)<<std::endl;
  59. double poids_max=0;
  60. for(uint i=0;i<sommetsSource->size();i++){
  61. poids_max+=(*g)[sommetsSource->at(i)]._weight;
  62. }
  63. poids_max/=2.;
  64. double poids;
  65. if(distance == -1){
  66. poids=(*g)[sommetsSource->at(rand)]._weight;
  67. sommetsDestination->push_back(sommetsSource->at(rand));
  68. sommetsSource->erase(sommetsSource->begin() + rand);
  69. }else{
  70. poids=(*g)[rand]._weight;
  71. sommetsDestination->push_back(rand);
  72. suprim_val(*sommetsSource,rand);
  73. }
  74. int cpt = 0;
  75. // std::cout<<"taille sommetsSource avant le while : "<<sommetsSource->size()<<std::endl;
  76. while(poids<poids_max && sommetsSource->size()>1)
  77. {
  78. //std::cout<<"taille sommetsSource dans le while "<<sommetsSource->size()<<std::endl;
  79. if(cpt<sommetsDestination->size() )
  80. adjacence_ggp(sommetsDestination->at(cpt),sommets_adj,g);
  81. else{
  82. int val=rand_fini(0,sommetsSource->size()-1);
  83. sommetsDestination->push_back(sommetsSource->at(val));
  84. sommetsSource->erase(sommetsSource->begin() + val);
  85. adjacence_ggp(sommetsDestination->at(cpt),sommets_adj,g);
  86. }
  87. /*std::cout<<"adj :"<<std::endl;
  88. for(uint a = 0; a<sommets_adj.size(); a++){
  89. std::cout<<sommets_adj.at(a)<<std::endl;
  90. }*/
  91. if(sommets_adj.size()==0)
  92. {
  93. //std::cout<<"Je suis sorti car pas de voisin !!!! "<<std::endl;
  94. for (uint i=0; i<sommetsSource->size();i++)
  95. {
  96. for (uint j=0; j<sommetsDestination->size();j++)
  97. {
  98. remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  99. }
  100. }
  101. sort(sommetsDestination->begin(), sommetsDestination->end());
  102. return;
  103. }
  104. else{
  105. for(uint i =0; i<sommets_adj.size(); i++){
  106. if(In_tab(*sommetsDestination,sommets_adj.at(i))!=1 && sommetsSource->size()!=1){
  107. sommetsDestination->push_back(sommets_adj.at(i));
  108. poids+=(*g)[sommets_adj.at(i)]._weight;
  109. suprim_val(*sommetsSource, sommets_adj[i]);
  110. }
  111. if(poids>poids_max || sommetsSource->size()==1)
  112. break;
  113. }
  114. /*std::cout<<"Sommets_source :"<<std::endl;
  115. for(uint i =0; i<sommetsSource->size();i++){
  116. std::cout<<sommetsSource->at(i)<<",";
  117. }
  118. std::cout<<std::endl;
  119. std::cout<<"Sommets_destination :"<<std::endl;
  120. for(uint i =0; i<sommetsDestination->size();i++){
  121. std::cout<<sommetsDestination->at(i)<<",";
  122. }
  123. std::cout<<std::endl;*/
  124. if(poids>poids_max || sommetsSource->size()==1){
  125. for (uint i=0; i<sommetsSource->size();i++)
  126. {
  127. for (uint j=0; j<sommetsDestination->size();j++)
  128. {
  129. remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  130. }
  131. }
  132. sort(sommetsDestination->begin(), sommetsDestination->end());
  133. return;
  134. }
  135. sommets_adj.clear();
  136. cpt++;
  137. }
  138. }
  139. /*for (uint i=0; i<sommetsSource->size();i++)
  140. {
  141. for (uint j=0; j<sommetsDestination->size();j++)
  142. {
  143. remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  144. }
  145. }*/
  146. sort(sommetsDestination->begin(), sommetsDestination->end());
  147. }
  148. void Iter_2l(EntiersEntiers &part, int nbr_parties, UnorientedGraph *g,
  149. const std::string &nom_cut, int nbr_tirage, const std::string &nom_strat, int distance)
  150. {
  151. for(int i = 0; i<floor(log(nbr_parties)/log(2)); i++)
  152. {
  153. for(int j = 0; j< pow(2,i);j++)
  154. {
  155. if(distance == -1){
  156. Optimisation_method_neighbour(g,part,j,nbr_tirage, nom_cut, nom_strat);
  157. }else{
  158. Optimisation_method_neighbour_distance(g,part,j,nbr_tirage, distance, nom_cut, nom_strat);
  159. }
  160. }
  161. }
  162. }
  163. void bissectionRec(UnorientedGraph *g, EntiersEntiers &Partition,
  164. int nbr_parties, const std::string &nom_cut, int nbr_tirage, const std::string &nom_strat, int distance)
  165. {
  166. if((nbr_parties&(nbr_parties-1))==0)
  167. {
  168. //std::cout<<"C'est de la forme 2l : "<<nbr_parties<<std::endl;
  169. Iter_2l(Partition,nbr_parties,g,nom_cut,nbr_tirage,nom_strat, distance);
  170. }
  171. else
  172. {
  173. int puissance_2=0;
  174. Entiers tailles;
  175. while(pow(2,puissance_2)<nbr_parties)
  176. puissance_2++;
  177. Iter_2l(Partition,pow(2,puissance_2-1),g,nom_cut,nbr_tirage,nom_strat, distance);
  178. for(unsigned int i = 0; i< Partition.size() -1 ; i++)
  179. {
  180. for(EntiersEntiers::iterator it1 = Partition.begin() + i ; it1!=Partition.end(); it1++)
  181. {
  182. if((*it1)->size() > Partition.at(i)->size())
  183. Partition.at(i)->swap(**it1);
  184. }
  185. }
  186. for(int j = 0; j<nbr_parties-pow(2,puissance_2-1);j++)
  187. {
  188. if(distance == -1){
  189. Optimisation_method_neighbour(g,Partition,j,nbr_tirage, nom_cut, nom_strat);
  190. }else{
  191. Optimisation_method_neighbour_distance(g,Partition,j,nbr_tirage, distance, nom_cut, nom_strat);
  192. }
  193. }
  194. }
  195. }
  196. void gggp_pond(UnorientedGraph *g, Entiers *sommetsSource,
  197. Entiers *sommetsDestination, EntiersEntiers &Partition, int rand, int distance)
  198. {
  199. Entiers sommets_adj;
  200. if(sommetsSource->size()==1){
  201. Entiers tailles;
  202. for(uint i=0;i<Partition.size();i++){
  203. tailles.push_back(Partition.at(i)->size());
  204. }
  205. uint tmp=*max_element(tailles.begin(),tailles.end());
  206. for(uint i=0; i<Partition.size();i++){
  207. if(Partition.at(i)->size()==tmp)
  208. {
  209. gggp_pond(g,Partition[i],sommetsDestination,Partition,rand_fini(0,Partition.at(i)->size()),distance);
  210. return;
  211. }
  212. }
  213. }
  214. double poids_max=0;
  215. for(uint i=0;i<sommetsSource->size();i++){
  216. poids_max+=(*g)[sommetsSource->at(i)]._weight;
  217. }
  218. poids_max/=2.;
  219. double poids;
  220. std::vector<float> sommets_cut;
  221. float cut;
  222. if(distance == -1){
  223. poids=(*g)[sommetsSource->at(rand)]._weight;
  224. cut = Degree(*g,sommetsSource->at(rand));
  225. //std::cout<<"verif rand : "<<rand<<std::endl;
  226. sommetsDestination->push_back(sommetsSource->at(rand));
  227. sommetsSource->erase(sommetsSource->begin() + rand);
  228. }else{
  229. poids=(*g)[rand]._weight;
  230. cut = Degree(*g,rand);
  231. sommetsDestination->push_back(rand);
  232. suprim_val(*sommetsSource,rand);
  233. }
  234. while(poids<poids_max && sommetsSource->size()>1)
  235. {
  236. Liste_Voisin(*sommetsDestination,sommets_adj,*g);
  237. if(sommets_adj.size()==0)
  238. {
  239. for (uint i=0; i<sommetsSource->size();i++)
  240. {
  241. for (uint j=0; j<sommetsDestination->size();j++)
  242. {
  243. remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  244. }
  245. }
  246. sort(sommetsDestination->begin(), sommetsDestination->end());
  247. return;
  248. }
  249. else{
  250. sort(sommets_adj.begin(), sommets_adj.end());
  251. for(uint i=0;i<sommets_adj.size();i++)
  252. {
  253. sommets_cut.push_back(modif_Cout_coupe(*sommetsDestination,sommets_adj.at(i),cut,g));
  254. }
  255. cut = *min_element(sommets_cut.begin(),sommets_cut.end());
  256. sommetsDestination->push_back(sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  257. poids+=(*g)[sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]]._weight;
  258. suprim_val(*sommetsSource, sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  259. suprim_val(sommets_adj, sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  260. sommets_cut.clear();
  261. }
  262. }
  263. sort(sommetsDestination->begin(), sommetsDestination->end());
  264. /*for (uint i=0; i<sommetsSource->size();i++)
  265. {
  266. for (uint j=0; j<sommetsDestination->size();j++)
  267. {
  268. remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  269. }
  270. }*/
  271. }
  272. // void gggp_pond(UnorientedGraph *g, Entiers *sommetsSource,
  273. // Entiers *sommetsDestination, EntiersEntiers &Partition)
  274. // {
  275. // int val;
  276. // Entiers sommets_adj;
  277. // if(sommetsSource->size()==1){
  278. // val=0;
  279. // //std::cout<<"Entré dans le debug ! "<<std::endl;
  280. // Entiers tailles;
  281. // for(uint i=0;i<Partition.size();i++){
  282. // tailles.push_back(Partition.at(i)->size());
  283. // }
  284. // uint tmp=*max_element(tailles.begin(),tailles.end());
  285. // for(uint i=0; i<Partition.size();i++){
  286. // if(Partition.at(i)->size()==tmp)
  287. // {
  288. // gggp_pond(g,Partition[i],sommetsDestination,Partition);
  289. // return;
  290. // }
  291. // }
  292. // }
  293. // else
  294. // val=rand_fini(0,sommetsSource->size()-1);//Tirage aléatoire de l'indice du premier sommet entre 0 et taille du tableau -1
  295. // //std::cout<<"val : "<<sommetsSource->at(val)<<std::endl;
  296. // double poids_max=0;
  297. // for(uint i=0;i<sommetsSource->size();i++){
  298. // poids_max+=(*g)[sommetsSource->at(i)]._weight;
  299. // }
  300. // poids_max/=2.;
  301. // double poids=(*g)[sommetsSource->at(val)]._weight;
  302. // std::vector<float> sommets_cut;
  303. // float cut = Degree(*g,sommetsSource->at(val));
  304. // sommetsDestination->push_back(sommetsSource->at(val));
  305. // sommetsSource->erase(sommetsSource->begin() + val);
  306. // // std::cout<<"taille sommetsSource avant le while : "<<sommetsSource->size()<<std::endl;
  307. // while(poids<poids_max && sommetsSource->size()>1)
  308. // {
  309. // //std::cout<<"taille sommetsSource dans le while "<<sommetsSource->size()<<std::endl;
  310. // Liste_Voisin(*sommetsDestination,sommets_adj,*g);
  311. // if(sommets_adj.size()==0)
  312. // {
  313. // //std::cout<<"Je suis sorti car pas de voisin !!!! "<<std::endl;
  314. // for (uint i=0; i<sommetsSource->size();i++)
  315. // {
  316. // for (uint j=0; j<sommetsDestination->size();j++)
  317. // {
  318. // remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  319. // }
  320. // }
  321. // sort(sommetsDestination->begin(), sommetsDestination->end());
  322. // return;
  323. // }
  324. // else{
  325. // sort(sommets_adj.begin(), sommets_adj.end());
  326. // /*std::cout<<"adj :"<<std::endl;
  327. // for(uint a = 0; a<sommets_adj.size(); a++){
  328. // std::cout<<sommets_adj.at(a)<<std::endl;
  329. // }
  330. // std::cout<<std::endl;*/
  331. // for(uint i=0;i<sommets_adj.size();i++)
  332. // {
  333. // sommets_cut.push_back(modif_Cout_coupe(*sommetsDestination, sommets_adj.at(i), cut, g));
  334. // // sommets_cut.push_back(Cout_coupe_pond(*sommetsDestination,sommets_adj[i],*g));
  335. // }
  336. // /*std::cout<<"cut :"<<std::endl;
  337. // for(uint a = 0; a<sommets_cut.size(); a++){
  338. // std::cout<<sommets_cut.at(a)<<std::endl;
  339. // }
  340. // std::cout<<std::endl;*/
  341. // sommetsDestination->push_back(sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  342. // //std::cout<<"Sommet deplacé : "<<sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]<<std::endl;
  343. // poids+=(*g)[sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]]._weight;
  344. // suprim_val(*sommetsSource, sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  345. // suprim_val(sommets_adj, sommets_adj[recherche_val2(sommets_cut,*min_element(sommets_cut.begin(),sommets_cut.end()))]);
  346. // sommets_cut.clear();
  347. // }
  348. // /*for(uint i =0; i<sommetsSource->size();i++){
  349. // std::cout<<sommetsSource->at(i)<<",";
  350. // }
  351. // std::cout<<std::endl;
  352. // for(uint i =0; i<sommetsDestination->size();i++){
  353. // std::cout<<sommetsDestination->at(i)<<",";
  354. // }
  355. // std::cout<<std::endl;*/
  356. // }
  357. // for (uint i=0; i<sommetsSource->size();i++)
  358. // {
  359. // for (uint j=0; j<sommetsDestination->size();j++)
  360. // {
  361. // remove_edge(sommetsSource->at(i),sommetsDestination->at(j),*g);
  362. // }
  363. // }
  364. // sort(sommetsDestination->begin(), sommetsDestination->end());
  365. // //std::cout<<"fin du gggp_pond"<<std::endl;
  366. // }
  367. // void Iter_2l(EntiersEntiers &part, int nbr_parties, UnorientedGraph *g,
  368. // const std::string &nom)
  369. // {
  370. // if (nom!="gggp_pond"){
  371. // //std::cout<<"je jsuis dans gggp"<<std::endl;
  372. // for(int i = 0; i<floor(log(nbr_parties)/log(2)); i++)
  373. // {
  374. // //std::cout<<"Et un tours de plus !!!! "<<std::endl;
  375. // for(int j = 0; j< pow(2,i);j++)
  376. // {
  377. // Entiers *Q = new Entiers();
  378. // gggp(g,part[j],Q,part);
  379. // part.push_back(Q);
  380. // }
  381. // }
  382. // } else {
  383. // //std::cout<<"je jsuis dans gggp_pond"<<std::endl;
  384. // for(int i = 0; i<floor(log(nbr_parties)/log(2)); i++)
  385. // {
  386. // //std::cout<<"Et un tours de plus !!!! "<<std::endl;
  387. // for(int j = 0; j< pow(2,i);j++)
  388. // {
  389. // Entiers *Q = new Entiers();
  390. // gggp_pond(g,part.at(j),Q,part);
  391. // //std::clog<<"sortie du gggp_pond"<<std::endl;
  392. // part.push_back(Q);
  393. // }
  394. // //std::cout<<"\n"<<std::endl;
  395. // }
  396. // }
  397. // }
  398. // void bissectionRec(UnorientedGraph *g, EntiersEntiers &Partition,
  399. // int nbr_parties, const std::string &nom)
  400. // {
  401. // if((nbr_parties&(nbr_parties-1))==0)
  402. // {
  403. // //std::cout<<"C'est de la forme 2l : "<<nbr_parties<<std::endl;
  404. // Iter_2l(Partition,nbr_parties,g,nom);
  405. // }
  406. // else
  407. // {
  408. // int puissance_2=0;
  409. // Entiers tailles;
  410. // while(pow(2,puissance_2)<nbr_parties)
  411. // puissance_2++;
  412. // Iter_2l(Partition,pow(2,puissance_2-1),g,nom);
  413. // for(unsigned int i = 0; i< Partition.size() -1 ; i++)
  414. // {
  415. // for(EntiersEntiers::iterator it1 = Partition.begin() + i ; it1!=Partition.end(); it1++)
  416. // {
  417. // if((*it1)->size() > Partition.at(i)->size())
  418. // Partition.at(i)->swap(**it1);
  419. // }
  420. // }
  421. // for(int j = 0; j<nbr_parties-pow(2,puissance_2-1);j++)
  422. // {
  423. // Entiers *Q = new Entiers();
  424. // if(nom!="gggp_pond")
  425. // gggp(g,Partition.at(j),Q,Partition);
  426. // else
  427. // gggp_pond(g,Partition.at(j),Q,Partition);
  428. // Partition.push_back(Q);
  429. // }
  430. // }
  431. // // std::cout<<"Partition avant affinage "<<std::endl;
  432. // // for(uint i = 0 ; i<Partition.size(); i++){
  433. // // for(uint j = 0 ; j<Partition.at(i)->size(); j++){
  434. // // std::cout<<Partition.at(i)->at(j)<<" ";
  435. // // }
  436. // // std::cout<<"\n"<<std::endl;
  437. // // }
  438. // }
  439. /**
  440. * Fonction réalisant un partitionnement pseudo aléatoire suivant un voisinage.
  441. * @param *g : adresse d'un graphe de type boost graphe undirected
  442. * @param Partition : vecteur contenant des vecteurs d'entiers [tableau contenant les parties de la partition]
  443. * @param nbr_partie : entier correspondant au nombre de parties voulues pour la partition
  444. * @return
  445. */
  446. void Pseudo_random_partitioning(UnorientedGraph *g, EntiersEntiers &Partition,
  447. uint nbr_parties)
  448. {
  449. /*
  450. * Principe : distribution des sommets de la première partie en plusieurs autres parties
  451. * Le partitionnement étant pseudo aléatoire il n'y a pas de contrainte stricte sur le nombre
  452. * de sommets par partie
  453. */
  454. uint size = Partition.at(0)->size();
  455. uint cpt_sommets=1;
  456. int val;
  457. uint cpt;
  458. if(nbr_parties==size){
  459. for(uint i = 0; i < nbr_parties;i++){
  460. if(Partition.at(0)->size()!=1)
  461. {
  462. val=rand_fini(0,Partition.at(0)->size()-1);//tirage aléatoire d'un sommets
  463. }
  464. else
  465. val=0;
  466. int vertex = Partition.at(0)->at(val);
  467. Entiers *part = new Entiers();
  468. part->push_back(vertex);// ajout du sommet tiré
  469. suprim_val(*Partition.at(0),vertex);//suppression du sommet dans la premiere partie
  470. }
  471. }
  472. /*
  473. * Boucle sur le nombre de partie à réaliser
  474. */
  475. for(uint i = 0; i < nbr_parties-1;i++){
  476. if(Partition.at(0)->size()!=1)
  477. {
  478. val=rand_fini(0,Partition.at(0)->size()-1);//tirage aléatoire d'un sommets
  479. }
  480. else
  481. val=0;
  482. int vertex = Partition.at(0)->at(val);
  483. /*
  484. * Initialisation d'un pointeur sur un vecteur d'entier, dans notre cas
  485. * la n+1 ième partie de la partition
  486. */
  487. Entiers *part = new Entiers();
  488. part->push_back(vertex);// ajout du sommet tiré
  489. suprim_val(*Partition.at(0),vertex);//suppression du sommet dans la premiere partie
  490. cpt=1;
  491. /*
  492. * Pour chaque element de la nouvelle partie faire
  493. */
  494. for(uint j = 0; j<part->size();j++){
  495. /*
  496. * Détermination des voisins de chacun des sommets de cette nouvelle
  497. * partie et ajoue de ce voisin si celui-ci est présent dans la première partie (Partition[0])
  498. */
  499. tie(neighbourIt, neighbourEnd) = adjacent_vertices(part->at(j),*g);
  500. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  501. if(In_tab(*Partition.at(0),*neighbourIt)==1){
  502. // std::cout<<"le voisin déplacé est : "<<*neighbourIt<<std::endl;
  503. part->push_back(*neighbourIt);
  504. cpt_sommets++;
  505. suprim_val(*Partition.at(0),*neighbourIt);
  506. cpt++;
  507. }
  508. /*
  509. * Si le nombre moyen de sommets est atteind dans la partie on sort de la boucle des voisins
  510. * Même chose si l'on a rencontré le nombre total de sommets
  511. */
  512. if(cpt==(size/nbr_parties)+1)
  513. break;
  514. if(cpt_sommets==size)
  515. break;
  516. }
  517. /*
  518. * Même chose
  519. */
  520. if(cpt==(size/nbr_parties)+1)
  521. break;
  522. if(cpt_sommets==size)
  523. break;
  524. }
  525. Partition.push_back(part);// ajoue de la nouvelle partie à la partition
  526. if(cpt_sommets==size)
  527. break;
  528. }
  529. }
  530. EntiersEntiers Random_partitioning(UnorientedGraph *g,
  531. uint nbr_parties)
  532. {
  533. EntiersEntiers Partition;
  534. Entiers random_order; //gestion d'un tableau contenant tout les sommets et ranger de façon aléatoire
  535. for (uint i=0 ; i<num_vertices(*g) ; i++)
  536. random_order.push_back(i);
  537. for (uint j=0 ; j<num_vertices(*g)-1 ; j++) {
  538. int rand_pos = rand()%(num_vertices(*g)-j)+j;
  539. int tmp = random_order.at(j);
  540. random_order.at(j) = random_order.at(rand_pos);
  541. random_order.at(rand_pos) = tmp;
  542. }
  543. uint size = num_vertices(*g)/nbr_parties;
  544. for(uint j = 0 ; j < nbr_parties-1 ; j++){
  545. Entiers *part = new Entiers();
  546. for(uint i = j*size; i<(j+1)*size; i++){
  547. part->push_back(random_order.at(i));
  548. }
  549. Partition.push_back(part);
  550. }
  551. Entiers *part = new Entiers();
  552. for(uint i = (nbr_parties-1)*size; i < random_order.size(); i++){
  553. part->push_back(random_order.at(i));
  554. }
  555. Partition.push_back(part);
  556. for(uint i = 0 ; i < Partition.size() ; i++){
  557. sort(Partition.at(i)->begin(),Partition.at(i)->end());
  558. }
  559. return Partition;
  560. }
  561. OrientedGraphs Multiniveau(uint niveau_contraction,
  562. UnorientedGraph *g,
  563. UnorientedGraph *graph_origin,
  564. OrientedGraph *go,
  565. int nbr_parties,
  566. int nbr_tirage,
  567. std::string contraction,
  568. std::string type_methode,
  569. std::string choix_affinage,
  570. std::string type_cut,
  571. Edges& /* edge_partie */,
  572. OutputEdgeList& outputedgelist,
  573. InputEdgeList& inputedgelist,
  574. Connections& connections,
  575. std::vector<double> &Cut, int distance)
  576. {
  577. EntiersEntiers Partition;
  578. Entiers *part = new Entiers();
  579. Base_Graph baseg;
  580. baseg.push_back(g);
  581. ListEntiersEntiers liste_corr;
  582. uint cpt =0;
  583. int val_cpt = num_vertices(*g);
  584. bool stop = false;
  585. if (niveau_contraction == val_cpt) {
  586. stop = true;
  587. }
  588. while(stop != true)
  589. {
  590. if(contraction == "HEM")
  591. stop = contraction_HEM(baseg.at(cpt),baseg,liste_corr,niveau_contraction,val_cpt);
  592. else
  593. stop = contraction_Random_Maching(baseg.at(cpt),baseg,liste_corr,niveau_contraction,val_cpt);
  594. cpt++;
  595. }
  596. for(int i = 0; i<num_vertices(*baseg.at(baseg.size() - 1)); i++){
  597. part->push_back(i);
  598. }
  599. Partition.push_back(part);
  600. UnorientedGraph copy_graph;
  601. boost::copy_graph(*baseg.at(baseg.size()-1),copy_graph);
  602. // std::cout<<"Graphe contracté : "<<std::endl;
  603. // for (uint i = 0; i< baseg.size(); i++) {
  604. // tie(vertexIt, vertexEnd) = vertices(*baseg[i]);
  605. // for (; vertexIt != vertexEnd; ++vertexIt) {
  606. // std::cout << (*baseg[i])[*vertexIt]._index
  607. // << " est connecté avec ";
  608. // tie(neighbourIt, neighbourEnd) = adjacent_vertices(*vertexIt,
  609. // *baseg[i]);
  610. // for (; neighbourIt != neighbourEnd; ++neighbourIt)
  611. // std::cout << (*baseg[i])[*neighbourIt]._index << " ";
  612. // std::cout << " et son poids est de "
  613. // << (*baseg[i])[*vertexIt]._weight<<std::endl;
  614. // }
  615. // std::cout << std::endl;
  616. // }
  617. if(type_methode == "gggp" || type_methode == "ggp"){
  618. bissectionRec(baseg.at(baseg.size()-1),Partition,nbr_parties,type_cut,nbr_tirage,type_methode, distance);
  619. double best_cut = Cut_cluster(Partition,copy_graph,type_cut);
  620. Cut.push_back(best_cut);
  621. //std::cout<<"Meilleur coût de coupe : "<<best_cut<<std::endl;
  622. std::cout<<std::endl;
  623. }
  624. else
  625. Partition = Random_partitioning(baseg.at(baseg.size()-1),nbr_parties);
  626. // std::cout<<std::endl;
  627. ListEntiersEntiers::iterator lit(liste_corr.end());
  628. bool proj;
  629. uint taille_list = liste_corr.size();
  630. if(liste_corr.size()==0){
  631. taille_list = 1;
  632. proj = true;
  633. }
  634. else{
  635. lit--;
  636. proj = false;
  637. }
  638. for(uint y =0; y<taille_list;y++){
  639. if(proj != true){
  640. // std::cout<<"Projection "<<std::endl;
  641. projection(Partition,lit);
  642. // std::cout<<std::endl;
  643. double cut = Cut_cluster(Partition,*baseg.at(baseg.size()-2-y),type_cut);
  644. std::cout<<"Cout de coupe avant affinage : "<<cut<<std::endl;
  645. std::cout<<std::endl;
  646. std::cout<<"Affinage "<<std::endl;
  647. if(choix_affinage=="charge")
  648. Affinage_equilibrage_charge(baseg.at(baseg.size()-2-y),Partition);
  649. else
  650. Affinage_recherche_locale(baseg.at(baseg.size()-2-y),Partition,cut,type_cut);
  651. lit--;
  652. }
  653. else{
  654. std::cout<<"Pas de projection "<<std::endl;
  655. // std::cout<<std::endl;
  656. /*if(nbr_parties != num_vertices(*g)){
  657. // std::cout<<"Affinage "<<std::endl;
  658. double cut = Cut_cluster(Partition,*graph_origin,type_cut);
  659. std::cout<<"Cout de coupe avant affinage : "<<cut<<std::endl;
  660. if(choix_affinage=="charge")
  661. Affinage_equilibrage_charge(graph_origin,Partition);
  662. else{
  663. Affinage_recherche_locale(graph_origin,Partition,cut,type_cut);
  664. std::cout<<"Cout de coupe après affinage : "<<cut<<std::endl;
  665. }
  666. }*/
  667. // else
  668. // std::cout<<"Pas d'affinage "<<std::endl;
  669. }
  670. }
  671. OrientedGraphs Graphes = Graph_Partition(Partition, go, graph_origin, outputedgelist,
  672. inputedgelist, connections);
  673. std::vector<std::string> color;
  674. color.push_back("[color=blue2, fontcolor=blue2];");
  675. color.push_back("[color=red, fontcolor=red];");
  676. color.push_back("[color=green, fontcolor=green];");
  677. color.push_back("[color=yellow, fontcolor=yellow2];");
  678. color.push_back("[color=saddlebrown, fontcolor=saddlebrown];");
  679. color.push_back("[color=turquoise, fontcolor=turquoise];");
  680. color.push_back("[color=orange, fontcolor=orange];");
  681. color.push_back("[color=olivedrab, fontcolor=olivedrab];");
  682. color.push_back("[color=indigo, fontcolor=indigo];");
  683. color.push_back("[color=gold, fontcolor=gold];");
  684. color.push_back("[color=slateblue2, fontcolor=slateblue2];");
  685. color.push_back("[color=dimgrey, fontcolor=dimgrey];");
  686. color.push_back("[color=cyan, fontcolor=cyan];");
  687. color.push_back("[color=purple1, fontcolor=purpule1];");
  688. color.push_back("[color=crimson, fontcolor=crimson];");
  689. color.push_back("[color=black, fontcolor=black];");
  690. std::ofstream fichier2 ("../../sortie_graphe/graph_partition_38_4_1.txt", std::ios::out);
  691. fichier2<<"digraph G {"<<std::endl;
  692. tie(vertexIto, vertexEndo) = vertices(*go);
  693. for (; vertexIto != vertexEndo; ++vertexIto) {
  694. fichier2<<(*go)[*vertexIto]._index<<"-> {";
  695. tie(neighbourIto, neighbourEndo) = adjacent_vertices(*vertexIto,
  696. *go);
  697. for (; neighbourIto != neighbourEndo; ++neighbourIto){
  698. fichier2<<(*go)[*neighbourIto]._index<<";";
  699. }
  700. fichier2<<"}"<<std::endl;
  701. }
  702. for(uint k=0; k<Partition.size(); k++){
  703. for(uint j=0; j<Partition.at(k)->size(); j++)
  704. {
  705. fichier2<<Partition.at(k)->at(j)<<color.at(k)<<std::endl;
  706. }
  707. }
  708. fichier2<<"}";
  709. fichier2.close();
  710. //double cut = Cut_cluster(Partition,*graph_origin,"cut");
  711. // std::cout<<"Cout de coupe engendré par le partitionnement: "<<cut<<std::endl;
  712. for(EntiersEntiers::iterator it = Partition.begin(); it != Partition.end(); it++)
  713. {
  714. delete *it;
  715. *it = NULL;
  716. }
  717. for(ListEntiersEntiers::iterator it = liste_corr.begin(); it != liste_corr.end(); it++)
  718. {
  719. for(EntiersEntiers::iterator it1 = (*it)->begin(); it1 != (*it)->end(); it1++)
  720. {
  721. delete *it1;
  722. *it1 = NULL;
  723. }
  724. delete *it;
  725. *it = NULL;
  726. }
  727. for(Base_Graph::iterator it = baseg.begin(); it != baseg.end(); it++)
  728. {
  729. delete *it;
  730. *it = NULL;
  731. }
  732. return Graphes;
  733. }
  734. void Optimisation_method_neighbour(UnorientedGraph *g, EntiersEntiers &Partition, int index_partition, int nbr_tirage, const std::string &name_cut, const std::string &name_strat){
  735. /*Initialisation des parametres*/
  736. //UnorientedGraph *copy_g_ref = new UnorientedGraph();
  737. Entiers *part2_cons = new Entiers();
  738. Entiers *part_cour_cons = new Entiers();
  739. Entiers Random_list_vertices;
  740. double cut=1000000000.;
  741. //boost::copy_graph(*g,*copy_g_ref);
  742. for (int i=0 ; i<Partition.at(index_partition)->size() ; i++)
  743. Random_list_vertices.push_back(i);
  744. for (int j=0 ; j<Partition.at(index_partition)->size()-1 ; j++) {
  745. int rand_pos = rand()%(Partition.at(index_partition)->size()-j)+j;
  746. int tmp = Random_list_vertices[j];
  747. Random_list_vertices[j] = Random_list_vertices[rand_pos];
  748. Random_list_vertices[rand_pos] = tmp;
  749. }
  750. if(Partition.at(index_partition)->size()< nbr_tirage)
  751. nbr_tirage = Partition.at(index_partition)->size();
  752. /*Boucle de conservation de la meilleure bissection*/
  753. for(int k = 0; k<nbr_tirage; k++){
  754. //UnorientedGraph *copy_g = new UnorientedGraph();
  755. Entiers *part2 = new Entiers();
  756. Entiers *tmp_part = new Entiers();
  757. double new_cut;
  758. //boost::copy_graph(*g,*copy_g);
  759. /*Recopie de la partie faisant l'objet de la bissection */
  760. for(int t=0; t<Partition.at(index_partition)->size();t++){
  761. tmp_part->push_back(Partition.at(index_partition)->at(t));
  762. }
  763. /*std::cout<<"Ensembles verification !!! "<<std::endl;
  764. std::cout<<"Ensemble 1 : ";
  765. for(uint i = 0; i < tmp_part->size(); i++){
  766. std::cout<<tmp_part->at(i)<<" ";
  767. }
  768. std::cout<<std::endl;
  769. std::cout<<"Ensemble 2 : ";
  770. for(uint j = 0; j < part2->size(); j++){
  771. std::cout<<part2->at(j)<<" ";
  772. }
  773. std::cout<<std::endl;*/
  774. //gggp_pond(copy_g,tmp_part,part2,Partition);
  775. if(name_strat == "gggp"){
  776. gggp_pond(g,tmp_part,part2,Partition,Random_list_vertices.at(k),-1);
  777. } else if(name_strat == "ggp"){
  778. ggp(g,tmp_part,part2,Partition,Random_list_vertices.at(k),-1);
  779. } else {
  780. std::cout<<"Nom de stratégie de partitionnement non valide ! "<<std::endl;
  781. }
  782. /*std::cout<<"Ensemble 1 : ";
  783. for(uint i = 0; i < tmp_part->size(); i++){
  784. std::cout<<tmp_part->at(i)<<" ";
  785. }
  786. std::cout<<std::endl;
  787. std::cout<<"Ensemble 2 : ";
  788. for(uint j = 0; j < part2->size(); j++){
  789. std::cout<<part2->at(j)<<" ";
  790. }
  791. std::cout<<std::endl;
  792. std::cout<<std::endl;*/
  793. new_cut = Best_Cut_cluster(Partition, tmp_part, part2, index_partition,*g,name_cut);
  794. //std::cout<<"Cout de coupe initial : "<<cut<<" "<<"Résutlat : "<<new_cut<<std::endl;
  795. //std::cout<<std::endl;
  796. if(new_cut<cut){ /*conservation de l'information en cas d'amélioration de la contrainte*/
  797. cut = new_cut;
  798. //copy_g_ref = copy_g;
  799. part2_cons = part2;
  800. part_cour_cons = tmp_part;
  801. }
  802. else{
  803. //delete copy_g;
  804. delete tmp_part;
  805. delete part2;
  806. }
  807. }
  808. for (uint i=0; i<part_cour_cons->size();i++)
  809. {
  810. for (uint j=0; j<part2_cons->size();j++)
  811. {
  812. remove_edge(part_cour_cons->at(i),part2_cons->at(j),*g);
  813. }
  814. }
  815. /*Modification des informations*/
  816. Partition.at(index_partition)=part_cour_cons;
  817. Partition.push_back(part2_cons);
  818. //g=copy_g_ref;
  819. }
  820. void Optimisation_method_neighbour_distance(UnorientedGraph *g, EntiersEntiers &Partition, int index_partition, int nbr_tirage, int distance,
  821. const std::string &name_cut, const std::string &name_strat){
  822. //Initialisation des parametres
  823. Entiers *part2_cons = new Entiers();
  824. Entiers *part_cour_cons = new Entiers();
  825. double cut=1000000000.;
  826. int val;
  827. std::list<int> vertex_list;
  828. for(int verx =0; verx<Partition.at(index_partition)->size(); verx++){
  829. vertex_list.push_back(Partition.at(index_partition)->at(verx));
  830. }
  831. /*std::list<int>::iterator toto;
  832. for(toto = vertex_list.begin(); toto != vertex_list.end(); toto ++)
  833. std::cout<<*toto<<" ";
  834. std::cout<<std::endl;*/
  835. if(Partition.at(index_partition)->size()< nbr_tirage)
  836. nbr_tirage = Partition.at(index_partition)->size();
  837. //Boucle de conservation de la meilleure bissection
  838. for(int k = 0; k<nbr_tirage; k++){
  839. //Tirage
  840. std::list<int>::iterator Iter;
  841. if(vertex_list.size()!=0){
  842. //std::cout<<"Il reste des sommets à tirer"<<std::endl;
  843. Iter = vertex_list.begin();
  844. for(int i = 0; i<rand_fini(0,vertex_list.size()); i++){
  845. Iter++;
  846. }
  847. val = *Iter;
  848. //std::cout<<"Sommet tiré : "<<*Iter<<std::endl;
  849. tirage_distance(g, *Iter, vertex_list, distance);
  850. }
  851. else{
  852. std::cout<<"Tous les sommets sont verrouillés"<<std::endl;
  853. break;
  854. }
  855. Entiers *part2 = new Entiers();
  856. Entiers *tmp_part = new Entiers();
  857. double new_cut;
  858. //Recopie de la partie faisant l'objet de la bissection
  859. for(int t=0; t<Partition.at(index_partition)->size();t++){
  860. tmp_part->push_back(Partition.at(index_partition)->at(t));
  861. }
  862. //std::cout<<"Sommet tirée avant entré dans gggp : "<<*Iter<<std::endl;
  863. if(name_strat == "gggp"){
  864. gggp_pond(g,tmp_part,part2,Partition,val,distance);
  865. } else if(name_strat == "ggp"){
  866. ggp(g,tmp_part,part2,Partition,val,distance);
  867. } else {
  868. std::cout<<"Nom de stratégie de partitionnement non valide ! "<<std::endl;
  869. }
  870. /*std::cout<<"tmp_part"<<std::endl;
  871. for(int alpha = 0; alpha<tmp_part->size(); alpha ++){
  872. std::cout<<tmp_part->at(alpha)<<" ";
  873. }
  874. std::cout<<std::endl;
  875. std::cout<<"part2"<<std::endl;
  876. for(int alpho = 0; alpho<part2->size(); alpho ++){
  877. std::cout<<part2->at(alpho)<<" ";
  878. }
  879. std::cout<<std::endl;
  880. std::cout<<std::endl;*/
  881. new_cut = Best_Cut_cluster(Partition, tmp_part, part2, index_partition, *g,name_cut);
  882. std::cout<<"Cout de coupe : "<<new_cut<<std::endl;
  883. if(new_cut<cut){ //conservation de l'information en cas d'amélioration de la contrainte
  884. cut = new_cut;
  885. part2_cons = part2;
  886. part_cour_cons = tmp_part;
  887. }
  888. else{
  889. delete tmp_part;
  890. delete part2;
  891. }
  892. //std::cout<<"Un tour de plus dans la boucle k"<<std::endl;
  893. }
  894. for (uint i=0; i<part_cour_cons->size();i++)
  895. {
  896. for (uint j=0; j<part2_cons->size();j++)
  897. {
  898. remove_edge(part_cour_cons->at(i),part2_cons->at(j),*g);
  899. }
  900. }
  901. //Modification des informations
  902. Partition.at(index_partition)=part_cour_cons;
  903. Partition.push_back(part2_cons);
  904. std::cout<<std::endl;
  905. std::cout<<"Bissection réalisé"<<std::endl;
  906. std::cout<<std::endl;
  907. }
  908. void tirage_distance(UnorientedGraph *g, int tirage, std::list<int> &vertex_list, int distance){
  909. std::vector<std::list<int> > vertex_delete;
  910. std::list<int> liste1;
  911. std::list<int> vd;
  912. liste1.push_back(tirage); /*modif*/
  913. vertex_delete.push_back(liste1);
  914. for(int i=0; i<distance; i++){
  915. std::list<int> liste_tmp;
  916. std::list<int>::iterator Ite_tmp;
  917. for(Ite_tmp = vertex_delete.at(i).begin(); Ite_tmp != vertex_delete.at(i).end(); Ite_tmp ++){
  918. tie(neighbourIt, neighbourEnd) = adjacent_vertices(*Ite_tmp,*g);
  919. for (; neighbourIt != neighbourEnd; ++neighbourIt){
  920. liste_tmp.push_back(*neighbourIt);/*modif*/
  921. }
  922. }
  923. liste_tmp.sort();
  924. liste_tmp.unique();
  925. vertex_delete.push_back(liste_tmp);
  926. }
  927. std::cout<<std::endl;
  928. /*for(int i =0; i<vertex_delete.size(); i++){
  929. std::list<int>::iterator Ite_tmp;
  930. for(Ite_tmp = vertex_delete.at(i).begin(); Ite_tmp != vertex_delete.at(i).end(); Ite_tmp ++){
  931. std::cout<<*Ite_tmp<<" ";
  932. }
  933. std::cout<<std::endl;
  934. }
  935. std::cout<<std::endl;*/
  936. for(int index = 0; index < vertex_delete.size(); index ++){
  937. vd.merge(vertex_delete.at(index));
  938. }
  939. vd.unique();
  940. /*std::list<int>::iterator Ite_tmp;
  941. for(Ite_tmp = vd.begin(); Ite_tmp != vd.end(); Ite_tmp ++){
  942. std::cout<<*Ite_tmp<<" ";
  943. }
  944. std::cout<<std::endl;*/
  945. std::list<int>::iterator Ite;
  946. for(Ite = vd.begin(); Ite != vd.end(); Ite ++){
  947. vertex_list.remove(*Ite);
  948. }
  949. }
  950. } } } // namespace paradevs tests boost_graph