Class PBConstraint ================== .. _pb-constraint: Constructor ----------- PBConstraint(weightedlits: [:ref:`WeightedLits `], :ref:`comparator `, bound: int) Creates an object to represents a Pseudo-Boolean constraint. >>> import pypblib.pblib >>> wl_list = [pypblib.pblib.WeightedLit(1,3), pypblib.pblib.WeightedLit(2,1), pypblib.pblib.WeightedLit(3,5)] >>> constr = pypblib.pblib.PBConstraint(wl_list, pypblib.pblib.GEQ, 1) >>> constr +3x1+x2+5x3>=1 >>> from pypblib import pblib >>> w1 = pblib.WeightedLit(1, 2) >>> w2 = pblib.WeightedLit(2, 1) >>> w3 = pblib.WeightedLit(3, -2) >>> constr = pblib.PBConstraint([w1, w2, w3], pblib.LEQ, 1) >>> constr +2x1+x2-2x3<=1 PBConstraint(weightedlits: [:ref:`WeightedLits `], :ref:`comparator `, bound_geq: int, bound_leq: int) Creates an object to represents a Pseudo-Boolean constraint, in case :ref:`pblib.BOTH ` comparator and bound less-equal and greater-equal are different. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 2) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> comp = pblib.BOTH >>> constr = pblib.PBConstraint([wl1, wl2, wl3], comp, 2, 0) >>> constr 0<=+2x1+x2-1x3<=2 Methods summary --------------- +---------------------------------------+----------------------------------------------------------------------------+ | Return Type | Method | +=======================================+============================================================================+ | void | :ref:`add_conditional(cond: int) ` | +---------------------------------------+----------------------------------------------------------------------------+ | void | :ref:`add_conditionals(conds: [int]) ` | +---------------------------------------+----------------------------------------------------------------------------+ | [int] | :ref:`get_conditionals() ` | +---------------------------------------+----------------------------------------------------------------------------+ | void | :ref:`clear_conditionals() ` | +---------------------------------------+----------------------------------------------------------------------------+ |:ref:`comparator` | :ref:`get_comparator() ` | +---------------------------------------+----------------------------------------------------------------------------+ | long | :ref:`get_leq() ` | +---------------------------------------+----------------------------------------------------------------------------+ | long | :ref:`get_geq() ` | +---------------------------------------+----------------------------------------------------------------------------+ | long | :ref:`get_min_sum() ` | +---------------------------------------+----------------------------------------------------------------------------+ | long | :ref:`get_max_sum() ` | +---------------------------------------+----------------------------------------------------------------------------+ | int | :ref:`get_n() ` | +---------------------------------------+----------------------------------------------------------------------------+ |[:ref:`WeightedLit`] | :ref:`get_weighted_literals() ` | +---------------------------------------+----------------------------------------------------------------------------+ | :ref:`PBConstraint` | :ref:`get_geq_constraint() ` | +---------------------------------------+----------------------------------------------------------------------------+ | :ref:`PBConstraint` | :ref:`get_leq_constraint() ` | +---------------------------------------+----------------------------------------------------------------------------+ | void | :ref:`set_comparator ` (:ref:`comparator `) | +---------------------------------------+----------------------------------------------------------------------------+ | void | :ref:`set_leq(leq_bound: long) ` | +---------------------------------------+----------------------------------------------------------------------------+ | void | :ref:`set_geq(geq_bound: long) ` | +---------------------------------------+----------------------------------------------------------------------------+ Methods details --------------- .. _add-conditional: .. method:: add_conditional(cond: int) Adds a conditional to the constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, -1) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], comp, 2, 0) >>> constr.add_conditional(3) .. _add-conditionals: .. method:: add_conditionals(conds: [int]) Adds a list of conditionals to the constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, -1) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], comp, 2, 0) >>> constr.add_conditionals([1, 2]) .. _get-conditionals: .. method:: get_conditionals() -> [int] Returns a list of conditionals. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, -1) >>> constr.add_conditionals([1, 2]) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], comp, 2, 0) >>> cond = constr.get_conditionals() >>> cond [1, 2] .. _clear-conditionals: .. method:: clear_conditional() Removes all conditionals in a constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, -1) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], comp, 2, 0) >>> constr.add_conditionals([1, 2]) >>> cond = constr.get_conditionals() >>> cond [1, 2] >>> cond = constr.clear_conditionals() >>> cond >>> .. _get-comparator: .. method:: get_comparator() -> comparator Returns an int to represents the :ref:`comparator ` (GEQ = 0, LEQ = 1 and BOTH = 2). >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> constr = pblib.PBConstraint([wl1, wl2], pblib.GEQ, 1) >>> comp = constr.get_comparator() >>> print(comp) 0 .. _get-leq: .. method:: get_leq() -> long Returns the less-equal value bound of the constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> constr = pblib.PBConstraint([wl1, wl2], pblib.LEQ, 1) >>> n = constr.get_leq() >>> print(n) 1 .. _get-geq: .. method:: get_geq() -> long Returns the greater-equal value bound of the constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> constr = pblib.PBConstraint([wl1, wl2], pblib.GEQ, 0) >>> n = constr.get_geq() >>> print(n) 0 .. _get-min-sum: .. method:: get_min_sum() -> long Returns the minimum sum of literals' weights. >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> wls = [wl1, wl2, wl3] >>> constr = pblib.PBConstraint(wls, pblib.LEQ, 4) >>> s = constr.get_min_sum() >>> print("Min = ", s) >>> Min = -1 .. _get-max-sum: .. method:: get_max_sum() -> long Returns the maximum sum of literals' weights. >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> wls = [wl1, wl2, wl3] >>> constr = pblib.PBConstraint(wls, pblib.LEQ, 4) >>> s = constr.get_min_sum() >>> print("Max = ", s) >>> Max = 2 .. _get-n: .. method:: get_n() -> int Returns the number of weigted literals in the constraint. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> constr = pblib.PBConstraint([wl1, wl2], pblib.GEQ, 1) >>> n = constr.get_n() >>> print(n) 2 >>> wl3 = pblib.WeightedLit(3, 2) >>> constr2 = pblib.PBConstraint([wl1, wl2, wl3], pblib.GEQ, 1) >>> n2 = constr2.get_n() >>> print(n2) 3 .. _get-weighted-literals: .. method:: get_weighted_literals() -> [WeightedLiterals] Returns a copy of the vector of weighted literals which PBContraint contains. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 2) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, 2) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], pblib.LEQ, 2) >>> w_lits = constr.get_weighted_literals() >>> print(w_lits) [WeightedLit(lit=1, weight=2), WeightedLit(lit=2, weight=2), WeightedLit(lit=3, weight=2)] >>> constr_2 = pblib.PBConstraint(w_lits, pblib.GEQ, 4) >>> constr_2 +2x1+2x2+2x3>=4 .. _get-geq-constraint: .. method:: get_geq_constraint() -> PBConstraint Takes a pseudo boolean constraint and returns a new PBConstraint changing the comparator to a greater-equal. However, the bound does not change. If the greater-equal bound is equal to zero (because the previous restriction had not been assigned any value), the bound will remain zero. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 2) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, 2) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], pblib.LEQ, 2) >>> constr +2x1+2x2+2x3<=2 >>> constr_2 = constr.get_geq_constraint() >>> constr_2 +2x1+2x2+2x3>=0 .. _get-leq-constraint: .. method:: get_leq_constraint() -> PBConstraint Takes a pseudo boolean constraint and returns a new PBConstraint changing the comparator to a less-equal. However, the bound does not change. If the less-equal bound is equal to zero (because the previous restriction had not been assigned any value), the bound will remain zero. >>> from pypblib import pblib >>> wl1 = pblib.WeightedLit(1, 2) >>> wl2 = pblib.WeightedLit(2, 2) >>> wl3 = pblib.WeightedLit(3, 2) >>> constr = pblib.PBConstraint([wl1, wl2, wl3], pblib.GEQ, 4) >>> constr +2x1+2x2+2x3>=4 >>> constr_2 = constr.get_leq_constraint() >>> constr_2 +2x1+2x2+2x3<=0 .. _set-comparator: .. method:: set_comparator(comparator) Sets the constraint's :ref:`comparator ` to specified value. >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> wls = [wl1, wl2, wl3] >>> constr = pblib.PBConstraint(wls, pblib.LEQ, 4) >>> print(constr.get_comparator()) 1 >>> constr.set_comparator(pblib.GEQ) >>> print(constr.get_comparator()) 0 .. _set-leq: .. method:: set_leq(leq_bound: long) Sets the less-equal bound to specified value. >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> wls = [wl1, wl2, wl3] >>> constr = pblib.PBConstraint(wls, pblib.LEQ, 2) >>> print(constr.get_leq()) 2 >>> constr.set_leq(1) >>> print(constr.get_leq()) 1 .. _set-geq: .. method:: set_geq(geq_bound: long) Sets the greater-equal bound to specified value. >>> wl1 = pblib.WeightedLit(1, 1) >>> wl2 = pblib.WeightedLit(2, 1) >>> wl3 = pblib.WeightedLit(3, -1) >>> wls = [wl1, wl2, wl3] >>> constr = pblib.PBConstraint(wls, pblib.GEQ, 1) >>> print(constr.get_geq()) 1 >> constr.set_geq(0) >> print(constr.get_geq()) 0