Implementacja tabu search dla TSP
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pea2plus.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include "Stopwatch.h"
  9. #include "ArrayGraph.h"
  10. #include "ListGraph.h"
  11. using namespace std;
  12. // USTAWIENIA
  13. // Liczba powtorzen automatycznych pomiarow
  14. const int measureIterations = 10;
  15. // liczba automatycznych pomiarow
  16. const int measureNumber = 4;
  17. // Czas zatrzymania algorytmu tabu search w kazdym z automatycznych pomiarow
  18. const int measureTabuStop[4] = {30, 60, 90, 120};
  19. // Maksymalna odleglosc miast przy automatycznym generowaniu
  20. const int measureSalesmanDistance = 400;
  21. // Wykorzystanie reprezentacji grafu w postaci list sasiedztwa...
  22. // ...zamiast (domyslnie) macierzy sasiedztwa
  23. // (wolniejsze obliczenia, mniejsze uzycie pamieci)
  24. bool useListGraph = false;
  25. // Domyslna kadencja tabu search - wybor automatyczny
  26. unsigned tabuLength = 0;
  27. // Domyslny stan dywersyfikacji
  28. bool tabuDiversification = true;
  29. // Domyslne kryterium dywersyfikacji, liczba iteracji bez poprawy
  30. int tabuIterationsToRestart = 10000;
  31. // Domyslny czas zatrzymania algorytmu tabu search [s]
  32. unsigned tabuStopTime = 60;
  33. // Domyslna liczba watkow tabu search
  34. unsigned tabuThreadsNumber = 2;
  35. void parseTSPLIB_FULL_MATRIX(const char *filename, Graph **graph)
  36. {
  37. // Parser plikow FULL_MATRIX z TSPLIB
  38. // Implementacja: Jan Potocki 2017
  39. string fileInput;
  40. ifstream salesmanDataFile;
  41. salesmanDataFile.open(filename);
  42. if(salesmanDataFile.is_open())
  43. {
  44. do
  45. salesmanDataFile >> fileInput;
  46. while(fileInput != "DIMENSION:");
  47. salesmanDataFile >> fileInput;
  48. int vertex = stoi(fileInput);
  49. do
  50. salesmanDataFile >> fileInput;
  51. while(fileInput != "EDGE_WEIGHT_TYPE:");
  52. salesmanDataFile >> fileInput;
  53. if(fileInput != "EXPLICIT")
  54. {
  55. cout << "+++ MELON MELON MELON +++ Nieobslugiwany format " << fileInput << " +++" << endl;
  56. return;
  57. }
  58. do
  59. salesmanDataFile >> fileInput;
  60. while(fileInput != "EDGE_WEIGHT_FORMAT:");
  61. salesmanDataFile >> fileInput;
  62. if(fileInput == "FULL_MATRIX")
  63. {
  64. if(*graph != NULL)
  65. delete *graph;
  66. if(useListGraph)
  67. *graph = new ListGraph(vertex);
  68. else
  69. *graph = new ArrayGraph(vertex);
  70. do
  71. salesmanDataFile >> fileInput;
  72. while(fileInput != "EDGE_WEIGHT_SECTION");
  73. for(int i = 0; i < vertex; i++)
  74. {
  75. for(int j = 0; j < vertex; j++)
  76. {
  77. salesmanDataFile >> fileInput;
  78. int weight = stoi(fileInput);
  79. if(i != j)
  80. (*graph)->addEdge(i, j, weight);
  81. }
  82. }
  83. cout << "Liczba wczytanych wierzcholkow: " << vertex << endl;
  84. cout << endl;
  85. }
  86. else
  87. {
  88. cout << "+++ MELON MELON MELON +++ Nieobslugiwany format " << fileInput << " +++" << endl;
  89. cout << endl;
  90. }
  91. salesmanDataFile.close();
  92. }
  93. else
  94. {
  95. cout << "+++ MELON MELON MELON +++ Brak pliku " << filename << " +++" << endl;
  96. cout << endl;
  97. }
  98. return;
  99. }
  100. void parseTSPLIB_EUC_2D(const char *filename, Graph **graph)
  101. {
  102. // Parser plikow EUC_2D z TSPLIB
  103. // Implementacja: Jan Potocki 2017
  104. string fileInput;
  105. vector<float> xCoord, yCoord;
  106. ifstream salesmanDataFile;
  107. salesmanDataFile.open(filename);
  108. if(salesmanDataFile.is_open())
  109. {
  110. do
  111. salesmanDataFile >> fileInput;
  112. while(fileInput != "DIMENSION:");
  113. salesmanDataFile >> fileInput;
  114. int vertex = stoi(fileInput);
  115. do
  116. salesmanDataFile >> fileInput;
  117. while(fileInput != "EDGE_WEIGHT_TYPE:");
  118. salesmanDataFile >> fileInput;
  119. if(fileInput == "EUC_2D")
  120. {
  121. if(*graph != NULL)
  122. delete *graph;
  123. if(useListGraph)
  124. *graph = new ListGraph(vertex);
  125. else
  126. *graph = new ArrayGraph(vertex);
  127. do
  128. salesmanDataFile >> fileInput;
  129. while(fileInput != "NODE_COORD_SECTION");
  130. for(int i = 0; i < vertex; i++)
  131. {
  132. salesmanDataFile >> fileInput;
  133. salesmanDataFile >> fileInput;
  134. xCoord.push_back(stof(fileInput));
  135. salesmanDataFile >> fileInput;
  136. yCoord.push_back(stof(fileInput));
  137. }
  138. // To daloby sie zrobic optymalniej (macierz symetryczna), ale nie chce mi sie ...
  139. // ..wole zoptymalizować czas programowania ;-)
  140. for(int i = 0; i < vertex; i++)
  141. {
  142. for(int j = 0; j < vertex; j++)
  143. {
  144. if(i != j)
  145. {
  146. float xDiff = xCoord.at(i) - xCoord.at(j);
  147. float yDiff = yCoord.at(i) - yCoord.at(j);
  148. int weight = nearbyint(sqrt(xDiff * xDiff + yDiff * yDiff));
  149. (*graph)->addEdge(i, j, weight);
  150. }
  151. }
  152. }
  153. cout << "Liczba wczytanych wierzcholkow: " << vertex << endl;
  154. cout << endl;
  155. }
  156. else
  157. {
  158. cout << "+++ MELON MELON MELON +++ Nieobslugiwany format " << fileInput << " +++" << endl;
  159. cout << endl;
  160. }
  161. salesmanDataFile.close();
  162. }
  163. else
  164. {
  165. cout << "+++ MELON MELON MELON +++ Brak pliku " << filename << " +++" << endl;
  166. cout << endl;
  167. }
  168. return;
  169. }
  170. unsigned autoTabuLength(Graph &graph)
  171. {
  172. unsigned computedTabuLength;
  173. computedTabuLength = (2 * graph.getVertexNumber() / 10) * 10;
  174. if(computedTabuLength == 0)
  175. computedTabuLength = 10;
  176. // PEA 2 Plus
  177. // Jan Potocki 2019
  178. return computedTabuLength;
  179. }
  180. void banner()
  181. {
  182. cout << "PEA Projekt 2 Plus v2.0.2" << endl;
  183. cout << "Jan Potocki 2017-2019" << endl;
  184. cout << "(beerware)" << endl;
  185. }
  186. int main(int argc, char *argv[])
  187. {
  188. Stopwatch clock; // czasomierz
  189. Graph *graph = NULL; // <- tu bedziemy zapisywac adresy przez caly program
  190. // Parsowanie parametrow z linii komend
  191. if(argc > 1)
  192. {
  193. for(int i = 1; i < argc; i++)
  194. {
  195. if(strcmp(argv[i], "-l") == 0)
  196. {
  197. useListGraph = true;
  198. }
  199. else if(strcmp(argv[i], "-t") == 0)
  200. {
  201. i++;
  202. int input = atoi(argv[i]);
  203. if(input > 0)
  204. tabuThreadsNumber = input;
  205. else
  206. cout << "+++ MELON MELON MELON +++ Nieprawidlowa liczba watkow +++" << endl << endl;
  207. }
  208. else if(strcmp(argv[i], "-fmatrix") == 0)
  209. {
  210. i++;
  211. parseTSPLIB_FULL_MATRIX(argv[i], &graph);
  212. }
  213. else if(strcmp(argv[i], "-feuc2d") == 0)
  214. {
  215. i++;
  216. parseTSPLIB_EUC_2D(argv[i], &graph);
  217. }
  218. else if(strcmp(argv[i], "-h") == 0)
  219. {
  220. banner();
  221. cout << endl << "Parametry:" << endl;
  222. cout << "-feuc2d [plik]" << "\t\t" << "wczytanie danych z pliku w formacie TSPLIB EUC_2D" << endl;
  223. cout << endl;
  224. cout << "-fmatrix [plik]" << "\t\t" << "wczytanie danych z pliku w formacie TSPLIB FULL_MATRIX" << endl;
  225. cout << endl;
  226. cout << "-l" << "\t\t\t" << "uzycie reprezentacji grafu w pamieci w postaci listy" << endl;
  227. cout << "\t\t\t" << "sasiedztwa (domyslnie macierz sasiedztwa)" << endl;
  228. cout << endl;
  229. cout << "-t [liczba]" << "\t\t" << "liczba watkow roboczych dla algorytmu tabu search" << endl;
  230. cout << endl;
  231. cout << "-h" << "\t\t\t" << "wyswietlenie pomocy (opisu parametrow)" << endl;
  232. return 0;
  233. // PEA 2 Plus
  234. // Jan Potocki 2019
  235. }
  236. else
  237. {
  238. cout << "Nieprawidlowy parametr, uzyj -h aby uzyskac pomoc" << endl;
  239. return 0;
  240. }
  241. }
  242. }
  243. banner();
  244. if(useListGraph)
  245. cout << "Uzycie listowej reprezentacji grafu" << endl;
  246. else
  247. cout << "Uzycie macierzowej reprezentacji grafu" << endl;
  248. cout << endl;
  249. int salesmanSelection;
  250. do
  251. {
  252. cout << "1. Wygeneruj losowe dane" << endl;
  253. cout << "2. Wyswietl dane" << endl;
  254. cout << "3. Ustawienia TS" << endl;
  255. cout << "4. Tabu search" << endl;
  256. cout << "5. Podzial i ograniczenia" << endl;
  257. cout << "6. Przeglad zupelny" << endl;
  258. cout << "7. Automatyczne pomiary (tabu search)" << endl;
  259. cout << "8. Wczytaj dane z pliku TSPLIB FULL_MATRIX" << endl;
  260. cout << "9. Wczytaj dane z pliku TSPLIB EUC_2D" << endl;
  261. cout << "Aby zakonczyc - 0" << endl;
  262. cout << "Wybierz: ";
  263. cin >> salesmanSelection;
  264. cout << endl;
  265. switch(salesmanSelection)
  266. {
  267. case 1:
  268. {
  269. int vertex;
  270. cout << "Liczba miast: ";
  271. cin >> vertex;
  272. cout << endl;
  273. if(graph != NULL)
  274. delete graph;
  275. if(useListGraph)
  276. graph = new ListGraph(vertex);
  277. else
  278. graph = new ArrayGraph(vertex);
  279. Graph::randomGenerateFullGraph(*graph, measureSalesmanDistance);
  280. }
  281. break;
  282. case 2:
  283. {
  284. if(graph != NULL)
  285. graph->displayGraph();
  286. else
  287. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  288. cout << endl;
  289. }
  290. break;
  291. case 3:
  292. {
  293. int settingsSelection;
  294. do
  295. {
  296. if(tabuDiversification == true)
  297. cout << "1. Przelacz dywersyfikacje" << "\t" << "(wlaczona)" << endl;
  298. else
  299. cout << "1. Przelacz dywersyfikacje" << "\t" << "(wylaczona)" << endl;
  300. cout << "2. Kryterium dywersyfikacji" << "\t" << "(" << tabuIterationsToRestart << " iteracji)" << endl;
  301. if(tabuLength == 0)
  302. cout << "3. Kadencja na liscie tabu" << "\t" << "(auto)" << endl;
  303. else
  304. cout << "3. Kadencja na liscie tabu" << "\t" << "(" << tabuLength << ")" << endl;
  305. cout << "4. Czas zatrzymania" << "\t\t" << "(" << tabuStopTime << " s)" << endl;
  306. cout << "Powrot - 0" << endl;
  307. cout << "Wybierz: ";
  308. cin >> settingsSelection;
  309. cout << endl;
  310. switch(settingsSelection)
  311. {
  312. case 1:
  313. {
  314. tabuDiversification = !tabuDiversification;
  315. if(tabuDiversification == true)
  316. cout << "Dywersyfikacja zostala wlaczona" << endl;
  317. else
  318. cout << "Dywersyfikacja zostala wylaczona" << endl;
  319. cout << endl;
  320. }
  321. break;
  322. case 2:
  323. {
  324. cout << "Podaj nowa liczbe iteracji bez poprawy: ";
  325. cin >> tabuIterationsToRestart;
  326. cout << endl;
  327. }
  328. break;
  329. case 3:
  330. {
  331. cout << "Podaj nowa kadencje (0 -> auto): ";
  332. cin >> tabuLength;
  333. cout << endl;
  334. }
  335. break;
  336. case 4:
  337. {
  338. cout << "Podaj nowy czas pracy [s]: ";
  339. cin >> tabuStopTime;
  340. cout << endl;
  341. }
  342. break;
  343. case 0:
  344. {
  345. }
  346. break;
  347. default:
  348. {
  349. cout << "Nieprawidlowy wybor" << endl;
  350. cout << endl;
  351. }
  352. }
  353. } while(settingsSelection != 0);
  354. }
  355. break;
  356. case 4:
  357. {
  358. if(graph != NULL)
  359. {
  360. if(tabuStopTime != 0)
  361. {
  362. unsigned effectiveTabuLength;
  363. if(tabuLength == 0)
  364. effectiveTabuLength = autoTabuLength(*graph);
  365. else
  366. effectiveTabuLength = tabuLength;
  367. if(tabuLength == 0)
  368. cout << "Kadencja: " << effectiveTabuLength << " (auto)" << endl;
  369. else
  370. cout << "Kadencja: " << effectiveTabuLength << endl;
  371. cout << "Czas zatrzymania algorytmu [s]: " << tabuStopTime << endl;
  372. if(tabuDiversification == true)
  373. cout << "Dywersyfikacja wlaczona, kryterium: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
  374. else
  375. cout << "Dywersyfikacja wylaczona" << endl;
  376. cout << "Liczba watkow: " << tabuThreadsNumber << endl;
  377. cout << endl;
  378. clock.start();
  379. vector<unsigned> route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, tabuDiversification, tabuIterationsToRestart, tabuStopTime, tabuThreadsNumber);
  380. clock.stop();
  381. // Wyswietlenie trasy
  382. unsigned distFromStart = 0;
  383. unsigned length = 0;
  384. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  385. for(int i = 1; i < route.size(); i++)
  386. {
  387. length = graph->getWeight(route.at(i - 1), route.at(i));
  388. distFromStart += length;
  389. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  390. }
  391. cout << "Dlugosc trasy: " << distFromStart << endl;
  392. cout << endl;
  393. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  394. }
  395. else
  396. {
  397. // Easter egg ;-)
  398. cout << "+++ MELON MELON MELON +++ Blad: Brak Sera! +++ !!!!! +++" << endl;
  399. }
  400. }
  401. else
  402. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  403. cout << endl;
  404. }
  405. break;
  406. case 5:
  407. {
  408. if(graph != NULL)
  409. {
  410. clock.start();
  411. vector<unsigned> route = Graph::travellingSalesmanBranchAndBound(*graph);
  412. clock.stop();
  413. // Wyswietlenie trasy
  414. unsigned distFromStart = 0;
  415. unsigned length = 0;
  416. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  417. for(int i = 1; i < route.size(); i++)
  418. {
  419. length = graph->getWeight(route.at(i - 1), route.at(i));
  420. distFromStart += length;
  421. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  422. }
  423. cout << "Dlugosc trasy: " << distFromStart << endl;
  424. cout << endl;
  425. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  426. }
  427. else
  428. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  429. cout << endl;
  430. }
  431. break;
  432. case 6:
  433. {
  434. if(graph != NULL)
  435. {
  436. clock.start();
  437. vector<unsigned> route = Graph::travellingSalesmanBruteForce(*graph);
  438. clock.stop();
  439. // Wyswietlenie trasy
  440. unsigned distFromStart = 0;
  441. unsigned length = 0;
  442. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  443. for(int i = 1; i < route.size(); i++)
  444. {
  445. length = graph->getWeight(route.at(i - 1), route.at(i));
  446. distFromStart += length;
  447. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  448. }
  449. cout << "Dlugosc trasy: " << distFromStart << endl;
  450. cout << endl;
  451. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  452. }
  453. else
  454. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  455. cout << endl;
  456. }
  457. break;
  458. case 7:
  459. {
  460. // PEA 2 Plus
  461. // Jan Potocki 2019
  462. if(graph != NULL)
  463. {
  464. unsigned effectiveTabuLength;
  465. if(tabuLength == 0)
  466. effectiveTabuLength = autoTabuLength(*graph);
  467. else
  468. effectiveTabuLength = tabuLength;
  469. int measureResult = -1;
  470. int measureResultDiv = -1;
  471. cout << "Pomiary dla problemu komiwojazera, tabu search" << tabuLength << endl;
  472. if(tabuLength == 0)
  473. cout << "Kadencja: " << effectiveTabuLength << " (auto)" << endl;
  474. else
  475. cout << "Kadencja: " << effectiveTabuLength << endl;
  476. cout << "Kryterium dywersyfikacji: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
  477. cout << "Liczba watkow: " << tabuThreadsNumber << endl;
  478. // Petla pomiarowa
  479. for(int krok = 0; krok < measureIterations; krok++)
  480. {
  481. for(int i = 0; i < measureNumber; i++)
  482. {
  483. vector<unsigned> route;
  484. unsigned routeLength;
  485. // Bez dywersyfikacji
  486. cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " bez dywersyfikacji)..." << endl;
  487. route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, false, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
  488. routeLength = 0;
  489. for(int j = 1; j < route.size(); j++)
  490. routeLength += graph->getWeight(route.at(j - 1), route.at(j));
  491. if(measureResult == -1 || measureResult > routeLength)
  492. measureResult = routeLength;
  493. // Z dywersyfikacja
  494. cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " z dywersyfikacja)..." << endl;
  495. route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, true, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
  496. routeLength = 0;
  497. for(int j = 1; j < route.size(); j++)
  498. routeLength += graph->getWeight(route.at(j - 1), route.at(j));
  499. if(measureResultDiv == -1 || measureResultDiv > routeLength)
  500. measureResultDiv = routeLength;
  501. }
  502. }
  503. cout << "Zapis wynikow..." << endl;
  504. ofstream salesmanToFile;
  505. salesmanToFile.open("wyniki-komiwojazer-ts.txt");
  506. salesmanToFile << "czas - bez dywersyfikacji - z dywersyfikacja" << endl;
  507. for(int i = 0; i < measureNumber; i++)
  508. {
  509. salesmanToFile << measureTabuStop[i] << " [s]: " << measureResult << ' ' << measureResultDiv << endl;
  510. }
  511. salesmanToFile.close();
  512. cout << "Gotowe!" << endl;
  513. cout << endl;
  514. }
  515. else
  516. {
  517. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  518. cout << endl;
  519. }
  520. }
  521. break;
  522. case 8:
  523. {
  524. // PEA 2 Plus
  525. // Jan Potocki 2019
  526. string filename;
  527. cout << "Podaj nazwe pliku: ";
  528. cin >> filename;
  529. parseTSPLIB_FULL_MATRIX(filename.c_str(), &graph);
  530. }
  531. break;
  532. case 9:
  533. {
  534. // PEA 2 Plus
  535. // Jan Potocki 2019
  536. string filename;
  537. cout << "Podaj nazwe pliku: ";
  538. cin >> filename;
  539. parseTSPLIB_EUC_2D(filename.c_str(), &graph);
  540. }
  541. break;
  542. case 0:
  543. {
  544. }
  545. break;
  546. default:
  547. {
  548. cout << "Nieprawidlowy wybor" << endl;
  549. cout << endl;
  550. }
  551. }
  552. } while(salesmanSelection != 0);
  553. if(graph != NULL)
  554. delete graph;
  555. cout << "Konczenie..." << endl;
  556. // Easter egg :-P
  557. cout << '"' << "Myslak Stibbons niepokoil sie HEX-em." << endl;
  558. cout << "Nie wiedzial, jak dziala, chociaz wszyscy uwazali, ze wie." << endl;
  559. cout << "Oczywiscie, calkiem niezle orientowal sie w niektorych elementach;" << endl;
  560. cout << "byl tez pewien, ze HEX mysli o problemach, przeksztalcajac je" << endl;
  561. cout << "w liczby i mielac ..." << '"' << endl;
  562. cout << "(Terry Pratchett, " << '"' << "Wiedzmikolaj" << '"' << ", tlumaczenie Piotr Cholewa)" << endl;
  563. cout << endl;
  564. return 0;
  565. }