Beispiel #1
0
def a_star(start, goals, walls, heuristic, cornered=True, verbose=False):
    #frontier will look like [f(path), cost(path), [path], ...]
    frontier = []
    heappush(frontier, (0, 0, [start]))  #heap prioritized by f()

    while frontier:
        path_tup = heappop(frontier)  #select first tuple from frontier

        # last_vertex is last elem in path from current path tuple
        last_vertex = path_tup[2][-1]
        if is_goal(last_vertex, goals):
            if verbose:  #if requested, print solution path
                for matrices in path_tup[2]:
                    for line in matrices:
                        print(line)
                    print()
            print("cost\t:", path_tup[1])  #print cost for convinience
            print("length\t:",
                  len(path_tup[2]) - 1)  #print length for convinience
            return path_tup
        for next_tup in neighbors(last_vertex,
                                  with_cost=True):  #for each neighb

            #calculate future cost
            future_cost = heuristic(next_tup[1], goals, corn=cornered)
            if next_tup[1] in path_tup[2]:  #avoid cycles
                continue
            new_cost = path_tup[1] + next_tup[0]  #add cost of new node
            combined_cost = new_cost + future_cost  #calculate f()
            new_tup = (combined_cost, new_cost, path_tup[2] + [next_tup[1]])
            heappush(frontier, new_tup)  #prioritize by f()
    return None
Beispiel #2
0
def Astar_stuck2(start,
                 goals,
                 walls,
                 heuristic,
                 box_ls,
                 goal_ls,
                 verbose=False):

    start_perm = to_perm(start)  #find perm of start matrix
    max_pot, length, type_count = get_info(start_perm)  #get level info

    start_int = rank(start_perm, max_pot, length, type_count)  #get rank index

    #frontier will look like [f(path), cost(path), [path], [# boxes in borders]]
    frontier = []  #initiliaze frontier
    heappush(frontier,
             (0, 0, [start_int], box_ls))  #add (0, cost, index) tuple

    is_visited = [False for i in range(max_pot)]  #initiliaze visited array
    is_visited[start_int] = True  #mark start as visited

    while frontier:
        path_tup = heappop(frontier)  #take first path tuple
        last_vertex = path_tup[2][-1]  #take last element of path

        #unrank to multiset, and convert to matrix to check for goal and neighbs
        last_permut = unrank(last_vertex, max_pot, length, type_count)
        last_matrix = to_matrix(last_permut, walls)

        if is_goal(last_matrix, goals):  #if boxes match goals, finish
            if verbose:  #if asked print solution path
                print(path_tup[2])
            print("cost\t:", path_tup[1])  #print cost for convinience
            print("length\t:", len(path_tup[2]) - 1)  #print length for conv
            return path_tup

        for next_tup in neighbors(last_matrix, with_cost=True):

            ls = path_tup[3]  #list of number of boxes in border
            check, ls = is_stuck(goal_ls, ls[:], next_tup[-1])
            if check:  #if in a stuck state
                future_cost = inf
            else:  #if not in a stuck state
                future_cost = heuristic(next_tup[1], goals, corn=False)

            #convert from matrix to permutation, then to index
            temp_perm = to_perm(next_tup[1])
            int_next = rank(temp_perm, max_pot, length, type_count)

            if is_visited[int_next]:  #if already visited skip
                continue

            new_cost = path_tup[1] + next_tup[0]  #add cost of new vertex
            combined_cost = new_cost + future_cost  #calculate f()
            new_tup = (combined_cost, new_cost, path_tup[2] + [int_next], ls)
            is_visited[int_next] = True  #set new int to visited
            heappush(frontier, new_tup)  #prioritize by f()
    return None
Beispiel #3
0
def b_bound2(start,
             goals,
             walls,
             heuristic,
             bnd=inf,
             cornered=True,
             verbose=False):

    soln = []
    bound = bnd
    cnt = 0

    start_perm = to_perm(start)
    max_pot, length, type_count = get_info(start_perm)
    start_int = rank(start_perm, max_pot, length, type_count)

    frontier = []
    frontier.append([start_int])

    while frontier:

        path = frontier.pop()
        last_int = path[-1]

        last_permut = unrank(last_int, max_pot, length, type_count)
        last_matrix = to_matrix(last_permut, walls)

        if is_goal(last_matrix, goals):
            if verbose:
                print(path)
            bound = len(path) - 1
            soln = path[:]
        if len(path) - 1 >= bound:
            continue
        for next_matrix, _ in neighbors(last_matrix):

            temp_perm = to_perm(next_matrix)
            int_next = rank(temp_perm, max_pot, length, type_count)

            if int_next in path:
                continue

            new_path = path + [int_next]
            frontier.append(new_path)
    print("length", len(soln) - 1)
    return soln
