gggp.cpp 37 KB

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