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;
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;
261 cout <<
"Number of nodes: " << num_nodes << endl;
265 if (
string(
"-h") != argv[1] )
267 cerr <<
"Unknow option " << argv[2] << endl;
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;
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;
315 vector< vector<SCIP_VAR*> > arc_var( num_nodes );
316 for (
int i = 0; i < num_nodes; ++i)
319 for (
int j = 0; j < i; ++j)
333 NULL, NULL, NULL, NULL, NULL) );
341 vector< vector<SCIP_CONS*> > arc_con( num_nodes );
342 for (
int i = 0; i < num_nodes; ++i)
345 for (
int j = 0; j < i; ++j)
370 for (
int i = 1; i < num_nodes; ++i)
388 for (
int j = 0; j < num_nodes; ++j)
400 for (
int i = 1; i < num_nodes; ++i)
423 arc_var, arc_con, part_con);
454 for (
int i = 0; i < num_nodes; ++i)
460 for (
int j = 0; j < i; ++j)
475 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)
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)
SCIPInterval sqrt(const SCIPInterval &x)
int read_problem(const char *filename, int &num_nodes, int &capacity, vector< int > &demand, vector< vector< int > > &dist)
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 execmain(int argc, char **argv)
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)
SCIP_RETCODE SCIPfree(SCIP **scip)