Exemplo n.º 1
0
def nbest(obs, kb, maxdepth, n, skolemize=True):
    '''Returns n-best conjunctions of etcetera literals that logically entail the observations'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    pr2beat = 0.0
    nbest = []  # solutions
    nbestPr = []  # probabilities
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs
    ]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > n:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0]  # only if full
    nbest.reverse()  # [0] is now highest
    if skolemize:
        return [unify.skolemize(r) for r in nbest]
    else:
        return nbest
Exemplo n.º 2
0
def nbest(obs, kb, indexed_kb, maxdepth, n, skolemize=True):
    pr2beat = 0.0
    nbest = []  # solutions
    nbestPr = []  # probabilities
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs
    ]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))

        # check if the solution contains etcetera literals only.
        if bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > n:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0]  # only if full
    nbest.reverse()  # [0] is now highest
    if skolemize:
        return [unify.skolemize(r) for r in nbest]
    else:
        return nbest
Exemplo n.º 3
0
def etcAbduction(obs, kb, maxdepth, skolemize = True):
    '''Trying something faster'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    res = []
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        res.extend(abduction.crunch(u))
    res.sort(key=lambda item: jointProbability(item), reverse=True)
    if skolemize:
        return [unify.skolemize(r) for r in res]
    else:
        return res
Exemplo n.º 4
0
def etcAbduction(obs, kb, maxdepth, skolemize = True):
    '''Exhuastive search for conjunctions of etctera literals that logically entail the observations'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    res = []
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        res.extend(abduction.crunch(u))
    res.sort(key=lambda item: jointProbability(item), reverse=True)
    if skolemize:
        return [unify.skolemize(r) for r in res]
    else:
        return res
Exemplo n.º 5
0
def etcAbduction(obs, kb, indexed_kb, maxdepth, skolemize=True):
    '''Trying something faster'''
    res = []
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs
    ]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        res.extend(abduction.crunch(u))
    res.sort(key=lambda item: jointProbability(item), reverse=True)
    if skolemize:
        return [unify.skolemize(r) for r in res]
    else:
        return res
Exemplo n.º 6
0
def incremental1(obs, kb, maxdepth, n, w, b, skolemize=True):
    # n = n-best
    # w = window size
    # b = beam of running candidate interpretations
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    previous = []  # proofs of the previous
    while len(obs) > 0:
        window = obs[0:w]
        obs = obs[w:]
        listoflists = [
            abduction.and_or_leaflists([ob], indexed_kb, maxdepth)
            for ob in window
        ]
        if len(previous) > 0:
            listoflists.append(previous)
        pr2beat = 0.0
        nbest = []  # solutions
        nbestPr = []  # probabilities
        for u in itertools.product(*listoflists):
            u = list(itertools.chain.from_iterable(u))
            if etcetera.bestCaseProbability(u) > pr2beat:
                for solution in abduction.crunch(u):
                    jpr = etcetera.jointProbability(solution)
                    if jpr > pr2beat:
                        insertAt = bisect.bisect_left(nbestPr, jpr)
                        nbest.insert(insertAt, solution)
                        nbestPr.insert(insertAt, jpr)
                        if len(nbest) > b:
                            nbest.pop(0)
                            nbestPr.pop(0)
                            pr2beat = nbestPr[0]  # only if full
        nbest.reverse()  # [0] is now highest
        previous = nbest
    if skolemize:
        return [unify.skolemize(r)
                for r in previous[0:n]]  # only skolemize nbest
    else:
        return previous[0:n]
Exemplo n.º 7
0
def nbest(obs, kb, maxdepth, n, skolemize = True):
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    pr2beat = 0.0
    nbest = [] # solutions
    nbestPr = [] # probabilities
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > n:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0] # only if full
    nbest.reverse() # [0] is now highest
    if skolemize:
        return [unify.skolemize(r) for r in nbest]
    else:
        return nbest
Exemplo n.º 8
0
def incremental2(obs, kb, maxdepth, n, w, b, skolemize=True):
    # n = n-best
    # w = window size
    # b = beam of running candidate interpretations
    iteration = 1  # count for skolem constants
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    previous = []  # proofs of the previous
    remaining = obs[:]  # obs yet to be interpretated

    # first, interpret the first window as normal
    window = remaining[0:w]
    remaining = remaining[w:]
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in window
    ]
    pr2beat = 0.0
    nbest = []  # solutions
    nbestPr = []  # probabilities
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if etcetera.bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = etcetera.jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > b:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0]  # only if full
    nbest.reverse()  # [0] is now highest
    previous = nbest
    pre = "$" + str(iteration) + ":"
    previous = [unify.skolemize(r, prefix=pre)
                for r in previous]  # skolemize the past (required)

    # next, interpret remaining windows in a special way
    while len(remaining) > 0:
        iteration += 1
        window = remaining[0:w]
        remaining = remaining[w:]
        pr2beat = 0.0
        nbest = []  # solutions
        nbestPr = []  # probabilities
        for previousSolution in previous:
            previousSolutionJpr = etcetera.jointProbability(previousSolution)
            context = getContext(previousSolution, obs, kb)
            listoflists = [
                contextual_and_or_leaflists([ob], indexed_kb, maxdepth,
                                            context) for ob in window
            ]

            for u in itertools.product(*listoflists):
                u = list(itertools.chain.from_iterable(u))
                if etcetera.bestCaseProbability(
                        u) * previousSolutionJpr > pr2beat:
                    for solution in abduction.crunch(u):
                        jpr = etcetera.jointProbability(
                            solution) * previousSolutionJpr
                        if jpr > pr2beat:
                            insertAt = bisect.bisect_left(nbestPr, jpr)
                            nbest.insert(insertAt,
                                         previousSolution + solution)  # joined
                            nbestPr.insert(insertAt, jpr)
                            if len(nbest) > b:
                                nbest.pop(0)
                                nbestPr.pop(0)
                                pr2beat = nbestPr[0]  # only if full
        nbest.reverse()  # [0] is now highest
        previous = nbest
        pre = "$" + str(iteration) + ":"
        previous = [unify.skolemize(r, prefix=pre)
                    for r in previous]  # skolemize the past (required)
    return previous[0:n]