77 vector<vector<int> >& dist
80 static const string DIMENSION =
"DIMENSION";
81 static const string DEMAND_SECTION =
"DEMAND_SECTION";
82 static const string DEPOT_SECTION =
"DEPOT_SECTION";
83 static const string EDGE_WEIGHT_TYPE =
"EDGE_WEIGHT_TYPE";
84 static const string EUC_2D =
"EUC_2D";
85 static const string EXPLICIT =
"EXPLICIT";
86 static const string LOWER_DIAG_ROW =
"LOWER_DIAG_ROW";
87 static const string EDGE_WEIGHT_FORMAT =
"EDGE_WEIGHT_FORMAT";
88 static const string EDGE_WEIGHT_SECTION =
"EDGE_WEIGHT_SECTION";
89 static const string NODE_COORD_SECTION =
"NODE_COORD_SECTION";
90 static const string CAPACITY =
"CAPACITY";
92 ifstream file(filename);
96 cerr <<
"Cannot open file " << filename << endl;
100 string edge_weight_type =
"";
101 string edge_weight_format =
"";
114 if ( key == DIMENSION )
119 demand.resize(num_nodes, 0);
120 dist.resize(num_nodes);
121 for (
int i = 0; i < num_nodes; ++i)
122 dist[i].resize(i, 0);
125 if ( key == CAPACITY )
130 else if ( key == EDGE_WEIGHT_TYPE )
133 file >> edge_weight_type;
134 if ( edge_weight_type != EUC_2D && edge_weight_type != EXPLICIT )
136 cerr <<
"Wrong " << EDGE_WEIGHT_TYPE <<
" " << edge_weight_type << endl;
139 if ( edge_weight_type == EUC_2D )
141 x.resize(num_nodes, 0);
142 y.resize(num_nodes, 0);
145 else if ( key == EDGE_WEIGHT_FORMAT )
148 file >> edge_weight_format;
150 else if ( key == EDGE_WEIGHT_FORMAT +
":" )
152 file >> edge_weight_format;
154 else if ( key == EDGE_WEIGHT_SECTION )
156 if ( edge_weight_type != EXPLICIT || edge_weight_format != LOWER_DIAG_ROW )
158 cerr <<
"Error. Unsupported edge length type." << endl;
161 for (
int i = 0; i < num_nodes; ++i)
163 for (
int j = 0; j < i; ++j)
171 else if ( key == NODE_COORD_SECTION )
173 if ( edge_weight_type != EUC_2D )
175 cerr <<
"Error. Data file contains " << EDGE_WEIGHT_TYPE <<
" " << edge_weight_type <<
" and " << NODE_COORD_SECTION << endl;
178 for (
int i = 0; i < num_nodes; ++i)
186 cerr <<
"Error reading " << NODE_COORD_SECTION << endl;
192 for (
int i = 0; i < num_nodes; ++i)
194 for (
int j = 0; j < i; ++j)
196 int dx = x[i] - x[j];
197 int dy = y[i] - y[j];
198 dist[i][j] = int( sqrt((
double)dx*dx + dy*dy) + 0.5 );
202 else if ( key == DEMAND_SECTION )
204 for (
int i = 0; i < num_nodes; ++i)
211 cerr <<
"Error reading " << DEMAND_SECTION << endl;
217 else if ( key == DEPOT_SECTION )
219 for (
int i = 0; i != -1 ;)
222 if ( i != -1 && i != 1 )
224 cerr <<
"Error: This file specifies other depots than 1." << endl;
231 (void) getline(file, dummy);
245 cout <<
"Solving the vehicle routing problem using SCIP." << endl;
246 cout <<
"Implemented by Andreas Bley." << endl << endl;
248 if ( argc != 2 && argc != 3 )
250 cerr <<
"Usage: vrp [-h] datafile" << endl;
251 cerr <<
"Options:" << endl;
252 cerr <<
" -h Uses hop limit instead of capacity limit for tours."<< endl;
261 static const char* VRP_PRICER_NAME =
"VRP_Pricer";
263 vector<vector<int> > dist;
268 if (
read_problem(argv[argc-1], num_nodes, capacity, demand, dist) )
270 cerr <<
"Error reading data file " << argv[argc-1] << endl;
274 cout <<
"Number of nodes: " << num_nodes << endl;
278 if (
string(
"-h") != argv[1] )
280 cerr <<
"Unknow option " << argv[2] << endl;
284 int total_demand = 0;
285 for (
int i = 1; i< num_nodes; ++i)
286 total_demand += demand[i];
288 if( total_demand == 0.0 )
290 cerr <<
"Total demand is zero!" << endl;
294 capacity = (num_nodes - 1) * capacity / total_demand;
295 demand.assign(num_nodes, 1);
297 cout <<
"Max customers per tour: " << capacity << endl << endl;
300 cout <<
"Max demand per tour: " << capacity << endl << endl;
328 vector< vector<SCIP_VAR*> > arc_var( num_nodes );
329 for (
int i = 0; i < num_nodes; ++i)
332 for (
int j = 0; j < i; ++j)
346 NULL, NULL, NULL, NULL, NULL) );
354 vector< vector<SCIP_CONS*> > arc_con( num_nodes );
355 for (
int i = 0; i < num_nodes; ++i)
358 for (
int j = 0; j < i; ++j)
383 for (
int i = 1; i < num_nodes; ++i)
401 for (
int j = 0; j < num_nodes; ++j)
413 for (
int i = 1; i < num_nodes; ++i)
436 arc_var, arc_con, part_con);
467 for (
int i = 0; i < num_nodes; ++i)
473 for (
int j = 0; j < i; ++j)
488 int main(
int argc,
char** argv)
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
#define BMScheckEmptyMemory()
SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
static SCIP_RETCODE execmain(int argc, char **argv)
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
SCIP_Real SCIPinfinity(SCIP *scip)
int SCIPsnprintf(char *t, int len, const char *s,...)
SCIP_PRICER * SCIPfindPricer(SCIP *scip, const char *name)
enum SCIP_Retcode SCIP_RETCODE
SCIP_RETCODE SCIPincludeObjPricer(SCIP *scip, scip::ObjPricer *objpricer, SCIP_Bool deleteobject)
SCIP_RETCODE SCIPcreate(SCIP **scip)
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPaddCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
C++ wrapper for default SCIP plugins.
SCIP_RETCODE SCIPsolve(SCIP *scip)
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
C++ wrapper classes for SCIP.
SCIP_RETCODE SCIPactivatePricer(SCIP *scip, SCIP_PRICER *pricer)
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
void SCIPprintVersion(SCIP *scip, FILE *file)
int main(int argc, char **argv)
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
SCIP_RETCODE SCIPcreateVar(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype, SCIP_Bool initial, SCIP_Bool removable, SCIP_DECL_VARDELORIG((*vardelorig)), SCIP_DECL_VARTRANS((*vartrans)), SCIP_DECL_VARDELTRANS((*vardeltrans)), SCIP_DECL_VARCOPY((*varcopy)), SCIP_VARDATA *vardata)
SCIP_RETCODE SCIPcreateConsLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
static int read_problem(const char *filename, int &num_nodes, int &capacity, vector< int > &demand, vector< vector< int > > &dist)
SCIP_RETCODE SCIPfree(SCIP **scip)