Skip to content

Commit

Permalink
routing: Fix parsers export
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Sep 30, 2024
1 parent e34c9ee commit 7ab4c1f
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 184 deletions.
57 changes: 33 additions & 24 deletions ortools/routing/parsers/cvrptw_lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/container/btree_set.h"
#include "absl/random/distributions.h"
#include "absl/random/random.h"
#include "absl/strings/str_format.h"
#include "ortools/base/logging.h"
#include "ortools/constraint_solver/constraint_solver.h"
Expand Down Expand Up @@ -158,12 +160,17 @@ int64_t StopServiceTimePlusTransition::Compute(NodeIndex from,
: stop_time_ + transition_time_(from, to);
}

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) {
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 std::vector<std::string>& dimension_names) {
std::vector<const RoutingDimension*> dimensions;
for (const std::string& dimension_name : dimension_names) {
dimensions.push_back(&routing.GetDimensionOrDie(dimension_name));
}

// Display plan cost.
std::string plan_output = absl::StrFormat("Cost %d\n", plan.ObjectiveValue());

Expand Down Expand Up @@ -208,6 +215,18 @@ void DisplayPlan(
}

// Display actual output for each vehicle.
const auto str_append_variable =
[&plan, &plan_output](const IntVar* var, const std::string& name) {
if (var == nullptr || !plan.Contains(var)) return;
const int64_t var_min = plan.Min(var);
const int64_t var_max = plan.Max(var);
if (var_min == var_max) {
absl::StrAppendFormat(&plan_output, "%s(%d) ", name, var_min);
} else {
absl::StrAppendFormat(&plan_output, "%s(%d, %d) ", name, var_min,
var_max);
}
};
for (int route_number = 0; route_number < routing.vehicles();
++route_number) {
int64_t order = routing.Start(route_number);
Expand All @@ -216,26 +235,16 @@ void DisplayPlan(
plan_output += "Empty\n";
} else {
while (true) {
operations_research::IntVar* const load_var =
capacity_dimension.CumulVar(order);
operations_research::IntVar* const time_var =
time_dimension.CumulVar(order);
operations_research::IntVar* const slack_var =
routing.IsEnd(order) ? nullptr : time_dimension.SlackVar(order);
if (slack_var != nullptr && plan.Contains(slack_var)) {
absl::StrAppendFormat(
&plan_output, "%d Load(%d) Time(%d, %d) Slack(%d, %d)",
manager.IndexToNode(order).value(), plan.Value(load_var),
plan.Min(time_var), plan.Max(time_var), plan.Min(slack_var),
plan.Max(slack_var));
} else {
absl::StrAppendFormat(&plan_output, "%d Load(%d) Time(%d, %d)",
manager.IndexToNode(order).value(),
plan.Value(load_var), plan.Min(time_var),
plan.Max(time_var));
absl::StrAppendFormat(&plan_output, "%d ",
manager.IndexToNode(order).value());
for (const RoutingDimension* dimension : dimensions) {
str_append_variable(dimension->CumulVar(order), dimension->name());
operations_research::IntVar* const slack_var =
routing.IsEnd(order) ? nullptr : dimension->SlackVar(order);
str_append_variable(slack_var, dimension->name() + "Slack");
}
if (routing.IsEnd(order)) break;
plan_output += " -> ";
plan_output += "-> ";
order = plan.Value(routing.NextVar(order));
}
plan_output += "\n";
Expand Down
34 changes: 16 additions & 18 deletions ortools/routing/parsers/cvrptw_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,44 +93,42 @@ class RandomDemand {
// Service time (proportional to demand) + transition time callback.
class ServiceTimePlusTransition {
public:
ServiceTimePlusTransition(
int64_t time_per_demand_unit,
operations_research::RoutingNodeEvaluator2 demand,
operations_research::RoutingNodeEvaluator2 transition_time);
ServiceTimePlusTransition(int64_t time_per_demand_unit,
RoutingNodeEvaluator2 demand,
RoutingNodeEvaluator2 transition_time);
int64_t Compute(RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) const;

private:
const int64_t time_per_demand_unit_;
operations_research::RoutingNodeEvaluator2 demand_;
operations_research::RoutingNodeEvaluator2 transition_time_;
RoutingNodeEvaluator2 demand_;
RoutingNodeEvaluator2 transition_time_;
};

// Stop service time + transition time callback.
class StopServiceTimePlusTransition {
public:
StopServiceTimePlusTransition(
int64_t stop_time, const LocationContainer& location_container,
operations_research::RoutingNodeEvaluator2 transition_time);
StopServiceTimePlusTransition(int64_t stop_time,
const LocationContainer& location_container,
RoutingNodeEvaluator2 transition_time);
int64_t Compute(RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) const;

private:
const int64_t stop_time_;
const LocationContainer& location_container_;
operations_research::RoutingNodeEvaluator2 demand_;
operations_research::RoutingNodeEvaluator2 transition_time_;
RoutingNodeEvaluator2 demand_;
RoutingNodeEvaluator2 transition_time_;
};

// Route plan displayer.
// TODO(user): Move the display code to the routing library.
void DisplayPlan(
const operations_research::RoutingIndexManager& manager,
const operations_research::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);
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 std::vector<std::string>& dimension_names);

} // namespace operations_research::routing

Expand Down
31 changes: 16 additions & 15 deletions ortools/routing/samples/cvrp_disjoint_tw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,37 @@

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/random/random.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/routing/index_manager.h"
#include "ortools/routing/parameters.h"
#include "ortools/routing/parameters.pb.h"
#include "ortools/routing/parsers/cvrptw_lib.h"
#include "ortools/routing/routing.h"
#include "ortools/routing/types.h"

using operations_research::Assignment;
using operations_research::DefaultRoutingSearchParameters;
using operations_research::GetSeed;
using operations_research::LocationContainer;
using operations_research::RandomDemand;
using operations_research::RoutingDimension;
using operations_research::RoutingIndexManager;
using operations_research::RoutingModel;
using operations_research::RoutingNodeIndex;
using operations_research::RoutingSearchParameters;
using operations_research::ServiceTimePlusTransition;
using operations_research::Solver;
using operations_research::routing::DefaultRoutingSearchParameters;
using operations_research::routing::GetSeed;
using operations_research::routing::LocationContainer;
using operations_research::routing::RandomDemand;
using operations_research::routing::RoutingDimension;
using operations_research::routing::RoutingIndexManager;
using operations_research::routing::RoutingModel;
using operations_research::routing::RoutingNodeIndex;
using operations_research::routing::RoutingSearchParameters;
using operations_research::routing::ServiceTimePlusTransition;

ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem.");
ABSL_FLAG(int, vrp_vehicles, 20, "Number of vehicles in the problem.");
Expand Down Expand Up @@ -189,9 +192,7 @@ int main(int argc, char** argv) {
if (solution != nullptr) {
DisplayPlan(manager, routing, *solution,
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
kMaxNodesPerGroup, kSameVehicleCost,
routing.GetDimensionOrDie(kCapacity),
routing.GetDimensionOrDie(kTime));
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
} else {
LOG(INFO) << "No solution found.";
}
Expand Down
31 changes: 16 additions & 15 deletions ortools/routing/samples/cvrptw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,36 @@
// to be in meters and times in seconds.

#include <cstdint>
#include <cstdlib>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/random/random.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/routing/index_manager.h"
#include "ortools/routing/parameters.h"
#include "ortools/routing/parameters.pb.h"
#include "ortools/routing/parsers/cvrptw_lib.h"
#include "ortools/routing/routing.h"
#include "ortools/routing/types.h"

using operations_research::Assignment;
using operations_research::DefaultRoutingSearchParameters;
using operations_research::GetSeed;
using operations_research::LocationContainer;
using operations_research::RandomDemand;
using operations_research::RoutingDimension;
using operations_research::RoutingIndexManager;
using operations_research::RoutingModel;
using operations_research::RoutingNodeIndex;
using operations_research::RoutingSearchParameters;
using operations_research::ServiceTimePlusTransition;
using operations_research::routing::DefaultRoutingSearchParameters;
using operations_research::routing::GetSeed;
using operations_research::routing::LocationContainer;
using operations_research::routing::RandomDemand;
using operations_research::routing::RoutingDimension;
using operations_research::routing::RoutingIndexManager;
using operations_research::routing::RoutingModel;
using operations_research::routing::RoutingNodeIndex;
using operations_research::routing::RoutingSearchParameters;
using operations_research::routing::ServiceTimePlusTransition;

ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem");
ABSL_FLAG(int, vrp_vehicles, 20, "Number of vehicles in the problem");
Expand Down Expand Up @@ -174,9 +177,7 @@ int main(int argc, char** argv) {
if (solution != nullptr) {
DisplayPlan(manager, routing, *solution,
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
kMaxNodesPerGroup, kSameVehicleCost,
routing.GetDimensionOrDie(kCapacity),
routing.GetDimensionOrDie(kTime));
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
} else {
LOG(INFO) << "No solution found.";
}
Expand Down
31 changes: 16 additions & 15 deletions ortools/routing/samples/cvrptw_soft_capacity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,36 @@
// distance. Distances are assumed to be in meters and times in seconds.

#include <cstdint>
#include <cstdlib>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/random/random.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/routing/index_manager.h"
#include "ortools/routing/parameters.h"
#include "ortools/routing/parameters.pb.h"
#include "ortools/routing/parsers/cvrptw_lib.h"
#include "ortools/routing/routing.h"
#include "ortools/routing/types.h"

using operations_research::Assignment;
using operations_research::DefaultRoutingSearchParameters;
using operations_research::GetSeed;
using operations_research::LocationContainer;
using operations_research::RandomDemand;
using operations_research::RoutingDimension;
using operations_research::RoutingIndexManager;
using operations_research::RoutingModel;
using operations_research::RoutingNodeIndex;
using operations_research::RoutingSearchParameters;
using operations_research::ServiceTimePlusTransition;
using operations_research::routing::DefaultRoutingSearchParameters;
using operations_research::routing::GetSeed;
using operations_research::routing::LocationContainer;
using operations_research::routing::RandomDemand;
using operations_research::routing::RoutingDimension;
using operations_research::routing::RoutingIndexManager;
using operations_research::routing::RoutingModel;
using operations_research::routing::RoutingNodeIndex;
using operations_research::routing::RoutingSearchParameters;
using operations_research::routing::ServiceTimePlusTransition;

ABSL_FLAG(int, vrp_orders, 100, "Number of nodes in the problem.");
ABSL_FLAG(int, vrp_vehicles, 20, "Number of vehicles in the problem.");
Expand Down Expand Up @@ -198,9 +201,7 @@ int main(int argc, char** argv) {
if (solution != nullptr) {
DisplayPlan(manager, routing, *solution,
absl::GetFlag(FLAGS_vrp_use_same_vehicle_costs),
kMaxNodesPerGroup, kSameVehicleCost,
routing.GetDimensionOrDie(kCapacity),
routing.GetDimensionOrDie(kTime));
kMaxNodesPerGroup, kSameVehicleCost, {kCapacity, kTime});
} else {
LOG(INFO) << "No solution found.";
}
Expand Down
33 changes: 17 additions & 16 deletions ortools/routing/samples/cvrptw_with_breaks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,41 @@
// day or two smaller ones which can be taken during a longer period of the day.

#include <cstdint>
#include <cstdlib>
#include <random>
#include <string>
#include <utility>
#include <vector>

#include "absl/flags/flag.h"
#include "absl/random/random.h"
#include "absl/strings/str_cat.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/commandlineflags.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/base/types.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/routing/enums.pb.h"
#include "ortools/routing/index_manager.h"
#include "ortools/routing/parameters.h"
#include "ortools/routing/parameters.pb.h"
#include "ortools/routing/parsers/cvrptw_lib.h"
#include "ortools/routing/routing.h"
#include "ortools/routing/types.h"

using operations_research::Assignment;
using operations_research::DefaultRoutingSearchParameters;
using operations_research::FirstSolutionStrategy;
using operations_research::GetSeed;
using operations_research::IntervalVar;
using operations_research::LocationContainer;
using operations_research::RandomDemand;
using operations_research::RoutingDimension;
using operations_research::RoutingIndexManager;
using operations_research::RoutingModel;
using operations_research::RoutingNodeIndex;
using operations_research::RoutingSearchParameters;
using operations_research::ServiceTimePlusTransition;
using operations_research::Solver;
using operations_research::routing::DefaultRoutingSearchParameters;
using operations_research::routing::FirstSolutionStrategy;
using operations_research::routing::GetSeed;
using operations_research::routing::LocationContainer;
using operations_research::routing::RandomDemand;
using operations_research::routing::RoutingDimension;
using operations_research::routing::RoutingIndexManager;
using operations_research::routing::RoutingModel;
using operations_research::routing::RoutingNodeIndex;
using operations_research::routing::RoutingSearchParameters;
using operations_research::routing::ServiceTimePlusTransition;

ABSL_FLAG(int, vrp_orders, 100, "Nodes in the problem.");
ABSL_FLAG(int, vrp_vehicles, 20,
Expand Down Expand Up @@ -227,9 +230,7 @@ int main(int argc, char** argv) {
LOG(INFO) << break_interval.Var()->name() << " unperformed";
}
}
DisplayPlan(manager, routing, *solution, false, 0, 0,
routing.GetDimensionOrDie(kCapacity),
routing.GetDimensionOrDie(kTime));
DisplayPlan(manager, routing, *solution, false, 0, 0, {kCapacity, kTime});
} else {
LOG(INFO) << "No solution found.";
}
Expand Down
Loading

0 comments on commit 7ab4c1f

Please sign in to comment.