Esempio n. 1
0
def test_is_cnf():
    x, y, z = symbols('x,y,z')
    assert is_cnf(x) is True
    assert is_cnf(x | y | z) is True
    assert is_cnf(x & y & z) is True
    assert is_cnf((x | y) & z) is True
    assert is_cnf((x & y) | z) is False
Esempio n. 2
0
def test_is_cnf():
    x, y, z = symbols('x,y,z')
    assert is_cnf(x) is True
    assert is_cnf(x | y | z) is True
    assert is_cnf(x & y & z) is True
    assert is_cnf((x | y) & z) is True
    assert is_cnf((x & y) | z) is False
Esempio n. 3
0
def expr_to_satfmt(expr, mapper, convert_cnf=True):
    """
    Takes a sympy formula in CNF, return a list of lists of integers for
    use with pycosat
    """

    # ensure CNF
    cnf_expr = None
    if convert_cnf and not is_cnf(expr):
        print(
            "Got a non-CNF expression, converting... (depending on the size and "
            "structure of the expression, this could take a prohibitively long time)"
        )
        # see https://cs.stackexchange.com/a/41071/97082 for an explanation
        # if this becomes a problem, Tseitin transforms might be worth exploring
        # for now I'm just gonna try to stick to CNF everywhere

        cnf_expr = to_cnf(expr, simplify=True, force=True)
    else:
        cnf_expr = expr

    # make list of lists of integers
    clauses = []
    for clause in cnf_expr.args:
        if type(clause) in [Symbol, Not]:
            clauses.append([mapper.to_int(clause)])
        else:
            clauses.append(list(map(mapper.to_int, clause.args)))

    return clauses
Esempio n. 4
0
    def addToKnowledgeBase(self, box):
        expression_box_neighbours = S.false
        neighbours = self.getNeighbours(box)
        if box.clue == 0:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    self.fringe.remove(neighbour)
                # neighbour.solved = True
                # self.solvedBoxes += 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.symbol: False})
                if (neighbour.solved == False):
                    self.observedMineField[neighbour.row][
                        neighbour.col].prob = 0.0
                    self.fringe.append(neighbour)
        elif box.clue == 8:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    self.fringe.remove(neighbour)
                neighbour.solved = True
                neighbour.mineFlag = IS_MINE
                self.solvedBoxes += 1
                # self.unseenBoxes -= 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.symbol: True})
                self.observedMineField[neighbour.row][neighbour.col].prob = 1.0

        else:
            for ci in range(0, box.clue + 1):
                for subset in itertools.combinations(neighbours, box.clue):
                    expression_subset = S.true

                    for neighbour in neighbours:
                        if neighbour not in subset:
                            expression_subset = expression_subset & ~neighbour.symbol
                        else:
                            expression_subset = expression_subset & neighbour.symbol
                    expression_box_neighbours = expression_box_neighbours | (
                        expression_subset)
            for neighbour in neighbours:
                if neighbour.solved == True:
                    if neighbour.mineFlag:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.symbol: True})
                    else:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.symbol: False})

            # print("Expression Generated:", expression_box_neighbours)
            expr = expression_box_neighbours

            if not is_cnf(expr):
                expr = to_cnf(expr, simplify=True)
            # print("Converted new expression")
            # print("After CNF:", expr)

            self.updateKnowledgeBase(expr, box)
Esempio n. 5
0
    def addInfoToKnowledgeBase(self, box):
        expression_box_neighbours = S.false
        neighbours = self.getNeighbours(box)
        if box.clue == 0:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    self.fringe.remove(neighbour)
                # neighbour.solved = True
                # self.solvedBoxes += 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.denotation: False})
                if (neighbour.solved == False):
                    self.agentObservedMineField[neighbour.row][
                        neighbour.col].prob = 0.0
                    self.fringe.append(neighbour)
        elif box.clue == 8:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    self.fringe.remove(neighbour)
                neighbour.solved = True
                neighbour.mineFlag = IS_MINE
                self.solvedBoxes += 1
                # self.unseenBoxes -= 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.denotation: True})
                self.agentObservedMineField[neighbour.row][
                    neighbour.col].prob = 1.0

        else:
            for clue_i in range(0, box.clue + 1):
                for subset in itertools.combinations(neighbours, box.clue):
                    expression_subset = S.true

                    for neighbour in neighbours:
                        if neighbour not in subset:
                            expression_subset = expression_subset & ~neighbour.denotation
                        else:
                            expression_subset = expression_subset & neighbour.denotation
                    expression_box_neighbours = expression_box_neighbours | (
                        expression_subset)
            for neighbour in neighbours:
                if neighbour.solved:
                    if neighbour.mineFlag:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.denotation: True})
                    else:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.denotation: False})

            expression = expression_box_neighbours

            if not is_cnf(expression):
                expression = to_cnf(expression, simplify=True)

            self.updateKnowledgeBaseInfo(expression, box)
