Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for additional cost elements #1849

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ortools/constraint_solver/java/routing.i
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ import java.util.function.LongUnaryOperator;
%rename (compactAssignment) RoutingModel::CompactAssignment;
%rename (computeLowerBound) RoutingModel::ComputeLowerBound;
%rename (costVar) RoutingModel::CostVar;
%rename (addExtraCost) RoutingModel::AddExtraCost;
%rename (costsAreHomogeneousAcrossVehicles) RoutingModel::CostsAreHomogeneousAcrossVehicles;
%rename (debugOutputAssignment) RoutingModel::DebugOutputAssignment;
%rename (end) RoutingModel::End;
Expand Down
3 changes: 3 additions & 0 deletions ortools/constraint_solver/routing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2147,6 +2147,9 @@ void RoutingModel::CloseModelWithParameters(
for (int i = 0; i < same_vehicle_costs_.size(); ++i) {
cost_elements.push_back(CreateSameVehicleCost(i));
}
// Add in any extra cost elements
cost_elements.insert(cost_elements.end(), extra_costs_.begin(), extra_costs_.end());

cost_ = solver_->MakeSum(cost_elements)->Var();
cost_->set_name("Cost");

Expand Down
10 changes: 10 additions & 0 deletions ortools/constraint_solver/routing.h
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,15 @@ class RoutingModel {
}
extra_filters_.push_back(filter);
}
// Adds a custom extra cost to the list of cost elements that make up the
// objective value
void AddExtraCost(IntVar* intVar) {
CHECK(intVar != nullptr);
if (closed_) {
LOG(WARNING) << "Model is closed, cost addition will be ignored.";
}
extra_costs_.push_back(intVar);
}

/// Model inspection.
/// Returns the variable index of the starting node of a vehicle route.
Expand Down Expand Up @@ -1679,6 +1688,7 @@ class RoutingModel {
std::vector<LocalSearchFilter*> filters_;
std::vector<LocalSearchFilter*> feasibility_filters_;
std::vector<LocalSearchFilter*> extra_filters_;
std::vector<IntVar*> extra_costs_;
#ifndef SWIG
std::vector<std::pair<IntVar*, int64>> finalizer_variable_cost_pairs_;
std::vector<std::pair<IntVar*, int64>> finalizer_variable_target_pairs_;
Expand Down