def travelling_salesman(): N = 50 random = Random() random.setSeed(0) 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() fill = [N] * N ranges = array('i', fill) odd_mimic = DiscreteUniformDistribution(ranges) odd = DiscretePermutationDistribution(N) ef = TravelingSalesmanRouteEvaluationFunction(points) nf = SwapNeighbor() mf = SwapMutation() cf = TravelingSalesmanCrossOver(ef) rhc_generic("TSPrhc50", ef, odd, nf, 1.0, 10000, 10, 5) sa_generic("TSPsa50", ef, odd, nf, 1.0, 10000, 10, 5, ([1E12, 1E6], [0.999, 0.99, 0.95])) ga_generic("TSPga50", ef, odd, mf, cf, 50.0, 10000, 10, 1, ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02])) mimic_discrete("TSPmimic50", ef, odd_mimic, ranges, 300.0, 10000, 10, 1, ([200], [100], [0.1, 0.5, 0.9])) print "TSP all done"
def knapsack(): # Random number generator */ random = Random() random.setSeed(0) NUM_ITEMS = 40 # The number of items COPIES_EACH = 4 # The number of copies each MAX_WEIGHT = 50 # The maximum weight for a single element MAX_VOLUME = 50 # The maximum volume for a single element KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # The volume of the knapsack # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() rhc_generic("KnSrhc50", ef, odd, nf, 1.0, 10000, 10, 5) sa_generic("KnSsa50", ef, odd, nf, 1.0, 10000, 10, 5, ([1E12, 1E6], [0.999, 0.99, 0.95])) ga_generic("KnSga50", ef, odd, mf, cf, 50.0, 10000, 10, 1, ([2000, 200], [0.5, 0.25], [0.25, 0.1, 0.02])) mimic_discrete("KnSmimic50", ef, odd, ranges, 300.0, 10000, 10, 1, ([200], [100], [0.1, 0.5, 0.9])) print "KnS all done"
def get_ef(self): """Creates a new travelling salesman route evaluation function with the specified class variables. Returns ranges (array): Array of values as specified by N. ef (TravelingSalesmanEvaluationFunction): Evaluation function. """ random = Random() points = [[0 for x in xrange(2)] for x in xrange(self.N)] for i in range(0, len(points)): points[i][0] = random.nextDouble() points[i][1] = random.nextDouble() # create ranges fill = [self.N] * self.N ranges = array('i', fill) if self.subtype == 'route': return ranges, TravelingSalesmanRouteEvaluationFunction(points) elif self.subtype == 'sort': return ranges, TravelingSalesmanSortEvaluationFunction(points)
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 run_knapsack(): # Random number generator */ random = Random() # The number of items NUM_ITEMS = 40 # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) 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: " + str(ef.value(rhc.getOptimal())) sa_results = [] sa_times = [] for i in iters: print(i) for j in range(num_repeats): start = time.time() sa = SimulatedAnnealing(100, .95, hcp) fit = FixedIterationTrainer(sa, i) fit.train() end = time.time() sa_results.append(ef.value(sa.getOptimal())) sa_times.append(end - start) #print "SA: " + str(ef.value(sa.getOptimal())) ga_results = [] ga_times = [] for i in iters: print(i) for j in range(num_repeats): start = time.time() ga = StandardGeneticAlgorithm(200, 150, 25, gap) fit = FixedIterationTrainer(ga, i) fit.train() end = time.time() ga_results.append(ef.value(sa.getOptimal())) ga_times.append(end - start) #print "GA: " + str(ef.value(ga.getOptimal())) mimic_results = [] mimic_times = [] for i in iters[0:6]: print(i) for j in range(num_repeats): start = time.time() mimic = MIMIC(200, 100, pop) fit = FixedIterationTrainer(mimic, i) fit.train() end = time.time() mimic_results.append(ef.value(mimic.getOptimal())) mimic_times.append(end - start) #print "MIMIC: " + str(ef.value(mimic.getOptimal())) with open('knapsack.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
# The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 iters = 800000 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
def run_all(): problem = 'knapsack' # Random number generator */ random = Random() # The number of items NUM_ITEMS = 40 # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) maxEpochs = 400 columns = ['problem','label','score','epoch','time','avgTrainTime','iterations'] outFile = open(filename+'_all.csv','wb') fout = csv.writer(outFile,delimiter=',') fout.writerow(columns) def run_algo(alg,fit,label,iters): print(alg) trainTimes = [0.] trainTime = [] scores = [0] deltaScores = [] for epoch in range(0,maxEpochs,1): st = time.clock() fit.train() et = time.clock() trainTimes.append(trainTimes[-1]+(et-st)) trainTime.append((et-st)) rollingMean = 10 avgTime = (math.fsum(trainTime[-rollingMean:]) / float(rollingMean)) score = ef.value(alg.getOptimal()) scores.append(score) deltaScores.append(math.fabs(scores[-2] - scores[-1])) # trialString = '{}-{}-{}-{}'.format(label,score,epoch,trainTimes[-1]) trialData = [problem,label,score,epoch,trainTimes[-1],avgTime,iters] print(trialData) fout.writerow(trialData) iters = 10 rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iters) run_algo(rhc,fit,'RHC',10) startTemp = 1E11 coolingFactor = .95 sa = SimulatedAnnealing(startTemp, coolingFactor, hcp) fit = FixedIterationTrainer(sa, iters) run_algo(sa,fit,'HCP',10) population = 300 mates = 100 mutations = 50 ga = StandardGeneticAlgorithm(population, mates, mutations, gap) fit = FixedIterationTrainer(ga, iters) run_algo(ga,fit,'GA',10) samples = 200 keep = 20 mimic = MIMIC(samples, keep, pop) fit = FixedIterationTrainer(mimic, iters) run_algo(mimic,fit,'MIMIC',10) outFile.close()
def knapsackfunc(NUM_ITEMS, iterations): rhcMult = 600 saMult = 600 gaMult = 4 mimicMult = 3 # Random number generator */ random = Random() # The number of items #NUM_ITEMS = 40 # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) optimalOut = [] timeOut = [] evalsOut = [] for niter in iterations: iterOptimalOut = [NUM_ITEMS, niter] iterTimeOut = [NUM_ITEMS, niter] iterEvals = [NUM_ITEMS, 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 optimum: " + str(rhcOptimal) print "RHC time: " + str(rhcTime) iterOptimalOut.append(rhcOptimal) iterTimeOut.append(rhcTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() sa = SimulatedAnnealing(100, .95, hcp) fit = FixedIterationTrainer(sa, niter*saMult) fit.train() end = time.time() saOptimal = ef.value(sa.getOptimal()) saTime = end-start print "SA optimum: " + str(saOptimal) print "SA time: " + str(saTime) iterOptimalOut.append(saOptimal) iterTimeOut.append(saTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() ga = StandardGeneticAlgorithm(200, 150, 25, gap) fit = FixedIterationTrainer(ga, niter*gaMult) fit.train() end = time.time() gaOptimal = ef.value(ga.getOptimal()) gaTime = end - start print "GA optimum: " + str(gaOptimal) print "GA time: " + str(gaTime) iterOptimalOut.append(gaOptimal) iterTimeOut.append(gaTime) functionEvals = ef.getNumEvals() ef.zeroEvals() iterEvals.append(functionEvals) start = time.time() mimic = MIMIC(200, 100, pop) fit = FixedIterationTrainer(mimic, niter*mimicMult) fit.train() end = time.time() mimicOptimal = ef.value(mimic.getOptimal()) mimicTime = end - start print "MIMIC optimum: " + str(mimicOptimal) print "MIMIC time: " + str(mimicTime) 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]
MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
def nextDouble(self): return Random.nextDouble(self) * self.multiplier
from array import array """ Commandline parameter(s): none """ # 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) rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, 200000) fit.train() print "RHC Inverse of Distance: " + str(ef.value(rhc.getOptimal())) print "Route:"
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]
# NeuralNetwork. Build a bayesian Self-Organizing Map. Example I from java.util import Random from jhplot import * h1 = H1D("Data",20, -100.0, 300.0) r = Random() for i in range(2000): h1.fill(100+r.nextGaussian()*100) h1.fill(100+r.nextDouble()*100) p1d=P1D(h1,0,0) # write to a file p1d.toFile("data.txt") bs=HBsom() bs.setNPoints(30) bs.setData(p1d) bs.visible()
# @SciViewService svs # An example script that opens SciView and generates 25 random colored cubes from java.util import Random from cleargl import GLVector from array import array sv = svs.getOrCreateActiveSciView() num_points = 25 rng = Random() points = [[ rng.nextDouble() * 10, rng.nextDouble() * 10, rng.nextDouble() * 10 ] for k in range(num_points)] for point in points: box = sv.addBox() box.setPosition(GLVector(array('f', [point[0], point[1], point[2]]))) box.getMaterial().setDiffuse( GLVector( array('f', [rng.nextDouble(), rng.nextDouble(), rng.nextDouble()])))
""" Commandline parameter(s): none """ # Random number generator */ random = Random() # dimension N = 2 # number of peaks K = 50 # means of k-peaks mean = [[50*random.nextDouble() for x in xrange(N)] for x in xrange(K)] # standard deviations of k-peaks std = [[20*random.nextDouble() for x in xrange(N)] for x in xrange(K)] # heights of k-peaks height = [1000*random.nextDouble() for x in xrange(K)]; # range of bit strings fill = [100] * N ranges = array('i', fill) ef = KHillsEvaluationFunction(mean, std, height) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges)
def main(): # The number of items NUM_ITEMS = 40 # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 iterations = 20000 gaIters = 1000 mimicIters = 1000 gaPop = 200 gaMate = 150 gaMutate = 25 mimicSamples = 200 mimicToKeep = 100 saTemp = 100 saCooling = .95 alg = 'all' run = 0 settings = [] try: opts, args = getopt.getopt(sys.argv[1:], "ahrsgmn:N:c:w:v:i:", ["gaIters=", "mimicIters=","gaPop=", "gaMate=", "gaMutate=", "mimicSamples=", "mimicToKeep=", "saTemp=", "saCooling="]) except: print 'knapsack.py -i <iterations> -n <NUM_ITEMS> -c <COPIES_EACH> -w <MAX_WEIGHT> -v <MAX_VOLUME>' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'knapsack.py -i <iterations> -n <NUM_ITEMS> -c <COPIES_EACH> -w <MAX_WEIGHT> -v <MAX_VOLUME>' sys.exit(1) elif opt == '-i': iterations = int(arg) elif opt == '-N': NUM_ITEMS = int(arg) elif opt == '-c': COPIES_EACH = int(arg) elif opt == '-w': MAX_WEIGHT = int(arg) elif opt == '-v': MAX_VOLUME = int(arg) elif opt == '-n': run = int(arg) elif opt == '-r': alg = 'RHC' elif opt == '-s': alg = 'SA' elif opt == '-g': alg = 'GA' elif opt == '-m': alg = 'MIMIC' elif opt == '-a': alg = 'all' elif opt == '--gaPop': gaPop = int(arg) elif opt == '--gaMate': gaMate = int(arg) elif opt == '--gaMutate': gaMutate = int(arg) elif opt == '--mimicSamples': mimicSamples = int(arg) elif opt == '--mimicToKeep': mimicToKeep = int(arg) elif opt == '--saTemp': saTemp = float(arg) elif opt == '--saCooling': saCooling = float(arg) elif opt == '--gaIters': gaIters = int(arg) elif opt == '--mimicIters': mimicIters = int(arg) vars ={ 'NUM_ITEMS' : NUM_ITEMS, 'COPIES_EACH' : COPIES_EACH, 'MAX_WEIGHT' : MAX_WEIGHT, 'MAX_VOLUME' : MAX_VOLUME, 'iterations' : iterations, 'gaIters' : gaIters, 'mimicIters' : mimicIters, 'gaPop' : gaPop, 'gaMate' : gaMate, 'gaMutate' : gaMutate, 'mimicSamples' : mimicSamples, 'mimicToKeep' : mimicToKeep, 'saTemp' : saTemp, 'saCooling' : saCooling, 'alg' : alg, 'run' : run } settings = getSettings(alg, settings, vars) # Random number generator */ random = Random() # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) if alg == 'RHC' or alg == 'all': rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iterations) fit.train() print "RHC: " + str(ef.value(rhc.getOptimal())) rows = [] row = [] row.append("Evaluation Function Value") row.append(str(ef.value(rhc.getOptimal()))) rows.append(row) output2('Knapsack', 'RHC', rows, settings) rows = [] buildFooter("Knapsack", "RHC", rows, settings) outputFooter("Knapsack", "RHC", rows , settings) if alg == 'SA' or alg == 'all': sa = SimulatedAnnealing(saTemp, saCooling, hcp) fit = FixedIterationTrainer(sa, iterations) fit.train() rows = [] row = [] row.append("Evaluation Function Value") row.append(ef.value(sa.getOptimal())) rows.append(row) print "SA: " + str(ef.value(sa.getOptimal())) output2('Knapsack', 'SA', rows, settings) rows = [] buildFooter("Knapsack", "SA", rows, settings) outputFooter("Knapsack", "SA", rows, settings) if alg == 'GA' or alg == 'all': ga = StandardGeneticAlgorithm(gaPop, gaMate, gaMutate, gap) fit = FixedIterationTrainer(ga, gaIters) fit.train() rows = [] row = [] row.append("Evaluation Function Value") row.append(ef.value(ga.getOptimal())) rows.append(row) print "GA: " + str(ef.value(ga.getOptimal())) output2('Knapsack', 'GA', rows, settings) buildFooter("Knapsack", "GA", rows, settings) outputFooter("Knapsack", "GA", rows , settings) if alg == 'MIMIC' or alg == 'all': mimic = MIMIC(mimicSamples, mimicToKeep, pop) fit = FixedIterationTrainer(mimic, mimicIters) fit.train() print "MIMIC: " + str(ef.value(mimic.getOptimal())) rows = [] row = [] row.append("Evaluation Function Value") row.append(ef.value(mimic.getOptimal())) rows.append(row) output2('Knapsack', 'MIMIC', rows, settings) rows = [] buildFooter("Knapsack", "MIMIC", rows, settings) outputFooter("Knapsack", "MIMIC", rows , settings)
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
def solveit(oaname, params): iterations = 10000 tryi = 1 # Random number generator */ random = Random() # The number of items NUM_ITEMS = 40 # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) 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 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 'score: %.3f' % score print 'train time: %d secs' % (int(timeit.default_timer() - starttime)) scsv = 'kn-%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 "####" return ef.value(alg_func.getOptimal()) """ Commandline parameter(s): none """ # set N value. This is the number of points N = 25 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) # #Try 5 rounds of RHC # expt = "expt_Restarts" # for i in range(5): # rhc = RandomizedHillClimbing(hcp) # train(rhc, "RHC", ef, 20000, "round=" + str(i), expt)
def run_all_2(N=40,fout=None): maxEpochs = 10**5 maxTime = 300 #5 minutes problem = 'knapsack' # Random number generator */ random = Random() # The number of items NUM_ITEMS = N # The number of copies each COPIES_EACH = 4 # The maximum weight for a single element MAX_WEIGHT = 50 # The maximum volume for a single element MAX_VOLUME = 50 # The volume of the knapsack KNAPSACK_VOLUME = MAX_VOLUME * NUM_ITEMS * COPIES_EACH * .4 # create copies fill = [COPIES_EACH] * NUM_ITEMS copies = array('i', fill) # create weights and volumes fill = [0] * NUM_ITEMS weights = array('d', fill) volumes = array('d', fill) for i in range(0, NUM_ITEMS): weights[i] = random.nextDouble() * MAX_WEIGHT volumes[i] = random.nextDouble() * MAX_VOLUME # create range fill = [COPIES_EACH + 1] * NUM_ITEMS ranges = array('i', fill) ef = KnapsackEvaluationFunction(weights, volumes, KNAPSACK_VOLUME, copies) odd = DiscreteUniformDistribution(ranges) nf = DiscreteChangeOneNeighbor(ranges) mf = DiscreteChangeOneMutation(ranges) cf = UniformCrossOver() df = DiscreteDependencyTree(.1, ranges) hcp = GenericHillClimbingProblem(ef, odd, nf) gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf) pop = GenericProbabilisticOptimizationProblem(ef, odd, df) def run_algo(alg,fit,label,difficulty,iters): trainTimes = [0.] trainTime = [] scoreChange = [0.] stuckCount = 10**3 prev = 0. for epoch in range(0,maxEpochs,1): st = time.clock() fit.train() et = time.clock() trainTimes.append(trainTimes[-1]+(et-st)) trainTime.append((et-st)) rollingMean = 10 avgTime = (math.fsum(trainTime[-rollingMean:]) / float(rollingMean)) score = ef.value(alg.getOptimal()) # trialString = '{}-{}-{}-{}'.format(label,score,epoch,trainTimes[-1]) trialData = [problem,difficulty,label,score,epoch,trainTimes[-1],avgTime,iters] # print(trialData) # fout.writerow(trialData) # print(trialData) print(trialData,max(scoreChange)) # print(max(scoreChange)) # optimum = (difficulty-1-T) + difficulty # if score >= optimum: break scoreChange.append(abs(score-prev)) prev = score scoreChange = scoreChange[-stuckCount:] # print(scoreChange) if max(scoreChange) < 1.0: break if trainTimes[-1] > maxTime: break # print(trialData) fout.writerow(trialData) iters = 1000 rhc = RandomizedHillClimbing(hcp) fit = FixedIterationTrainer(rhc, iters) run_algo(rhc,fit,'RHC',N,iters) iters = 1000 startTemp = 1E10 coolingFactor = .99 sa = SimulatedAnnealing(startTemp, coolingFactor, hcp) fit = FixedIterationTrainer(sa, iters) run_algo(sa,fit,'SA',N,iters) iters = 10 population = 300 mates = 100 mutations = 50 ga = StandardGeneticAlgorithm(population, mates, mutations, gap) fit = FixedIterationTrainer(ga, iters) run_algo(ga,fit,'GA',N,iters) iters = 10 samples = 200 keep = 20 mimic = MIMIC(samples, keep, pop) fit = FixedIterationTrainer(mimic, iters) run_algo(mimic,fit,'MIMIC',N,iters)