From 46edfcb35bde1afb528c22324796e1e4e7db2b6d Mon Sep 17 00:00:00 2001 From: Jeremy Saklad Date: Tue, 28 Sep 2021 16:34:50 -0500 Subject: [PATCH] Override CpModel.AddDivisionEquality The overridden method supports enforcement literals using indirection. --- bonemarketsolver/objects/bone_market_model.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bonemarketsolver/objects/bone_market_model.py b/bonemarketsolver/objects/bone_market_model.py index 1b7f524..ff62701 100644 --- a/bonemarketsolver/objects/bone_market_model.py +++ b/bonemarketsolver/objects/bone_market_model.py @@ -22,6 +22,17 @@ Set `upto` to a value that is unlikely to come into play. Each parameter is interpreted as a BoundedLinearExpression, and a layer of indirection is applied such that each Constraint in the returned tuple can accept an enforcement literal.""" return self.AddAllowedAssignments((target, var), ((int(base**exp), base) for base in range(upto + 1))) + def AddDivisionEquality(self, target, num, denom): + """Adds `target == num // denom` (integer division rounded towards 0). + +Each parameter is interpreted as a BoundedLinearExpression, and a layer of indirection is applied such that each Constraint in the returned tuple can accept an enforcement literal.""" + intermediate_target, target_constraint = self.NewIntermediateIntVar(target, f'{repr(target)} == {repr(num)} // {repr(denom)}: target') + intermediate_num, num_constraint = self.NewIntermediateIntVar(num, f'{repr(target)} == {repr(num)} // {repr(denom)}: num', lb = 0) + intermediate_denom, denom_constraint = self.NewIntermediateIntVar(denom, f'{repr(target)} == {repr(num)} // {repr(denom)}: denom', lb = 0) + + super().AddDivisionEquality(intermediate_target, intermediate_num, intermediate_denom) + return (target_constraint, num_constraint, denom_constraint) + def AddIf(self, variable, *constraints): """Add constraints to the model, only enforced if the specified variable is true.