30 #include "absl/random/random.h"
31 #include "google/protobuf/text_format.h"
39 #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,
"Number of nodes in the problem.");
56 ABSL_FLAG(
int, vrp_vehicles, 20,
"Number of vehicles in the problem.");
57 ABSL_FLAG(
int, vrp_windows, 5,
"Number of disjoint windows per node.");
58 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
59 "Use deterministic random seeds.");
61 "Use same vehicle costs in the routing model");
62 ABSL_FLAG(std::string, routing_search_parameters,
"",
63 "Text proto RoutingSearchParameters (possibly partial) that will "
64 "override the DefaultRoutingSearchParameters()");
71 int main(
int argc,
char** argv) {
73 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
74 <<
"Specify an instance size greater than 0.";
75 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
76 <<
"Specify a non-null vehicle fleet size.";
82 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
86 const int64_t kXMax = 100000;
87 const int64_t kYMax = 100000;
88 const int64_t kSpeed = 10;
90 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
91 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
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 kTimePerDemandUnit = 300;
120 const int64_t kHorizon = 24 * 3600;
123 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
124 return demand.Demand(i, j);
126 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
131 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
133 kHorizon, kHorizon,
false,
kTime);
138 std::mt19937 randomizer(
139 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
140 for (
int order = 1; order < manager.
num_nodes(); ++order) {
141 std::vector<int64_t> forbid_points(2 * absl::GetFlag(FLAGS_vrp_windows), 0);
142 for (
int i = 0; i < forbid_points.size(); ++i) {
143 forbid_points[i] = absl::Uniform<int32_t>(randomizer, 0, kHorizon);
145 std::sort(forbid_points.begin(), forbid_points.end());
146 std::vector<int64_t> forbid_starts(1, 0);
147 std::vector<int64_t> forbid_ends;
148 for (
int i = 0; i < forbid_points.size(); i += 2) {
149 forbid_ends.push_back(forbid_points[i]);
150 forbid_starts.push_back(forbid_points[i + 1]);
152 forbid_ends.push_back(kHorizon);
154 time_dimension.
CumulVar(order), forbid_starts, forbid_ends));
158 const int64_t kPenalty = 10000000;
162 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
167 if (absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs)) {
168 std::vector<int64_t> group;
177 if (!group.empty()) {
184 CHECK(google::protobuf::TextFormat::MergeFromString(
185 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
187 if (solution !=
nullptr) {
189 absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
194 LOG(INFO) <<
"No solution found.";
An Assignment is a variable -> domains mapping, used to report solutions to the user.
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.
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 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.
Constraint * MakeNotMemberCt(IntExpr *const expr, const std::vector< int64_t > &values)
expr not in set.
void AddConstraint(Constraint *const c)
Adds the constraint 'c' to the model.
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()