29 #include "absl/random/random.h"
30 #include "google/protobuf/text_format.h"
38 #include "ortools/constraint_solver/routing_parameters.pb.h"
39 #include "ortools/graph/graph_builder.h"
50 using operations_research::RoutingNodeIndex;
51 using operations_research::RoutingSearchParameters;
54 ABSL_FLAG(
int, vrp_orders, 100,
"Nodes in the problem.");
56 "Size of Traveling Salesman Problem instance.");
57 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
58 "Use deterministic random seeds.");
60 "Use same vehicle costs in the routing model");
61 ABSL_FLAG(std::string, routing_search_parameters,
"",
62 "Text proto RoutingSearchParameters (possibly partial) that will "
63 "override the DefaultRoutingSearchParameters()");
65 "Number of precedence indices. Precedences will be chosen "
66 "randomly with the constraint that they don't form cycles.");
68 "The offset that applies to the precedences. For each pair linked "
69 "by a precedence constraint, pair.second can only start after the "
70 "start of pair.first + offset.");
77 int main(
int argc,
char** argv) {
79 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
80 <<
"Specify an instance size greater than 0.";
81 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
82 <<
"Specify a non-null vehicle fleet size.";
88 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
92 const int64_t kXMax = 100000;
93 const int64_t kYMax = 100000;
94 const int64_t kSpeed = 10;
96 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
97 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
104 [&locations, &manager](int64_t i, int64_t j) {
111 const int64_t kVehicleCapacity = 40;
112 const int64_t kNullCapacitySlack = 0;
114 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
117 [&
demand, &manager](int64_t i, int64_t j) {
118 return demand.Demand(manager.IndexToNode(i),
119 manager.IndexToNode(j));
121 kNullCapacitySlack, kVehicleCapacity,
125 const int64_t kTimePerDemandUnit = 300;
126 const int64_t kHorizon = 24 * 3600;
129 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
130 return demand.Demand(i, j);
132 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
137 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
139 kHorizon, kHorizon,
true,
kTime);
143 std::mt19937 randomizer(
144 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
145 const int64_t kTWDuration = 5 * 3600;
146 for (
int order = 1; order < manager.
num_nodes(); ++order) {
147 const int64_t
start =
148 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
153 const int64_t kPenalty = 10000000;
157 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
162 if (absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs)) {
163 std::vector<int64_t> group;
172 if (!group.empty()) {
181 if (absl::GetFlag(FLAGS_vrp_precedences) > 0) {
183 std::vector<std::pair<int, int>> precedences;
184 GraphBuilder::RandomEdges(
185 GraphBuilder::DISALLOW_ALL_CYCLES, absl::GetFlag(FLAGS_vrp_orders),
186 absl::GetFlag(FLAGS_vrp_precedences), randomizer, &precedences);
188 LOG(INFO) <<
"Adding precedences: ";
189 for (
const std::pair<int, int>& precedence : precedences) {
190 LOG(INFO) << precedence.first <<
" -> " << precedence.second;
192 {precedence.first, precedence.second,
193 absl::GetFlag(FLAGS_vrp_precedence_offset)});
198 CHECK(google::protobuf::TextFormat::MergeFromString(
199 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
201 if (solution !=
nullptr) {
203 absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
208 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.
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.
void AddNodePrecedence(NodePrecedence precedence)
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 AddSoftSameVehicleConstraint(const std::vector< int64_t > &indices, int64_t cost)
Adds a soft constraint to force a set of variable indices to be on the same vehicle.
RoutingDimension * GetMutableDimension(const std::string &dimension_name) const
Returns a dimension from its name.
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 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...
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.
int main(int argc, char **argv)
const int64_t kMaxNodesPerGroup
ABSL_FLAG(int, vrp_orders, 100, "Nodes in the problem.")
const int64_t kSameVehicleCost
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()