def test_depth(): assert (a | b).depth == 1 assert (a | (b & c)).depth == 2 assert (a | (b & (c | d))).depth == 3 assert (a & b).depth == 1 assert (a & (b | c)).depth == 2 assert (a & (b | (c & d))).depth == 3 assert Not(a | b).depth == 2 assert Not(a | (b & c)).depth == 3 assert Not(a | (b & (c | d))).depth == 4 assert Xor(a, b, c).depth == 1 assert Xor(a, b, c | d).depth == 2 assert Xor(a, b, c | Xor(d, e)).depth == 3 assert Equal(a, b, c).depth == 1 assert Equal(a, b, c | d).depth == 2 assert Equal(a, b, c | Xor(d, e)).depth == 3 assert Implies(p, q).depth == 1 assert Implies(p, a | b).depth == 2 assert Implies(p, Xor(a, b)).depth == 2 assert ITE(s, a, b).depth == 1 assert ITE(s, a | b, b).depth == 2 assert ITE(s, a | b, Xor(a, b)).depth == 2
def test_implies(): # Function assert Implies(~p, q).support == {p, q} # Expression assert Implies(0, 0) is EXPRONE assert Implies(0, 1) is EXPRONE assert Implies(1, 0) is EXPRZERO assert Implies(1, 1) is EXPRONE assert Implies(0, p) is EXPRONE assert Implies(1, p) == p assert Implies(p, 0) == ~p assert Implies(p, 1) is EXPRONE assert Implies(p, p) is EXPRONE assert Implies(p, ~p) == ~p assert Implies(~p, p) == p assert str(p >> q) == "Implies(p, q)" assert str((a & b) >> (c | d)) == "Implies(And(a, b), Or(c, d))" assert (p >> q).restrict({p: 0}) is EXPRONE assert (p >> q).compose({q: a}).equivalent(p >> a) assert Not(p >> q).equivalent(p & ~q) assert ((a & b) >> (c | d)).depth == 2 f = Implies(p, 1, simplify=False) assert str(f) == "Implies(p, 1)"
def test_not(): assert Not(0) is One assert Not(1) is Zero assert Not(~a) is a assert Not(a) is ~a assert Not(~a | a) is Zero assert Not(~a & a) is One assert str(Not(~a | b)) == "Not(Or(~a, b))" assert str(Not(~a | b | 0, simplify=False)) == "Not(Or(Or(~a, b), 0))" assert ~~a is a assert ~~~a is ~a assert ~~~~a is a
def test_expr2dimacssat(): assert_raises(ValueError, expr2dimacssat, Xor(0, a, simplify=False)) ret = expr2dimacssat(Xor(a, ~b)) assert ret in {'p satx 2\nxor(-2 1)', 'p satx 2\nxor(1 -2)'} ret = expr2dimacssat(Xor(a, Equal(b, ~c))) assert ret in { 'p satex 3\nxor(=(2 -3) 1)', 'p satex 3\nxor(1 =(2 -3))', 'p satex 3\nxor(=(-3 2) 1)', 'p satex 3\nxor(1 =(-3 2))' } ret = expr2dimacssat(Equal(a, ~b)) assert ret in {'p sate 2\n=(1 -2)', 'p sate 2\n=(-2 1)'} ret = expr2dimacssat(And(a, ~b)) assert ret in {'p sat 2\n*(1 -2)', 'p sat 2\n*(-2 1)'} ret = expr2dimacssat(Or(a, ~b)) assert ret in {'p sat 2\n+(1 -2)', 'p sat 2\n+(-2 1)'} ret = expr2dimacssat(Not(a | ~b)) assert ret in {'p sat 2\n-(+(1 -2))', 'p sat 2\n-(+(-2 1))'}
def test_not(): # Function assert Not(~a | b).support == {a, b} # Expression assert Not(0) is EXPRONE assert Not(1) is EXPRZERO assert Not(a) == ~a assert Not(~a) == a assert ~(~a) == a assert ~(~(~a)) == ~a assert ~(~(~(~a))) == a assert Not(a | ~a) is EXPRZERO assert Not(a & ~a) is EXPRONE
def test_not(): # Function assert Not(~a | b).support == {a, b} # Expression assert Not(0) is One assert Not(1) is Zero assert Not(a) is ~a assert Not(~a) is a assert ~(~a) is a assert ~(~(~a)) is ~a assert ~(~(~(~a))) is a assert Not(a | ~a) is Zero assert Not(a & ~a) is One
def test_basic(): a, b, c, d, p, q, s = map(exprvar, 'abcdpqs') assert expr("a & ~b | b & ~c").equivalent(a & ~b | b & ~c) assert expr("p => q").equivalent(~p | q) assert expr("a <=> b").equivalent(~a & ~b | a & b) assert expr("s ? a : b").equivalent(s & a | ~s & b) assert expr("Not(a)").equivalent(Not(a)) assert expr("Or(a, b, c)").equivalent(Or(a, b, c)) assert expr("And(a, b, c)").equivalent(And(a, b, c)) assert expr("Xor(a, b, c)").equivalent(Xor(a, b, c)) assert expr("Xnor(a, b, c)").equivalent(Xnor(a, b, c)) assert expr("Equal(a, b, c)").equivalent(Equal(a, b, c)) assert expr("Unequal(a, b, c)").equivalent(Unequal(a, b, c)) assert expr("Implies(p, q)").equivalent(Implies(p, q)) assert expr("ITE(s, a, b)").equivalent(ITE(s, a, b)) assert expr("Nor(a, b, c)").equivalent(Nor(a, b, c)) assert expr("Nand(a, b, c)").equivalent(Nand(a, b, c)) assert expr("OneHot0(a, b, c)").equivalent(OneHot0(a, b, c)) assert expr("OneHot(a, b, c)").equivalent(OneHot(a, b, c)) assert expr("Majority(a, b, c)").equivalent(Majority(a, b, c)) assert expr("AchillesHeel(a, b, c, d)").equivalent(AchillesHeel( a, b, c, d))
exprvar, 'cvp' ) # creates boolean variables c, v, and p representing contribute, vote, and punish evars = [c, v, p] uniqid2evar = {vv.uniqid: vv for vv in evars} # scenarios # --- scen = 0 if scen == 0: # no punishers, no voters f = And(Not(p), Not(v), Or(c, ~c, simplify=False), simplify=False) suffix = '0_nopun_novot' elif scen == 1: # voters and punishers # punishers don't cooperate f = And(Not(And(p, c)), Not(And(~c, v)), Or(c, ~c, simplify=False), Not(And(p, v)), simplify=False) # punish non-cooperation only poss_prules = [[And(~p, ~c)]] suffix = '1_punnocs'