예제 #1
0
def find_idastar_route(source, target):
    def g(node):
        return node.path_cost

    def h(node):
        junction = roads[node.state]
        target = roads[goal.state]
        x = compute_distance(junction.lat, junction.lon, target.lat,
                             target.lon) / 110
        return x

    roads = load()
    #roads = load('israel.csv',0,50)
    start = Node(roads[source])
    goal = Node(roads[target])
    p = Problem(start, goal, roads)
    limit_cost = h(start)
    path = [start.state]
    while (True):
        import time
        t = time.time
        next = ida_star(path, p, start, limit_cost, f=lambda n: g(n) + h(n))
        if next == -1:
            print(t - time.time)
            return path[::-1]
        if next == math.inf:
            return None
        limit_cost = next
예제 #2
0
def graph_search_for_vis(problem):
    """Search through the successors of a problem to find a goal.
    The argument frontier should be an empty queue.
    If two paths reach a state, only use the first one. [Figure 3.7]"""
    # we use these two variables at the time of visualisations
    iterations = 0
    all_node_colors = []
    node_colors = {
        k: 'white'
        for k in set(problem.reachable_positions(problem.initial))
    }

    frontier = [(Node(problem.initial))]
    explored = set()

    # modify the color of frontier nodes to orange
    node_colors[Node(problem.initial).state] = "orange"
    iterations += 1
    all_node_colors.append(dict(node_colors))

    while frontier:
        # Popping first node of stack
        node = frontier.pop()

        # modify the currently searching node to red
        node_colors[node.state] = "red"
        iterations += 1
        all_node_colors.append(dict(node_colors))

        if problem.goal_test([node.state for node in node.path()]):
            # modify goal node to green after reaching the goal
            node_colors[node.state] = "green"
            iterations += 1
            all_node_colors.append(dict(node_colors))
            return (iterations, all_node_colors, node)

        explored.add(node.state)
        frontier.extend(
            child for child in node.expand(problem)
            if child.state not in explored and child not in frontier)

        for n in frontier:
            # modify the color of frontier nodes to orange
            node_colors[n.state] = "orange"
            iterations += 1
            all_node_colors.append(dict(node_colors))

        # modify the color of explored nodes to gray
        node_colors[node.state] = "gray"
        iterations += 1
        all_node_colors.append(dict(node_colors))

    return None
예제 #3
0
def best_first_graph_search_for_vis(problem, f):
    iterations = 0
    all_node_colors = []
    node_colors = {k: 'white' for k in set(problem.reachable_positions(problem.initial))}

    node = Node(problem.initial)

    node_colors[node.state] = "red"
    iterations += 1
    all_node_colors.append(dict(node_colors))

    frontier = PriorityQueue()
    frontier.append(node, f(node))

    node_colors[node.state] = "orange"
    iterations += 1
    all_node_colors.append(dict(node_colors))

    explored = set()
    while not frontier.isEmpty():
        node = frontier.pop()

        node_colors[node.state] = "red"
        iterations += 1
        all_node_colors.append(dict(node_colors))

        if problem.goal_test([node.state for node in node.path()]):
            node_colors[node.state] = "green"
            iterations += 1
            all_node_colors.append(dict(node_colors))
            return (iterations, all_node_colors, node)

        explored.add(node.state)
        for child in node.expand(problem):
            if child.state not in explored and child not in frontier:
                frontier.append(child, f(child))
                node_colors[child.state] = "orange"
                iterations += 1
                all_node_colors.append(dict(node_colors))
            elif child in frontier:
                incumbent = frontier[child]
                if f(child) < f(incumbent):
                    del frontier[incumbent]
                    frontier.append(child, f(child))
                    node_colors[child.state] = "orange"
                    iterations += 1
                    all_node_colors.append(dict(node_colors))

        node_colors[node.state] = "gray"
        iterations += 1
        all_node_colors.append(dict(node_colors))
    return iterations, all_node_colors, node
