Implementacja tabu search dla TSP
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 = 5000;
  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 = 10 + (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 << endl;
  385. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  386. for(int i = 1; i < route.size(); i++)
  387. {
  388. length = graph->getWeight(route.at(i - 1), route.at(i));
  389. distFromStart += length;
  390. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  391. }
  392. cout << "Dlugosc trasy: " << distFromStart << endl;
  393. cout << endl;
  394. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  395. }
  396. else
  397. {
  398. // Easter egg ;-)
  399. cout << "+++ MELON MELON MELON +++ Blad: Brak Sera! +++ !!!!! +++" << endl;
  400. }
  401. }
  402. else
  403. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  404. cout << endl;
  405. }
  406. break;
  407. case 5:
  408. {
  409. if(graph != NULL)
  410. {
  411. clock.start();
  412. vector<unsigned> route = Graph::travellingSalesmanBranchAndBound(*graph);
  413. clock.stop();
  414. // Wyswietlenie trasy
  415. unsigned distFromStart = 0;
  416. unsigned length = 0;
  417. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  418. for(int i = 1; i < route.size(); i++)
  419. {
  420. length = graph->getWeight(route.at(i - 1), route.at(i));
  421. distFromStart += length;
  422. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  423. }
  424. cout << "Dlugosc trasy: " << distFromStart << endl;
  425. cout << endl;
  426. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  427. }
  428. else
  429. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  430. cout << endl;
  431. }
  432. break;
  433. case 6:
  434. {
  435. if(graph != NULL)
  436. {
  437. clock.start();
  438. vector<unsigned> route = Graph::travellingSalesmanBruteForce(*graph);
  439. clock.stop();
  440. // Wyswietlenie trasy
  441. unsigned distFromStart = 0;
  442. unsigned length = 0;
  443. cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
  444. for(int i = 1; i < route.size(); i++)
  445. {
  446. length = graph->getWeight(route.at(i - 1), route.at(i));
  447. distFromStart += length;
  448. cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
  449. }
  450. cout << "Dlugosc trasy: " << distFromStart << endl;
  451. cout << endl;
  452. cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
  453. }
  454. else
  455. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  456. cout << endl;
  457. }
  458. break;
  459. case 7:
  460. {
  461. // PEA 2 Plus
  462. // Jan Potocki 2019
  463. if(graph != NULL)
  464. {
  465. unsigned effectiveTabuLength;
  466. if(tabuLength == 0)
  467. effectiveTabuLength = autoTabuLength(*graph);
  468. else
  469. effectiveTabuLength = tabuLength;
  470. int measureResult = -1;
  471. int measureResultDiv = -1;
  472. cout << "Pomiary dla problemu komiwojazera, tabu search" << tabuLength << endl;
  473. if(tabuLength == 0)
  474. cout << "Kadencja: " << effectiveTabuLength << " (auto)" << endl;
  475. else
  476. cout << "Kadencja: " << effectiveTabuLength << endl;
  477. cout << "Kryterium dywersyfikacji: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
  478. cout << "Liczba watkow: " << tabuThreadsNumber << endl;
  479. // Petla pomiarowa
  480. for(int krok = 0; krok < measureIterations; krok++)
  481. {
  482. for(int i = 0; i < measureNumber; i++)
  483. {
  484. vector<unsigned> route;
  485. unsigned routeLength;
  486. // Bez dywersyfikacji
  487. cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " bez dywersyfikacji)..." << endl;
  488. route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, false, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
  489. routeLength = 0;
  490. for(int j = 1; j < route.size(); j++)
  491. routeLength += graph->getWeight(route.at(j - 1), route.at(j));
  492. if(measureResult == -1 || measureResult > routeLength)
  493. measureResult = routeLength;
  494. // Z dywersyfikacja
  495. cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " z dywersyfikacja)..." << endl;
  496. route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, true, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
  497. routeLength = 0;
  498. for(int j = 1; j < route.size(); j++)
  499. routeLength += graph->getWeight(route.at(j - 1), route.at(j));
  500. if(measureResultDiv == -1 || measureResultDiv > routeLength)
  501. measureResultDiv = routeLength;
  502. }
  503. }
  504. cout << "Zapis wynikow..." << endl;
  505. ofstream salesmanToFile;
  506. salesmanToFile.open("wyniki-komiwojazer-ts.txt");
  507. salesmanToFile << "czas - bez dywersyfikacji - z dywersyfikacja" << endl;
  508. for(int i = 0; i < measureNumber; i++)
  509. {
  510. salesmanToFile << measureTabuStop[i] << " [s]: " << measureResult << ' ' << measureResultDiv << endl;
  511. }
  512. salesmanToFile.close();
  513. cout << "Gotowe!" << endl;
  514. cout << endl;
  515. }
  516. else
  517. {
  518. cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
  519. cout << endl;
  520. }
  521. }
  522. break;
  523. case 8:
  524. {
  525. // PEA 2 Plus
  526. // Jan Potocki 2019
  527. string filename;
  528. cout << "Podaj nazwe pliku: ";
  529. cin >> filename;
  530. parseTSPLIB_FULL_MATRIX(filename.c_str(), &graph);
  531. }
  532. break;
  533. case 9:
  534. {
  535. // PEA 2 Plus
  536. // Jan Potocki 2019
  537. string filename;
  538. cout << "Podaj nazwe pliku: ";
  539. cin >> filename;
  540. parseTSPLIB_EUC_2D(filename.c_str(), &graph);
  541. }
  542. break;
  543. case 0:
  544. {
  545. }
  546. break;
  547. default:
  548. {
  549. cout << "Nieprawidlowy wybor" << endl;
  550. cout << endl;
  551. }
  552. }
  553. } while(salesmanSelection != 0);
  554. if(graph != NULL)
  555. delete graph;
  556. cout << "Konczenie..." << endl;
  557. // Easter egg :-P
  558. cout << '"' << "Myslak Stibbons niepokoil sie HEX-em." << endl;
  559. cout << "Nie wiedzial, jak dziala, chociaz wszyscy uwazali, ze wie." << endl;
  560. cout << "Oczywiscie, calkiem niezle orientowal sie w niektorych elementach;" << endl;
  561. cout << "byl tez pewien, ze HEX mysli o problemach, przeksztalcajac je" << endl;
  562. cout << "w liczby i mielac ..." << '"' << endl;
  563. cout << "(Terry Pratchett, " << '"' << "Wiedzmikolaj" << '"' << ", tlumaczenie Piotr Cholewa)" << endl;
  564. cout << endl;
  565. return 0;
  566. }