Esempio n. 1
0
def main():

    # Task 1
    map = Map_Obj(1)
    start_state = (map, map.get_start_pos()[0], map.get_start_pos()[1])
    a_star = AStar(start_state, walking_distance, generate_adjacent_states,
                   goal_evaluate)
    node_path = a_star.run()

    pos_path = []
    for node in node_path:
        pos_path.append([node.state[1], node.state[2]])
    print('Positions in the path of task 1:')
    print(pos_path)

    visualise_path_map(map, pos_path)

    # Task 2
    map = Map_Obj(2)
    start_state = (map, map.get_start_pos()[0], map.get_start_pos()[1])
    a_star = AStar(start_state, walking_distance, generate_adjacent_states,
                   goal_evaluate)
    node_path = a_star.run()

    pos_path = []
    for node in node_path:
        pos_path.append([node.state[1], node.state[2]])
    print('Positions in the path of task 2:')
    print(pos_path)

    visualise_path_map(map, pos_path)
Esempio n. 2
0
class main():
    map = Map_Obj(4)  #Oppretter map-objekt(oppgavenr)
    astar = astar()  #Oppretter astar-objekt

    X = astar.findPath(
        map)  #Finner X, maalnoden med foreldrenodene som gir beste løsning
    astar.print_path(map, X)  #Printer ruten på kartet

    map.show_map()  #Viser kartet med rute
Esempio n. 3
0
    def __init__(self, task=1):
        self.state_dictionary = {}

        self.open = [
        ]  # Sorted by ascending f values, nodes with lot of promise popped early, contains unexpanded nodes
        self.closed = []  # no order, contains expanded nodes
        self.map = Map_Obj(task)  # the map
        # creates a new state with coordinates of the goal position from map
        self.goal = State(tuple(self.map.get_goal_pos()))
        # creates a new state as current state with the coordinates of the start position from map
        self.current_state = State(tuple(self.map.get_start_pos()))

        self.root_node = Node(self.current_state)  # root of the search tree
        self.root_node.g = 0
        self.heuretic_evaluation(self.root_node)
        # all of the f, g, h variables are taken from the appendix added to the task.
        self.root_node.f = self.root_node.g + self.root_node.h

        # adding the root node to the open list
        self.open.append(self.root_node)
        self.solution_node = None  # Hopefully the goal
Esempio n. 4
0
def main():
    map_obj = Map_Obj(task=5)

    start_state = (map_obj, *map_obj.get_start_pos())
    heuristic_func = samf_chase_heuristic

    output = a_star(start_state,
                    heuristic_func,
                    successors_gen,
                    goal_predicate,
                    cost_func=cost_func)
    for coords in output:
        map_obj.set_cell_value(coords, "☺", str_map=True)

    map_obj.show_map()
Esempio n. 5
0
def main():
    for task in range(
            1, 5):  # runs through tasks 1 - 4, continues after key input
        map_obj = Map_Obj(task=task)

        start_state = (map_obj, *map_obj.get_start_pos())
        heuristic_func = manhattan

        output = a_star(start_state,
                        heuristic_func,
                        successors_gen,
                        goal_predicate,
                        cost_func=cost_func)
        for coords in output:
            map_obj.set_cell_value(coords, "☺", str_map=True)

        map_obj.show_map()
        input()
Esempio n. 6
0
from Strucutres import Astar

"""
To run the code:

1. Declare an instance of Astar by providing the path to the map, start and ending points.
Example: task1 = Astar('Samfundet_map_1.csv', [27,18], [40, 32])

2. To see the map, run the show_map method by providing the path
Example: task1_map.show_map(task1.path)
"""

# Task 1

task1 = Astar('Samfundet_map_1.csv', [27,18], [40, 32])
task1_map = Map_Obj(1)
task1_map.show_map(task1.path)

# Task 2

task2 = Astar('Samfundet_map_1.csv', [8, 5], [40, 32])
task2_map = Map_Obj(2)
task2_map.show_map(task2.path)

# Task 3

task3 = Astar('Samfundet_map_2.csv', [28, 32], [6, 32])
task3_map = Map_Obj(3)
task3_map.show_map(task3.path)

# Task 4
Esempio n. 7
0
                    add = False

            #if we do not find a better f in opened, add the new node to the list
            if add == True:
                opened.append(new_node)

        #for task 5 we have a moving goal, update the goal position by calling tick() every iteration
        if moving_goal == True:
            map_obj.goal_pos = map_obj.tick()

    return None  #did not find any path


