27 #include "absl/random/random.h"
28 #include "google/protobuf/text_format.h"
36 #include "ortools/constraint_solver/routing_parameters.pb.h"
47 using operations_research::RoutingNodeIndex;
48 using operations_research::RoutingSearchParameters;
51 ABSL_FLAG(
int, vrp_orders, 100,
"Number of nodes in the problem.");
52 ABSL_FLAG(
int, vrp_vehicles, 20,
"Number of vehicles in the problem.");
54 "Hard capacity for a vehicle; set to 0 to disable the hard capacity "
57 "Soft capacity for a vehicle; set to 0 to disable the soft capacity "
59 ABSL_FLAG(
int, vrp_vehicle_soft_capacity_cost, 5000,
60 "Cost of using a vehicle beyond its soft capacity (per unit "
61 "of storage over the soft capacity)");
62 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
63 "Use deterministic random seeds.");
65 "Use same vehicle costs in the routing model");
66 ABSL_FLAG(std::string, routing_search_parameters,
"",
67 "Text proto RoutingSearchParameters (possibly partial) that will "
68 "override the DefaultRoutingSearchParameters()");
75 int main(
int argc,
char** argv) {
77 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
78 <<
"Specify an instance size greater than 0.";
79 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
80 <<
"Specify a non-null vehicle fleet size.";
81 if (absl::GetFlag(FLAGS_vrp_vehicle_hard_capacity) > 0 &&
82 absl::GetFlag(FLAGS_vrp_vehicle_soft_capacity) > 0) {
83 CHECK_LT(absl::GetFlag(FLAGS_vrp_vehicle_soft_capacity),
84 absl::GetFlag(FLAGS_vrp_vehicle_hard_capacity))
85 <<
"The hard capacity must be higher than the soft capacity.";
93 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
97 const int64_t kXMax = 100'000;
98 const int64_t kYMax = 100'000;
99 const int64_t kSpeed = 10;
101 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
102 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
109 [&locations, &manager](int64_t i, int64_t j) {
116 const int64_t kNullCapacitySlack = 0;
118 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
123 return demand.Demand(manager.IndexToNode(i), manager.IndexToNode(j));
125 kNullCapacitySlack, absl::GetFlag(FLAGS_vrp_vehicle_hard_capacity),
131 const int num_vehicles = absl::GetFlag(FLAGS_vrp_vehicles);
132 for (
int vehicle = 0; vehicle < num_vehicles; ++vehicle) {
134 routing.
End(vehicle), absl::GetFlag(FLAGS_vrp_vehicle_soft_capacity),
135 absl::GetFlag(FLAGS_vrp_vehicle_soft_capacity_cost));
139 const int64_t kTimePerDemandUnit = 300;
140 const int64_t kHorizon = 24 * 3600;
143 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
144 return demand.Demand(i, j);
146 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
151 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
153 kHorizon, kHorizon,
true,
kTime);
157 std::mt19937 randomizer(
158 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
159 const int64_t kTWDuration = 5 * 3600;
160 for (
int order = 1; order < manager.
num_nodes(); ++order) {
161 const int64_t
start =
162 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
167 const int64_t kPenalty = 10'000'000;
171 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
176 if (absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs)) {
177 std::vector<int64_t> group;
186 if (!group.empty()) {
193 CHECK(google::protobuf::TextFormat::MergeFromString(
194 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
196 if (solution !=
nullptr) {
198 absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
203 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 SetCumulVarSoftUpperBound(int64_t index, int64_t upper_bound, int64_t coefficient)
Sets a soft upper bound to the cumul variable of a given variable index.
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...
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.
int main(int argc, char **argv)
ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem.")
const int64_t kMaxNodesPerGroup
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()