28 #include "absl/random/random.h"
29 #include "google/protobuf/text_format.h"
37 #include "ortools/constraint_solver/routing_parameters.pb.h"
50 using operations_research::RoutingNodeIndex;
51 using operations_research::RoutingSearchParameters;
55 ABSL_FLAG(
int, vrp_orders, 100,
"Nodes in the problem.");
57 "Size of Traveling Salesman Problem instance.");
58 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
59 "Use deterministic random seeds.");
60 ABSL_FLAG(std::string, routing_search_parameters,
"",
61 "Text proto RoutingSearchParameters (possibly partial) that will "
62 "override the DefaultRoutingSearchParameters()");
67 int main(
int argc,
char** argv) {
69 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
70 <<
"Specify an instance size greater than 0.";
71 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
72 <<
"Specify a non-null vehicle fleet size.";
78 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
82 const int64_t kXMax = 100000;
83 const int64_t kYMax = 100000;
84 const int64_t kSpeed = 10;
86 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
87 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
94 [&locations, &manager](int64_t i, int64_t j) {
101 const int64_t kVehicleCapacity = 40;
102 const int64_t kNullCapacitySlack = 0;
104 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
107 [&
demand, &manager](int64_t i, int64_t j) {
108 return demand.Demand(manager.IndexToNode(i),
109 manager.IndexToNode(j));
111 kNullCapacitySlack, kVehicleCapacity,
115 const int64_t kTimePerDemandUnit = 300;
116 const int64_t kHorizon = 24 * 3600;
119 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
120 return demand.Demand(i, j);
122 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
127 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
129 kHorizon, kHorizon,
false,
kTime);
133 std::mt19937 randomizer(
134 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
135 const int64_t kTWDuration = 5 * 3600;
136 for (
int order = 1; order < manager.
num_nodes(); ++order) {
137 const int64_t
start =
138 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
144 std::vector<IntVar*> start_end_times;
145 for (
int i = 0; i < absl::GetFlag(FLAGS_vrp_vehicles); ++i) {
146 start_end_times.push_back(time_dimension.
CumulVar(routing.
End(i)));
147 start_end_times.push_back(time_dimension.
CumulVar(routing.
Start(i)));
150 const int64_t kVehicleSetup = 180;
152 std::vector<IntervalVar*> intervals;
154 "depot_interval", &intervals);
156 const int64_t kDepotCapacity = 5;
157 std::vector<int64_t> depot_usage(start_end_times.size(), 1);
159 solver->
MakeCumulative(intervals, depot_usage, kDepotCapacity,
"depot"));
161 for (
int i = 0; i < start_end_times.size(); ++i) {
166 const int64_t kPenalty = 100000;
170 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
176 CHECK(google::protobuf::TextFormat::MergeFromString(
177 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
179 if (solution !=
nullptr) {
185 LOG(INFO) <<
"No solution found.";
An Assignment is a variable -> domains mapping, used to report solutions to the user.
virtual void SetRange(int64_t l, int64_t u)
This method sets both the min and the max of the expression.
The class IntVar is a subset of IntExpr.
Interval variables are often used in scheduling.
int64_t ManhattanTime(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
void AddRandomLocation(int64_t x_max, int64_t y_max)
int64_t ManhattanDistance(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
Dimensions represent quantities accumulated at nodes along the routes.
IntVar * CumulVar(int64_t index) const
Get the cumul, transit and slack variables for the given node (given as int64_t var index).
Manager for any NodeIndex <-> variable index conversion.
NodeIndex IndexToNode(int64_t index) const
int64_t NodeToIndex(NodeIndex node) const
void AddVariableMinimizedByFinalizer(IntVar *var)
Adds a variable to minimize in the solution finalizer.
Solver * solver() const
Returns the underlying constraint solver.
DisjunctionIndex AddDisjunction(const std::vector< int64_t > &indices, int64_t penalty=kNoPenalty, int64_t max_cardinality=1)
Adds a disjunction constraint on the indices: exactly 'max_cardinality' of the indices are active.
const Assignment * SolveWithParameters(const RoutingSearchParameters &search_parameters, std::vector< const Assignment * > *solutions=nullptr)
Solves the current routing model with the given parameters.
int RegisterTransitCallback(TransitCallback2 callback)
int64_t Start(int vehicle) const
Model inspection.
void SetArcCostEvaluatorOfAllVehicles(int evaluator_index)
Sets the cost function of the model such that the cost of a segment of a route between node 'from' an...
int64_t End(int vehicle) const
Returns the variable index of the ending node of a vehicle route.
bool AddDimension(int evaluator_index, int64_t slack_max, int64_t capacity, bool fix_start_cumul_to_zero, const std::string &name)
Model creation.
const RoutingDimension & GetDimensionOrDie(const std::string &dimension_name) const
Returns a dimension from its name. Dies if the dimension does not exist.
void AddConstraint(Constraint *const c)
Adds the constraint 'c' to the model.
Constraint * MakeCumulative(const std::vector< IntervalVar * > &intervals, const std::vector< int64_t > &demands, int64_t capacity, const std::string &name)
This constraint forces that, for any integer t, the sum of the demands corresponding to an interval c...
void MakeFixedDurationIntervalVarArray(int count, int64_t start_min, int64_t start_max, int64_t duration, bool optional, const std::string &name, std::vector< IntervalVar * > *const array)
This method fills the vector with 'count' interval variables built with the corresponding parameters.
int main(int argc, char **argv)
ABSL_FLAG(int, vrp_orders, 100, "Nodes in the problem.")
void InitGoogle(const char *usage, int *argc, char ***argv, bool deprecated)
void DisplayPlan(const RoutingIndexManager &manager, const RoutingModel &routing, const operations_research::Assignment &plan, bool use_same_vehicle_costs, int64_t max_nodes_per_group, int64_t same_vehicle_cost, const operations_research::RoutingDimension &capacity_dimension, const operations_research::RoutingDimension &time_dimension)
int32_t GetSeed(bool deterministic)
RoutingSearchParameters DefaultRoutingSearchParameters()