Ejemplo n.º 1
0
def andor(start):
    heap = [(0, 0, LinkedList([(0, 0, 0, start, Nil)]), Nil)]
    while True:
        try:
            f, g, nodes, solved = heapq.heappop(heap)
        except IndexError:  # fronta je prazdna
            raise ValueError("Reseni neexistuje.")
        if nodes == Nil:  # seznam uzlu k vyreseni je prazdny
            return reconstruct_search_tree(solved)
        _, g1, c, node, path = nodes.head
        if is_goal(node):
            solved = Cons((node, Cons(node, path)), solved)
            heapq.heappush(heap, (f - h(node) - c, g - c, nodes.tail, solved))
        elif f <= biggest:
            succ = get_successors(node)
            if succ is None:  # narazili jsme na necilovy uzel
                continue
            op, successors = succ
            path1 = Cons(node, path)
            if op == "and":
                nodes1 = nodes.tail
                for m, c in successors:
                    if not member(m, path1):
                        nodes1 = insert((g1 + c + h(m), g1 + c, c, m, path1),
                                        nodes1)
                        f = g + c + h(m)
                        g = g + c
                heapq.heappush(heap, (f, g, nodes1, solved))
            if op == "or":
                for m, c in successors:
                    if not member(m, path1):
                        nodes1 = insert((g1 + c + h(m), g1 + c, c, m, path1),
                                        nodes.tail)
                        heapq.heappush(heap,
                                       (g + c + h(m), g + c, nodes1, solved))
def expand(path, tree, bound):
    if len(tree) == 3: # a leaf
        node, f_, g = tree
        if is_goal(node):
            yield (None, "yes", (f_, Cons(node, path)))
        if f_ <= bound:
            succ = Nil
            for m, c in move_anyYC(node):
                if not member(m, path):
                    succ = Cons((m, c), succ)
            if succ == Nil:
                yield (None, "never", None)
            else:
                trees = succlist(g, succ)
                f1 = bestf(trees)
                for tree1, solved, sol in expand(path, (node, f1, g, trees), bound):
                    yield (tree1, solved, sol)
        elif f_ > bound:
            yield (tree, "no", None)
    else: # a tree
        node, f_, g, trees = tree
        if trees == Nil:
            yield (None, "never", None)
        else:
            if f_ <= bound:
                bound1 = min(bound, bestf(trees.tail))
                for t1, solved1, sol1 in expand(Cons(node, path), trees.head, bound1):
                    for tree1, solved, sol in continue_(path, (node, f_, g, Cons(t1, trees.tail)),
                                                        bound, solved1, sol1):
                        yield (tree1, solved, sol)
            elif f_ > bound:
                yield (tree, "no", None)
Ejemplo n.º 3
0
def divide(h, xs):
    if xs == Nil:
        return (Nil, Nil)
    ms, vs = divide(h, xs.tail)
    if xs.head <= h:
        return (Cons(xs.head, ms), vs)
    else:
        return (ms, Cons(xs.head, vs))
Ejemplo n.º 4
0
def tt_check_all(kb, alpha, symbols, model):
    if symbols == Nil:
        if pl_true(kb, model):
            return pl_true(alpha, model)
        return True
    p = symbols.head
    return tt_check_all(kb, alpha, symbols.tail, Cons((p, True), model)) and \
           tt_check_all(kb, alpha, symbols.tail, Cons((p, False), model))
Ejemplo n.º 5
0
def insert(node, nodes):
    if nodes == Nil:
        return LinkedList([node])
    f = node[0]
    f1 = nodes.head[0]
    if f <= f1:
        return Cons(node, nodes)
    return Cons(nodes.head, insert(node, nodes.tail))
Ejemplo n.º 6
0
def depth_first_search(start_node):
    paths = [Cons(start_node, Nil)]  # pouzijeme pythonovsky seznam jako LIFO
    while len(paths) > 0:
        path = paths.pop()
        node = path.head
        if is_goal(node):
            yield path
        for node1 in move_anyY(node):
            paths.append(Cons(node1, path))
Ejemplo n.º 7
0
def insert_idle(a, active):
    if active == Nil:
        return
    t, b = active.head
    l = active.tail
    if a < b:
        yield Cons(("idle", b), active)
    else:
        for l1 in insert_idle(a, l):
            yield Cons((t, b), l1)
