⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ils.cpp

📁 随机vrp 3
💻 CPP
字号:
#include "Problem.h"
#include "Solution.h"
#include "orOpt.h"
#include "Control.h"
#include "Random.h"
#include "farthestInsertion.h"

#include <fstream.h>
#include <iostream.h>
#include <math.h>
using namespace std;

int main( int argc, char** argv ) {


  Control control( argc, argv );


  Random* rnd =  new Random((unsigned) control.getSeed());;
  Problem *problem = new Problem( control );
  int n = problem->numberOfCustomers;

  while( control.triesLeft() ) {

    control.beginTry();    
    
    Solution currentSolution(rnd, control, problem );
    Solution bestSolution(rnd, control, problem );

    farthestInsertion(currentSolution);

    control.setCurrentCost( currentSolution.computeExpectedCost());

    double best = currentSolution.expectedCost;
    bestSolution.copySolution(currentSolution);

    while ( control.timeLeft() ) {

      orOpt(rnd,control, currentSolution);
      
      //cout << "local search= " << currentSolution.expectedCost << endl;//////////


      control.setCurrentCost( currentSolution.computeExpectedCost());
         
      if (currentSolution.expectedCost < best) { 
	bestSolution.copySolution(currentSolution);
	best = currentSolution.expectedCost; 
      }
      else 
	currentSolution.copySolution(bestSolution); 

      double best_perturbed = INT_MAX;
      
      Solution PbestSolution(rnd, control, problem ); 
      int count = 0;
      
      while ( count < n ){ 
	currentSolution.copySolution( bestSolution);
	currentSolution.perturbTSP(); 
	currentSolution.computeExpectedCost();
	control.setCurrentCost( currentSolution.expectedCost); 
	if (currentSolution.expectedCost < best) {
	  bestSolution.copySolution(currentSolution);
	  best = currentSolution.expectedCost; 
	  break;
	}
	else if (currentSolution.expectedCost - best < n/10)
	  break;	

	if (best_perturbed > currentSolution.expectedCost - best) {
	  PbestSolution.copySolution( currentSolution);
	  best_perturbed = currentSolution.expectedCost - best;
	}
	count ++;
      }      
      if (count == n) 
	currentSolution.copySolution( PbestSolution);
      
      control.setCurrentCost( currentSolution.computeExpectedCost());

    }
    
    bestSolution.copySolution(currentSolution); 
    
    control.endTry();
    bestSolution.printOn(control.getOutputStream());
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -