25 #include "absl/functional/bind_front.h"
26 #include "absl/random/random.h"
27 #include "google/protobuf/text_format.h"
35 #include "ortools/constraint_solver/routing_parameters.pb.h"
38 #include "ortools/util/step_function.h"
48 using operations_research::RoutingNodeIndex;
49 using operations_research::RoutingSearchParameters;
51 using operations_research::StepFunction;
53 ABSL_FLAG(
int, vrp_orders, 25,
"Nodes in the problem.");
55 "Size of Traveling Salesman Problem instance.");
56 ABSL_FLAG(
bool, vrp_use_deterministic_random_seed,
false,
57 "Use deterministic random seeds.");
58 ABSL_FLAG(std::string, routing_search_parameters,
"",
59 "Text proto RoutingSearchParameters (possibly partial) that will "
60 "override the DefaultRoutingSearchParameters()");
62 static const char kTime[] =
"Time";
74 PolyaUrn(
int red_balls,
int blue_balls,
int seed)
75 : red_balls_(red_balls),
76 all_balls_(red_balls + blue_balls),
78 CHECK_LT(0, red_balls_);
79 CHECK_LT(red_balls_, all_balls_);
84 CHECK_LT(0, red_balls_);
85 CHECK_LT(red_balls_, all_balls_);
87 const double return_value =
static_cast<double>(red_balls_) / all_balls_;
88 red_balls_ += (absl::Uniform(generator_, 0, all_balls_) < red_balls_);
91 CHECK_LT(0, return_value);
92 CHECK_LT(return_value, 1);
93 return return_value - 0.5;
99 std::mt19937 generator_;
104 int64_t interval_end,
int seed) {
105 PolyaUrn random_generator(1, 1, seed);
107 for (int64_t step = 0; step < interval_end; step += step_size) {
108 result.AddStepToEnd(step, 2 * mean * random_generator.
Next() - mean);
110 result.AddStepToEnd(interval_end, 0);
118 : distance_evaluator_(distance_evaluator), max_time_(max_time) {}
125 static const int magic_number = 0xfe3498aa;
127 (from.value() ^ magic_number) * (to.value() ^ (~magic_number));
129 const int64_t mean_deviation = sqrt(
distance);
130 const StepFunction deviation =
132 const std::function<int64_t(int64_t)> travel_time =
136 return RoutingModel::MakeStateDependentTransit(travel_time, 0, max_time_);
144 const int64_t max_time_;
147 int main(
int argc,
char** argv) {
149 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_orders))
150 <<
"Specify an instance size greater than 0.";
151 CHECK_LT(0, absl::GetFlag(FLAGS_vrp_vehicles))
152 <<
"Specify a non-null vehicle fleet size.";
159 absl::GetFlag(FLAGS_vrp_vehicles), kDepot);
163 const int64_t kXMax = 1000;
164 const int64_t kYMax = 1000;
165 const int64_t kSpeed = 10;
167 kSpeed, absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
168 for (
int location = 0; location <= absl::GetFlag(FLAGS_vrp_orders);
174 const int64_t kVehicleCapacity = 40;
175 const int64_t kNullCapacitySlack = 0;
177 absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed));
180 [&
demand, &manager](int64_t i, int64_t j) {
181 return demand.Demand(manager.IndexToNode(i),
182 manager.IndexToNode(j));
184 kNullCapacitySlack, kVehicleCapacity,
188 const int64_t kTimePerDemandUnit = 3;
189 const int64_t kHorizon = 24 * 36;
192 [&
demand](RoutingNodeIndex i, RoutingNodeIndex j) {
193 return demand.Demand(i, j);
195 [&locations](RoutingNodeIndex i, RoutingNodeIndex j) {
200 return time.Compute(manager.IndexToNode(i), manager.IndexToNode(j));
202 kHorizon, kHorizon,
true,
kTime);
205 const int64_t max_time = manager.
num_nodes() * (kXMax + kYMax) / kSpeed;
216 std::mt19937 randomizer(
217 GetSeed(absl::GetFlag(FLAGS_vrp_use_deterministic_random_seed)));
219 const int64_t kTWDuration = 5 * 36;
220 for (
int order = 1; order < manager.
num_nodes(); ++order) {
221 const int64_t
start =
222 absl::Uniform<int32_t>(randomizer, 0, kHorizon - kTWDuration);
227 const int64_t kPenalty = 10000000;
229 order < routing.
nodes(); ++order) {
230 std::vector<int64_t> orders(1, manager.
NodeToIndex(order));
236 CHECK(google::protobuf::TextFormat::MergeFromString(
237 absl::GetFlag(FLAGS_routing_search_parameters), &
parameters));
239 if (solution !=
nullptr) {
245 LOG(INFO) <<
"No solution found.";
PolyaUrn(int red_balls, int blue_balls, int seed)
TrafficTransitionEvaluator(const LocationContainer &distance_evaluator, int64_t max_time)
RoutingModel::StateDependentTransit Run(const RoutingIndexManager &manager, int64_t from_index, int64_t to_index)
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 SetSpanCostCoefficientForAllVehicles(int64_t coefficient)
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 AddDimensionDependentDimensionWithVehicleCapacity(const std::vector< int > &pure_transits, const std::vector< int > &dependent_transits, const RoutingDimension *base_dimension, int64_t slack_max, std::vector< int64_t > vehicle_capacities, bool fix_start_cumul_to_zero, const std::string &name)
Creates a dimension with transits depending on the cumuls of another dimension.
int RegisterStateDependentTransitCallback(VariableIndexEvaluator2 callback)
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)
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.
StepFunction RandomStepFunction(int64_t mean, int64_t step_size, int64_t interval_end, int seed)
static const char kTime[]
int main(int argc, char **argv)
ABSL_FLAG(int, vrp_orders, 25, "Nodes in the problem.")
static const char kCapacity[]
static const char kTimeDepedentCost[]
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()