Beispiel #4
0
def best_fs2(start, goals, walls, heuristic, cornered=True, verbose=False):

    start_perm = to_perm(start)      #find permut of start matrix
    max_pot, length, type_count = get_info(start_perm) #get level info

    start_int = rank(start_perm, max_pot, length, type_count)   #get rank index

    frontier = []                                   #initialize frontier
    heappush(frontier, (0,[start_int]))             #add (0, int) tuple

    is_visited = [False for i in range(max_pot)]    #initialize visited array
    is_visited[start_int] = True                    #mark start as visited

    while frontier:
        path_tup = heappop(frontier)        #take first path tuple from frontier
        last_vertex = path_tup[1][-1]       #take last element of path in tuple

        #unrank to multiset, and convert to matrix to check for goal and neighbs
        last_permut = unrank(last_vertex, max_pot, length, type_count)
        last_matrix = to_matrix(last_permut, walls)

        if is_goal(last_matrix, goals):     #if boxes match goals, return soln
            if verbose:                     #if asked, print solution path
                print(path_tup[1])
            print("length\t:", len(path_tup[1])-1)      #print length of soln
            return path_tup
        for next_matrix, _ in neighbors(last_matrix):   #for each neighbor

            #calculate future cost
            next_cost = heuristic(next_matrix, goals, corn=True)

            #convert from matrix to permutation, then to index
            temp_perm = to_perm(next_matrix)            #conv to permutation
            int_next = rank(temp_perm, max_pot, length, type_count) #get index

            if is_visited[int_next]:                #if already visited skip
                continue

            new_tup = (next_cost, path_tup[1] + [int_next])
            is_visited[int_next] = True
            heappush(frontier, new_tup)
    return None
Beispiel #5
0
def best_fs(start, goals, walls, heuristic, cornered=True, verbose=False):
    #frontier will look like [f(path), cost(path), [path], ...]
    frontier = []                               #initialize frontier
    heappush(frontier, (0, [start]))            #push tuple (0, start_matrix)
    while frontier:
        path_tup = heappop(frontier)            #take first tuple in frontier
        last_vertex = path_tup[1][-1]           #set equal to matrix in tuple
        if is_goal(last_vertex, goals):         #if boxes match goals, return
            if verbose:
                print(path_tup[1])
            print("length\t:", len(path_tup[1])-1)      #print soln length
            return path_tup
        for next_matrix, _ in neighbors(last_vertex):   #for each neighbor

            #calculate future cost
            next_cost = heuristic(next_matrix, goals, corn=cornered)
            if next_matrix in path_tup[1]:              #don't run into a cycle
                continue
            new_tup = (next_cost, path_tup[1] + [next_matrix])
            heappush(frontier, new_tup)       #prioritize by future cost
    return None
Beispiel #6
0
def Astar_stuck(start,
                goals,
                walls,
                heuristic,
                box_ls,
                goal_ls,
                verbose=False):
    frontier = []
    heappush(frontier, (0, 0, [start], box_ls))

    while frontier:
        path_tup = heappop(frontier)
        last_matrix = path_tup[2][-1]

        if is_goal(last_matrix, goals):
            if verbose:
                for matrices in path_tup[2]:
                    for line in matrices:
                        print(line)
                    print()
            print("cost\t:", path_tup[1])
            print("length\t:", len(path_tup[2]) - 1)
            return path_tup[2]
        for next_tup in neighbors(last_matrix, with_cost=True):
            ls = path_tup[3]
            check, ls = is_stuck(goal_ls, ls[:], next_tup[-1])
            if check:  #if in a stuck state
                future_cost = inf
            else:  #if not in a stuck state
                future_cost = heuristic(next_tup[1], goals, corn=False)

            if next_tup[1] in path_tup[2]:
                continue
            new_cost = path_tup[1] + next_tup[0]
            combined_cost = new_cost + future_cost
            new_tup = (combined_cost, new_cost, path_tup[2] + [next_tup[1]],
                       ls)
            heappush(frontier, new_tup)
    return None
Beispiel #7
0
def b_bound(start,
            goals,
            walls,
            heuristic,
            bnd=inf,
            cornered=True,
            verbose=False):

    soln = []
    bound = bnd
    cnt = 0

    frontier = []
    frontier.append([start])

    while frontier:
        path = frontier.pop()
        last_matrix = path[-1]
        if is_goal(last_matrix, goals):
            cnt = 1
            if verbose:
                for matrices in path:
                    for line in matrices:
                        print(line)
                    print()
            bound = len(path) - 1
            soln = path[:]
        if len(path) - 1 >= bound:
            continue
        for next_matrix, _ in neighbors(last_matrix):
            if next_matrix in path:
                continue
            new_path = path + [next_matrix]
            frontier.append(new_path)
    print("length\t:", len(soln) - 1)
    return soln