Esempio n. 6
0
def test_issue_18904():
    x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15 = symbols('x1:16')
    eq = (( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x8 & x9 )  |
        ( x1 & x2 & x3 & x4 & x5 & x6 & x7 & x10 & x9 )  |
        ( x1 & x11 & x3 & x12 & x5 & x13 & x14 & x15 & x9 ))
    assert is_cnf(to_cnf(eq))
    raises(ValueError, lambda: to_cnf(eq, simplify=True))
    for f, t in zip((And, Or), (to_cnf, to_dnf)):
        eq = f(x1, x2, x3, x4, x5, x6, x7, x8, x9)
        raises(ValueError, lambda: to_cnf(eq, simplify=True))
        assert t(eq, simplify=True, force=True) == eq
Esempio n. 7
0
def test_is_cnf():
    assert is_cnf(x) is True
    assert is_cnf(x | y | z) is True
    assert is_cnf(x & y & z) is True
    assert is_cnf((x | y) & z) is True
    assert is_cnf((x & y) | z) is False
    assert is_cnf(~(x & y) | z) is False
Esempio n. 8
0
def test_is_cnf():
    assert is_cnf(x) is True
    assert is_cnf(x | y | z) is True
    assert is_cnf(x & y & z) is True
    assert is_cnf((x | y) & z) is True
    assert is_cnf((x & y) | z) is False
    assert is_cnf(~(x & y) | z) is False
Esempio n. 9
0
    def _process_expr(self):
        self._num_vars = len(self._expr.binary_symbols)
        self._lit_to_var = [None] + sorted(self._expr.binary_symbols, key=str)
        self._var_to_lit = dict(
            zip(self._lit_to_var[1:], range(1, self._num_vars + 1)))

        if self._optimization or (not is_cnf(self._expr)
                                  and not is_dnf(self._expr)):
            expr = simplify_logic(self._expr)
        else:
            expr = self._expr

        if isinstance(expr, BooleanTrue):
            ast = 'const', 1
        elif isinstance(expr, BooleanFalse):
            ast = 'const', 0
        else:
            ast = get_ast(self._var_to_lit, expr)

        if ast[0] == 'or':
            self._nf = DNF(ast, num_vars=self._num_vars)
        else:
            self._nf = CNF(ast, num_vars=self._num_vars)
Esempio n. 10
0
    def add_premise(self, sentence):
        """
        :param sentence: sentence to be added
        :param rank: rank corresponding to sentence.
        :return:
        """
        if not is_cnf(sentence):
            sentence = to_cnf(sentence)
        premises = self.fetch_premises()

        if not PL_resolution(premises, sentence):
            logging.warning(
                f'{sentence} introduces a contradiction in the KB.')
            # return None
        if sentence in premises:
            logging.warning(f'{sentence} was already part of the KB.')
            return sentence

        premises.append(sentence)
        updated_ranks = self.update_ranks_of_existing_premises(premises)
        self.premises = [
            (belief, rank) for belief, rank in zip(premises, updated_ranks)
        ]  #zip updated ranks and premises and store in
        return sentence