Ejemplo n.º 8
0
def breadth_first_search(start_node):
    paths = deque([Cons(start_node,
                        Nil)])  # pouzijeme pythonovskou frontu jako FIFO
    while len(paths) > 0:
        path = paths.popleft()
        node = path.head
        if is_goal(node):
            yield path
        for node1 in move_anyY(node):
            paths.append(Cons(node1, path))
Ejemplo n.º 9
0
def insert(task, active, f1):
    s, a = task
    if active == Nil:
        return (Cons((s, a), Nil), a)
    t, b = active.head
    l = active.tail
    if a <= b:
        return (Cons((s, a), active), f1)
    l1, f2 = insert((s, a), l, f1)
    return (Cons((t, b), l1), f2)
def depth_first_search(start_node, max_depth):
    paths = [(Cons(start_node,
                   Nil), max_depth)]  # pouzijeme pythonovsky seznam jako LIFO
    while len(paths) > 0:
        path, remaining_depth = paths.pop()
        node = path.head
        if is_goal(node):
            yield path
        if remaining_depth > 0:
            for node1 in move_anyY(node):
                paths.append((Cons(node1, path), remaining_depth - 1))
Ejemplo n.º 11
0
def insert(t, trees):
    if trees == Nil:
        return Cons(t, Nil)
    t1 = trees.head
    ts = trees.tail
    if is_solved(t1):
        return Cons(t, trees)
    if is_solved(t):
        return Cons(t1, insert(t, ts))
    if f(t) <= f(t1):
        return Cons(t, trees)
    return Cons(t1, insert(t, ts))
Ejemplo n.º 12
0
def perm3(xs):
    if xs == Nil:
        yield Nil
    else:
        ys = Nil
        while xs != Nil:
            result = xs.tail
            for y in ys:
                result = Cons(y, result)
            for zs in perm3(result):
                yield Cons(xs.head, zs)
            ys = Cons(xs.head, ys)
            xs = xs.tail
Ejemplo n.º 13
0
def breadth_first_search(paths):
    path = paths.head
    node = path.head
    if is_goal(node):
        yield path
    new_paths = Nil
    for node1 in move_anyY(node):
        if not member(node1, path):
            new_paths = Cons(Cons(node1, path), new_paths)
    if new_paths != Nil:
        paths1 = append(paths.tail, new_paths)
    else:
        paths1 = paths.tail
    for path in breadth_first_search(paths1):
        yield path
Ejemplo n.º 14
0
def cnt_classes(classes, exs):
    if classes == Nil:
        return Nil
    c = classes.head
    nc = cnt_class(c, exs)
    ncs = cnt_classes(classes.tail, exs)
    return Cons(nc, ncs)
def get_successors(node):
    if node[0] == "direct":
        _, x, z = node
        succ = Nil
        if x in stateS and z in stateU:
            for y in borders:
                succ = Cons((("via", x, z, y), 0), succ)
        if x in distances:
            for y, d in distances[x]:
                succ = Cons((("direct", y, z), d), succ)
        if succ == Nil:
            return None
        return ("or", succ)
    if node[0] == "via":
        _, x, z, y = node
        return ("and", LinkedList([(("direct", x, y), 0), (("direct", y, z), 0)]))
def del1(x, ys):
    if ys == Nil:
        return
    if x == ys.head:
        yield ys.tail
    for zs in del1(x, ys.tail):
        yield Cons(ys.head, zs)
Ejemplo n.º 17
0
def qsort(xs):
    if xs == Nil:
        return Nil
    if xs.tail == Nil:
        return xs
    ms, vs = divide(xs.head, xs.tail)
    return append(qsort(ms), Cons(xs.head, qsort(vs)))
def depth_first_search(akumulator_path, node):
    akumulator_path1 = Cons(node, akumulator_path)
    if is_goal(node):
        yield akumulator_path1
    for node1 in move_anyY(node):
        for path in depth_first_search(akumulator_path1, node1):
            yield path
Ejemplo n.º 19
0
def cnt_classes_attv(att, val, classes, exs):
    if classes == Nil:
        return Nil
    c = classes.head
    nc = cnt_class_attv(att, val, c, exs)
    ncs = cnt_classes_attv(att, val, classes.tail, exs)
    return Cons(nc, ncs)
