gggp.cpp 32 KB

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