Exemplo n.º 1
0
def readEdges(fileName):
    #Open romEdges.txt to build edges for each vertice
    with open(fileName, "r") as edgesFile:
        for line in edgesFile:
            vs = line.rstrip().split(' ')
            v0 = railroadNet[vs[0]]
            v1 = railroadNet[vs[1]]
            d = calcdv(v0, v1)
            newEdge = nodeEdge(vs[0], vs[1], d)
            v0.addEdge(newEdge)
            v1.addEdge(newEdge)
Exemplo n.º 2
0
def printPathWithDistance(pathlist, clr):
    total_distance = 0.0
    print("\nThe shortest path is: \n")
    print(railroadNet[pathlist[0]].getFullName() + "  ")
    for index in range(1, len(pathlist)):
        preNode = railroadNet[pathlist[index - 1]]
        curNode = railroadNet[pathlist[index]]
        plt.plot([preNode.getY(), curNode.getY()],
                 [preNode.getX(), curNode.getX()],
                 Color=clr)
        plt.pause(0.1)
        d = calcdv(preNode, curNode)
        total_distance += d
        print(curNode.getFullName() + ":", d)
    print("The total distance of the path is:", round(total_distance, 1))
Exemplo n.º 3
0
def printPathWithDistance(pathlist, clr, ax):
    total_distance = 0.0
    refreshCount = 1000
    print("\nThe shortest path is: \n")
    print(startName + "  ")

    for index in range(1, len(pathlist)):
        preNode = railroadNet[pathlist[index-1]]
        curNode = railroadNet[pathlist[index]]
        ax.plot([preNode.getY(), curNode.getY()], [preNode.getX(), curNode.getX()], Color=clr)
        refreshCount -= 1
        if refreshCount == 0:
            plt.pause(0.000001)
            refreshCount = 1000
        d = calcdv(preNode , curNode  )
        
        nodeName = railroadNet[pathlist[index]].getFullName()
        if nodeName == '':
            nodeName = pathlist[index]
                               
        total_distance += d
        print (nodeName+ ":" ,  d)
    plt.pause(0.000001)
    print("The total distance of the path is:" ,  round(total_distance, 1))
Exemplo n.º 4
0
def aStarSearch(start, goal, fig, ax):
    # The set of nodes already evaluated (list date type)
    cloasedSet = []
    # The set of currently discovered nodes that are not evaluated yet.
    # Initially, only the start node is known.
    openSet = [start]
    # For each node, which node it can most efficiently be reached from.
    # If a node can be reached from many nodes, cameFrom will eventually contain the
    # most efficient previous step.
    comeFrom = {}
    # For each node, the cost of getting from the start node to that node.
    gScore = {}

    # For each node, the total cost of getting from the start node to the goal
    # by passing by that node. That value is partly known, partly heuristic.
    fScore = {}
    # set those f and g scores to very big value initially
    for name in nodeNames:
        if name != start:
            fScore[name] = 999999999.00
            gScore[name] = 999999999.00

    # For the first node, that value is completely heuristic.
    fScore[start] = heuristic_cost_estimate(start, goal)
    # The cost of going from start to start is zero.
    gScore[start] = 0.0
    currentNode = []
    previousPath = []
    currentPath = []
    while len(openSet) > 0:
        #time.sleep(0.5)
        printPath(previousPath, 'y')
        #the node in openSet having the lowest fScore[] value
        current = getNodeWithLowestFScore(openSet, fScore)
        if current == goal:
            return reconstruct_path(comeFrom, current, start)

        openSet.remove(current)
        cloasedSet.append(current)
        circle = plt.Circle(
            (railroadNet[current].getY(), railroadNet[current].getX()),
            0.2,
            facecolor='green',
            edgecolor='black')
        ax.add_artist(circle)

        plt.pause(0.1)

        currentNode = railroadNet[current]
        previousPath = list(currentPath)
        printPath(previousPath, 'y')
        currentPath = reconstruct_path(comeFrom, current, start)
        printPath(currentPath, 'r')
        for edge in currentNode.getEdges():
            if edge.getFromNodeName() == current:
                neighbor = edge.getToNodeName()
            else:
                neighbor = edge.getFromNodeName()
            neighborNode = railroadNet[neighbor]

            if neighbor in cloasedSet:
                # Ignore the neighbor which is already evaluated.
                continue

            # Discover a new node
            if neighbor not in openSet:
                openSet.append(neighbor)
                circle = plt.Circle((railroadNet[neighbor].getY(),
                                     railroadNet[neighbor].getX()),
                                    0.2,
                                    facecolor='blue',
                                    edgecolor='black')
                ax.add_artist(circle)
                plt.pause(0.1)
            # The distance from start to a neighbor
            tentative_gScore = gScore[current] + calcdv(
                currentNode, neighborNode)
            if tentative_gScore >= gScore[neighbor]:
                continue  # This is not a better path.

            # This path is the best until now. Record it!
            comeFrom[neighbor] = current
            gScore[neighbor] = tentative_gScore
            fScore[neighbor] = gScore[neighbor] + heuristic_cost_estimate(
                neighbor, goal)

    return []  # not found the path
Exemplo n.º 5
0
def heuristic_cost_estimate(node, goal):
    v_start = railroadNet[node]
    v_goal = railroadNet[goal]
    return calcdv(v_start, v_goal)
Exemplo n.º 6
0
#Open romEdges.txt to build edges for each vertice
with open("romEdges.txt", "r") as edgesFile:
    index = 0
    for line in edgesFile:
        vs = line.rstrip().split(' ')
        v0 = railroadNet[vs[0]]
        v1 = railroadNet[vs[1]]
        v0.addEdge(vs[1])

#calculate edge distance
sumD = 0.0
for name in shortNames:
    v = railroadNet[name]
    for edgeName in v.getEdges():
        ev = railroadNet[edgeName]
        d = calcdv(v, ev)
        sumD += d
        print("edge between " + v.getName() + " and " + ev.getName() + " is: ",
              d)

print("Sum of all edges are:", round(sumD, 1))

#draw graph
fig, ax = plt.subplots()
ax.set_xlim(min(y_values) - 0.2, max(y_values) + 0.2)
ax.set_ylim(min(x_values) - 0.2, max(x_values) + 0.2)
for name in shortNames:
    v = railroadNet[name]
    #class matplotlib.pyplot.Circle(xy, radius=5, **kwargs)
    circle = plt.Circle((v.getY(), v.getX()),
                        0.2,