2. Class PBConstraint¶
2.1. Constructor¶
PBConstraint(weightedlits: [WeightedLits], 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: [WeightedLits], comparator, bound_geq: int, bound_leq: int)
Creates an object to represents a Pseudo-Boolean constraint, in case 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
2.2. Methods summary¶
Return Type | Method |
---|---|
void | add_conditional(cond: int) |
void | add_conditionals(conds: [int]) |
[int] | get_conditionals() |
void | clear_conditionals() |
comparator | get_comparator() |
long | get_leq() |
long | get_geq() |
long | get_min_sum() |
long | get_max_sum() |
int | get_n() |
[WeightedLit] | get_weighted_literals() |
PBConstraint | get_geq_constraint() |
PBConstraint | get_leq_constraint() |
void | set_comparator (comparator) |
void | set_leq(leq_bound: long) |
void | set_geq(geq_bound: long) |
2.3. Methods details¶
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
(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
() → [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_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
() → comparator¶Returns an int to represents the 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
() → 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
() → 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
() → 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
() → 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
() → 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
() → [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
() → 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
() → 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
(comparator)¶Sets the constraint’s 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
(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
(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