26 #include "absl/random/random.h"
27 #include "google/protobuf/text_format.h"
35 #include "ortools/constraint_solver/routing_parameters.pb.h"
46 using operations_research::RoutingNodeIndex;
47 using operations_research::RoutingSearchParameters;
50 ABSL_FLAG(
int, vrp_orders, 100,
"Nodes in the problem.");
52 "Size of Traveling Salesman Problem instance.");
53 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
54 "Use deterministic random seeds.");
55 ABSL_FLAG(std::string, routing_search_parameters,
"",
56 "Text proto RoutingSearchParameters (possibly partial) that will "
57 "override the DefaultRoutingSearchParameters()");
65 const int64_t kRefuelNodeRatio = 10;
66 return (node % kRefuelNodeRatio == 0);
69 int main(
int argc,
char** argv) {
71 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
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.";
80 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
84 const int64_t kXMax = 100000;
85 const int64_t kYMax = 100000;
86 const int64_t kSpeed = 10;
88 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
89 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
96 [&locations, &manager](int64_t i, int64_t j) {
103 const int64_t kVehicleCapacity = 40;
104 const int64_t kNullCapacitySlack = 0;
106 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
109 [&
demand, &manager](int64_t i, int64_t j) {
110 return demand.Demand(manager.IndexToNode(i),
111 manager.IndexToNode(j));
113 kNullCapacitySlack, kVehicleCapacity,
117 const int64_t kTimePerDemandUnit = 300;
118 const int64_t kHorizon = 24 * 3600;
121 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
122 return demand.Demand(i, j);
124 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
129 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
131 kHorizon, kHorizon,
true,
kTime);
138 std::mt19937 randomizer(
139 144 +
GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
140 const int64_t kTWDuration = 5 * 3600;
141 for (
int order = 1; order < manager.
num_nodes(); ++order) {
143 const int64_t
start =
144 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
152 const int64_t kFuelCapacity = kXMax + kYMax;
155 [&locations, &manager](int64_t i, int64_t j) {
156 return locations.NegManhattanDistance(manager.IndexToNode(i),
157 manager.IndexToNode(j));
159 kFuelCapacity, kFuelCapacity,
false,
kFuel);
161 for (
int order = 0; order < routing.
Size(); ++order) {
171 const int64_t kPenalty = 100000;
174 order < routing.
nodes(); ++order) {
175 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
181 CHECK(google::protobuf::TextFormat::MergeFromString(
182 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
184 if (solution !=
nullptr) {
190 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.
virtual void SetValue(int64_t v)
This method sets the value 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 * SlackVar(int64_t index) const
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.
bool IsStart(int64_t index) const
Returns true if 'index' represents the first node of a route.
void AddVariableMinimizedByFinalizer(IntVar *var)
Adds a variable to minimize in the solution finalizer.
int64_t Size() const
Returns the number of next variables in the model.
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)
ABSL_FLAG(int, vrp_orders, 100, "Nodes in the problem.")
bool IsRefuelNode(int64_t node)
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()