Skip to content

Commit c0b063d

Browse files
committed
Added support for PenaltyCostBehavior
1 parent 424a0f9 commit c0b063d

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

ext/or-tools/routing.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ namespace Rice::detail {
6565
private:
6666
Arg* arg_ = nullptr;
6767
};
68+
69+
template<>
70+
struct Type<RoutingModel::PenaltyCostBehavior> {
71+
static bool verify() { return true; }
72+
};
73+
74+
template<>
75+
class From_Ruby<RoutingModel::PenaltyCostBehavior> {
76+
public:
77+
From_Ruby() = default;
78+
79+
explicit From_Ruby(Arg* arg) : arg_(arg) { }
80+
81+
double is_convertible(VALUE value) { return Convertible::Exact; }
82+
83+
RoutingModel::PenaltyCostBehavior convert(VALUE x) {
84+
auto s = Symbol(x).str();
85+
if (s == "penalize_once") {
86+
return RoutingModel::PenaltyCostBehavior::PENALIZE_ONCE;
87+
} else if (s == "penalize_per_inactive") {
88+
return RoutingModel::PenaltyCostBehavior::PENALIZE_PER_INACTIVE;
89+
} else {
90+
throw std::runtime_error("Unknown penalty cost behavior: " + s);
91+
}
92+
}
93+
94+
private:
95+
Arg* arg_ = nullptr;
96+
};
6897
} // namespace Rice::detail
6998

7099
void init_routing(Rice::Module& m) {
@@ -297,9 +326,6 @@ void init_routing(Rice::Module& m) {
297326
return operations_research::DefaultRoutingModelParameters();
298327
});
299328

300-
// keep Rice happy
301-
Rice::define_enum_under<RoutingModel::PenaltyCostBehavior>("PenaltyCostBehavior", m);
302-
303329
Rice::define_class_under<RoutingModel::ResourceGroup>(m, "ResourceGroup");
304330

305331
Rice::define_class_under<RoutingModel>(m, "RoutingModel")
@@ -350,7 +376,7 @@ void init_routing(Rice::Module& m) {
350376
.define_method("add_resource_group", &RoutingModel::AddResourceGroup)
351377
.define_method("dimension_resource_group_indices", &RoutingModel::GetDimensionResourceGroupIndices)
352378
.define_method("dimension_resource_group_index", &RoutingModel::GetDimensionResourceGroupIndex)
353-
.define_method("add_disjunction", &RoutingModel::AddDisjunction, Rice::Arg("_indices"), Rice::Arg("_penalty"), Rice::Arg("_max_cardinality") = static_cast<int64_t>(1), Rice::Arg("_penalty_cost_behavior") = RoutingModel::PenaltyCostBehavior::PENALIZE_ONCE)
379+
.define_method("_add_disjunction", &RoutingModel::AddDisjunction)
354380
.define_method("disjunction_indices", &RoutingModel::GetDisjunctionIndices)
355381
.define_method("disjunction_penalty", &RoutingModel::GetDisjunctionPenalty)
356382
.define_method("disjunction_max_cardinality", &RoutingModel::GetDisjunctionMaxCardinality)

lib/or_tools/routing_model.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,9 @@ def solve(
1717
search_parameters.log_search = log_search unless log_search.nil?
1818
solve_with_parameters(search_parameters)
1919
end
20+
21+
def add_disjunction(indices, penalty, max_cardinality = 1, penalty_cost_behavior = :penalize_once)
22+
_add_disjunction(indices, penalty, max_cardinality, penalty_cost_behavior)
23+
end
2024
end
2125
end

0 commit comments

Comments
 (0)