82 vector<vector<int> >& dist
85 static const string DIMENSION =
"DIMENSION";
86 static const string DEMAND_SECTION =
"DEMAND_SECTION";
87 static const string DEPOT_SECTION =
"DEPOT_SECTION";
88 static const string EDGE_WEIGHT_TYPE =
"EDGE_WEIGHT_TYPE";
89 static const string EUC_2D =
"EUC_2D";
90 static const string EXPLICIT =
"EXPLICIT";
91 static const string LOWER_DIAG_ROW =
"LOWER_DIAG_ROW";
92 static const string EDGE_WEIGHT_FORMAT =
"EDGE_WEIGHT_FORMAT";
93 static const string EDGE_WEIGHT_SECTION =
"EDGE_WEIGHT_SECTION";
94 static const string NODE_COORD_SECTION =
"NODE_COORD_SECTION";
95 static const string CAPACITY =
"CAPACITY";
97 ifstream file(filename);
101 cerr <<
"Cannot open file " << filename << endl;
105 string edge_weight_type =
"";
106 string edge_weight_format =
"";
119 if ( key == DIMENSION )
124 demand.resize(num_nodes, 0);
125 dist.resize(num_nodes);
126 for (
int i = 0; i < num_nodes; ++i)
127 dist[i].resize(i, 0);
130 if ( key == CAPACITY )
135 else if ( key == EDGE_WEIGHT_TYPE )
138 file >> edge_weight_type;
139 if ( edge_weight_type != EUC_2D && edge_weight_type != EXPLICIT )
141 cerr <<
"Wrong " << EDGE_WEIGHT_TYPE <<
" " << edge_weight_type << endl;
144 if ( edge_weight_type == EUC_2D )
146 x.resize(num_nodes, 0);
147 y.resize(num_nodes, 0);
150 else if ( key == EDGE_WEIGHT_FORMAT )
153 file >> edge_weight_format;
155 else if ( key == EDGE_WEIGHT_FORMAT +
":" )
157 file >> edge_weight_format;
159 else if ( key == EDGE_WEIGHT_SECTION )
161 if ( edge_weight_type != EXPLICIT || edge_weight_format != LOWER_DIAG_ROW )
163 cerr <<
"Error. Unsupported edge length type." << endl;
166 for (
int i = 0; i < num_nodes; ++i)
168 for (
int j = 0; j < i; ++j)
176 else if ( key == NODE_COORD_SECTION )
178 if ( edge_weight_type != EUC_2D )
180 cerr <<
"Error. Data file contains " << EDGE_WEIGHT_TYPE <<
" " << edge_weight_type <<
" and " << NODE_COORD_SECTION << endl;
183 for (
int i = 0; i < num_nodes; ++i)
191 cerr <<
"Error reading " << NODE_COORD_SECTION << endl;
197 for (
int i = 0; i < num_nodes; ++i)
199 for (
int j = 0; j < i; ++j)
201 int dx = x[i] - x[j];
202 int dy = y[i] - y[j];
203 dist[i][j] = int( sqrt((
double)dx*dx + dy*dy) + 0.5 );
207 else if ( key == DEMAND_SECTION )
209 for (
int i = 0; i < num_nodes; ++i)
216 cerr <<
"Error reading " << DEMAND_SECTION << endl;
222 else if ( key == DEPOT_SECTION )
224 for (
int i = 0; i != -1 ;)
227 if ( i != -1 && i != 1 )
229 cerr <<
"Error: This file specifies other depots than 1." << endl;
236 (void) getline(file, dummy);
250 cout <<
"Solving the vehicle routing problem using SCIP." << endl;
251 cout <<
"Implemented by Andreas Bley." << endl << endl;
253 if ( argc != 2 && argc != 3 )
255 cerr <<
"Usage: vrp [-h] datafile" << endl;
256 cerr <<
"Options:" << endl;
257 cerr <<
" -h Uses hop limit instead of capacity limit for tours."<< endl;
266 static const char* VRP_PRICER_NAME =
"VRP_Pricer";
268 vector<vector<int> > dist;
273 if (
read_problem(argv[argc-1], num_nodes, capacity, demand, dist) )
275 cerr <<
"Error reading data file " << argv[argc-1] << endl;
279 cout <<
"Number of nodes: " << num_nodes << endl;
283 if (
string(
"-h") != argv[1] )
285 cerr <<
"Unknow option " << argv[2] << endl;
289 int total_demand = 0;
290 for (
int i = 1; i< num_nodes; ++i)
291 total_demand += demand[i];
293 if( total_demand == 0.0 )
295 cerr <<
"Total demand is zero!" << endl;
299 capacity = (num_nodes - 1) * capacity / total_demand;
300 demand.assign(num_nodes, 1);
302 cout <<
"Max customers per tour: " << capacity << endl << endl;
305 cout <<
"Max demand per tour: " << capacity << endl << endl;
333 vector< vector<SCIP_VAR*> > arc_var( num_nodes );
334 for (
int i = 0; i < num_nodes; ++i)
337 for (
int j = 0; j < i; ++j)
351 NULL, NULL, NULL, NULL, NULL) );
359 vector< vector<SCIP_CONS*> > arc_con( num_nodes );
360 for (
int i = 0; i < num_nodes; ++i)
363 for (
int j = 0; j < i; ++j)
388 for (
int i = 1; i < num_nodes; ++i)
406 for (
int j = 0; j < num_nodes; ++j)
418 for (
int i = 1; i < num_nodes; ++i)
441 arc_var, arc_con, part_con);
472 for (
int i = 0; i < num_nodes; ++i)
478 for (
int j = 0; j < i; ++j)
493 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)