Exemplo n.º 1
0
def subformula(formula):
    sf = set()
    formulas = []
    formula = rewrite_implication(formula)

    if (">>" in formula) or ("<=>" in formula):
        formula = logic.expr(formula)
        formula = logic.eliminate_implications(formula)

    formula = logic.expr(formula)
    sf.update(set([formula]))
    formulas.append(formula)

    while True:
        n = len(formulas)

        for f in formulas:
            f = logic.literal_symbol(f)
            sf.update(set([f]))

            con = logic.conjuncts(f)
            sf.update(set(con))

            dis = logic.disjuncts(f)
            sf.update(set(dis))

        for f in sf:
            if f not in formulas:
                formulas.append(f)

        if n == len(formulas):
            return formulas
Exemplo n.º 2
0
    def _find_safe(self, i, j):
        new = neighbors(i, j) - self.visited
        new -= self.safe
        new -= self.danger

        new_safe = set()
        new_danger = set()

        if (self.world[i][j][0] == 0) and (self.world[i][j][3] == 0):
            self.fringe -= set((i, j))
            new_safe |= new
        else:
            self.fringe |= new

        print "fringe: ", self.fringe

        fringe = self.fringe.copy()
        if len(self.visited) > 1:
            for x in fringe:
                if self.kb.ask(expr('(P%s%s | W%s%s)' % (x*2))):
                    new_danger.add(x)
                    self.fringe.remove(x)
                    ev = event.FoundDangerEvent(x)
                    self.ev_manager.post(ev)
                elif self.kb.ask(expr('(~P%s%s & ~W%s%s)' % (x*2))):
                    new_safe.add(x)
                    self.fringe.remove(x)
        if len(new_safe) > 0:
            self.safe |= (new_safe)
        if len(new_danger) > 0:
            self.danger |= new_danger

        print "safe list: ", self.safe
Exemplo n.º 3
0
Arquivo: kb.py Projeto: ushair/Wumpus
    def __init__(self, sentence=None):
        self.k = expr('~P00 & ~W00')

        li = []
        for i in range(4):
            for j in range(4):
                for l, r in (('B', 'P'), ('S', 'W')):
                    left = "%s%s%s" % (l, i, j)
                    right_list = []
                    for s, t in ai.neighbors(i, j):
                        right_list.append("%s%s%s" % (r, s, t))
                    li.append("(%s <=> (%s))" % \
                              (left, ' | '.join(right_list)))
        e = expr(' & '.join(li))
        self.tell(e)

        # one and only one wumpus
        li = ['W%s%s' % (i, j) for i in range(4) for j in range(4)]
        e = expr(' | '.join(li))
        self.tell(e)


        li = ['(~W%s%s | ~W%s%s)' % \
              (i, j, x, y)
              for i in range(4) \
              for j in range(4) \
              for x in range(4) \
              for y in range(4) \
              if not ((i == x) and (j == y))]
        e = expr(' & '.join(li))
        self.tell(e)
Exemplo n.º 4
0
    def __init__(self, sentence=None):
        self.k = expr('~P00 & ~W00')

        li = []        
        for i in range(4):
            for j in range(4):
                for l, r in (('B', 'P'), ('S', 'W')):
                    left = "%s%s%s" % (l, i, j)
                    right_list = []
                    for s, t in ai.neighbors(i, j):
                        right_list.append("%s%s%s" % (r, s, t))
                    li.append("(%s <=> (%s))" % \
                              (left, ' | '.join(right_list)))
        e = expr(' & '.join(li))
        self.tell(e)

        # one and only one wumpus
        li = ['W%s%s' % (i, j) for i in range(4) for j in range(4)]
        e = expr(' | '.join(li))
        self.tell(e)


        li = ['(~W%s%s | ~W%s%s)' % \
              (i, j, x, y)
              for i in range(4) \
              for j in range(4) \
              for x in range(4) \
              for y in range(4) \
              if not ((i == x) and (j == y))]
        e = expr(' & '.join(li))
        self.tell(e)
Exemplo n.º 5
0
def res(formula):
    formula = rewrite_implication(formula)

    formula = logic.to_cnf(logic.expr(formula))
    clauses = logic.conjuncts(formula)

    print "CNF: " + cnf(formula)
    m = 1
    for c in clauses:
        if logic.tt_true(c):
            clauses.remove(c)
        else:
            a = remove_double_bracket(cnf(c))
            print str(m) + ": " + a
            m += 1

    new = set()

    while True:
        n = len(clauses)
        pairs = [(clauses[i], clauses[j]) for i in range(n)
                 for j in range(i + 1, n)]

        for (ci, cj) in pairs:
            resolvents = logic.pl_resolve(ci, cj)

            if (len(resolvents) != 0):
                if (resolvents[0] not in new):
                    if (resolvents[0] is logic.FALSE):
                        print str(m) + ": {}"
                    else:
                        print str(m) + ": " + remove_double_bracket(
                            cnf(resolvents[0])) + " --- (" + str(
                                clauses.index(ci) +
                                1) + ", " + str(clauses.index(cj) + 1) + ")"
                        m += 1

            if logic.FALSE in resolvents:
                return True

            new.update(set(resolvents))

        if new.issubset(clauses):
            return False

        for c in new:
            if c not in clauses:
                clauses.append(c)
