Esempio n. 1
0
def main():
    read_write = TspRW()  # create a read_write object
    read_write.read_file(
        raw_input('Enter file to read from: '
                  ))  # reads all the coordinates of citties from an ASCII file
    l = read_write.labels
    c = read_write.coordinates
    handle = Nearest_Neighbour(
        l, c)  # create a Nearest_neigbour class from the given vertices

    # for each vertex
    for i in handle.vertices:
        handle.mark_unvisited_cities()  # mark all the citites unvisited
        handle.empty_tour()  # empties the tour
        handle.startcity = i  # set the city to start city
        handle.currentcity = handle.startcity  # sets the start city to current city
        handle.mark_visited()  # marks the city visited

        # while there is an unvisited city
        while len(handle.coordinates) != 0:
            handle.calculate_nearneighbor(
            )  # find the nearest neighbour for the current city
            handle.add_currcity_tour()  # adds the current city to the tour
            handle.add_tourCost()  # adds the cost to the tour
            handle.currentcity = handle.nearestcity  # sets nearest city to current city
            handle.mark_visited()  # marks the current city visited
        e = Edge(handle.currentcity, handle.startcity
                 )  # creates the last edge between the current and start city
        handle.best_tour.add_edge(e)  # add that last edge to best tour

        # updates best_tour_so_far if the last tour was lest costly
        if handle.cost_of_tour < handle.cost_of_best_tour_so_far:
            handle.cost_of_best_tour_so_far = handle.cost_of_tour
            handle.best_tour_so_far = handle.best_tour
            handle.best_label_so_far = handle.best_label
            handle.best_coordinates_so_far = handle.best_coordinates

    # write the coordinates to an ASCII file with .tour extension
    read_write.coordinates = handle.best_coordinates_so_far
    read_write.labels = handle.best_label_so_far
    read_write.write_file()

    # print handle.best_tour_so_far
    # print handle.cost_of_best_tour_so_far
    # print handle.best_label_so_far
    # print handle.best_coordinates_so_far

    # for i in range(len(handle.best_label_so_far)):
    #     handle.best_label_so_far[i].pos = handle.best_coordinates_so_far

    # The following two lines allows to swtich the layout of the graph displayed
    layout = CartesianLayout(handle.best_tour_so_far)
    # layout = RandomLayout(handle.best_tour_so_far)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(handle.best_tour_so_far, layout)
    gw.mainloop()
Esempio n. 2
0
def main():
    read_write = TspRW()    # create a read_write object
    read_write.read_file(raw_input('Enter file to read from: '))    # reads all the coordinates of citties from an ASCII file
    l = read_write.labels   
    c = read_write.coordinates
    handle = Nearest_Neighbour(l, c)    # create a Nearest_neigbour class from the given vertices
    
    # for each vertex
    for i in handle.vertices:
        handle.mark_unvisited_cities()  # mark all the citites unvisited
        handle.empty_tour() # empties the tour
        handle.startcity = i    # set the city to start city
        handle.currentcity = handle.startcity   # sets the start city to current city
        handle.mark_visited()   # marks the city visited
        
        # while there is an unvisited city
        while len(handle.coordinates)!= 0:  
            handle.calculate_nearneighbor() # find the nearest neighbour for the current city
            handle.add_currcity_tour()  # adds the current city to the tour
            handle.add_tourCost()   # adds the cost to the tour
            handle.currentcity = handle.nearestcity # sets nearest city to current city
            handle.mark_visited()   # marks the current city visited
        e = Edge(handle.currentcity, handle.startcity)  # creates the last edge between the current and start city
        handle.best_tour.add_edge(e)    # add that last edge to best tour
        
        # updates best_tour_so_far if the last tour was lest costly
        if handle.cost_of_tour < handle.cost_of_best_tour_so_far :  
            handle.cost_of_best_tour_so_far = handle.cost_of_tour
            handle.best_tour_so_far = handle.best_tour
            handle.best_label_so_far = handle.best_label
            handle.best_coordinates_so_far = handle.best_coordinates

    # write the coordinates to an ASCII file with .tour extension
    read_write.coordinates = handle.best_coordinates_so_far
    read_write.labels = handle.best_label_so_far
    read_write.write_file()

    # print handle.best_tour_so_far
    # print handle.cost_of_best_tour_so_far
    # print handle.best_label_so_far
    # print handle.best_coordinates_so_far

    # for i in range(len(handle.best_label_so_far)):
    #     handle.best_label_so_far[i].pos = handle.best_coordinates_so_far

    # The following two lines allows to swtich the layout of the graph displayed
    layout = CartesianLayout(handle.best_tour_so_far)
    # layout = RandomLayout(handle.best_tour_so_far)

    # draw the graph
    gw = GraphWorld()
    gw.show_graph(handle.best_tour_so_far, layout)
    gw.mainloop()
