update examples

This commit is contained in:
Corentin Le Molgat
2021-04-02 14:58:16 +02:00
parent 7a4f8e242c
commit baf02eca7e
33 changed files with 381 additions and 380 deletions

View File

@@ -66,7 +66,7 @@ int32 GetSeed() {
// Cost/distance functions.
// Sample function.
int64 MyDistance(RoutingIndexManager::NodeIndex from,
int64_t MyDistance(RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) {
// Put your distance code here.
return (from + to).value(); // for instance
@@ -77,8 +77,8 @@ class RandomMatrix {
public:
explicit RandomMatrix(int size) : size_(size) {}
void Initialize() {
matrix_ = absl::make_unique<int64[]>(size_ * size_);
const int64 kDistanceMax = 100;
matrix_ = absl::make_unique<int64_t[]>(size_ * size_);
const int64_t kDistanceMax = 100;
ACMRandom randomizer(GetSeed());
for (RoutingIndexManager::NodeIndex from(0); from < size_; ++from) {
for (RoutingIndexManager::NodeIndex to(0); to < size_; ++to) {
@@ -90,17 +90,17 @@ class RandomMatrix {
}
}
}
int64 Distance(RoutingIndexManager::NodeIndex from,
int64_t Distance(RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) const {
return matrix_[MatrixIndex(from, to)];
}
private:
int64 MatrixIndex(RoutingIndexManager::NodeIndex from,
int64_t MatrixIndex(RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) const {
return (from * size_ + to).value();
}
std::unique_ptr<int64[]> matrix_;
std::unique_ptr<int64_t[]> matrix_;
const int size_;
};
@@ -120,31 +120,31 @@ void Tsp() {
// Setting the cost function.
// Put a permanent callback to the distance accessor here. The callback
// has the following signature: ResultCallback2<int64, int64, int64>.
// has the following signature: ResultCallback2<int64_t, int64_t, int64_t>.
// The two arguments are the from and to node inidices.
RandomMatrix matrix(absl::GetFlag(FLAGS_tsp_size));
if (absl::GetFlag(FLAGS_tsp_use_random_matrix)) {
matrix.Initialize();
const int vehicle_cost = routing.RegisterTransitCallback(
[&matrix, &manager](int64 i, int64 j) {
[&matrix, &manager](int64_t i, int64_t j) {
return matrix.Distance(manager.IndexToNode(i),
manager.IndexToNode(j));
});
routing.SetArcCostEvaluatorOfAllVehicles(vehicle_cost);
} else {
const int vehicle_cost =
routing.RegisterTransitCallback([&manager](int64 i, int64 j) {
routing.RegisterTransitCallback([&manager](int64_t i, int64_t j) {
return MyDistance(manager.IndexToNode(i), manager.IndexToNode(j));
});
routing.SetArcCostEvaluatorOfAllVehicles(vehicle_cost);
}
// Forbid node connections (randomly).
ACMRandom randomizer(GetSeed());
int64 forbidden_connections = 0;
int64_t forbidden_connections = 0;
while (forbidden_connections <
absl::GetFlag(FLAGS_tsp_random_forbidden_connections)) {
const int64 from = randomizer.Uniform(absl::GetFlag(FLAGS_tsp_size) - 1);
const int64 to =
const int64_t from = randomizer.Uniform(absl::GetFlag(FLAGS_tsp_size) - 1);
const int64_t to =
randomizer.Uniform(absl::GetFlag(FLAGS_tsp_size) - 1) + 1;
if (routing.NextVar(from)->Contains(to)) {
LOG(INFO) << "Forbidding connection " << from << " -> " << to;
@@ -161,12 +161,12 @@ void Tsp() {
// Only one route here; otherwise iterate from 0 to routing.vehicles() - 1
const int route_number = 0;
std::string route;
for (int64 node = routing.Start(route_number); !routing.IsEnd(node);
for (int64_t node = routing.Start(route_number); !routing.IsEnd(node);
node = solution->Value(routing.NextVar(node))) {
absl::StrAppend(&route, manager.IndexToNode(node).value(), " (", node,
") -> ");
}
const int64 end = routing.End(route_number);
const int64_t end = routing.End(route_number);
absl::StrAppend(&route, manager.IndexToNode(end).value(), " (", end, ")");
LOG(INFO) << route;
} else {