Exemplo n.º 6
0
 def new_literals(self, clause):
     """Generate new literals based on known predicate symbols.
     Generated literal must share atleast one variable with clause"""
     share_vars = variables(clause[0])
     for l in clause[1]:
         share_vars.update(variables(l))
     for pred, arity in self.pred_syms:
         new_vars = {
             standardize_variables(expr('x'))
             for _ in range(arity - 1)
         }
         for args in product(share_vars.union(new_vars), repeat=arity):
             if any(var in share_vars for var in args):
                 # make sure we don't return an existing rule
                 if not Expr(pred, args) in clause[1]:
                     yield Expr(pred, *[var for var in args])
Exemplo n.º 7
0
def cnf(formula):
    result = "{"
    formula = str(formula)

    formula = rewrite_implication(formula)

    formula = logic.to_cnf(logic.expr(formula))
    clauses = logic.conjuncts(formula)

    clauses = set(clauses)
    clauses = list(clauses)

    for c in clauses:
        if logic.tt_true(c):
            clauses.remove(c)

    for c in clauses:
        if logic.tt_true(c) is False:
            result = result + "{"
            lits = logic.disjuncts(c)

            lits = set(lits)
            lits = list(lits)

            for s in lits:
                if lits.index(s) == len(lits) - 1:
                    result = result + str(s) + "}"
                else:
                    result = result + str(s) + ", "

            if clauses.index(c) != len(clauses) - 1:
                result = result + ","

    result = result + "}"

    return result
Exemplo n.º 8
0
    def _do(self, action):
        facing_dict = {
                facing_list['up']: (0, 1),
                facing_list['right']: (1, 0),
                facing_list['down']: (0, -1),
                facing_list['left']: (-1, 0),
                }
        if action is action_list['forward']:
            dx, dy = facing_dict[self.facing]
            new_pos = (self.pos[0]+dx, self.pos[1]+dy)
            if new_pos in neighbors(*self.pos):
                ev = event.PlayerForwardEvent(new_pos)
                self.ev_manager.post(ev)

                if new_pos not in self.visited:
                    i, j = new_pos
                    percept = self.world[i][j]
                    ev = event.PlayerPerceiveEvent(percept)
                    self.ev_manager.post(ev)

                    self.visited.add((i, j))
                    if (i, j) in self.safe:
                        self.safe.remove((i, j))
                    self.known_world[i][j] = percept

                    if percept[1] == 2:
                        self.gold_picked = True
                        self._do(action_list['pick'])
                    elif (percept[2] == 2) or (percept[4] == 2):
                        self.alive = False
                        ev = event.PlayerDieEvent()
                        self.ev_manager.post(ev)
                    else:
                        c = 0
                        know = []
                        for x in percept:
                            if x == 2:
                                know.append("%s%s%s" % (map_list[c], i, j))
                            elif x == 0:
                                know.append("~%s%s%s" % (map_list[c], i, j))
                            c += 1
                        if len(know) > 0:
                            print "tell kb: ", ' & '.join(know)
                            self.kb.tell(expr(' & '.join(know)))

                        self._find_safe(i, j)
                self.pos = new_pos
        elif action is action_list['left']:
            self.facing = (self.facing - 1) % 4
            
            ev = event.PlayerTurnEvent(
                event.PlayerTurnEvent.direction_list['left'], self.facing)
            self.ev_manager.post(ev)

        elif action is action_list['right']:
            self.facing = (self.facing + 1) % 4

            ev = event.PlayerTurnEvent(
                event.PlayerTurnEvent.direction_list['right'], self.facing)
            self.ev_manager.post(ev)

        elif action is action_list['shoot']:
            def all_along(pos, facing):
                all_pos = []
                dx, dy = facing_dict[facing]
                x, y = (self.pos[0] + dx, self.pos[1] + dy)
                while (x in range(4)) and (y in range(4)):
                    all_pos.append((x, y))
                    x, y = (x + dx, y + dy)
                return all_pos
            for i, j in all_along(self.pos, self.facing):
                if self.world[i][j][4]:
                    self._wumpus_die(i, j)
                    
                    ev = event.PlayerShootEvent()
                    self.ev_manager.post(ev)

        elif action is action_list['pick']:
            i, j = (self.pos[0], self.pos[1])
            self.world[i][j][1] = 0
            self.gold_picked = True

            ev = event.PlayerPickEvent(self.pos)
            self.ev_manager.post(ev)