Esempio n. 11
0
    def addInfoToKnowledgeBase(self, box):
        expression_box_neighbours = S.false
        neighbours = self.getNeighbours(box)

        # CASE 1: The clue is 0 i.e. all neighbours are 'SAFE
        if box.clue == 0:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    self.fringe.remove(neighbour)
                # neighbour.solved = True
                # self.solvedBoxes += 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.denotation: False})
                if (neighbour.solved == False):
                    self.agentObservedMineField[neighbour.row][
                        neighbour.col].prob = 0.0
                    self.fringe.append(neighbour)

        # CASE 2: 100 percent confidence in all the neighbours being a mine
        elif box.clue == 8:
            for neighbour in neighbours:
                if neighbour in self.fringe:
                    # As 'Pass by Value' is being used here
                    self.fringe.remove(neighbour)
                neighbour.solved = True
                neighbour.mineFlag = IS_MINE
                self.solvedBoxes += 1
                self.knowledgeBase = self.knowledgeBase.subs(
                    {neighbour.denotation: True})
                self.agentObservedMineField[neighbour.row][
                    neighbour.col].prob = 1.0

        # CASE 3: The clue lies between 0 and 1 (exclusive)
        else:
            for subset in itertools.combinations(neighbours, box.clue):
                expression_subset = S.true

                # Forming the expression for the neighbours of a given box
                for neighbour in neighbours:
                    if neighbour not in subset:
                        expression_subset = expression_subset & ~neighbour.denotation
                    else:
                        expression_subset = expression_subset & neighbour.denotation
                expression_box_neighbours = expression_box_neighbours | expression_subset

            # Updating the expression obtained above
            for neighbour in neighbours:
                if neighbour.solved:
                    if neighbour.mineFlag:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.denotation: True})
                    else:
                        expression_box_neighbours = expression_box_neighbours.subs(
                            {neighbour.denotation: False})

            # print("Expression Generated:", expression_box_neighbours)
            expression = expression_box_neighbours

            if not is_cnf(expression):
                expression = to_cnf(expression, simplify=True)
            # print("Converted new expression")
            # print("After CNF:", expr)

            self.updateKnowledgeBaseInfo(expression, box)
from sympy.logic.boolalg import ITE, And, Xor, Or
from sympy.logic.boolalg import to_cnf, to_dnf
from sympy.logic.boolalg import is_cnf, is_dnf
from sympy.abc import A, B, C
from sympy.abc import X, Y, Z
from sympy.abc import a, b, c

ITE(True, False, True)
ITE(Or(True, False), And(True, True), Xor(True, True))
ITE(a, b, c)
ITE(True, a, b)
ITE(False, a, b)
ITE(a, b, c)

to_cnf(~(A | B) | C)
to_cnf((A | B) & (A | ~A), True)

to_dnf(Y & (X | Z))
to_dnf((X & Y) | (X & ~Y) | (Y & Z) | (~Y & Z), True)

is_cnf(X | Y | Z)
is_cnf(X & Y & Z)
is_cnf((X & Y) | Z)
is_cnf(X & (Y | Z))

is_dnf(X | Y | Z)
is_dnf(X & Y & Z)
is_dnf((X & Y) | Z)
is_dnf(X & (Y | Z))
Esempio n. 13
0
def test_issue_9949():
    assert is_cnf(to_cnf((b > -5) | (a > 2) & (a < 4)))
Esempio n. 14
0
from sympy.abc import A, B, D

print(to_cnf(~(A | B) | D))
print(to_cnf((A | B) & (A | ~A), True))

#dnf
from sympy.logic.boolalg import to_dnf
from sympy.abc import A, B, C

print(to_dnf(B & (A | C)))
print(to_dnf((A & B) | (A & ~B) | (B & C) | (~B & C), True))

#to check cnf and dnf
from sympy.logic.boolalg import is_cnf
from sympy.abc import A, B, C
from sympy.logic.boolalg import is_dnf
from sympy.abc import A, B, C

print(is_cnf(A | B | C))
print(is_cnf((A & B) | C))
print(is_dnf(A | B | C))
print(is_dnf(A & (B | C)))

#simplify logic

from sympy.logic import simplify_logic
from sympy.abc import x, y, z
from sympy import S

b = (~x & ~y & ~z) | (~x & ~y & z)
print(simplify_logic(b))
Esempio n. 15
0
def test_is_cnf():
    x, y, z = symbols('xyz')
    assert is_cnf(x | y | z) == True
    assert is_cnf(x & y & z) == True
    assert is_cnf((x | y) & z) == True
    assert is_cnf((x & y) | z) == False
Esempio n. 16
0
 def checkcnf():
     from sympy.logic.boolalg import is_cnf
     from sympy.abc import A, B, C
     expr = input(("Enter a logic expression to check for CNF"))
     print(is_cnf(expr))
Esempio n. 17
0
def test_is_cnf():
    x, y, z = symbols('xyz')
    assert is_cnf(x | y | z) == True
    assert is_cnf(x & y & z) == True
    assert is_cnf((x | y) & z) == True
    assert is_cnf((x & y) | z) == False