def _PopCountBVSum(vars, tree_addition=False, retBitVector=False): from monosat.bvtheory import BitVector bvwidth = math.ceil(math.log(len(vars), 2)) if not tree_addition: sm = bv(bvwidth, 0) for v in vars[i]: sm = If(v, sm + 1, sm) else: elements = [ If(v, BitVector(bvwidth, 1), BitVector(bvwidth, 0)) for v in vars ] while len(elements) > 1: if len(elements) % 2 == 1: elements.append(bv(bvwidth, 0)) next_elements = [] for a, b in zip(elements[::2], elements[1::2]): next_elements.append(a + b) elements = next_elements assert len(elements) == 1 sm = elements[0] # Add some redundant constraints, so that the sovler doesn't need to work to learn them: output = sm.bits() any_vars = Or(vars) any_outs = Or(output) AssertEq(any_vars, any_outs) if retBitVector: return sm else: return output
def _PopCountBV(vars, retBitVector=False): from monosat.bvtheory import BitVector bvwidth = math.ceil(math.log(len(vars), 2)) sm = BitVector(bvwidth, 'popcount', vars) #Add some redundant constraints, so that the sovler doesn't need to work to learn them: output = sm.bits() any_vars = Or(vars) any_outs = Or(output) AssertEq(any_vars, any_outs) if retBitVector: return sm else: return output
def _PopCountBV(vars,retBitVector=False): from monosat.bvtheory import BitVector bvwidth = math.ceil( math.log(len(vars),2)) sm = BitVector(bvwidth,'popcount',vars) #Add some redundant constraints, so that the sovler doesn't need to work to learn them: output= sm.bits() any_vars=Or(vars) any_outs = Or(output) AssertEq(any_vars,any_outs) if retBitVector: return sm else: return output
def minimize(bitvector_or_literals, weights=None): if isinstance(bitvector_or_literals, Var): bitvector_or_literals = [bitvector_or_literals] if isinstance(weights, int): weights = [weights] if isinstance(bitvector_or_literals, BitVector.getID()): Monosat().minimizeBV(bitvector_or_literals) else: lit_ints = [l.getLit() for l in bitvector_or_literals] if weights is None: Monosat().minimizeLits(lit_ints) else: Monosat().minimizeWeightedLits(lit_ints, weights)