コード例 #1
0
ファイル: TSPSolver.py プロジェクト: mkumar23/Algorithm_TSP
def solver(argv):
    for i,x in enumerate(argv):
        if argv[i]=='-alg':
            algo=argv[i+1]
        elif argv[i]=='-inst':
            fileName=argv[i+1]
        elif argv[i]=='-time':
            cutoff=int(argv[i+1])
        elif argv[i]=='-seed':
            seed=int(argv[i+1])
#     fileName='ulysses16.tsp'
    G, optimalCost=createGraph(fileName)

    print optimalCost    
    if algo=='BnB':
        with open(fileName[:-4]+'_BnB_'+str(cutoff)+'.trace','w') as ftrace:
            tour,foundCost=branchAndBound(G,cutoff,ftrace)
    elif algo=='Approx':
        tour,foundCost=mst_approx(G)
    elif algo=='Heur':
        foundCost=greedy_approx(G)
    elif algo=='LS1':
        with open(fileName[:-4]+'_LS1_'+str(seed)+'_'+str(cutoff)+'.trace','w') as ftrace:
            tour,foundCost=simulated_annealiing(G,seed,cutoff,ftrace)
    elif algo=='LS2':
        with open(fileName[:-4]+'_LS2_'+str(seed)+'_'+str(cutoff)+'.trace','w') as ftrace:
            tour,foundCost=two_opt(G,cutoff,seed,ftrace)
    
    print tour,foundCost
コード例 #2
0
def simulated_annealiing(G,seed,cutoff,ftrace):
# 	G, optimal = readData.createGraph(sys.argv[1])
	nodes = G.nodes()
	random.seed(seed)
	random.shuffle(nodes)
	
	nodes,_=mst_approx(G)
	T =  G.get_edge_data(max(G.edges())[0], max(G.edges())[1])['weight'] * len(G.nodes())
	r =  0.9999
	final_nodes = SA(nodes, T, 0.0001, r, G,cutoff,ftrace)
# 	print weight(final_nodes, G), optimal
	return final_nodes, weight(final_nodes, G)
コード例 #3
0
ファイル: TSPSolver.py プロジェクト: mkumar23/Algorithm_TSP
def solver(argv):
    for i, x in enumerate(argv):
        if argv[i] == '-alg':
            algo = argv[i + 1]
        elif argv[i] == '-inst':
            fileName = argv[i + 1]
        elif argv[i] == '-time':
            cutoff = int(argv[i + 1])
        elif argv[i] == '-seed':
            seed = int(argv[i + 1])


#     fileName='ulysses16.tsp'
    G, optimalCost = createGraph(fileName)

    print optimalCost
    if algo == 'BnB':
        with open(fileName[:-4] + '_BnB_' + str(cutoff) + '.trace',
                  'w') as ftrace:
            tour, foundCost = branchAndBound(G, cutoff, ftrace)
    elif algo == 'Approx':
        tour, foundCost = mst_approx(G)
    elif algo == 'Heur':
        foundCost = greedy_approx(G)
    elif algo == 'LS1':
        with open(
                fileName[:-4] + '_LS1_' + str(seed) + '_' + str(cutoff) +
                '.trace', 'w') as ftrace:
            tour, foundCost = simulated_annealiing(G, seed, cutoff, ftrace)
    elif algo == 'LS2':
        with open(
                fileName[:-4] + '_LS2_' + str(seed) + '_' + str(cutoff) +
                '.trace', 'w') as ftrace:
            tour, foundCost = two_opt(G, cutoff, seed, ftrace)

    print tour, foundCost
コード例 #4
0
def solver(argv):
    for i,x in enumerate(argv):
        if argv[i]=='-alg':
            algo=argv[i+1]
        elif argv[i]=='-inst':
            fileName=argv[i+1]
        elif argv[i]=='-time':
            cutoff=int(argv[i+1])
        elif argv[i]=='-seed':
            seed=int(argv[i+1])
    G, optimalCost=createGraph(fileName)

    if algo=='BnB':
        with open(fileName[:-4]+'_BnB_'+str(cutoff)+'.trace','w') as ftrace:
            tour,foundCost=branchAndBound(G,cutoff,ftrace)
    elif algo=='Approx':
        tour,foundCost=mst_approx(G)
    elif algo=='Heur':
        tour,foundCost=greedy_approx(G)
    elif algo=='LS1':
        with open(fileName[:-4]+'_LS1_'+str(cutoff)+'_'+str(seed)+'.trace','w') as ftrace:
            tour,foundCost=simulated_annealiing(G,seed,cutoff,ftrace)
    elif algo=='LS2':
        with open(fileName[:-4]+'_LS2_'+str(cutoff)+'_'+str(seed)+'.trace','w') as ftrace:
            tour,foundCost=two_opt(G,seed,cutoff,ftrace)
    
    if algo=='LS1' or algo=='LS2':
        solutionFIleName=fileName[:-4]+'_'+algo+'_'+str(cutoff)+'_'+str(seed)+'.sol'
    else:
        solutionFIleName=fileName[:-4]+'_'+algo+'_'+str(cutoff)+'.sol'
        
    with open(solutionFIleName,'w') as fsol:
        if foundCost!=-1:
            fsol.write(str(foundCost)+'\n')
            tourString=','.join(list(map(str,tour)))+','+str(tour[0])+'\n'
            fsol.write(tourString)