Skip to content

Commit

Permalink
Merge pull request #99 from QuMuLab/97-big-andor
Browse files Browse the repository at this point in the history
[addendum] Allowing for iterable arguments on the And/Or features.
  • Loading branch information
karishmadaga authored Jun 4, 2021
2 parents 3776811 + 4e51387 commit a34f6d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions bauhaus/core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import weakref
from collections.abc import Iterable
# add try import
import nnf
from functools import wraps
Expand Down Expand Up @@ -269,11 +270,20 @@ def compile(self):
return self.args[0].compile().negate() | self.args[1].compile()


def _flatten_and_build_andor(args, andor):
all_args = []
for arg in args:
if isinstance(arg, Iterable):
all_args.extend(list(arg))
else:
all_args.append(arg)
return CustomNNF(andor, all_args)

def And(*args):
return CustomNNF('and', args)
return _flatten_and_build_andor(args, 'and')

def Or(*args):
return CustomNNF('or', args)
return _flatten_and_build_andor(args, 'or')


def proposition(encoding: Encoding):
Expand Down
2 changes: 2 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def test_andor():
c1, c2, d1 = C(), C(), D()
c.add_constraint(And(~c1, (c2 & d1)))
c.add_constraint(Or(c1 >> c2, d1))
c.add_constraint(And(Or([c1, c2]), d1))
c.add_constraint(Or(And({c1, c2}), d1))
T = c.compile()
assert T.satisfiable()

Expand Down

0 comments on commit a34f6d2

Please sign in to comment.