def run_mimic(t, samples, keep, m): fill = [N] * N ranges = array('i', fill) ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscreteUniformDistribution(ranges) fname = outfile.format('MIMIC{}_{}_{}'.format(samples, keep, m), str(t + 1)) base.write_header(fname) df = DiscreteDependencyTree(m, ranges) ef = TravelingSalesmanRouteEvaluationFunction(points) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) mimic = MIMIC(samples, keep, pop) fit = FixedIterationTrainer(mimic, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) fevals = ef.fevals score = ef.value(mimic.getOptimal()) ef.fevals -= 1 st = '{},{},{},{}\n'.format(i, score, times[-1], fevals) # print st base.write_to_file(fname, st) return
def run_sa(t, CE): fname = outfile.format('SA{}'.format(CE), str(t + 1)) ef = TravelingSalesmanRouteEvaluationFunction(points) hcp = GenericHillClimbingProblem(ef, odd, nf) sa = SimulatedAnnealing(1E10, CE, hcp) fit = FixedIterationTrainer(sa, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) fevals = ef.fevals score = ef.value(sa.getOptimal()) ef.fevals -= 1 st = '{},{},{},{}\n'.format(i, score, times[-1], fevals) # print st base.write_to_file(fname, st) return
def run_rhc(t): fname = outfile.format('RHC', str(t + 1)) ef = TravelingSalesmanRouteEvaluationFunction(points) hcp = GenericHillClimbingProblem(ef, odd, nf) rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) fevals = ef.fevals score = ef.value(rhc.getOptimal()) ef.fevals -= 1 st = '{},{},{},{}\n'.format(i, score, times[-1], fevals) # print st base.write_to_file(fname, st) return
def run_ga(t, pop, mate, mutate): fname = outfile.format('GA{}_{}_{}'.format(pop, mate, mutate), str(t + 1)) ef = TravelingSalesmanRouteEvaluationFunction(points) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) ga = StandardGeneticAlgorithm(pop, mate, mutate, gap) fit = FixedIterationTrainer(ga, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) fevals = ef.fevals score = ef.value(ga.getOptimal()) ef.fevals -= 1 st = '{},{},{},{}\n'.format(i, score, times[-1], fevals) # print st base.write_to_file(fname, st) return
points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) start = time.time() ga = StandardGeneticAlgorithm(param[0], param[1], param[2], gap) fit = FixedIterationTrainer(ga, num_iterations) fit.train() value = str(ef.value(ga.getOptimal())) print "GA Inverse of Distance: " + value end = time.time() print "Route:" path = [] for x in range(0, N): path.append(ga.getOptimal().getDiscrete(x)) print path print "Time -->", end - start results = { 'num_iterations': num_iterations, 'value': value, 'time': end - start }
for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, 200000) fit.train() print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) print "Route:" path = [] for x in range(0, N): path.append(rhc.getOptimal().getDiscrete(x)) print path sa = SimulatedAnnealing(1e12, 0.999, hcp) fit = FixedIterationTrainer(sa, 200000) fit.train() print "SA Inverse of Distance: " + str(ef.value(sa.getOptimal())) print "Route:" path = [] for x in range(0, N): path.append(sa.getOptimal().getDiscrete(x)) print path
for GA_MUTATION in GA_MUTATION_pool: ga = StandardGeneticAlgorithm(GA_POPULATION, GA_CROSSOVER, GA_MUTATION, gap) fit_ga = FixedIterationTrainer(ga, n_iteration) print("calculating for mutations = %d " % GA_MUTATION) # Training start_ga = time.time() fit_ga.train() end_ga = time.time() # Result extracting last_training_time_ga = end_ga - start_ga ga_training_time[n].append(last_training_time_ga) ga_fitness[n].append(ef.value(ga.getOptimal())) overall_ga_training_time = list_avg(*ga_training_time) overall_ga_fitness = list_avg(*ga_fitness) with open(OUTPUT_FILE, "w") as outFile: for i in range(1): outFile.write(','.join([ "ga_mutations", "ga_fitness", "ga_training_time"]) + '\n') for i in range(len(GA_MUTATION_pool)): outFile.write(','.join([ str(GA_MUTATION_pool[i]), str(overall_ga_fitness[i]), str(overall_ga_training_time[i])]) + '\n')
df = DiscreteDependencyTree(m, ranges) f = open(fname, 'w') f.write('iterations,fitness,time,fevals\n') f.close() ef = TravelingSalesmanRouteEvaluationFunction(points) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) mimic = MIMIC(samples, keep, pop) fit = FixedIterationTrainer(mimic, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) fevals = ef.fevals score = ef.value(mimic.getOptimal()) ef.fevals -= 1 st = '{},{},{},{}\n'.format(i, score, times[-1], fevals) print st f = open(fname, 'a') f.write(st) f.close() # RHC for t in range(numTrials): fname = outfile.format('RHC', str(t + 1)) f = open(fname, 'w') f.write('iterations,fitness,time,fevals\n') f.close() ef = TravelingSalesmanRouteEvaluationFunction(points) hcp = GenericHillClimbingProblem(ef, odd, nf) rhc = RandomizedHillClimbing(hcp)
start_sa = time.time() fit_sa.train() end_sa = time.time() start_ga = time.time() fit_ga.train() end_ga = time.time() start_mimic = time.time() fit_mimic.train() end_mimic = time.time() # Result handling last_train_time_rhc = end_rhc - start_rhc rhc_train_time[repetition].append(last_train_time_rhc) rhc_accuracy[repetition].append(ef.value(rhc.getOptimal())) last_train_time_sa = end_sa - start_sa sa_train_time[repetition].append(last_train_time_sa) sa_accuracy[repetition].append(ef.value(sa.getOptimal())) last_train_time_ga = end_ga - start_ga ga_train_time[repetition].append(last_train_time_ga) ga_accuracy[repetition].append(ef.value(ga.getOptimal())) last_train_time_mimic = end_mimic - start_mimic mimic_train_time[repetition].append(last_train_time_mimic) mimic_accuracy[repetition].append(ef.value(mimic.getOptimal())) while current_iteration_count <= MAX_ITERATION - ITERATION_STEP: print("Computing for %d iterations" %
for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) start = time.time() rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, num_iterations) fit.train() value = str(ef.value(rhc.getOptimal())) print "RHC Inverse of Distance: " + value end = time.time() print "Route:" path = [] for x in range(0, N): path.append(rhc.getOptimal().getDiscrete(x)) print path print "Time -->", end - start results = { 'num_iterations': num_iterations, 'value': value, 'time': end - start }
points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) start = time.time() rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iteration) fit.train() end = time.time() rhc_fit = ef.value(rhc.getOptimal()) rhc_time = end - start print "RHC: " + str(rhc_fit) start = time.time() sa = SimulatedAnnealing(1E12, .999, hcp) fit = FixedIterationTrainer(sa, iteration) fit.train() end = time.time() sa_fit = ef.value(sa.getOptimal()) sa_time = end - start print "SA: " + str(sa_fit) # if iteration <= 5000: # # start = time.time()
for r in range(reps): # create a new trainer each time row = {} for key, value in zip(keys, exper): row[key] = value print(row) fit = factory(row) # have to reset func eval per run ef.func_evals = 0 for i in range(0, max_iterations,num_iterations): start = clock() fit.train() stop = clock() rep_times.append(stop - start) func_eval = ef.func_evals fitness = ef.value(fit.trainer.getOptimal()) # log line = [name,r,i,fitness,rep_times[-1], func_eval] + list(exper) line = [str(x) for x in line] out.write(','.join(line) + os.linesep) print "Done " + name + " trainer..." # rhc = RandomizedHillClimbing(hcp) # fit = FixedIterationTrainer(rhc, 200000) # fit.train() # print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal()))
cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) ef = TravelingSalesmanSortEvaluationFunction(points); fill = [N] * N ranges = array('i', fill) odd = DiscreteUniformDistribution(ranges); df = DiscreteDependencyTree(.1, ranges); pop = GenericProbabilisticOptimizationProblem(ef, odd, df); start = time.time() mimic = MIMIC(param[0], param[1], pop) fit = FixedIterationTrainer(mimic, num_iterations) fit.train() value = str(ef.value(mimic.getOptimal())) print "MIMIC Inverse of Distance: " + value end = time.time() print "Route:" path = [] optimal = mimic.getOptimal() fill = [0] * optimal.size() ddata = array('d', fill) for i in range(0, len(ddata)): ddata[i] = optimal.getContinuous(i) order = ABAGAILArrays.indices(optimal.size()) ABAGAILArrays.quicksort(ddata, order) print order end = time.time() results = { 'num_iterations': num_iterations,
ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, N) start = time.time() fit.train() end = time.time() training_time = end - start print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) OUTFILE = "%s%s.csv" % (OUTFILE_BASE, "RHC") with open(OUTFILE, 'a+') as f: f.write("%d,%f,%f\n" % (N, training_time, ef.value(rhc.getOptimal()))) sa = SimulatedAnnealing(1E12, .999, hcp) fit = FixedIterationTrainer(sa, N) start = time.time() fit.train() end = time.time() training_time = end - start print "SA Inverse of Distance: " + str(ef.value(sa.getOptimal())) OUTFILE = "%s%s.csv" % (OUTFILE_BASE, "SA") with open(OUTFILE, 'a+') as f:
for x in range(runs): ranges = array('i', [2] * N) fitness = TravelingSalesmanRouteEvaluationFunction(points) discrete_dist = DiscretePermutationDistribution(N) discrete_neighbor = SwapNeighbor() discrete_mutation = SwapMutation() discrete_dependency = DiscreteDependencyTree(.1, ranges) hill_problem = GHC(fitness, discrete_dist, discrete_neighbor) start = time.clock() rhc_problem = RHC(hill_problem) fit = FixedIterationTrainer(rhc_problem, iteration) fit.train() end = time.clock() full_time = end - start rhc_total += fitness.value(rhc_problem.getOptimal()) rhc_time += full_time rhc_total_avg = rhc_total / runs rhc_time_avg = rhc_time / runs data = '{},{},{}\n'.format(iteration, rhc_total_avg, rhc_time_avg) print(data) with open(output_directory, 'a') as f: f.write(data) #SA output_directory = "Results/Small/TSP_SA.csv" with open(output_directory, 'w') as f: f.write('iterations,fitness,time\n')
def main(): iterations = 200000 alg = 'all' gaPop = 2000 gaMate = 1500 gaMutate = 250 mimicSamples = 500 mimicToKeep = 100 saTemp = 1E12 saCooling = .999 gaIters = 1000 mimicIters = 1000 run = 0 settings = [] try: opts, args = getopt.getopt(sys.argv[1:], "ahrsgmn:i:", ["gaIters=", "mimicIters=", "gaPop=", "gaMate=", "gaMutate=", "mimicSamples=", "mimicToKeep=", "saTemp=", "saCooling="]) except: print 'travelingsalesman.py -i <iterations>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'travelingsalesman.py -i <iterations>' sys.exit(1) elif opt == '-i': if arg < 1: print 'Iterations must be greater than 0' sys.exit(2) iterations = int(arg) elif opt == '-a': alg = 'all' elif opt == '-r': alg = 'RHC' elif opt == '-s': alg = 'SA' elif opt == '-g': alg = 'GA' elif opt == '-m': alg = 'MIMIC' elif opt == '--gaPop': if arg < 1: print 'Population must be greater than 0' sys.exit(2) gaPop = int(arg) elif opt == '--gaMate': if arg < 1: print 'Mating must be greater than 0' sys.exit(2) gaMate = int(arg) elif opt == '--gaMutate': if arg < 1: print 'Mutators must be greater than 0' sys.exit(2) gaMutate = int(arg) elif opt == '--mimicSamples': if arg < 1: print 'MIMIC samples must be greater than 0' sys.exit(2) mimicSamples = int(arg) elif opt == '--mimicToKeep': if arg < 1: print 'MIMIC to keep must be greater than 0' sys.exit(2) mimicToKeep = int(arg) elif opt == '--saTemp': saTemp = float(arg) elif opt == '--saCooling': saCooling = float(arg) elif opt == '-n': run = int(arg) elif opt == '--gaIters': if arg < 1: print 'GA Iterations must be greater than 0' sys.exit(2) gaIters = int(arg) elif opt == '--mimicIters': if arg < 1: print 'MIMIC Iterations must be greater than 0' sys.exit(2) mimicIters = int(arg) vars = { 'iterations' : iterations, 'alg' : alg, 'gaPop' : gaPop, 'gaMate' : gaMate, 'gaMutate' : gaMutate, 'mimicSamples' : mimicSamples, 'mimicToKeep' : mimicToKeep, 'saTemp' : saTemp, 'saCooling' : saCooling, 'gaIters' : gaIters, 'mimicIters' : mimicIters, 'run' : run } settings = getSettings(alg, settings, vars) if gaPop < gaMate or gaPop < gaMutate or gaMate < gaMutate: pebkac({gaPop: 'total population',gaMate : 'mating population', gaMutate : 'mutating population'}, alg, 'total population', settings) if mimicSamples < mimicToKeep: pebkac({mimicSamples: 'mimic samples', mimicToKeep : 'mimic to keep'}, alg, 'mimic samples', settings) prob = 'Traveling Sales Problem' invDist = {} cities = CityList() N = len(cities) #random = Random() points = [[0 for x in xrange(2)] for x in xrange(N)] for i in range(0, len(points)): coords = cities.getCoords(i) points[i][0] = coords[0] points[i][1] = coords[1] ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) rows = [] if alg == 'RHC' or alg == 'all': print '\n----------------------------------' print 'Using Random Hill Climbing' for label, setting in settings: print label + ":" + str(setting) rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iterations) fit.train() path = [] for x in range(0,N): path.append(rhc.getOptimal().getDiscrete(x)) output(prob, 'RHC', path, points, settings) rows = [] row = [] row.append("Inverse of Distance") row.append(ef.value(rhc.getOptimal())) rows.append(row) invDist['RHC'] = ef.value(rhc.getOptimal()) buildFooter(prob, 'RHC', rows, settings) outputFooter(prob, 'RHC', rows, settings) if alg == 'SA' or alg == 'all': print 'Using Simulated Annealing' for label, setting in settings: print label + ":" + str(setting) sa = SimulatedAnnealing(saTemp, saCooling, hcp) fit = FixedIterationTrainer(sa, iterations) fit.train() path = [] for x in range(0,N): path.append(sa.getOptimal().getDiscrete(x)) output(prob, 'SA', path, points, settings) rows = [] row = [] row.append("Inverse of Distance") row.append(ef.value(sa.getOptimal())) rows.append(row) invDist['SA'] = ef.value(sa.getOptimal()) buildFooter(prob, 'SA', rows, settings) outputFooter(prob, 'SA', rows, settings) if alg == 'GA' or alg == 'all': print '\n----------------------------------' print 'Using Genetic Algorithm' for label, setting in settings: print label + ":" + str(setting) ga = StandardGeneticAlgorithm(gaPop, gaMate, gaMutate, gap) fit = FixedIterationTrainer(ga, gaIters) fit.train() path = [] for x in range(0,N): path.append(ga.getOptimal().getDiscrete(x)) output(prob, 'GA', path, points, settings) rows = [] row = [] row.append("Inverse of Distance") row.append(ef.value(ga.getOptimal())) rows.append(row) invDist['GA'] = ef.value(ga.getOptimal()) buildFooter(prob, 'GA', rows, settings) outputFooter(prob, 'GA', rows, settings) if alg == 'MIMIC' or alg == 'all': print '\n----------------------------------' print 'Using MIMIC' for label, setting in settings: print label + ":" + str(setting) # for mimic we use a sort encoding ef = TravelingSalesmanSortEvaluationFunction(points); fill = [N] * N ranges = array('i', fill) odd = DiscreteUniformDistribution(ranges); df = DiscreteDependencyTree(.1, ranges); pop = GenericProbabilisticOptimizationProblem(ef, odd, df); mimic = MIMIC(mimicSamples, mimicToKeep, pop) fit = FixedIterationTrainer(mimic, mimicIters) fit.train() path = [] optimal = mimic.getOptimal() fill = [0] * optimal.size() ddata = array('d', fill) for i in range(0,len(ddata)): ddata[i] = optimal.getContinuous(i) order = ABAGAILArrays.indices(optimal.size()) ABAGAILArrays.quicksort(ddata, order) output(prob, 'MIMIC', order, points, settings) rows = [] row = [] row.append("Inverse of Distance") row.append(ef.value(mimic.getOptimal())) rows.append(row) invDist['MIMIC'] = ef.value(mimic.getOptimal()) buildFooter(prob, 'MIMIC', rows, settings) outputFooter(prob, 'MIMIC', rows, settings) maxn = max(len(key) for key in invDist) maxd = max(len(str(invDist[key])) for key in invDist) print "Results" for result in invDist: print "%-*s %s %-*s" % (len('Best Alg') + 2, result, ':', maxd, invDist[result]) if alg == 'all': print "%-*s %s %-*s" % (len('Best Alg') + 2, 'Best Alg', ':', maxd, max(invDist.iterkeys(), key=(lambda key: invDist[key]))) print '----------------------------------'
mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) rhc = RandomizedHillClimbing(hcp) sa = SimulatedAnnealing(1E12, .999, hcp) ga = StandardGeneticAlgorithm(2000, 1500, 250, gap) rhc_f = open('out/op/salesman/rhc.csv', 'w') sa_f = open('out/op/salesman/sa.csv', 'w') ga_f = open('out/op/salesman/ga.csv', 'w') for i in range(ITERATIONS): rhc.train() rhc_fitness = ef.value(rhc.getOptimal()) rhc_f.write('{},{}\n'.format(i, rhc_fitness)) sa.train() sa_fitness = ef.value(sa.getOptimal()) sa_f.write('{},{}\n'.format(i, sa_fitness)) ga.train() ga_fitness = ef.value(ga.getOptimal()) ga_f.write('{},{}\n'.format(i, ga_fitness)) rhc_f.close() sa_f.close() ga_f.close() # for mimic we use a sort encoding
hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) iterations = range(1000, 50000, 5000) rhc_results = [] sa_results = [] ga_results = [] import time rhc_startTime = time.time() for iteration in iterations: rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iteration) fit.train() rhc_optimalVal = ef.value(rhc.getOptimal()) # print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) # print "Route:" # path = [] # for x in range(0,N): # path.append(rhc.getOptimal().getDiscrete(x)) # print path rhc_results.append(rhc_optimalVal) rhc_endTime = time.time() print "RHC Completed in: " + str(rhc_endTime - rhc_startTime) sa_startTime = time.time() for iteration in iterations: sa = SimulatedAnnealing(1E12, .999, hcp) fit = FixedIterationTrainer(sa, iteration) fit.train()
hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) nsample = 10 niters = [50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000] ################################################################################################## #-------------------------------------- R-Hill Climbing ------------------------------------------ rhc = RandomizedHillClimbing(hcp) for iters in niters: start = time.time() fit = FixedIterationTrainer(rhc, iters) value = 0 for isample in range(nsample): fit.train() value += ef.value(rhc.getOptimal()) end = time.time() clock_time = (end - start) / nsample value = round(value / nsample, 2) print "RHC " + str(1 / value), iters, clock_time path = [] for x in range(0, N): path.append(rhc.getOptimal().getDiscrete(x)) print "Rout_RHC", path[x], points[path[x]][0], points[path[x]][1] ################################################################################################### #--------------------------------- Simulated Annealing -------------------------------------------- sa = SimulatedAnnealing(1E12, .999, hcp) for iters in niters: start = time.time()
for CE in [0.15, 0.35, 0.55, 0.75, 0.95]: fname = outfile.replace('@ALG@', 'SA{}'.format(CE)).replace('@N@', str(t + 1)) with open(fname, 'w') as f: f.write('iterations,fitness,time,fevals\n') ef = TravelingSalesmanRouteEvaluationFunction(points) hcp = GenericHillClimbingProblem(ef, odd, nf) sa = SimulatedAnnealing(1E10, CE, hcp) fit = FixedIterationTrainer(sa, 10) times = [0] for i in range(0, maxIters, 10): start = clock() fit.train() elapsed = time.clock() - start times.append(times[-1] + elapsed) score = ef.value(sa.getOptimal()) st = '{},{},{}\n'.format(i, score, times[-1]) print st with open(fname, 'a') as f: f.write(st) ''' #GA for t in range(numTrials): for pop,mate,mutate in product([100],[50,30,10],[30,10]): fname = outfile.replace('@ALG@','GA{}_{}_{}'.format(pop,mate,mutate)).replace('@N@',str(t+1)) with open(fname,'w') as f: f.write('iterations,fitness,time,fevals\n') ef = TravelingSalesmanRouteEvaluationFunction(points) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) ga = StandardGeneticAlgorithm(pop, mate, mutate, gap) fit = FixedIterationTrainer(ga, 10)
def run_traveling_salesman(): # set N value. This is the number of points N = 50 random = Random() points = [[0 for x in xrange(2)] for x in xrange(N)] for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) iters = [50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000, 50000, 100000] num_repeats = 5 rhc_results = [] rhc_times = [] for i in iters: print(i) for j in range(num_repeats): start = time.time() rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, i) fit.train() end = time.time() rhc_results.append(ef.value(rhc.getOptimal())) rhc_times.append(end - start) print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) # print "Route:" # path = [] # for x in range(0,N): # path.append(rhc.getOptimal().getDiscrete(x)) # print path sa_results = [] sa_times = [] for i in iters: print(i) for j in range(num_repeats): start = time.time() sa = SimulatedAnnealing(1E12, .999, hcp) fit = FixedIterationTrainer(sa, i) fit.train() sa_results.append(ef.value(sa.getOptimal())) sa_times.append(end - start) print "SA Inverse of Distance: " + str(ef.value(sa.getOptimal())) # print "Route:" # path = [] # for x in range(0,N): # path.append(sa.getOptimal().getDiscrete(x)) # print path ga_results = [] ga_times = [] for i in iters: print(i) for j in range(num_repeats): start = time.time() ga = StandardGeneticAlgorithm(2000, 1500, 250, gap) fit = FixedIterationTrainer(ga, i) fit.train() end = time.time() ga_results.append(ef.value(ga.getOptimal())) print "GA Inverse of Distance: " + str(ef.value(ga.getOptimal())) ga_times.append(end - start) # print "Route:" # path = [] # for x in range(0,N): # path.append(ga.getOptimal().getDiscrete(x)) # print path # for mimic we use a sort encoding ef = TravelingSalesmanSortEvaluationFunction(points) fill = [N] * N ranges = array('i', fill) odd = DiscreteUniformDistribution(ranges) df = DiscreteDependencyTree(.1, ranges) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) mimic_results = [] mimic_times = [] for i in iters[0:6]: print(i) for j in range(num_repeats): start = time.time() mimic = MIMIC(500, 100, pop) fit = FixedIterationTrainer(mimic, i) fit.train() end = time.time() mimic_results.append(ef.value(mimic.getOptimal())) print "MIMIC Inverse of Distance: " + str( ef.value(mimic.getOptimal())) # print "Route:" # path = [] # optimal = mimic.getOptimal() # fill = [0] * optimal.size() # ddata = array('d', fill) # for i in range(0,len(ddata)): # ddata[i] = optimal.getContinuous(i) # order = ABAGAILArrays.indices(optimal.size()) # ABAGAILArrays.quicksort(ddata, order) # print order mimic_times.append(end - start) with open('travelingsalesman.csv', 'w') as csvfile: writer = csv.writer(csvfile) writer.writerow(rhc_results) writer.writerow(rhc_times) writer.writerow(sa_results) writer.writerow(sa_times) writer.writerow(ga_results) writer.writerow(ga_times) writer.writerow(mimic_results) writer.writerow(mimic_times) return rhc_results, rhc_times, sa_results, sa_times, ga_results, ga_times, mimic_results, mimic_times
def travelingsalesmanfunc(N, iterations): rhcMult = 1500 saMult = 1500 gaMult = 1 mimicMult = 3 random = Random() points = [[0 for x in xrange(2)] for x in xrange(N)] for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() optimalOut = [] timeOut = [] evalsOut = [] for niter in iterations: ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) iterOptimalOut = [N, niter] iterTimeOut = [N, niter] iterEvals = [N, niter] start = time.time() rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, niter * rhcMult) fit.train() end = time.time() rhcOptimal = ef.value(rhc.getOptimal()) rhcTime = end - start print "RHC Inverse of Distance: optimum: " + str(rhcOptimal) print "RHC time: " + str(rhcTime) #print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) print "Route:" path = [] for x in range(0, N): path.append(rhc.getOptimal().getDiscrete(x)) print path iterOptimalOut.append(rhcOptimal) iterTimeOut.append(rhcTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() sa = SimulatedAnnealing(1E12, .999, hcp) fit = FixedIterationTrainer(sa, niter * saMult) fit.train() end = time.time() saOptimal = ef.value(sa.getOptimal()) saTime = end - start print "SA Inverse of Distance optimum: " + str(saOptimal) print "SA time: " + str(saTime) #print "SA Inverse of Distance: " + str(ef.value(sa.getOptimal())) print "Route:" path = [] for x in range(0, N): path.append(sa.getOptimal().getDiscrete(x)) print path iterOptimalOut.append(saOptimal) iterTimeOut.append(saTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() ga = StandardGeneticAlgorithm(2000, 1500, 250, gap) fit = FixedIterationTrainer(ga, niter * gaMult) fit.train() end = time.time() gaOptimal = ef.value(ga.getOptimal()) gaTime = end - start print "GA Inverse of Distance optimum: " + str(gaOptimal) print "GA time: " + str(gaTime) #print "GA Inverse of Distance: " + str(ef.value(ga.getOptimal())) print "Route:" path = [] for x in range(0, N): path.append(ga.getOptimal().getDiscrete(x)) print path iterOptimalOut.append(gaOptimal) iterTimeOut.append(gaTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() # for mimic we use a sort encoding ef = TravelingSalesmanSortEvaluationFunction(points) fill = [N] * N ranges = array('i', fill) odd = DiscreteUniformDistribution(ranges) df = DiscreteDependencyTree(.1, ranges) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) start = time.time() mimic = MIMIC(500, 100, pop) fit = FixedIterationTrainer(mimic, niter * mimicMult) fit.train() end = time.time() mimicOptimal = ef.value(mimic.getOptimal()) mimicTime = end - start print "MIMIC Inverse of Distance optimum: " + str(mimicOptimal) print "MIMIC time: " + str(mimicTime) #print "MIMIC Inverse of Distance: " + str(ef.value(mimic.getOptimal())) print "Route:" path = [] optimal = mimic.getOptimal() fill = [0] * optimal.size() ddata = array('d', fill) for i in range(0, len(ddata)): ddata[i] = optimal.getContinuous(i) order = ABAGAILArrays.indices(optimal.size()) ABAGAILArrays.quicksort(ddata, order) print order iterOptimalOut.append(mimicOptimal) iterTimeOut.append(mimicTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) optimalOut.append(iterOptimalOut) timeOut.append(iterTimeOut) evalsOut.append(iterEvals) return [optimalOut, timeOut, evalsOut]
def solveit(oaname, params): # set N value. This is the number of points N = 50 iterations = 1000 tryi = 1 random = Random() points = [[0 for x in xrange(2)] for x in xrange(N)] for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) if oaname == "RHC": iterations = int(params[0]) tryi = int(params[1]) oa = RandomizedHillClimbing(hcp) if oaname == "SA": oa = SimulatedAnnealing(float(params[0]), float(params[1]), hcp) if oaname == "GA": iterations=1000 oa = StandardGeneticAlgorithm(int(params[0]), int(params[1]), int(params[2]), gap) if oaname == "MMC": iterations=1000 # for mimic we use a sort encoding ef = TravelingSalesmanSortEvaluationFunction(points) fill = [N] * N ranges = array('i', fill) odd = DiscreteUniformDistribution(ranges) df = DiscreteDependencyTree(.1, ranges) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) oa = MIMIC(int(params[0]), int(params[1]), pop) print "Running %s using %s for %d iterations, try %d" % (oaname, ','.join(params), iterations, tryi) print "="*20 starttime = timeit.default_timer() output = [] for i in range(iterations): oa.train() if i%10 == 0: optimal = oa.getOptimal() score = ef.value(optimal) elapsed = int(timeit.default_timer()-starttime) output.append([str(i), str(score), str(elapsed)]) print 'Inverse of Distance [score]: %.3f' % score print 'train time: %d secs' % (int(timeit.default_timer()-starttime)) scsv = 'tsp-%s-%s.csv' % (oaname, '-'.join(params)) print "Saving to %s" % (scsv), with open(scsv, 'w') as csvf: writer = csv.writer(csvf) for row in output: writer.writerow(row) print "saved." print "="*20 print "Route:" if oaname == 'MMC': optimal = oa.getOptimal() fill = [0] * optimal.size() ddata = array('d', fill) for i in range(0,len(ddata)): ddata[i] = optimal.getContinuous(i) order = ABAGAILArrays.indices(optimal.size()) ABAGAILArrays.quicksort(ddata, order) print order else: path = [] for x in range(0,N): path.append(oa.getOptimal().getDiscrete(x)) print path
start_sa = time.time() fit_sa.train() end_sa = time.time() start_ga = time.time() fit_ga.train() end_ga = time.time() start_mimic = time.time() fit_mimic.train() end_mimic = time.time() # Result extracting last_training_time_rhc = end_rhc - start_rhc rhc_training_time[n].append(last_training_time_rhc) rhc_fitness[n].append(ef.value(rhc.getOptimal())) last_training_time_sa = end_sa - start_sa sa_training_time[n].append(last_training_time_sa) sa_fitness[n].append(ef.value(sa.getOptimal())) last_training_time_ga = end_ga - start_ga ga_training_time[n].append(last_training_time_ga) ga_fitness[n].append(ef.value(ga.getOptimal())) last_training_time_mimic = end_mimic - start_mimic mimic_training_time[n].append(last_training_time_mimic) mimic_fitness[n].append(ef.value(mimic.getOptimal())) overall_rhc_training_time = list_avg(*rhc_training_time) overall_rhc_fitness = list_avg(*rhc_fitness)
points[i][1] = random.nextDouble() ef = TravelingSalesmanRouteEvaluationFunction(points) odd = DiscretePermutationDistribution(N) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) expt = "expt_avg" rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, 200000) score_RHC.append(train(rhc, "RHC", ef, 200000, "test", expt)) print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) sa = SimulatedAnnealing(1E9, .98, hcp) fit = FixedIterationTrainer(sa, 200000) score_SA.append(train(sa, "SA", ef, 200000, "test", expt)) print "SA Inverse of Distance: " + str(ef.value(sa.getOptimal())) ga = StandardGeneticAlgorithm(225, 40, 5, gap) fit = FixedIterationTrainer(ga, 1000) score_GA.append(train(ga, "GA", ef, 40000, "test", expt)) print "GA Inverse of Distance: " + str(ef.value(ga.getOptimal())) # for mimic we use a sort encoding ef = TravelingSalesmanSortEvaluationFunction(points) fill = [N] * N ranges = array('i', fill)
mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) iters_list = [100, 500, 1000, 2500, 5000, 7500, 10000, 20000] print "Random Hill Climb" rhc = RandomizedHillClimbing(hcp) for iters in iters_list: fit = FixedIterationTrainer(rhc, iters) start = time.time() fit.train() dur = time.time() - start print "Iters: " + str(iters) + ", Fitness: " + str( ef.value(rhc.getOptimal())) + ", Dur: " + str(dur) # print "Route:" # path = [] # for x in range(0,N): # path.append(rhc.getOptimal().getDiscrete(x)) # print path print "Simulated Annealing" # 1e13, 0.8, 1e12 0.85, ... dang temp = 1E13 cooling_rate = 0.90 sa = SimulatedAnnealing(temp, cooling_rate, hcp) for iters in iters_list: fit = FixedIterationTrainer(sa, iters) start = time.time() fit.train()