#%%
#Task 1
map1 = Map_Obj(1)
path = aStar(map1)
print("Task 1: The shortest path is", path, "\n")
#%%

#%%
##Vizualize
for i in path[1:]:
    map1.replace_map_values(i, 5, map1.goal_pos)
map1.show_map()
#%%
#Task 2
map2 = Map_Obj(2)
path2 = aStar(map2)
print("Task 2: The shortest path is", path2, "\n")
from Map import Map_Obj
from astar import *

# Iterates through task 1,2,3 and 4 and displays all shortest paths
for i in [1, 2, 3, 4]:
    map = Map_Obj(task=i)
    end_node = aStar(map)
    path = trace_path(end_node)
    finalpath(map, path)
Esempio n. 9
0
class BestSearchFirst:
    def __init__(self, task=1):
        self.state_dictionary = {}

        self.open = [
        ]  # Sorted by ascending f values, nodes with lot of promise popped early, contains unexpanded nodes
        self.closed = []  # no order, contains expanded nodes
        self.map = Map_Obj(task)  # the map
        # creates a new state with coordinates of the goal position from map
        self.goal = State(tuple(self.map.get_goal_pos()))
        # creates a new state as current state with the coordinates of the start position from map
        self.current_state = State(tuple(self.map.get_start_pos()))

        self.root_node = Node(self.current_state)  # root of the search tree
        self.root_node.g = 0
        self.heuretic_evaluation(self.root_node)
        # all of the f, g, h variables are taken from the appendix added to the task.
        self.root_node.f = self.root_node.g + self.root_node.h

        # adding the root node to the open list
        self.open.append(self.root_node)
        self.solution_node = None  # Hopefully the goal

    def arc_cost(self, p, c):
        """ This calculates the arc cost between two nodes (parent and child) using the cost of the child node (the one you are moving to)"""
        return self.map.get_cell_value(c.state.coordinates)

    def propagate_path_improvements(self, p):
        """ Goes through the children of a parent node and updates the cost of
        going to the child from the parent if it less than the current cost of going to the child.
        This is done recursively """
        for c in p.childs:
            if p.g + self.arc_cost(p, c) < c.g:
                c.parent = p
                c.g = p.g + self.arc_cost(p, c)
                c.f = c.g + c.h
                self.propagate_path_improvements(c)

    def agenda_loop(self):
        """ Agenda loop """
        it = 0
        solution = False
        while not solution:
            it += 1
            if not self.open:
                # in these tasks with obvious solutions something must be wrong with the implementation
                print("Something definitely went wrong!")
                return False
            x = self.open.pop()
            self.closed.append(x)
            solution_state = None
            if self.check_solution(x):
                solution = True
                self.solution_node = x
            else:
                # generating the successor nodes, expanding x
                successors = self.generate_successor_nodes(x)

                for s in successors:  # go through each of these children
                    in_open = False
                    in_closed = False
                    if s in self.open:
                        in_open = True
                        s_star = self.open[self.open.index(s)]
                        if s.state == s_star.state:
                            s = s_star
                    elif s in self.closed:
                        in_closed = True
                        s_star = self.closed[self.closed.index(s)]
                        if s.state == s_star.state:
                            s = s_star
                    # s is a child of x even though x might not be the best parent
                    x.childs.append(s)
                    if not in_open and not in_closed:
                        # attach the new succesor (it just got explored) with parent x
                        self.attach_and_eval(s, x)
                        self.open.append(s)  # we will expand this node later
                        self.open = sorted(self.open,
                                           key=lambda val: val.f,
                                           reverse=True)
                        # sorting open list in descending based on f value, pop function takes from the back of the list
                    elif x.g + self.arc_cost(
                            x, s) < s.g:  # found cheaper path to s
                        # attaches s to a new parent x
                        self.attach_and_eval(s, x)
                        if in_closed:
                            # propagate the improved path to s through all of s children (and their children)
                            self.propagate_path_improvements(s)
        return solution, solution_state

    def attach_and_eval(self, c, p):
        """ Simply attaches a child node to a node that is now considered its best parent (so far) """
        c.parent = p
        c.g = p.g + self.arc_cost(p, c)
        self.heuretic_evaluation(c)
        c.f = c.g + c.h

    # This should be changed, need to have a dictionary from coordinates to State
    def generate_successor_nodes(self, node):
        """ Given a node in the search tree this generates all possible succesor states to the node's state """
        directions = [
            (1, 0), (-1, 0), (0, 1), (0, -1)
        ]  # directions you are allowed to move in (up, right, left, down)
        successor_nodes = []  # all the successors

        for i in directions:
            x = node.state.coordinates[0] + i[0]
            y = node.state.coordinates[1] + i[1]
            if tuple((x, y)) in self.state_dictionary:
                successor_nodes.append(self.state_dictionary.get(tuple(
                    (x, y))))
            else:
                value = self.map.get_cell_value((x, y))
                if value != -1:  # if it isn't a wall we will add the new node and state to the successors
                    state = State((x, y))
                    child = Node(state)
                    self.state_dictionary[tuple((x, y))] = child
                    self.heuretic_evaluation(child)
                    child.g = node.g + self.arc_cost(node, child)
                    child.f = child.g + child.h
                    successor_nodes.append(child)
        return successor_nodes

    def heuretic_evaluation(self, node):
        """ Gives a heuretic evaluation of the distance to the goal using manhattan distance  """
        heuretic = abs(self.goal.coordinates[0] - node.state.coordinates[0]
                       ) + abs(self.goal.coordinates[1] -
                               node.state.coordinates[1])
        node.h = heuretic
        return heuretic

    def check_solution(self, node):
        """ Compares the state of the nodes to the goal state (by comparing coordinates) """
        return node.state == self.goal

    def print_solution(self):
        """ Finds the given solution path by going through the parent of the solution node and going backwards to the root node, then shows the solution using the map """
        child = self.solution_node
        parent = self.solution_node.parent
        solution_list = [self.solution_node.state.coordinates]

        while parent != None:
            solution_list.append(parent.state.coordinates)
            child = parent
            parent = child.parent
        self.map.show_solution(solution_list)
