Skip to content

Commit

Permalink
[CP-SAT} add support for element with affine expressions in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 18, 2024
1 parent dd03f0c commit b620d7e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
46 changes: 37 additions & 9 deletions ortools/sat/cp_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -927,24 +927,52 @@ Constraint CpModelBuilder::AddAllDifferent(
}

Constraint CpModelBuilder::AddVariableElement(
IntVar index, absl::Span<const IntVar> variables, IntVar target) {
LinearExpr index, absl::Span<const IntVar> variables, LinearExpr target) {
ConstraintProto* const proto = cp_model_.add_constraints();
proto->mutable_element()->set_index(index.index_);
proto->mutable_element()->set_target(target.index_);
*proto->mutable_element()->mutable_linear_index() = LinearExprToProto(index);
*proto->mutable_element()->mutable_linear_target() =
LinearExprToProto(target);
for (const IntVar& var : variables) {
proto->mutable_element()->add_vars(var.index_);
*proto->mutable_element()->add_exprs() = LinearExprToProto(var);
}
return Constraint(proto);
}

Constraint CpModelBuilder::AddElement(IntVar index,
Constraint CpModelBuilder::AddElement(LinearExpr index,
absl::Span<const int64_t> values,
IntVar target) {
LinearExpr target) {
ConstraintProto* const proto = cp_model_.add_constraints();
proto->mutable_element()->set_index(index.index_);
proto->mutable_element()->set_target(target.index_);
*proto->mutable_element()->mutable_linear_index() = LinearExprToProto(index);
*proto->mutable_element()->mutable_linear_target() =
LinearExprToProto(target);
for (int64_t value : values) {
proto->mutable_element()->add_vars(IndexFromConstant(value));
proto->mutable_element()->add_exprs()->set_offset(value);
}
return Constraint(proto);
}

Constraint CpModelBuilder::AddElement(LinearExpr index,
absl::Span<const LinearExpr> expressions,
LinearExpr target) {
ConstraintProto* const proto = cp_model_.add_constraints();
*proto->mutable_element()->mutable_linear_index() = LinearExprToProto(index);
*proto->mutable_element()->mutable_linear_target() =
LinearExprToProto(target);
for (const LinearExpr& expr : expressions) {
*proto->mutable_element()->add_exprs() = LinearExprToProto(expr);
}
return Constraint(proto);
}

Constraint CpModelBuilder::AddElement(
LinearExpr index, std::initializer_list<LinearExpr> expressions,
LinearExpr target) {
ConstraintProto* const proto = cp_model_.add_constraints();
*proto->mutable_element()->mutable_linear_index() = LinearExprToProto(index);
*proto->mutable_element()->mutable_linear_target() =
LinearExprToProto(target);
for (const LinearExpr& expr : expressions) {
*proto->mutable_element()->add_exprs() = LinearExprToProto(expr);
}
return Constraint(proto);
}
Expand Down
18 changes: 14 additions & 4 deletions ortools/sat/cp_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -845,13 +845,23 @@ class CpModelBuilder {
Constraint AddAllDifferent(std::initializer_list<LinearExpr> exprs);

/// Adds the element constraint: variables[index] == target
Constraint AddVariableElement(IntVar index,
Constraint AddVariableElement(LinearExpr index,
absl::Span<const IntVar> variables,
IntVar target);
LinearExpr target);

/// Adds the element constraint: expressions[index] == target.
Constraint AddElement(LinearExpr index,
absl::Span<const LinearExpr> expressions,
LinearExpr target);

/// Adds the element constraint: expressions[index] == target.
Constraint AddElement(LinearExpr index,
std::initializer_list<LinearExpr> expressions,
LinearExpr target);

/// Adds the element constraint: values[index] == target
Constraint AddElement(IntVar index, absl::Span<const int64_t> values,
IntVar target);
Constraint AddElement(LinearExpr index, absl::Span<const int64_t> values,
LinearExpr target);

/**
* Adds a circuit constraint.
Expand Down

0 comments on commit b620d7e

Please sign in to comment.