Exemplo n.º 1
0
def search_shortest_dist(graph, start, end):
    # Check if the graph is valid. If it is not, return error string.
    if graph is None: return 'graph is null: Function Failed'
    # Declare a queue and enqueue the starting node.
    q = priority_queue.PriorityQueue()
    q.put(start, 0)
    curr_cost = {}
    curr_cost[start] = 0

    # Declare a string to count factory stops and int to count miles traversed
    # Set it to blank space.
    solution = ''
    miles_traveled = 0
    nodes_expanded = 0
    # While the q is not empty, get the next node on the queue
    # and check for the goal. If at the goal, return the
    # string of stops and the nodes expanded. Else,
    # expand the node to the queue.
    prev = None
    while not q.empty():
        cur = q.get()
        if prev == cur:
            prev = cur
            cur = q.get()
        nodes_expanded = nodes_expanded + 1
        end[1].add_comp(cur)
        end[2].add_comp(cur)
        end[3].add_comp(cur)
        end[4].add_comp(cur)
        end[5].add_comp(cur)
        if prev is not None and prev != cur:
            miles_traveled = miles_traveled + graph.cost(prev, cur)
        if cur != start:
            solution = solution + cur
        #the widgets are complete, return
        if end[1].done and end[2].done and end[3].done and end[4].done and end[
                5].done:
            return (solution, miles_traveled, nodes_expanded)
        #add the neighbors to the queue in order of their cost plus current cost
        for next in graph.neighbors(cur):
            update_cost = curr_cost[cur] + graph.cost(cur, next)
            #if the nighbor isn't in the queue or the new cost is less than it's original cost
            if next not in curr_cost or update_cost < curr_cost[next]:
                curr_cost[next] = update_cost
            prior = update_cost + dist_hist(graph.weights[cur], end, next, 5)
            q.put(next, prior)
        prev = cur
    # Return the failed because solution was not found
    solution = 'FAILED'
    miles_traveled = -1
    return (solution, miles_traveled, nodes_expanded)
Exemplo n.º 2
0
def bellman(graph, start, maxLen):
    initMap = {start: 0}
    dist = [initMap]

    for k in range(1, maxLen + 1):
        prevMap = dist[k - 1]
        currMap = {}
        for y in prevMap:
            for x in graph.parseNout(y):
                if x not in currMap or currMap[x] > prevMap[y] + graph.cost(y, x):
                    currMap[x] = prevMap[y] + graph.cost(y, x)
        dist.append(currMap)

    return dist
Exemplo n.º 3
0
def aStarSearch(graph, start, goal):
    frontier = PriorityQueue()
    frontier.put(start, 0)
    cameFrom = {}
    costSoFar = {}
    cameFrom[start] = None
    costSoFar[start] = 0

    while not frontier.empty():
        current = frontier.get()

        if current == goal:
            break

        for next in graph.neighbors(current):
            newCost = costSoFar[current] + graph.cost(current, next)
            if next not in costSoFar or newCost < costSoFar[next]:
                costSoFar[next] = newCost
                priority = newCost + heuristic(goal, next)
                frontier.put(next, priority)
                cameFrom[next] = current

    current = goal
    path = [current]
    while current != start:
        current = cameFrom[current]
        path.append(current)
    path.reverse()

    return path
Exemplo n.º 4
0
def search_smallest_stops(graph, start, end):
    # Check if the graph is valid. If it is not, return error string.
    if graph is None: return 'graph is null: Function Failed'

    # Declare a queue and enqueue the starting node.
    q = priority_queue.PriorityQueue()
    q.put(start, 0)
    curr_cost = {}
    curr_cost[start] = 0

    # Declare a string to count factory stops
    # Set it to blank space.
    solution = ''
    nodes_expanded = 0

    # While the q is not empty, get the next node on the queue
    # and check for the goal. If at the goal, copy the successful
    # path to the array of mazedata and then return 1. Else,
    # expand the node to the queue.
    prev = None
    while not q.empty():
        cur = q.get()
        if prev == cur:
            prev = cur
            cur = q.get()
        nodes_expanded = nodes_expanded + 1
        end[1].add_comp(cur)
        end[2].add_comp(cur)
        end[3].add_comp(cur)
        end[4].add_comp(cur)
        end[5].add_comp(cur)
        if cur != start:
            solution = solution + cur

        if end[1].done and end[2].done and end[3].done and end[4].done and end[
                5].done:
            return (solution, nodes_expanded)

        for next in graph.neighbors(cur):
            update_cost = curr_cost[cur] + graph.cost(cur, next)
            if next not in curr_cost or update_cost < curr_cost[next]:
                curr_cost[next] = update_cost
            prior = curr_cost[next] + stop_hist(end, next, 5)
            q.put(next, prior)
        prev = cur

    # Return the failed because solution was not found
    solution = 'FAILED: ' + solution
    return (solution, nodes_expanded)
Exemplo n.º 5
0
def getMinCostWalk(graph, dist, start, target, length):
    walk = []
    currVertex = target
    currLength = length
    while currLength > 0:
        walk.insert(0, currVertex)
        for prevVertex in graph.parseNin(currVertex):
            if prevVertex in dist[currLength - 1] and \
                    (dist[currLength - 1][prevVertex] + graph.cost(prevVertex, currVertex) == dist[currLength][
                        currVertex]):
                currVertex = prevVertex
                break

        currLength = currLength - 1

    walk.insert(0, start)
    return walk
Exemplo n.º 6
0
from heapq import heappush, heappop
from graph import goal_test, successors, start_node, heuristic, cost

current_node = start_node  # we are given the starting state
history = set()  # keep track of nodes already visited
queue = []  # priority queue
while not goal_test(current_node):
    for child in successors(current_node):
        if child not in history:
            h = heuristic(child)
            g = cost(current_node)
            f = g + h
            heappush(queue, (f, child))  # add nodes to list
    current_node = heappop(queue)  # go to the next state