|
|
Go to the documentation of this file. 47 #include "objscip/objscip.h" 48 #include "objscip/objscipdefplugins.h" 65 vector<vector<int> >& dist 68 static const string DIMENSION = "DIMENSION"; 69 static const string DEMAND_SECTION = "DEMAND_SECTION"; 70 static const string DEPOT_SECTION = "DEPOT_SECTION"; 71 static const string EDGE_WEIGHT_TYPE = "EDGE_WEIGHT_TYPE"; 72 static const string EUC_2D = "EUC_2D"; 73 static const string EXPLICIT = "EXPLICIT"; 74 static const string LOWER_DIAG_ROW = "LOWER_DIAG_ROW"; 75 static const string EDGE_WEIGHT_FORMAT = "EDGE_WEIGHT_FORMAT"; 76 static const string EDGE_WEIGHT_SECTION = "EDGE_WEIGHT_SECTION"; 77 static const string NODE_COORD_SECTION = "NODE_COORD_SECTION"; 78 static const string CAPACITY = "CAPACITY"; 80 ifstream file(filename); 84 cerr << "Cannot open file " << filename << endl; 88 string edge_weight_type = ""; 89 string edge_weight_format = ""; 102 if ( key == DIMENSION ) 107 demand.resize(num_nodes, 0); 108 dist.resize(num_nodes); 109 for ( int i = 0; i < num_nodes; ++i) 110 dist[i].resize(i, 0); 113 if ( key == CAPACITY ) 118 else if ( key == EDGE_WEIGHT_TYPE ) 121 file >> edge_weight_type; 122 if ( edge_weight_type != EUC_2D && edge_weight_type != EXPLICIT ) 124 cerr << "Wrong " << EDGE_WEIGHT_TYPE << " " << edge_weight_type << endl; 127 if ( edge_weight_type == EUC_2D ) 129 x.resize(num_nodes, 0); 130 y.resize(num_nodes, 0); 133 else if ( key == EDGE_WEIGHT_FORMAT ) 136 file >> edge_weight_format; 138 else if ( key == EDGE_WEIGHT_FORMAT + ":" ) 140 file >> edge_weight_format; 142 else if ( key == EDGE_WEIGHT_SECTION ) 144 if ( edge_weight_type != EXPLICIT || edge_weight_format != LOWER_DIAG_ROW ) 146 cerr << "Error. Unsupported edge length type." << endl; 149 for ( int i = 0; i < num_nodes; ++i) 151 for ( int j = 0; j < i; ++j) 159 else if ( key == NODE_COORD_SECTION ) 161 if ( edge_weight_type != EUC_2D ) 163 cerr << "Error. Data file contains " << EDGE_WEIGHT_TYPE << " " << edge_weight_type << " and " << NODE_COORD_SECTION << endl; 166 for ( int i = 0; i < num_nodes; ++i) 174 cerr << "Error reading " << NODE_COORD_SECTION << endl; 180 for ( int i = 0; i < num_nodes; ++i) 182 for ( int j = 0; j < i; ++j) 184 int dx = x[i] - x[j]; 185 int dy = y[i] - y[j]; 186 dist[i][j] = int( sqrt(( double)dx*dx + dy*dy) + 0.5 ); 190 else if ( key == DEMAND_SECTION ) 192 for ( int i = 0; i < num_nodes; ++i) 199 cerr << "Error reading " << DEMAND_SECTION << endl; 205 else if ( key == DEPOT_SECTION ) 207 for ( int i = 0; i != -1 ;) 210 if ( i != -1 && i != 1 ) 212 cerr << "Error: This file specifies other depots than 1." << endl; 219 (void) getline(file, dummy); 232 cout << "Solving the vehicle routing problem using SCIP." << endl; 233 cout << "Implemented by Andreas Bley." << endl << endl; 235 if ( argc != 2 && argc != 3 ) 237 cerr << "Usage: vrp [-h] datafile" << endl; 238 cerr << "Options:" << endl; 239 cerr << " -h Uses hop limit instead of capacity limit for tours."<< endl; 240 return SCIP_INVALIDDATA; 248 static const char* VRP_PRICER_NAME = "VRP_Pricer"; 250 vector<vector<int> > dist; 255 if ( read_problem(argv[argc-1], num_nodes, capacity, demand, dist) ) 257 cerr << "Error reading data file " << argv[argc-1] << endl; 258 return SCIP_READERROR; 261 cout << "Number of nodes: " << num_nodes << endl; 265 if ( string( "-h") != argv[1] ) 267 cerr << "Unknow option " << argv[2] << endl; 268 return SCIP_PARAMETERUNKNOWN; 271 int total_demand = 0; 272 for ( int i = 1; i< num_nodes; ++i) 273 total_demand += demand[i]; 275 if( total_demand == 0.0 ) 277 cerr << "Total demand is zero!" << endl; 278 return SCIP_INVALIDDATA; 281 capacity = (num_nodes - 1) * capacity / total_demand; 282 demand.assign(num_nodes, 1); 284 cout << "Max customers per tour: " << capacity << endl << endl; 287 cout << "Max demand per tour: " << capacity << endl << endl; 294 SCIP_CALL( SCIPcreate(&scip) ); 300 SCIPprintVersion(scip, NULL); 301 SCIPinfoMessage(scip, NULL, "\n"); 304 SCIP_CALL( SCIPincludeDefaultPlugins(scip) ); 307 SCIP_CALL( SCIPsetIntParam(scip, "display/verblevel", 5) ); 311 SCIP_CALL( SCIPcreateProb(scip, "VRP", 0, 0, 0, 0, 0, 0, 0) ); 315 vector< vector<SCIP_VAR*> > arc_var( num_nodes ); 316 for ( int i = 0; i < num_nodes; ++i) 318 arc_var[i].resize(i, (SCIP_VAR*) NULL); 319 for ( int j = 0; j < i; ++j) 322 (void) SCIPsnprintf(var_name, 255, "E%d_%d", i, j ); 324 SCIP_CALL( SCIPcreateVar(scip, 330 SCIP_VARTYPE_INTEGER, 333 NULL, NULL, NULL, NULL, NULL) ); 334 SCIP_CALL( SCIPaddVar(scip, var) ); 341 vector< vector<SCIP_CONS*> > arc_con( num_nodes ); 342 for ( int i = 0; i < num_nodes; ++i) 344 arc_con[i].resize(i, (SCIP_CONS*)NULL); 345 for ( int j = 0; j < i; ++j) 348 (void) SCIPsnprintf(con_name, 255, "A%d_%d", i, j); 349 SCIP_VAR* idx = arc_var[i][j]; 350 SCIP_Real coeff = -1; 351 SCIP_CALL( SCIPcreateConsLinear(scip, &con, con_name, 1, &idx, &coeff, 364 SCIP_CALL( SCIPaddCons(scip, con) ); 370 for ( int i = 1; i < num_nodes; ++i) 373 (void) SCIPsnprintf(con_name, 255, "D%d", i); 374 SCIP_CALL( SCIPcreateConsLinear(scip, &con, con_name, 0, 0, 0, 387 SCIP_CALL( SCIPaddCons(scip, con) ); 388 for ( int j = 0; j < num_nodes; ++j) 392 SCIP_CALL( SCIPaddCoefLinear(scip, con, i > j ? arc_var[i][j] : arc_var[j][i], 1.0) ); 398 vector<SCIP_CONS*> part_con(num_nodes, (SCIP_CONS*)NULL); 399 for ( int i = 1; i < num_nodes; ++i) 401 SCIP_CONS* con = NULL; 402 (void) SCIPsnprintf(con_name, 255, "C%d", i); 403 SCIP_CALL( SCIPcreateConsLinear( scip, &con, con_name, 0, NULL, NULL, 416 SCIP_CALL( SCIPaddCons(scip, con) ); 422 arc_var, arc_con, part_con); 424 SCIP_CALL( SCIPincludeObjPricer(scip, vrp_pricer_ptr, true) ); 427 SCIP_CALL( SCIPactivatePricer(scip, SCIPfindPricer(scip, VRP_PRICER_NAME)) ); 436 SCIP_CALL( SCIPsolve(scip) ); 442 SCIP_CALL( SCIPprintStatistics(scip, NULL) ); 444 SCIP_CALL( SCIPprintBestSol(scip, NULL, FALSE) ); 451 SCIP_CALL( SCIPfree(&scip) ); 453 BMScheckEmptyMemory(); 458 int main( int argc, char** argv) 460 return execmain(argc, argv) != SCIP_OKAY ? 1 : 0;
int read_problem(const char *filename, int &num_nodes, int &capacity, vector< int > &demand, vector< vector< int > > &dist)
int main(int argc, char **argv)
SCIP_RETCODE execmain(int argc, char **argv)
|