?? control.cpp
字號:
#include "Control.h"
Control::Control( void ) {
}
Control::Control( int argc, char** argv ) {
// parse the command line options to set all vars
if( ( argc % 2 == 0 ) || ( argc == 1 ) ) {
cerr << "Parse error: Number of command line parameters incorrect\n";
cerr << "Usage:" << endl;
cerr << argv[ 0 ] << " -i InputFile [-o OutputFile] [-n NumberOfTries] [-s RandomSeed] [-t TimeLimit] [-q VehicleCapacity] [-p UseProxy]" << endl;
exit(1);
}
for( int i = 1; i < argc / 2 + 1; i++ ) {
parameters[ argv[ i * 2 - 1 ] ] = argv[ i * 2 ];
}
iterations =0;
nrTry = 0;
flagMatrix = false;
// check for input parameter
if( parameterExists( "-i") ) {
is = new ifstream( getStringParameter( "-i" ).c_str() );
flagMatrix = false;
}
else if (parameterExists( "-m") & flagMatrix == false){
is = new ifstream( getStringParameter( "-m" ).c_str() );
flagMatrix = true;
}
else {
cerr << "Error: No input file given, exiting" << endl;
cerr << "Usage:" << endl;
cerr << argv[ 0 ] << " -i [-m] InputFile [-o OutputFile] [-n NumberOfTries] [-s RandomSeed]" << endl;
exit(1);
}
// check for ouput parameter
if( parameterExists( "-o" ) ) {
os = new ofstream( getStringParameter( "-o" ).c_str() );
} else {
cerr << "Warning: No output file given, writing to stdout" << endl;
os = &cout;
}
// check for number of tries parameter
if( parameterExists( "-n" ) ) {
maxTry = getIntParameter( "-n" );
} else {
cerr << "Warning: Number of tries is set to default (1)" << endl;
maxTry = 1; // default number of tries
}
// check for time limit parameter
if( parameterExists( "-t" ) ) {
timeLimit = getDoubleParameter( "-t" );
} else {
cerr << "Warning: Time limit is set to default (10sec)" << endl;
timeLimit = 10; // default number of tries
}
// check for random seed
if( parameterExists( "-s" ) ) {
seed = getIntParameter( "-s" );
srand( seed );
} else {
seed = time( NULL );
cerr << "Warning: " << seed << " used as default random seed" << endl;
srand( seed );
}
//Check for vehicle capacity.
if( parameterExists( "-q" ) ) {
flagCapacity=true;
capacity = getIntParameter( "-q" );
cerr << "Warning: vehicle capacity is set to " << capacity << endl;
} else {
capacity = -1;
flagCapacity = false;
}
//Check for type of local search flag.
if( parameterExists( "-p" ) ) {
useProxy = getIntParameter( "-p" );
} else {
useProxy = 1;
cerr << "Warning: local search uses approximated delta evaluation by default" << endl;
}
//Print ot output stream the command line parameters.
(*os) << endl;
(*os) << "Parameter-settings:" << endl << endl;
if(flagMatrix == false)
(*os) << "Input file\t" << getStringParameter( "-i" ).c_str() << " " << endl;
else
(*os) << "Input file\t" << getStringParameter( "-m" ).c_str() << " " << endl;
(*os) << "Number of tries\t" << maxTry << endl;
(*os) << "Seed\t" << seed << endl;
(*os) << "Time limit\t" << timeLimit << endl;
if(flagCapacity == true)
(*os) << "VehicleCapacity\t" << capacity << endl;
(*os) << "useProxy (1 yes, 0 no)\t" << useProxy << endl;
(*os) << endl;
}
Control::~Control() {
}
bool
Control::parameterExists( string paramName ) {
for( map< string, string >::iterator i = parameters.begin(); i != parameters.end(); i++ ) {
if( i-> first == paramName )
return true;
}
return false;
}
int
Control::getIntParameter( string paramName ) {
if( parameterExists( paramName ) )
return atoi( parameters[paramName].c_str() );
else {
return 0;
}
}
double
Control::getDoubleParameter( string paramName ) {
if( parameterExists( paramName ) )
return atof( parameters[paramName].c_str() );
else {
return 0;
}
}
string
Control::getStringParameter( string paramName ) {
if( parameterExists( paramName ) )
return parameters[paramName];
else {
return 0;
}
}
void
Control::resetTime() {
timer.resetTime();
}
double
Control::getTime() {
return timer.elapsedTime( Timer::VIRTUAL );
}
void
Control::beginTry() {
srand( seed++ );
++nrTry;
(*os) << "begin try " << nrTry << endl;
resetTime();
bestCost = INT_MAX;
nrCost = 0;
nrDeltaCost = 0;
nrLen = 0;
nrDeltaLen = 0;
}
void
Control::endTry() {
(*os) << "end try " << nrTry << endl;
(*os) << "final " << bestCost << " time " << bestTime
<< " nrCost " << nrCost << " nrDeltaCost "
<< nrDeltaCost << " nrLen " << nrLen << " nrDeltaLen "
<< nrDeltaLen << endl;
}
void
Control::setCurrentCost( double cost ) {
double time = getTime();
if( time < timeLimit ) {
currentCost = cost;
if( currentCost < bestCost ) {
bestCost = currentCost;
bestTime = ( time < 0 ? 0.0 : time);
(*os) << "best " << bestCost << " time ";
os->flags( ios::fixed );
(*os) << ( time < 0 ? 0.0 : time ) ;
(*os) << " nrCost " << nrCost << " nrDeltaCost " << nrDeltaCost
<< " nrLen " << nrLen << " nrDeltaLen " << nrDeltaLen << endl;
}
}
}
int
Control:: getCapacity(){
if (flagCapacity)
return capacity;
else{
cerr << "Error: Control::getCapacity() when no capacity given in command line." << endl;
return 0;
}
}
bool
Control::capacityExists(){
return flagCapacity;
}
bool
Control::usingProxy(){
return (useProxy == 1);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -