change routing disjunction API to use list of int instead of int64 for nodes

This commit is contained in:
Laurent Perron
2018-12-12 11:02:24 +01:00
parent b5dcf54a27
commit 2861b24ddb
17 changed files with 42 additions and 44 deletions

View File

@@ -148,13 +148,13 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < manager.num_nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}
// Adding same vehicle constraint costs for consecutive nodes.
if (FLAGS_vrp_use_same_vehicle_costs) {
std::vector<int64> group;
std::vector<int> group;
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < manager.num_nodes(); ++order) {
group.push_back(manager.NodeToIndex(order));

View File

@@ -134,13 +134,13 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < manager.num_nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}
// Adding same vehicle constraint costs for consecutive nodes.
if (FLAGS_vrp_use_same_vehicle_costs) {
std::vector<int64> group;
std::vector<int> group;
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < manager.num_nodes(); ++order) {
group.push_back(manager.NodeToIndex(order));

View File

@@ -194,7 +194,7 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < routing.nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}

View File

@@ -156,7 +156,7 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < routing.nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}

View File

@@ -156,7 +156,7 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < manager.num_nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}

View File

@@ -183,7 +183,7 @@ int main(int argc, char** argv) {
const RoutingIndexManager::NodeIndex kFirstNodeAfterDepot(1);
for (RoutingIndexManager::NodeIndex order = kFirstNodeAfterDepot;
order < routing.nodes(); ++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}

View File

@@ -351,7 +351,7 @@ bool LoadAndSolve(const std::string& pdp_file,
const int64 kPenalty = 10000000;
for (RoutingIndexManager::NodeIndex order(1); order < routing.nodes();
++order) {
std::vector<int64> orders(1, manager.NodeToIndex(order));
std::vector<int> orders(1, manager.NodeToIndex(order));
routing.AddDisjunction(orders, kPenalty);
}

View File

@@ -269,7 +269,7 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
for (int order = 0; order < number_of_orders; ++order) {
time_dimension.CumulVar(order).SetRange(order_time_windows_[order].start_,
order_time_windows_[order].end_);
long[] orders = {order};
int[] orders = {order};
model.AddDisjunction(orders, order_penalties_[order]);
}

View File

@@ -221,7 +221,7 @@ public class CapacitatedVehicleRoutingProblemWithTimeWindows {
timeDimension
.cumulVar(order)
.setRange(orderTimeWindows.get(order).first, orderTimeWindows.get(order).second);
long[] orderIndices = {manager.nodeToIndex(order)};
int[] orderIndices = {manager.nodeToIndex(order)};
model.addDisjunction(orderIndices, orderPenalties.get(order));
}

View File

@@ -1353,7 +1353,7 @@ void RoutingModel::ComputeVehicleClasses() {
}
RoutingModel::DisjunctionIndex RoutingModel::AddDisjunction(
const std::vector<int64>& indices, int64 penalty, int64 max_cardinality) {
const std::vector<int>& indices, int64 penalty, int64 max_cardinality) {
CHECK_GE(max_cardinality, 1);
for (int i = 0; i < indices.size(); ++i) {
CHECK_NE(kUnassigned, indices[i]);
@@ -1361,7 +1361,7 @@ RoutingModel::DisjunctionIndex RoutingModel::AddDisjunction(
const DisjunctionIndex disjunction_index(disjunctions_.size());
disjunctions_.push_back({indices, {penalty, max_cardinality}});
for (const int64 index : indices) {
for (const int index : indices) {
index_to_disjunctions_[index].push_back(disjunction_index);
}
return disjunction_index;
@@ -1371,7 +1371,7 @@ std::vector<std::pair<int64, int64>>
RoutingModel::GetPerfectBinaryDisjunctions() const {
std::vector<std::pair<int64, int64>> var_index_pairs;
for (const Disjunction& disjunction : disjunctions_) {
const std::vector<int64>& var_indices = disjunction.indices;
const std::vector<int>& var_indices = disjunction.indices;
if (var_indices.size() != 2) continue;
const int64 v0 = var_indices[0];
const int64 v1 = var_indices[1];
@@ -1402,7 +1402,7 @@ void RoutingModel::IgnoreDisjunctionsAlreadyForcedToZero() {
}
IntVar* RoutingModel::CreateDisjunction(DisjunctionIndex disjunction) {
const std::vector<int64>& indices = disjunctions_[disjunction].indices;
const std::vector<int>& indices = disjunctions_[disjunction].indices;
const int indices_size = indices.size();
std::vector<IntVar*> disjunction_vars(indices_size);
for (int i = 0; i < indices_size; ++i) {
@@ -1427,11 +1427,11 @@ IntVar* RoutingModel::CreateDisjunction(DisjunctionIndex disjunction) {
}
}
void RoutingModel::AddSoftSameVehicleConstraint(
const std::vector<int64>& indices, int64 cost) {
void RoutingModel::AddSoftSameVehicleConstraint(const std::vector<int>& indices,
int64 cost) {
if (!indices.empty()) {
ValuedNodes<int64> same_vehicle_cost;
for (const int64 index : indices) {
for (const int index : indices) {
same_vehicle_cost.indices.push_back(index);
}
same_vehicle_cost.value = cost;
@@ -1448,7 +1448,7 @@ void RoutingModel::SetAllowedVehiclesForIndex(const std::vector<int>& vehicles,
}
}
void RoutingModel::AddPickupAndDelivery(int64 pickup, int64 delivery) {
void RoutingModel::AddPickupAndDelivery(int pickup, int delivery) {
AddPickupAndDeliverySetsInternal({pickup}, {delivery});
pickup_delivery_disjunctions_.push_back({kNoDisjunction, kNoDisjunction});
}
@@ -1463,7 +1463,7 @@ void RoutingModel::AddPickupAndDeliverySets(
}
void RoutingModel::AddPickupAndDeliverySetsInternal(
const std::vector<int64>& pickups, const std::vector<int64>& deliveries) {
const std::vector<int>& pickups, const std::vector<int>& deliveries) {
if (pickups.empty() || deliveries.empty()) {
return;
}
@@ -1509,8 +1509,7 @@ int RoutingModel::GetNumOfSingletonNodes() const {
}
IntVar* RoutingModel::CreateSameVehicleCost(int vehicle_index) {
const std::vector<int64>& indices =
same_vehicle_costs_[vehicle_index].indices;
const std::vector<int>& indices = same_vehicle_costs_[vehicle_index].indices;
CHECK(!indices.empty());
std::vector<IntVar*> vehicle_counts;
solver_->MakeIntVarArray(vehicle_vars_.size() + 1, 0, indices.size() + 1,

View File

@@ -156,7 +156,7 @@
#ifndef OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_H_
#define OR_TOOLS_CONSTRAINT_SOLVER_ROUTING_H_
#include <stddef.h>
#include <cstddef>
#include <functional>
#include <memory>
#include <queue>
@@ -536,7 +536,7 @@ class RoutingModel {
// performed, and therefore p == 0.
// Note: passing a vector with a single index will model an optional index
// with a penalty cost if it is not visited.
DisjunctionIndex AddDisjunction(const std::vector<int64>& indices,
DisjunctionIndex AddDisjunction(const std::vector<int>& indices,
int64 penalty = kNoPenalty,
int64 max_cardinality = 1);
// Returns the indices of the disjunctions to which an index belongs.
@@ -561,8 +561,7 @@ class RoutingModel {
#if !defined(SWIGPYTHON)
// Returns the variable indices of the nodes in the disjunction of index
// 'index'.
const std::vector<int64>& GetDisjunctionIndices(
DisjunctionIndex index) const {
const std::vector<int>& GetDisjunctionIndices(DisjunctionIndex index) const {
return disjunctions_[index].indices;
}
#endif // !defined(SWIGPYTHON)
@@ -592,7 +591,7 @@ class RoutingModel {
// Adds a soft contraint to force a set of variable indices to be on the same
// vehicle. If all nodes are not on the same vehicle, each extra vehicle used
// adds 'cost' to the cost function.
void AddSoftSameVehicleConstraint(const std::vector<int64>& indices,
void AddSoftSameVehicleConstraint(const std::vector<int>& indices,
int64 cost);
// Sets the vehicles which can visit a given node. If the node is in a
@@ -624,7 +623,7 @@ class RoutingModel {
// routing.AddPickupAndDelivery(index1, index2);
//
// TODO(user): Remove this when model introspection detects linked nodes.
void AddPickupAndDelivery(int64 pickup, int64 delivery);
void AddPickupAndDelivery(int pickup, int delivery);
// Same as AddPickupAndDelivery but notifying that the performed node from
// the disjunction of index 'pickup_disjunction' is on the same route as the
// performed node from the disjunction of index 'delivery_disjunction'.
@@ -1148,7 +1147,7 @@ class RoutingModel {
// when unperformed).
template <typename T>
struct ValuedNodes {
std::vector<int64> indices;
std::vector<int> indices;
T value;
};
struct DisjunctionValues {
@@ -1210,8 +1209,8 @@ class RoutingModel {
// Returns nullptr if no penalty cost, otherwise returns penalty variable.
IntVar* CreateDisjunction(DisjunctionIndex disjunction);
// Sets up pickup and delivery sets.
void AddPickupAndDeliverySetsInternal(const std::vector<int64>& pickups,
const std::vector<int64>& deliveries);
void AddPickupAndDeliverySetsInternal(const std::vector<int>& pickups,
const std::vector<int>& deliveries);
// Returns the cost variable related to the soft same vehicle constraint of
// index 'vehicle_index'.
IntVar* CreateSameVehicleCost(int vehicle_index);

View File

@@ -21,7 +21,7 @@ namespace {
// Compute set of disjunctions involved in a pickup and delivery pair.
template <typename Disjunctions>
void AddDisjunctionsFromNodes(const RoutingModel& model,
const std::vector<int64>& nodes,
const std::vector<int>& nodes,
Disjunctions* disjunctions) {
for (int64 node : nodes) {
for (const auto disjunction : model.GetDisjunctionIndices(node)) {

View File

@@ -120,9 +120,9 @@ void RoutingIndexManager::Initialize(
}
}
std::vector<int64> RoutingIndexManager::NodesToIndices(
std::vector<int> RoutingIndexManager::NodesToIndices(
const std::vector<NodeIndex>& nodes) const {
std::vector<int64> indices;
std::vector<int> indices;
indices.reserve(nodes.size());
for (const NodeIndex node : nodes) {
const int64 index = NodeToIndex(node);

View File

@@ -64,7 +64,7 @@ class RoutingIndexManager {
DCHECK_LT(node.value(), node_to_index_.size());
return node_to_index_[node];
}
std::vector<int64> NodesToIndices(const std::vector<NodeIndex>& nodes) const;
std::vector<int> NodesToIndices(const std::vector<NodeIndex>& nodes) const;
NodeIndex IndexToNode(int index) const {
DCHECK_GE(index, 0);
DCHECK_LT(index, index_to_node_.size());

View File

@@ -151,7 +151,7 @@ MakePairInactiveOperator::MakePairInactiveOperator(
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 1,
std::move(start_empty_path_class)) {
int64 max_pair_index = -1;
int max_pair_index = -1;
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
max_pair_index = std::max(max_pair_index, index_pair.second[0]);
@@ -189,7 +189,7 @@ PairRelocateOperator::PairRelocateOperator(
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
int max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
@@ -283,7 +283,7 @@ LightPairRelocateOperator::LightPairRelocateOperator(
const RoutingIndexPairs& index_pairs)
: PathWithPreviousNodesOperator(vars, secondary_vars, 2,
std::move(start_empty_path_class)) {
int64 max_pair_index = -1;
int max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
@@ -332,7 +332,7 @@ PairExchangeOperator::PairExchangeOperator(
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
int max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
@@ -415,7 +415,7 @@ PairExchangeRelocateOperator::PairExchangeRelocateOperator(
index_max = std::max(index_max, var->Max());
}
is_first_.resize(index_max + 1, false);
int64 max_pair_index = -1;
int max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);
@@ -673,7 +673,7 @@ IndexPairSwapActiveOperator::IndexPairSwapActiveOperator(
: PathWithPreviousNodesOperator(vars, secondary_vars, 1,
std::move(start_empty_path_class)),
inactive_node_(0) {
int64 max_pair_index = -1;
int max_pair_index = -1;
// TODO(user): Support pairs with disjunctions.
for (const auto& index_pair : index_pairs) {
max_pair_index = std::max(max_pair_index, index_pair.first[0]);

View File

@@ -182,9 +182,9 @@ class NodeDisjunctionFilter : public IntVarLocalSearchFilter {
i < active_per_disjunction_.size(); ++i) {
active_per_disjunction_[i] = 0;
inactive_per_disjunction_[i] = 0;
const std::vector<int64>& disjunction_indices =
const std::vector<int>& disjunction_indices =
routing_model_.GetDisjunctionIndices(i);
for (const int64 index : disjunction_indices) {
for (const int index : disjunction_indices) {
const bool index_synced = IsVarSynced(index);
if (index_synced) {
if (Value(index) != index) {

View File

@@ -42,7 +42,7 @@ typedef std::function<int64(RoutingNodeIndex, RoutingNodeIndex)>
typedef std::function<int64(int)> RoutingTransitCallback1;
typedef std::function<int64(int, int)> RoutingTransitCallback2;
// NOTE(user): keep the "> >" for SWIG.
typedef std::pair<std::vector<int64>, std::vector<int64> > RoutingIndexPair;
typedef std::pair<std::vector<int>, std::vector<int> > RoutingIndexPair;
typedef std::vector<RoutingIndexPair> RoutingIndexPairs;
} // namespace operations_research