📄 randomrestart.cpp
字号:
#include "Problem.h"
#include "Solution.h"
#include "orOptTSP.h"
#include "Control_fr.h"
#include "Random.h"
#include "farthestInsertion.h"
/*
randomRestart.cpp
Program which performs orOpt local search on random initial solution, for ten times.
Output: best result found, average CPU time for getting into a local optimum*10.
*/
#include <fstream.h>
#include <iostream.h>
#include <math.h>
#include <limits.h>
using namespace std;
int main( int argc, char** argv ) {
//Create a control object from the command line arguments.
Control_fr control( argc, argv );
//Random object.
Random* rnd = new Random((unsigned) control.getSeed());
ostream& output = control.getOutputStream();
int niter = control.getNumberOfIterations();
double best=1000000000;
//Initialize the problem instance, using input file stream and input data given by control.
Problem *problem = new Problem( control );
//Run a number of tries, control knows how many tries there should be done
//Tell control we are starting a new try.
control.beginTry();
Solution currentSolution( rnd, control, problem );
Solution bestSolution( rnd, control, problem );
while( (control.getIterations() < niter) & control.timeLeft()) {
//Initialize starting solution.
randomizedFarthestInsertion(rnd, currentSolution);
control.setCurrentCost( currentSolution.computeExpectedCost());
//orOpt local search may improve currentSolution.
orOptTSP(rnd, control, currentSolution);
if(currentSolution.expectedCost < best) {
best = currentSolution.expectedCost;
bestSolution.copySolution(currentSolution);
}
control.incrementIterations();
}
control.endTry();
bestSolution.printOn(output);
output << "iterations: " << control.getIterations() << endl;
output << "time:" << control.getTime() << endl;
/* //Initialize thresholds vector.
vector<int> thresholds(problem->numberOfCustomers, -1);
currentSolution.computeExpectedCostAndThresholds(thresholds);
currentSolution.printOn(cout, thresholds);
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -