daily pastebin goal
56%
SHARE
TWEET

2-opt best

shamiul93 Jan 20th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. /***************2-Opt best improve start***********/
  4. void TwoOptHeuristicBestImprovement(vector<int> vec) {
  5.     int ii = 0, jj = 0;
  6.     Tour.clear();
  7.     memset(Vis, 0, sizeof(Vis));
  8.  
  9.     Tour = vec;
  10.  
  11.     while (true) {
  12.         double Curr = Cost(Tour);
  13.         bool Changed = false;
  14.  
  15.         for (int i = 0; i < Tour.size(); i++) {
  16.             for (int j = i + 2; j < Tour.size(); j++) {
  17.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  18.                 double NewCost = Cost(Tour);
  19.                 if (NewCost < Curr) {
  20.                     Changed = true;
  21.                     ii = i;
  22.                     jj = j;
  23.                     Curr = NewCost;
  24.                 }
  25.                 reverse(Tour.begin() + i + 1, Tour.begin() + j + 1);
  26.             }
  27.             if (Changed) {
  28.                 reverse(Tour.begin() + ii + 1, Tour.begin() + jj + 1);
  29.                 break;
  30.             }
  31.         }
  32.  
  33.         if (!Changed)
  34.             break;
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top