Esempio n. 3
0
def main():

    time.clock()
    object = TwoOpt()   # create a two-opt object

    read_write = TspRW()    # create a read_write object
    read_write.read_file(raw_input('Enter file to read from: '))    # reads all the coordinates of citties from an ASCII file
    object.labels = read_write.labels   # store the labels in a list
    object.coordinates = read_write.coordinates      # store the coordinates in this list

    numRound = int(raw_input('Enter the number of random graphs you want to generate: '))
    roundTimes = int(raw_input('Enter the number of times you want to swap edges: '))

    for i in range(numRound):
        object.generate_rand_graph()    # generate a random graph
        for i in range(roundTimes):
            object.generate_rand_vertices()    # randomly generate two indices which have no common connection
            object.find_random_edge()   # generate two random edges from the given indices
            object.find_new_edge()  # find the new edge if swap were to happen.

            dx_oldtour = object.cal_distance_of_edge(object.edge1) + object.cal_distance_of_edge(object.edge2)  # difference in cost before swapping using two-opt
            dx_newtour = object.cal_distance_of_edge(object.new_edge1) + object.cal_distance_of_edge(object.new_edge2)    # difference in cost after adding two edges.
            if dx_newtour < dx_oldtour:
                object.remove_edges()
                object.add_edge_reversely()
        if object.costOfBestTour > object.costOfTour:
            bestTour = object.tour
            bestTour_vertices = object.tour_vertices
            object.costOfBestTour = object.costOfTour

    # write the tour in .tour ASCII file
    read_write.coordinates = []
    read_write.labels = []
    for i in bestTour_vertices:
        read_write.coordinates.append(i.pos)
    for i in bestTour_vertices:
        read_write.labels.append(i.label)
    read_write.write_file()

    print 'Run time: ', time.clock()

    # The following two lines allows to switch the layout of the graph displayed
    # layout = CartesianLayout(bestTour)
    layout = RandomLayout(bestTour)
    # layout = CircleLayout(bestTour)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(bestTour, layout)
    gw.mainloop()
Esempio n. 4
0
def main():

    time.clock()
    object = TwoOpt()  # create a two-opt object

    read_write = TspRW()  # create a read_write object
    read_write.read_file(
        raw_input('Enter file to read from: '
                  ))  # reads all the coordinates of citties from an ASCII file
    object.labels = read_write.labels  # store the labels in a list
    object.coordinates = read_write.coordinates  # store the coordinates in this list

    numRound = int(
        raw_input('Enter the number of random graphs you want to generate: '))
    roundTimes = int(
        raw_input('Enter the number of times you want to swap edges: '))

    for i in range(numRound):
        object.generate_rand_graph()  # generate a random graph
        for i in range(roundTimes):
            object.generate_rand_vertices(
            )  # randomly generate two indices which have no common connection
            object.find_random_edge(
            )  # generate two random edges from the given indices
            object.find_new_edge()  # find the new edge if swap were to happen.

            dx_oldtour = object.cal_distance_of_edge(
                object.edge1) + object.cal_distance_of_edge(
                    object.edge2
                )  # difference in cost before swapping using two-opt
            dx_newtour = object.cal_distance_of_edge(
                object.new_edge1) + object.cal_distance_of_edge(
                    object.new_edge2
                )  # difference in cost after adding two edges.
            if dx_newtour < dx_oldtour:
                object.remove_edges()
                object.add_edge_reversely()
        if object.costOfBestTour > object.costOfTour:
            bestTour = object.tour
            bestTour_vertices = object.tour_vertices
            object.costOfBestTour = object.costOfTour

    # write the tour in .tour ASCII file
    read_write.coordinates = []
    read_write.labels = []
    for i in bestTour_vertices:
        read_write.coordinates.append(i.pos)
    for i in bestTour_vertices:
        read_write.labels.append(i.label)
    read_write.write_file()

    print 'Run time: ', time.clock()

    # The following two lines allows to switch the layout of the graph displayed
    # layout = CartesianLayout(bestTour)
    layout = RandomLayout(bestTour)
    # layout = CircleLayout(bestTour)
    # draw the graph
    gw = GraphWorld()
    gw.show_graph(bestTour, layout)
    gw.mainloop()