Esempio n. 10
0
def best_first_search(task):

    #set open list and closed list empty
    openList = []
    closedList = []

    #read Map
    samf = Map_Obj(task)
    start, goal, endGoal, path = samf.fill_critical_positions(task)
    data, data_str = samf.read_map(path)

    #generate initial node
    initialNode = searchNode(start)  #assign state as current position of node
    initialNode.h = manhattan_distance(start, goal)
    initialNode.f = initialNode.g + initialNode.h

    #list of nodes visited
    visitedNodes = []  #updated each time a node is added to OpenList

    #push inital node to openList
    heapq.heappush(openList, initialNode)
    visitedNodes.append(initialNode)

    node = initialNode
    #Agenda Loop begins
    while node.state != goal:

        #check if empty list = failure
        if not openList:
            return 'no solution'

        node = heapq.heappop(openList)  #pop node from openList

        closedList.append(node)  #push note to closedList

        #check if success
        if node.state == goal:
            data_str = back_iterate(node, data_str, start)
            samf.show_map(data_str)

            print('total cost: ', node.f)
            return "success!"

        #generate childnodes/successors
        succ = generate_successors(node, data)

        for s in succ:

            #if s already exists:
            for v in visitedNodes:  #search for visited nodes
                if s.state == v.state:
                    s = v  #assign preExisting node to s

            #add s to kids list of current node
            node.kids.append(s)

            #if s is a new node
            if (s not in openList) and (s not in closedList):
                attach_and_eval(s, node, data,
                                goal)  #update parent-child realtionship
                heapq.heappush(openList, s)  #insert s in openList
                visitedNodes.append(s)  #s is now visited

            #if we find a cheaper path to s
            elif node.g + arc_cost(node, s, data) < s.g:
                attach_and_eval(s, node, data, goal)
                if s in closedList:
                    propagate_path_improvement(s, data)
Esempio n. 11
0
        # Generate the successors of the current node
        successors = generateAllSuccessors(current)
        for successor in successors:

            # If a successor is in the closed list, the optimal path to it has already been found
            if successor in CLOSED:
                continue

            # If a successor is in the open list, check if the path through the current node is shorter
            elif successor in OPEN:
                if current.g + arcCost(successor.pos) < successor.g:
                    attachAndEval(successor, current)

            # The node was expanded for the first now
            else:
                attachAndEval(successor, current)
                OPEN.append(successor)

    raise ValueError("Could not find a path")


map = Map_Obj(3)
path = aStar(map.start_pos, map.goal_pos)

