26 #include "absl/random/random.h"
27 #include "absl/strings/str_cat.h"
28 #include "google/protobuf/text_format.h"
36 #include "ortools/constraint_solver/routing_parameters.pb.h"
49 using operations_research::RoutingNodeIndex;
50 using operations_research::RoutingSearchParameters;
54 ABSL_FLAG(
int, vrp_stops, 25,
"Stop locations in the problem.");
55 ABSL_FLAG(
int, vrp_orders_per_stop, 5,
"Nodes for each stop.");
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_stops))
70 <<
"Specify an instance size greater than 0.";
71 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders_per_stop))
72 <<
"Specify an instance size greater than 0.";
73 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
74 <<
"Specify a non-null vehicle fleet size.";
75 const int vrp_orders =
76 absl::GetFlag(FLAGS_vrp_stops) * absl::GetFlag(FLAGS_vrp_orders_per_stop);
85 const int64_t kXMax = 100000;
86 const int64_t kYMax = 100000;
87 const int64_t kSpeed = 10;
89 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
90 for (
int stop = 0; stop <= absl::GetFlag(FLAGS_vrp_stops); ++stop) {
91 const int num_orders =
92 stop == 0 ? 1 : absl::GetFlag(FLAGS_vrp_orders_per_stop);
98 [&locations, &manager](int64_t i, int64_t j) {
105 const int64_t kVehicleCapacity = 40;
106 const int64_t kNullCapacitySlack = 0;
108 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
111 [&
demand, &manager](int64_t i, int64_t j) {
112 return demand.Demand(manager.IndexToNode(i),
113 manager.IndexToNode(j));
115 kNullCapacitySlack, kVehicleCapacity,
119 const int64_t kStopTime = 300;
120 const int64_t kHorizon = 24 * 3600;
122 kStopTime, locations,
123 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
128 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
130 kHorizon, kHorizon,
false,
kTime);
134 std::mt19937 randomizer(
135 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
136 const int64_t kTWDuration = 5 * 3600;
137 for (
int stop = 0; stop < absl::GetFlag(FLAGS_vrp_stops); ++stop) {
138 const int64_t
start =
139 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
140 for (
int stop_order = 0;
141 stop_order < absl::GetFlag(FLAGS_vrp_orders_per_stop); ++stop_order) {
143 stop * absl::GetFlag(FLAGS_vrp_orders_per_stop) + stop_order + 1;
150 std::vector<IntervalVar*> intervals;
151 for (
int stop = 0; stop < absl::GetFlag(FLAGS_vrp_stops); ++stop) {
152 std::vector<IntervalVar*> stop_intervals;
153 for (
int stop_order = 0;
154 stop_order < absl::GetFlag(FLAGS_vrp_orders_per_stop); ++stop_order) {
156 stop * absl::GetFlag(FLAGS_vrp_orders_per_stop) + stop_order + 1;
158 0, kHorizon, kStopTime,
true, absl::StrCat(
"Order", order));
169 IntVar*
const is_null_duration =
172 [&locations, order](int64_t
index) {
185 std::vector<int64_t> location_usage(stop_intervals.size(), 1);
187 stop_intervals, location_usage, 1, absl::StrCat(
"Client", stop)));
190 for (
int vehicle = 0; vehicle < manager.
num_vehicles(); ++vehicle) {
196 const int64_t kPenalty = 100000;
199 order < routing.
nodes(); ++order) {
200 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
206 CHECK(google::protobuf::TextFormat::MergeFromString(
207 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
209 if (solution !=
nullptr) {
214 LOG(INFO) <<
"Stop intervals:";
221 LOG(INFO) <<
"No solution found.";
An Assignment is a variable -> domains mapping, used to report solutions to the user.
int64_t StartValue(const IntervalVar *const var) const
int64_t PerformedValue(const IntervalVar *const var) const
virtual IntVar * Var()=0
Creates a variable from the expression.
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.
virtual IntExpr * SafeStartExpr(int64_t unperformed_value)=0
These methods create expressions encapsulating the start, end and duration of the interval var.
virtual IntExpr * PerformedExpr()=0
int64_t ManhattanTime(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
int64_t SameLocationFromIndex(int64_t node1, int64_t node2) const
void AddRandomLocation(int64_t x_max, int64_t y_max)
int64_t ManhattanDistance(RoutingIndexManager::NodeIndex from, RoutingIndexManager::NodeIndex to) const
virtual std::string name() const
Object naming.
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
int nodes() const
Sizes and indices Returns the number of nodes in the model.
IntVar * NextVar(int64_t index) const
!defined(SWIGPYTHON)
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)
void AddVariableMaximizedByFinalizer(IntVar *var)
Adds a variable to maximize in the solution finalizer (see above for information on the solution fina...
void AddIntervalToAssignment(IntervalVar *const interval)
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.
IntervalVar * MakeFixedDurationIntervalVar(int64_t start_min, int64_t start_max, int64_t duration, bool optional, const std::string &name)
Creates an interval var with a fixed duration.
void AddConstraint(Constraint *const c)
Adds the constraint 'c' to the model.
Constraint * MakeIsEqualCt(IntExpr *const v1, IntExpr *v2, IntVar *const b)
b == (v1 == v2)
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...
IntExpr * MakeElement(const std::vector< int64_t > &values, IntVar *const index)
values[index]
Constraint * MakeNonEquality(IntExpr *const left, IntExpr *const right)
left != right
int main(int argc, char **argv)
ABSL_FLAG(int, vrp_stops, 25, "Stop locations 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()