28 #include "absl/random/random.h"
29 #include "google/protobuf/text_format.h"
37 #include "ortools/constraint_solver/routing_parameters.pb.h"
48 using operations_research::RoutingNodeIndex;
49 using operations_research::RoutingSearchParameters;
52 ABSL_FLAG(
int, vrp_orders, 100,
"Number of nodes in the problem");
53 ABSL_FLAG(
int, vrp_vehicles, 20,
"Number of vehicles in the problem");
54 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
55 "Use deterministic random seeds");
57 "Use same vehicle costs in the routing model");
58 ABSL_FLAG(std::string, routing_search_parameters,
"",
59 "Text proto RoutingSearchParameters (possibly partial) that will "
60 "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,
true,
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);
143 const int64_t kPenalty = 10000000;
147 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
152 if (absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs)) {
153 std::vector<int64_t> group;
162 if (!group.empty()) {
169 CHECK(google::protobuf::TextFormat::MergeFromString(
170 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
172 if (solution !=
nullptr) {
174 absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
179 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.
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.
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
const int64_t kSameVehicleCost
ABSL_FLAG(int, vrp_orders, 100, "Number of 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()