ortools: support reification of xor, by rewriting to sum
@@ -448,10 +448,10 @@ class CPM_ortools(SolverInterface):
# reified case: var -> boolexpr, boolexpr -> var
if isinstance(cpm_expr.args[0], _BoolVarImpl):
# var -> boolexpr, natively supported by or-tools
- # But not voor xor!!
- if isinstance(cpm_expr.args[1], Operator) and cpm_expr.args[1].name == 'xor':
- raise NotImplementedError("Reified XOR not allowed by ORTools",cpm_expr,"and CPMpy does not yet work around it")
bvar = self.ort_var(cpm_expr.args[0])
+ # Special case for 'xor', which is not reifiable in ortools
+ if isinstance(cpm_expr.args[1], Operator) and cpm_expr.args[1].name == 'xor':
+ return self.post_constraint((sum(cpm_expr.args[1].args) == 1), reifiable=True).OnlyEnforceIf(bvar)
return self.post_constraint(cpm_expr.args[1], reifiable=True).OnlyEnforceIf(bvar)
else:
# boolexpr -> var, have to convert to ~var -> ~boolexpr