# Draw the path
for i in range(1, len(path) - 1):
    map.set_cell_value(path[i], "  ", str_map=True)

map.show_map()
Esempio n. 12
0

def manhattan(start: list, end: list):
    return np.abs(start[0] - end[0]) + np.abs(start[1] - end[1])


def attach_and_eval(child: Node, parent: Node, target: list):
    child.parent = parent
    child.g = parent.g + mp.get_cell_value(child.position)
    child.h = manhattan(child.position, target)
    child.f = child.g + child.h


def propagate_path_improvements(parent):
    for kid in parent.children:
        if (parent.g + mp.get_cell_value(kid.position) < kid.g):
            kid.parent = parent
            kid.g = parent.g + mp.get_cell_value(kid.position)
            kid.f = kid.g + kid.h
            propagate_path_improvements(kid)


mp = Map_Obj(task=4)

result_node, succ = best_first_search(mp.start_pos, mp.goal_pos)
if succ is True:
    while result_node is not mp.start_pos and result_node is not None:
        print(result_node.position)
        mp.set_cell_value(result_node.position, " G ")
        result_node = result_node.parent
    mp.show_map()
Esempio n. 13
0
def best_friend_search():
    open_nodes = []
    closed = []
    map_obj = Map_Obj(task=4)
    endpos = map_obj.get_goal_pos()

    start = Node(None, map_obj.get_start_pos(), endpos, 0)
    start.g = 0
    open_nodes.insert(0, start)

    i = 0

    while True:
        current = open_nodes.pop(0)

        if current.pos == endpos:
            print("Finished after {} iterations".format(i))
            endnode = current
            while current.parent is not None:
                map_obj.save_frame(i)
                print(current.pos)
                current = current.parent
                map_obj.set_cell_value(current.pos, 'p')
                i += 1
            map_obj.save_frame(i)
            return endnode, map_obj

        for pos in current.get_adjacent():
            if pos in [node.pos for node in closed]:
                continue

            if map_obj.get_cell_value(pos) == -1:
                continue

            cost = map_obj.get_cell_value(pos)
            if pos in [node.pos for node in open_nodes]:
                for node in open_nodes:
                    if node.pos == pos:
                        node.propogate(current)
                        break
                continue

            neighbour = Node(current, pos, endpos, current.g + cost)

            current.children.append(neighbour)
            priority_insert(open_nodes, neighbour)
            map_obj.set_cell_value(pos, 'o')

        closed.append(current)
        map_obj.set_cell_value(current.pos, 0)
        if len(open_nodes) == 0:
            print("No path found")
            return None, map_obj

        #if not i%10:
        map_obj.save_frame(i)
        i += 1
Esempio n. 14
0
    """
    # Extracting the start node of the current task
    start = map_obj.get_start_pos()
    # Extracting the goal node of the current task
    goal = map_obj.get_goal_pos()

    # Calling the A* algorithm for getting the parent dictionary and the goal
    parent, goal = astar(start, goal, map_obj, moving)

    # Starting at the goal node to draw the path
    node = (goal[0], goal[1])
    # Continuing until we hit start
    while parent[node] != (start[0], start[1]):
        # Coloring the path
        map_obj.set_cell_value(parent[node], ";")
        # Traversing up the search tree
        node = parent[node]
    
    # Showing the map with the colored path
    map_obj.show_map()

# Main
if __name__ == "__main__":
    # Getting the task number from the command line
    task = int(sys.argv[1])
    # Setting the moving variable as true or false
    moving = task == 5
    # Getting the map for the current task
    map_obj = Map_Obj(task=task)
    # Showing the map with the colored shortest path calculated with the A* algorithm
    findPath(map_obj, moving)
Esempio n. 15
0
import sys
import math
from Map import Map_Obj

task5 = False

#-----------------------------------------------------------------------------------
#                           INITIALIZATION OF THE MAP
#-----------------------------------------------------------------------------------

# If you run windows, uncomment the line which represents the task you want to run
# For Task 5, uncomment both lines under the '# Task 5' comment

# Task 1
map = Map_Obj(1)

# Task 2
#map = Map_Obj(2)

# Task 3
#map = Map_Obj(3)

# Task 4
#map = Map_Obj(4)

# Task 5
#map = Map_Obj(5)
#task5 = True

#-----------------------------------------------------------------------------------
#                   IMPLEMENTATION OF A* AND OTHER FUNCTIONS