예제 #4
0
def tree_depth_search_for_vis(problem, iterLim=100000):
    """Search through the successors of a problem to find a goal.
    The argument frontier should be an empty queue.
    Don't worry about repeated paths to a state. [Figure 3.7]"""

    # we use these two variables at the time of visualisations
    iterations = 0
    all_node_colors = []
    node_colors = {
        k: 'white'
        for k in set(problem.reachable_positions(problem.initial))
    }

    # Adding first node to the stack
    frontier = [Node(problem.initial)]

    node_colors[Node(problem.initial).state] = "orange"
    iterations += 1
    all_node_colors.append(dict(node_colors))

    explored = []
    while len(frontier) > 0 and iterations < iterLim:
        # Popping first node of stack
        node = frontier.pop()

        explored.append(node.state)
        # modify the currently searching node to red
        node_colors[node.state] = "red"
        iterations += 1
        all_node_colors.append(dict(node_colors))

        if problem.goal_test([node.state for node in node.path()]):
            # modify goal node to green after reaching the goal
            node_colors[node.state] = "green"
            iterations += 1
            all_node_colors.append(dict(node_colors))
            return (iterations, all_node_colors, node)

        for n in node.expand(problem):
            if n.state not in explored and n not in frontier:
                frontier.append(n)
                node_colors[n.state] = "orange"
                iterations += 1
                all_node_colors.append(dict(node_colors))

        # modify the color of explored nodes to gray
        node_colors[node.state] = "gray"
        iterations += 1
        all_node_colors.append(dict(node_colors))

    return (iterations, all_node_colors, node)
예제 #5
0
def find_astar_route(source, target):
    'call function to find path, and return list of indices'

    def g(node):
        return node.path_cost

    def h(node):
        junction = roads[node.state]
        target = roads[goal.state]
        x = compute_distance(junction.lat, junction.lon, target.lat,
                             target.lon) / 110
        return x

    roads = load()
    start = Node(roads[source])
    goal = Node(roads[target])
    p = Problem(start, goal, roads)
    return best_first_graph_search(p, f=lambda n: h(n) + g(n))
예제 #6
0
def find_ucs_rout(source, target):
    'call function to find path, and return list of indices'

    def g(node):
        return node.path_cost

    '''
    # roads = load()
    import csv
    from itertools import islice
    roads = load('israel.csv', 0, 300000)
    # roads = load()
    problems = []
    total_h = []
    junctions = roads.junctions()
    with tools.dbopen('problems.csv', 'rt') as f:
        it = islice(f, 0, sys.maxsize)
        for row in csv.reader(it):
            if len(row) != 0:
                start = Node(junctions[int(row[0])])
                goal = Node(junctions[int(row[1])])
                problem = Problem(start, goal, roads)
                problems.append(problem)
    paths = []
    times = []
    for p in problems:
        t = time.time()
        paths.append(best_first_graph_search(p, f=g))
        paths.append('\n')
        times.append(time.time() - t)
    file = open('ucs.txt', 'w')
    file.write(' '.join(str(j) for j in paths))
    file2 = open('ucsTimes.txt', 'w')
    file2.write(' '.join(str(j) for j in times))
    '''
    roads = load()
    start = Node(roads[source])
    goal = Node(roads[target])
    p = Problem(start, goal, roads)
    return best_first_graph_search(p, f=g)
예제 #7
0
def best_first_graph_search(problem, f):
    node = Node(problem.start.state)
    frontier = tools.PriorityQueue(f)  # Priority Queue
    frontier.append(node)
    log = []
    closed_list = set()
    while frontier:
        print(f'size of closed list:{len(closed_list)}')
        node = frontier.pop()
        if problem.is_goal(node.state):
            return node.path()
        closed_list.add(node)
        frontier_func = None
        for child in node.expand(problem):
            for n in frontier.heap:
                if n[1].state.index == child.state.index:
                    frontier_func = n[0]
            if child not in closed_list and child not in frontier:
                frontier.append(child)

            elif child in frontier and f(child) < frontier_func:
                del frontier[child]
                frontier.append(child)
    return []