コード例 #1
0
def main(argv):
    coords, distances = tsplib.load(argv[1])
    plot = plotting.TspPlot(coords)
    solution, cost = nearest_tour(distances, len(distances))
    plot.redraw_best(solution, cost)
    print cost
    while 1:
        pass
コード例 #2
0
ファイル: two_opt.py プロジェクト: hartror/gls_tsp
def main(argv):
    coords, distances = tsplib.load(argv[1])
    plot = plotting.TspPlot(coords)
    solution, cost = nearest_tour(distances, len(distances))
    print cost
    def calc_cost(solution):
        return tsplib.calc_cost(solution, distances)
    two_opt(solution, cost, len(distances), calc_cost, plot.redraw_best)
    while 1:
        pass
コード例 #3
0
def main(argv):
    coords, distances = tsplib.load(argv[1])
    plot = plotting.TspPlot(coords)
    solution, cost = nearest_tour(distances, len(distances))
    print cost

    def calc_cost(solution):
        return tsplib.calc_cost(solution, distances)

    two_opt(solution, cost, len(distances), calc_cost, plot.redraw_best)
    while 1:
        pass
def main():
    filename = input('TSP Instance: ')               #Get file input
    tspfile = tsplib.load(filename)                  #Load the TSP file based upon user input
    print("n = ", tspfile.vertex_count())            #Print out the # of vertices
    start = time.perf_counter()                      #Get beginning time
    result = tsp_algo(tspfile)                       #Find the optimal path
    end = time.perf_counter()                        #Get end time
    cost = cycle_weight(tspfile, result)             #Compute the cost

    #Prints out the result
    print('Elapsed time: ' + str(end - start))
    print('Optimal Cycle: ' + str(result))
    print('Optimal Cost: ' + str(cost))
コード例 #5
0
ファイル: gls.py プロジェクト: phuchiss8838/gls_tsp
def main(argv):
    alpha = float(argv[1])
    coords, distances = tsplib.load(argv[2])
    count = len(distances)
    solution, best_cost = nearest_tour(distances, len(distances))
    plot = plotting.TspPlot(coords, draw_guess=True)
    print best_cost

    penalties = numpy.copy(distances)
    penalties[:] = 0.0

    def calc_cost(solution):
        cost = tsplib.calc_cost(solution, distances)
        sol_pens = penalties[solution[:-1], solution[1:]]
        gls_cost = (cost +
                    alpha *
                    (best_cost/count+1) *
                    (sol_pens.sum() + penalties[solution[-1], solution[0]]))
        return gls_cost

    def penalise(solution):
        sol_dists = distances[solution[0:-1], solution[1:]]
        sol_dists = numpy.append(sol_dists, (distances[solution[0], solution[-1]],))
        sol_pens = penalties[solution[0:-1], solution[1:]]
        sol_pens = numpy.append(sol_pens, (penalties[solution[0], solution[-1]],))
        utility = sol_dists/(sol_pens+1)
        index_a = numpy.argmax(utility)
        index_b = index_a+1 if index_a != count-1 else 0
        a = solution[index_a]
        b = solution[index_b] 
        penalties[a,b] += 1.0
        penalties[b,a] += 1.0

    def redraw_guess(solution, cost):
        cost = tsplib.calc_cost(solution, distances) # ugh
        plot.redraw_guess(solution, cost)

    gls_cost = cost = best_cost
    best_solution = numpy.copy(solution)
    plot.redraw_best(best_solution, cost)

    while 1:
        solution, gls_cost = two_opt(solution, gls_cost, len(distances), calc_cost, redraw_guess)
        cost = tsplib.calc_cost(solution, distances)
        if cost < best_cost:
            print cost
            best_cost = cost
            best_solution[:] = solution
            plot.redraw_best(best_solution, cost)
        penalise(solution)
コード例 #6
0
def main():
    #Verify the correct number of command line arguments were used
    if len(sys.argv) != 2:
        print('error: you must supply exactly one arguments\n\n' +
              'usage: python3 tsp.py <weighted_graph.xml.zip file>')
        sys.exit(1)

    #Capture the command line arguments
    weighted_graph = sys.argv[1]

    print('TSP Instance:')                           #Get file input
    tspfile = tsplib.load(weighted_graph)            #Load the TSP file based upon user input
    print("n = ", tspfile.vertex_count())            #Print out the # of vertices
    start = time.perf_counter()                      #Get beginning time
    result = tsp_algo(tspfile)                       #Find the Hamiltonian cycle of minimum total weight
    end = time.perf_counter()                        #Get end time
    cost = cycle_weight(tspfile, result)             #Compute the cost

    #Prints out the result
    print('Elapsed time: ' + str(end - start))
    print('Optimal Cycle: ' + str(result))
    print('Optimal Cost: ' + str(cost))