Ejemplo n.º 20
0
def solveall(nodes):
    if nodes == Nil:
        yield Nil
    else:
        for tree in solve(nodes.head):
            for trees in solveall(nodes.tail):
                yield Cons(tree, trees)
Ejemplo n.º 21
0
def perm2(xs):
    if xs == Nil:
        yield Nil
    else:
        for y, ys in del1_anyX(xs):
            for zs in perm2(ys):
                yield Cons(y, zs)
Ejemplo n.º 22
0
def del_(xs, x):
    if xs == Nil:
        return
    if x == xs.head:
        yield xs.tail
    else:
        for ys in del_(xs.tail, x):
            yield Cons(xs.head, ys)
def solution(n=8):
    if n == 0:
        yield Nil
    else:
        for others in solution(n - 1):
            x = 9 - n
            for y in range(1, 9):
                if noattack(x, y, others):
                    yield Cons((x, y), others)
Ejemplo n.º 24
0
def depth_first_search(akumulator_path, node, remaining_depth):
    akumulator_path1 = Cons(node, akumulator_path)
    if is_goal(node):
        yield akumulator_path1
    if remaining_depth > 0:
        for node1 in move_anyY(node):
            for path in depth_first_search(akumulator_path1, node1,
                                           remaining_depth - 1):
                yield path
Ejemplo n.º 25
0
def get_example_classes(examples):
    if examples == Nil:
        return Nil
    example = examples.head
    class_, _ = example
    other_classes = get_example_classes(examples.tail)
    if not member(class_, other_classes):
        return Cons(class_, other_classes)
    return other_classes
Ejemplo n.º 26
0
def induce_trees(att, vals, rest_atts, exs):
    if vals is Nil:
        # no attributes, no subtrees
        return Nil
    val1 = vals.head
    example_subset = attval_subset(att, val1, exs)
    tree1 = induce_tree(rest_atts, example_subset)
    trees = induce_trees(att, vals.tail, rest_atts, exs)
    return Cons((val1, tree1), trees)
def path1(a, akumulator_cesty, graph, depth):
    if akumulator_cesty.head == a:
        yield akumulator_cesty
    else:
        y = akumulator_cesty.head
        for x in adjacent_anyX(y, graph):
            if not member(x, akumulator_cesty):
                for cesta in path1(a, Cons(x, akumulator_cesty), graph,
                                   depth + 1):
                    yield cesta
Ejemplo n.º 28
0
def filter_examples(examples, class_, attribute, value):
    if examples == Nil:
        return Nil
    example = examples.head
    class1, obj = example
    other_examples = filter_examples(examples.tail, class_, attribute, value)
    if class_ is None or class_ == class1:
        if member((attribute, value), obj):
            return Cons(example, other_examples)
    return other_examples
def move_anyYC(numbers):
    if numbers == Nil:
        return
    xb, yb = numbers.head
    if xb > 1:  # pohyb mezery doleva
        xl = xb - 1
        new_tail = replace((xl, yb), (xb, yb), numbers.tail)
        yield (Cons((xl, yb), new_tail), 1)
    if xb < 3:  # pohyb mezery doprava
        xr = xb + 1
        new_tail = replace((xr, yb), (xb, yb), numbers.tail)
        yield (Cons((xr, yb), new_tail), 1)
    if yb > 1:  # pohyb mezery dolu
        yd = yb - 1
        new_tail = replace((xb, yd), (xb, yb), numbers.tail)
        yield (Cons((xb, yd), new_tail), 1)
    if yb < 3:  # pohyb mezery nahoru
        yu = yb + 1
        new_tail = replace((xb, yu), (xb, yb), numbers.tail)
        yield (Cons((xb, yu), new_tail), 1)
def path1(a, akumulator_cesty, akumulator_ceny, graph, depth):
    if akumulator_cesty.head == a:
        yield (akumulator_cesty, akumulator_ceny)
    else:
        y = akumulator_cesty.head
        for x, cenaXY in adjacent_anyXcenaXY(y, graph):
            if not member(x, akumulator_cesty):
                for cesta, cena in path1(a, Cons(x, akumulator_cesty),
                                         akumulator_ceny + cenaXY, graph,
                                         depth + 1):
                    yield (cesta, cena)