Advertisement
shamiul93

tsp

Jan 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define SIMPLE_TEST 0
  4. #define RANDOM_TEST 1
  5. #define TWO_OPT_TEST 2
  6.  
  7. #define MAX 105
  8. #define MAX_CITY_NO 500
  9.  
  10. using namespace std;
  11.  
  12. struct Point {
  13.     double x;
  14.     double y;
  15.  
  16.     Point() {
  17.  
  18.     }
  19.  
  20.     Point(double a, double b) {
  21.         x = a;
  22.         y = b;
  23.     }
  24.  
  25.     double deg2rad(double deg) {
  26.         return deg * (3.14159 / 180);
  27.     }
  28.  
  29.  
  30.     double getDistanceFromLatLonInKm(double lat1, double lon1, double lat2, double lon2) {
  31.         double R = 6371; // Radius of the earth in km
  32.         double dLat = deg2rad(lat2 - lat1);  // deg2rad below
  33.         double dLon = deg2rad(lon2 - lon1);
  34.         double a = sin(dLat / 2.0) * sin(dLat / 2.0) +
  35.                    cos(deg2rad(lat1)) * cos(deg2rad(lat2)) * sin(dLon / 2) * sin(dLon / 2);
  36.         double c = 2 * atan2(sqrt(a), sqrt(1 - a));
  37.         double d = R * c; // Distance in km
  38.         return d;
  39.     }
  40.  
  41.  
  42.     double dist(Point other) {
  43. //        return getDistanceFromLatLonInKm(x, y, other.x, other.y);
  44.         return hypot(x - other.x, y - other.y);
  45.     }
  46. };
  47.  
  48. int n;
  49. Point pt[MAX];
  50. bool Vis[MAX];
  51. vector<int> Tour;
  52. int bestStartNNSimple;
  53.  
  54.  
  55. class distAndPoint {
  56. public:
  57.     Point p;
  58.     int d;
  59.     int realIdx;
  60.  
  61.     distAndPoint() {
  62.  
  63.     }
  64.  
  65.     distAndPoint(Point pp, int x, int ridx) {
  66.         p = pp;
  67.         d = x;
  68.         realIdx = ridx;
  69.     }
  70.  
  71.     bool operator<(const distAndPoint &dp) {
  72.         return (d < dp.d);
  73.     }
  74.  
  75. };
  76.  
  77.  
  78. double Cost(vector<int> Tour) {
  79.     double Ans = 0;
  80.     int Size = (int) Tour.size();
  81.     for (int i = 0; i < Size; i++) {
  82.         int xx = Tour[i];
  83.         int yy = Tour[(i + 1) % Size];
  84.         Ans += pt[xx].dist(pt[yy]);
  85.     }
  86.     return Ans;
  87. }
  88.  
  89. void PrintSolution(vector<int> Tour) {
  90.     printf("Solution Cost : %lf\n", Cost(Tour));
  91.     printf("Tour Description : ");
  92.     for (int i = 0; i < Tour.size(); i++)
  93.         printf("%d ", Tour[i]);
  94.     printf("\n\n");
  95. }
  96.  
  97.  
  98. class nnTourObject {
  99. public:
  100.     vector<int> tourList;
  101.     int cost;
  102.  
  103.     nnTourObject() {}
  104.  
  105.     double Cost() {
  106.         double Ans = 0;
  107.         int Size = static_cast<int>(tourList.size());
  108.         for (int i = 0; i < Size; i++) {
  109.             int xx = tourList[i];
  110.             int yy = tourList[(i + 1) % Size];
  111.             Ans += pt[xx].dist(pt[yy]);
  112.         }
  113.         return Ans;
  114.     }
  115.  
  116.     nnTourObject(vector<int> v) {
  117.         tourList = v;
  118.         cost = static_cast<int>(Cost());
  119.     }
  120.  
  121.     bool operator<(const nnTourObject &nt) {
  122.         return (cost < nt.cost);
  123.     }
  124. };
  125.  
  126. vector<nnTourObject> nnGreedySimple_5_Tours;
  127. vector<nnTourObject> nnGreedyRandom_5_Tours;
  128.  
  129. vector<nnTourObject> shGreedySimple_5_Tours;
  130. vector<nnTourObject> shGreedyRandom_5_Tours;
  131.  
  132.  
  133. vector<nnTourObject> twoOptFirst_4_Tours_NN;
  134. vector<nnTourObject> twoOptFirst_4_Tours_SH;
  135.  
  136. vector<nnTourObject> twoOptBest_4_Tours_NN;
  137. vector<nnTourObject> twoOptBest_4_Tours_SH;
  138.  
  139.  
  140. void printVector(vector<int> vec) {
  141.     for (int i = 0; i < vec.size(); i++) {
  142.         cout << vec[i] << " ";
  143.     }
  144.     cout << endl;
  145. }
  146.  
  147. /*****************NN Simple Start**********************/
  148.  
  149. int FindNearestUnvisited(int x) {
  150.     double Min = LLONG_MAX;
  151.     int Idx = -1;
  152.     for (int i = 0; i < n; i++) {
  153.         if (Vis[i])
  154.             continue;
  155.         if (pt[x].dist(pt[i]) < Min) {
  156.             Min = pt[x].dist(pt[i]);
  157.             Idx = i;
  158.         }
  159.     }
  160.     return Idx;
  161. }
  162.  
  163.  
  164. void nnGreedySimple(int startIdx) {
  165.     Tour.clear();
  166.     memset(Vis, 0, sizeof(Vis));
  167.  
  168.     int Start = startIdx;
  169.     int Last = Start;
  170.     Vis[Last] = 1;
  171.     Tour.push_back(Start);
  172.  
  173.     while (Tour.size() < n) {
  174.         int Idx = FindNearestUnvisited(Last);
  175.         Vis[Idx] = 1;
  176.         Tour.push_back(Idx);
  177.     }
  178. }
  179.  
  180. /*****************NN Simple End**********************/
  181.  
  182.  
  183. /*****************Savings Simple Start ****************/
  184.  
  185. int adj[MAX_CITY_NO][MAX_CITY_NO] = {};
  186. int parent[MAX_CITY_NO] = {};
  187.  
  188. void SavingAddEdge(int u, int v) {
  189.     ++adj[u][v];
  190.     ++adj[v][u];
  191. }
  192.  
  193. void SavingRemoveEdge(int u, int v) {
  194.     --adj[u][v];
  195.     --adj[v][u];
  196. }
  197.  
  198. bool isSavingSameRoute(int u, int v) {
  199.     int par_u = parent[u], par_v = parent[v];
  200.     return par_u == par_v;
  201. }
  202.  
  203. void SavingMergeRoute(int u, int v) {
  204.     int par_u = parent[u], par_v = parent[v];
  205.     int p = min(par_u, par_v);
  206.  
  207.     for (int i = 0; i < n; ++i) {
  208.         if (parent[i] == par_u || parent[i] == par_v)
  209.             parent[i] = p;
  210.     }
  211. }
  212.  
  213. class savingObject {
  214. public:
  215.     int u, v;
  216.     double savingsVal;
  217.  
  218.     bool operator<(const savingObject &obj) const {
  219.         return savingsVal < obj.savingsVal;
  220.     }
  221.  
  222.  
  223.     savingObject(int x, int y, int val) {
  224.         u = x;
  225.         v = y;
  226.         savingsVal = val;
  227.     }
  228. };
  229.  
  230. void SHGreedySimple(int randStart) {
  231.     Tour.clear();
  232.     memset(Vis, 0, sizeof(Vis));
  233.  
  234.     double dist[n][n];
  235.  
  236.     for (int u = 0; u < n; u++) {
  237.         for (int v = 0; v < n; v++) {
  238.             dist[u][v] = pt[u].dist(pt[v]);
  239.         }
  240.     }
  241.  
  242.     int d = randStart;
  243.  
  244.  
  245.     priority_queue<savingObject> pq;
  246.  
  247.     for (int u = 0, i = 0; u < n; ++u) {
  248.         for (int v = u + 1; v < n; ++v, ++i) {
  249.             if (u != d && v != d) {
  250.                 savingObject obj(u, v, static_cast<int>(dist[u][d] + dist[d][v] - dist[u][v]));
  251.                 pq.push(obj);
  252.             }
  253.         }
  254.     }
  255.  
  256.     memset(adj, 0, sizeof(adj)); // init to 0
  257.     memset(parent, -1, sizeof(parent));
  258.  
  259.     for (int u = 0; u < n; ++u) {
  260.         parent[u] = u;
  261.     }
  262.  
  263.     for (int u = 0; u < n; ++u) {
  264.         if (u != d) {
  265.             SavingAddEdge(u, d);
  266.             SavingAddEdge(u, d);
  267.         }
  268.     }
  269.  
  270.  
  271.     while (!pq.empty()) {
  272.         int u = pq.top().u;
  273.         int v = pq.top().v;
  274.         pq.pop();
  275.  
  276.         if (!isSavingSameRoute(u, v) && adj[u][d] != 0 && adj[d][v] != 0) {
  277.             SavingAddEdge(u, v);
  278.             SavingMergeRoute(u, v);
  279.             SavingRemoveEdge(u, d);
  280.             SavingRemoveEdge(v, d);
  281.         }
  282.     }
  283.  
  284.  
  285.     int s = d;
  286.     do {
  287.         for (int i = 0; i < n; ++i) {
  288.             if (adj[s][i] == 1) {
  289.                 Tour.push_back(s);
  290.                 SavingRemoveEdge(s, i); // removes the edge so that same edge doesn't come twice
  291.                 s = i;
  292.             }
  293.         }
  294.     } while (s != d);
  295.  
  296. }
  297.  
  298.  
  299. // 1 = NN , 2 = SH
  300. vector<nnTourObject> SimpleUtil(int NNorSH) {
  301.     string name = "";
  302.     vector<nnTourObject> nn_5_Tours;
  303.  
  304.     int t = 0;
  305.     while (t < 5) {
  306.         t++;
  307.  
  308.         int randomStart = rand() % n;
  309.  
  310.         if (NNorSH == 1) {
  311.             nnGreedySimple(randomStart);
  312.             name = "Nearest Neighbor Greedy Simple Tour: ";
  313.         } else if (NNorSH == 2) {
  314.             SHGreedySimple(randomStart);
  315.             name = "Savings Heuristic Greedy Simple Tour: ";
  316.         }
  317.  
  318.         nnTourObject nnTour(Tour);
  319.         nn_5_Tours.push_back(nnTour);
  320.     }
  321.  
  322.     sort(nn_5_Tours.begin(), nn_5_Tours.end());
  323.  
  324. #ifdef SIMPLE_TEST
  325.     cout << name << endl;
  326.     cout << "Best Case: " << endl;
  327.     cout << "   Start Node: " << nn_5_Tours[0].tourList[0] << endl;
  328.     cout << "   Tour : ";
  329.     printVector(nn_5_Tours[0].tourList);
  330.     cout << "   Cost: " << nn_5_Tours[0].cost << endl;
  331. #endif
  332.  
  333.     bestStartNNSimple = nn_5_Tours[0].tourList[0];
  334.  
  335. #ifdef SIMPLE_TEST
  336.     cout << "Worst Case: " << endl;
  337.     cout << "   Start Node: " << nn_5_Tours[4].tourList[0] << endl;
  338.     cout << "   Tour : ";
  339.     printVector(nn_5_Tours[4].tourList);
  340.     cout << "   Cost: " << nn_5_Tours[4].cost << endl;
  341. #endif
  342.  
  343.     double sum = 0;
  344.     for (int i = 0; i < nn_5_Tours.size(); i++) {
  345.         sum += nn_5_Tours[i].cost;
  346.     }
  347.     sum = sum / nn_5_Tours.size();
  348.  
  349. #ifdef SIMPLE_TEST
  350.     cout << "Average Cost: " << sum << endl;
  351. #endif
  352.     nnGreedySimple_5_Tours = nn_5_Tours;
  353.  
  354.     return nn_5_Tours;
  355. }
  356.  
  357. /******************* Savings Simple End *******************/
  358.  
  359.  
  360.  
  361. /***********NN Random Start***************/
  362.  
  363. int FindNearestRandomUnvisited(int x) {
  364.     vector<distAndPoint> nearest_nodes;
  365.  
  366.     for (int i = 0; i < n; i++) {
  367.         if (!Vis[i]) {
  368.             distAndPoint tem(pt[i], pt[x].dist(pt[i]), i);
  369.             nearest_nodes.push_back(tem);
  370.         }
  371.     }
  372.  
  373.     sort(nearest_nodes.begin(), nearest_nodes.end());
  374.  
  375.     int divisor = 5;
  376.     if (nearest_nodes.size() < 5) divisor = static_cast<int>(nearest_nodes.size());
  377.     int randomIdx = rand() % divisor;
  378.  
  379.     distAndPoint tem = nearest_nodes[randomIdx];
  380.     int Idx = tem.realIdx;
  381.  
  382.     return Idx;
  383. }
  384.  
  385.  
  386. void nnGreedyRandomized(int startIdx) {
  387.     Tour.clear();
  388.     memset(Vis, 0, sizeof(Vis));
  389.  
  390.     int Start = startIdx;
  391.     int Last = Start;
  392.     Vis[Last] = true;
  393.     Tour.push_back(Start);
  394.  
  395.     while (Tour.size() < n) {
  396.  
  397.         int Idx = FindNearestRandomUnvisited(Last);
  398.         Vis[Idx] = true;
  399.         Tour.push_back(Idx);
  400.     }
  401. }
  402.  
  403.  
  404.  
  405. /***********NN Random End***************/
  406.  
  407.  
  408. /*********Savings Random Start *****************/
  409.  
  410. void shGreedyRandomized(int Start) {
  411.     Tour.clear();
  412.     memset(Vis, 0, sizeof(Vis));
  413.  
  414.     double dist[n][n];
  415.  
  416.     for (int u = 0; u < n; u++) {
  417.         for (int v = 0; v < n; v++) {
  418.             dist[u][v] = pt[u].dist(pt[v]);
  419.         }
  420.     }
  421.  
  422.     int d = Start;
  423.  
  424.  
  425.     priority_queue<savingObject> pq;
  426.  
  427.     for (int u = 0, i = 0; u < n; ++u) {
  428.         for (int v = u + 1; v < n; ++v, ++i) {
  429.             if (u != d && v != d) {
  430.                 savingObject obj(u, v, static_cast<int>(dist[u][d] + dist[d][v] - dist[u][v]));
  431.                 pq.push(obj);
  432.             }
  433.         }
  434.     }
  435.  
  436.     memset(adj, 0, sizeof(adj)); // init to 0
  437.     memset(parent, -1, sizeof(parent));
  438.  
  439.     for (int u = 0; u < n; ++u) {
  440.         parent[u] = u;
  441.     }
  442.  
  443.     for (int u = 0; u < n; ++u) {
  444.         if (u != d) {
  445.             SavingAddEdge(u, d);
  446.             SavingAddEdge(u, d);
  447.         }
  448.     }
  449.  
  450.  
  451.     while (!pq.empty()) {
  452.  
  453.  
  454.         vector<savingObject> vec;
  455.         int highest = 5;
  456.         if (pq.size() < 5) {
  457.             highest = static_cast<int>(pq.size());
  458.         }
  459.         for (int i = 0; i < highest; i++) {
  460.             vec.push_back(pq.top());
  461.             pq.pop();
  462.         }
  463.  
  464.         int randomIdx = rand() % highest;
  465.  
  466.         int u = vec[randomIdx].u;
  467.         int v = vec[randomIdx].v;
  468.  
  469.         for (int i = 0; i < highest; i++) {
  470.             if (i == randomIdx) continue;
  471.             pq.push(vec[i]);
  472.         }
  473.         vec.clear();
  474.  
  475.         if (!isSavingSameRoute(u, v) && adj[u][d] != 0 && adj[d][v] != 0) {
  476.             SavingAddEdge(u, v);
  477.             SavingMergeRoute(u, v);
  478.             SavingRemoveEdge(u, d);
  479.             SavingRemoveEdge(v, d);
  480.         }
  481.     }
  482.  
  483.  
  484.     int s = d;
  485.     do {
  486.         for (int i = 0; i < n; ++i) {
  487.             if (adj[s][i] == 1) {
  488.                 Tour.push_back(s);
  489.                 SavingRemoveEdge(s, i); // removes the edge so that same edge doesn't come twice
  490.                 s = i;
  491.             }
  492.         }
  493.     } while (s != d);
  494.  
  495. }
  496.  
  497.  
  498. // NN = 1 , SH = 2
  499. vector<nnTourObject> RandomizedUtil(int NNorSH) {
  500.     string name = "";
  501.     vector<nnTourObject> nn_5_Tours;
  502.     vector<nnTourObject> simpleNNTour;
  503.  
  504.     int t = 0;
  505.  
  506.     while (t < 10) {
  507.         t++;
  508.         if (NNorSH == 1) {
  509.             nnGreedyRandomized(bestStartNNSimple);
  510.             name = "Nearest Neighbor Greedy Randomized Tour: ";
  511.         } else if (NNorSH == 2) {
  512.             shGreedyRandomized(bestStartNNSimple);
  513.             name = "Savings Heuristic Greedy Randomized Tour: ";
  514.         }
  515.         nnTourObject nnTour(Tour);
  516.         nn_5_Tours.push_back(nnTour);
  517.     }
  518.  
  519.     sort(nn_5_Tours.begin(), nn_5_Tours.end());
  520.  
  521. #ifdef RANDOM_TEST
  522.     cout << name << endl;
  523.     cout << "Best Case: " << endl;
  524.     cout << "   Start Node: " << nn_5_Tours[0].tourList[0] << endl;
  525.     cout << "   Tour : ";
  526.     printVector(nn_5_Tours[0].tourList);
  527.     cout << "   Cost: " << nn_5_Tours[0].cost << endl;
  528.  
  529.     cout << "Worst Case: " << endl;
  530.     cout << "   Start Node: " << nn_5_Tours[4].tourList[0] << endl;
  531.     cout << "   Tour : ";
  532.     printVector(nn_5_Tours[4].tourList);
  533.     cout << "   Cost: " << nn_5_Tours[4].cost << endl;
  534. #endif
  535.  
  536.     double sum = 0;
  537.     for (int i = 0; i < nn_5_Tours.size(); i++) {
  538.         sum += nn_5_Tours[i].cost;
  539.     }
  540.     sum = sum / nn_5_Tours.size();
  541.  
  542. #ifdef RANDOM_TEST
  543.     cout << "Average Cost: " << sum << endl;
  544. #endif
  545.  
  546.     return nn_5_Tours;
  547. }
  548.  
  549.  
  550. /********** Savings Random End ****************/
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557. /************2-Opt First improve Start***************/
  558. void TwoOptHeuristicFirstImprovement(vector<int> vec) {
  559.     Tour.clear();
  560.     memset(Vis, 0, sizeof(Vis));
  561.  
  562.     Tour = vec;
  563.  
  564.     while (true) {
  565.         double Curr = Cost(Tour);
  566.         bool Changed = false;
  567.  
  568.         for (int i = 0; i < Tour.size(); i++) {
  569.             for (int j = i + 2; j < Tour.size(); j++) {
  570.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  571.                 double NewCost = Cost(Tour);
  572.                 if (NewCost < Curr) {
  573.                     Changed = true;
  574.                     break;
  575.                 }
  576.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  577.             }
  578.             if (Changed)
  579.                 break;
  580.         }
  581.         if (!Changed)
  582.             break;
  583.     }
  584. }
  585.  
  586. /***************2-Opt best improve start***********/
  587. void TwoOptHeuristicBestImprovement(vector<int> vec) {
  588.     int ii = 0, jj = 0;
  589.     Tour.clear();
  590.     memset(Vis, 0, sizeof(Vis));
  591.  
  592.     Tour = vec;
  593.  
  594.     while (true) {
  595.         double Curr = Cost(Tour);
  596.         bool Changed = false;
  597.  
  598.         for (int i = 0; i < Tour.size(); i++) {
  599.             for (int j = i + 2; j < Tour.size(); j++) {
  600.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  601.                 double NewCost = Cost(Tour);
  602.                 if (NewCost < Curr) {
  603.                     Changed = true;
  604.                     ii = i;
  605.                     jj = j;
  606.                     Curr = NewCost;
  607.                 }
  608.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  609.             }
  610.             if (Changed) {
  611.                 reverse(Tour.begin() + ii + 1, Tour.begin() + jj + 1);
  612.                 break;
  613.             }
  614.         }
  615.  
  616.         if (!Changed)
  617.             break;
  618.     }
  619. }
  620.  
  621.  
  622. // twoOptType == 1 -> First , 2 -> Best
  623. // NNorSH == 1 -> NN , 2 -> SH
  624.  
  625. vector<nnTourObject> twoOptUtil(int NNorSH, int twoOptType) {
  626.     string name = "";
  627.     vector<nnTourObject> myTourResults;
  628.     vector<nnTourObject> bestToursOfNNHandSH;
  629.  
  630.     if (NNorSH == 1) {
  631.         bestToursOfNNHandSH.push_back(nnGreedyRandom_5_Tours[0]);
  632.         bestToursOfNNHandSH.push_back(nnGreedyRandom_5_Tours[1]);
  633.         bestToursOfNNHandSH.push_back(nnGreedyRandom_5_Tours[2]);
  634.         bestToursOfNNHandSH.push_back(nnGreedySimple_5_Tours[0]);
  635.  
  636.         if (twoOptType == 1) {
  637.             for (int i = 0; i < bestToursOfNNHandSH.size(); i++) {
  638.                 TwoOptHeuristicFirstImprovement(bestToursOfNNHandSH[i].tourList);
  639.                 nnTourObject tem(Tour);
  640.                 myTourResults.push_back(tem);
  641.             }
  642.             name = "2-Opt First for NNH Greedy Random.";
  643.         } else if (twoOptType == 2) {
  644.             for (int i = 0; i < bestToursOfNNHandSH.size(); i++) {
  645.                 TwoOptHeuristicBestImprovement(bestToursOfNNHandSH[i].tourList);
  646.                 nnTourObject tem(Tour);
  647.                 myTourResults.push_back(tem);
  648.             }
  649.             name = "2-Opt Best for NNH Greedy Random.";
  650.         }
  651.  
  652.     } else if (NNorSH == 2) {
  653.         //SH vectors go here.
  654.         bestToursOfNNHandSH.push_back(shGreedyRandom_5_Tours[0]);
  655.         bestToursOfNNHandSH.push_back(shGreedyRandom_5_Tours[1]);
  656.         bestToursOfNNHandSH.push_back(shGreedyRandom_5_Tours[2]);
  657.         bestToursOfNNHandSH.push_back(shGreedySimple_5_Tours[0]);
  658.  
  659.         if (twoOptType == 1) {
  660.             for (int i = 0; i < bestToursOfNNHandSH.size(); i++) {
  661.                 TwoOptHeuristicFirstImprovement(bestToursOfNNHandSH[i].tourList);
  662.                 nnTourObject tem(Tour);
  663.                 myTourResults.push_back(tem);
  664.             }
  665.             name = "2-Opt First for SH Greedy Random.";
  666.         } else if (twoOptType == 2) {
  667.             for (int i = 0; i < bestToursOfNNHandSH.size(); i++) {
  668.                 TwoOptHeuristicBestImprovement(bestToursOfNNHandSH[i].tourList);
  669.                 nnTourObject tem(Tour);
  670.                 myTourResults.push_back(tem);
  671.             }
  672.             name = "2-Opt First for SH Greedy Random.";
  673.         }
  674.     }
  675.  
  676.  
  677.     sort(myTourResults.begin(), myTourResults.end());
  678.  
  679. #ifdef TWO_OPT_TEST
  680.     cout << name << " : " << endl;
  681.     cout << "Best Case: " << endl;
  682.     cout << "   Start Node: " << myTourResults[0].tourList[0] << endl;
  683.     cout << "   Tour : ";
  684.     printVector(myTourResults[0].tourList);
  685.     cout << "   Cost: " << myTourResults[0].cost << endl;
  686.  
  687.     cout << "Worst Case: " << endl;
  688.     cout << "   Start Node: " << myTourResults[myTourResults.size() - 1].tourList[0] << endl;
  689.     cout << "   Tour : ";
  690.     printVector(myTourResults[myTourResults.size() - 1].tourList);
  691.     cout << "   Cost: " << myTourResults[myTourResults.size() - 1].cost << endl;
  692. #endif
  693.  
  694.     double sum = 0;
  695.     for (int i = 0; i < myTourResults.size(); i++) {
  696.         sum += myTourResults[i].cost;
  697.     }
  698.     sum = sum / myTourResults.size();
  699.  
  700. #ifdef TWO_OPT_TEST
  701.     cout << "Average Cost: " << sum << endl;
  702. #endif
  703.  
  704.     return myTourResults;
  705. }
  706.  
  707.  
  708. int main() {
  709. //    freopen("pr76.tsp", "r", stdin);
  710. //    freopen("pr76_out.txt", "w", stdout);
  711. //    cout << "Output For pr76.tsp Dataset." << endl << endl;
  712.  
  713.  
  714. //    freopen("berlin52.tsp", "r", stdin);
  715. //    freopen("berlin52_out.txt", "w", stdout);
  716. //    cout << "Output For berlin52.tsp Dataset." << endl << endl ;
  717.  
  718.  
  719.     freopen("st70.tsp", "r", stdin);
  720.     freopen("st70_out.txt", "w", stdout);
  721.     cout << "Output For st70.tsp Dataset." << endl << endl ;
  722.  
  723.     srand(time(NULL));
  724.  
  725.     scanf("%d", &n);
  726.     for (int i = 0; i < n; i++) { scanf("%lf %lf", &pt[i].x, &pt[i].y); }
  727.  
  728.     /***************Run NN_Greedy_Simple()*****************/
  729.  
  730.     cout << endl << endl << "##############################################################################" << endl
  731.          << endl;
  732.  
  733.     nnGreedySimple_5_Tours = SimpleUtil(1);
  734.     cout << endl << endl << "##############################################################################" << endl
  735.          << endl;
  736.  
  737.     shGreedySimple_5_Tours = SimpleUtil(2);
  738.     cout << endl << endl << "##############################################################################" << endl
  739.          << endl;
  740.  
  741.     nnGreedyRandom_5_Tours = RandomizedUtil(1);
  742.     cout << endl << endl << "##############################################################################" << endl
  743.          << endl;
  744.  
  745.     shGreedyRandom_5_Tours = RandomizedUtil(2);
  746.     cout << endl << endl << "##############################################################################" << endl
  747.          << endl;
  748.  
  749.     /* (nnsh, twoOptType)..
  750.      * NNorSH == 1 -> NN , 2 -> SH
  751.      * twoOptType == 1 -> First , 2 -> Best
  752.      */
  753.     twoOptFirst_4_Tours_NN = twoOptUtil(1, 1);
  754.     cout << endl << endl << "##############################################################################" << endl
  755.          << endl;
  756.  
  757.     twoOptFirst_4_Tours_SH = twoOptUtil(2, 1);
  758.     cout << endl << endl << "##############################################################################" << endl
  759.          << endl;
  760.  
  761.     twoOptBest_4_Tours_NN = twoOptUtil(1, 2);
  762.     cout << endl << endl << "##############################################################################" << endl
  763.          << endl;
  764.  
  765.     twoOptBest_4_Tours_SH = twoOptUtil(2, 2);
  766.     cout << endl << endl << "##############################################################################" << endl
  767.          << endl;
  768.  
  769.     return 0;
  770. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement