From 47d2cbc19b2d47307f2fc0708d06c1e6fa1f220f Mon Sep 17 00:00:00 2001 From: scivm Date: Tue, 14 Oct 2014 21:44:05 +0300 Subject: [PATCH] add jython version of traveling salesman --- jython/travelingsalesman.py | 129 ++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 jython/travelingsalesman.py diff --git a/jython/travelingsalesman.py b/jython/travelingsalesman.py new file mode 100644 index 00000000..5220fa40 --- /dev/null +++ b/jython/travelingsalesman.py @@ -0,0 +1,129 @@ +# traveling salesman algorithm implementation in jython +# This also prints the index of the points of the shortest route. +# To make a plot of the route, write the points at these indexes +# to a file and plot them in your favorite tool. +import sys +import os +import time + +import java.io.FileReader as FileReader +import java.io.File as File +import java.lang.String as String +import java.lang.StringBuffer as StringBuffer +import java.lang.Boolean as Boolean +import java.util.Random as Random + +import dist.DiscreteDependencyTree as DiscreteDependencyTree +import dist.DiscreteUniformDistribution as DiscreteUniformDistribution +import dist.Distribution as Distribution +import dist.DiscretePermutationDistribution as DiscretePermutationDistribution +import opt.DiscreteChangeOneNeighbor as DiscreteChangeOneNeighbor +import opt.EvaluationFunction as EvaluationFunction +import opt.GenericHillClimbingProblem as GenericHillClimbingProblem +import opt.HillClimbingProblem as HillClimbingProblem +import opt.NeighborFunction as NeighborFunction +import opt.RandomizedHillClimbing as RandomizedHillClimbing +import opt.SimulatedAnnealing as SimulatedAnnealing +import opt.example.FourPeaksEvaluationFunction as FourPeaksEvaluationFunction +import opt.ga.CrossoverFunction as CrossoverFunction +import opt.ga.SingleCrossOver as SingleCrossOver +import opt.ga.DiscreteChangeOneMutation as DiscreteChangeOneMutation +import opt.ga.GenericGeneticAlgorithmProblem as GenericGeneticAlgorithmProblem +import opt.ga.GeneticAlgorithmProblem as GeneticAlgorithmProblem +import opt.ga.MutationFunction as MutationFunction +import opt.ga.StandardGeneticAlgorithm as StandardGeneticAlgorithm +import opt.ga.UniformCrossOver as UniformCrossOver +import opt.prob.GenericProbabilisticOptimizationProblem as GenericProbabilisticOptimizationProblem +import opt.prob.MIMIC as MIMIC +import opt.prob.ProbabilisticOptimizationProblem as ProbabilisticOptimizationProblem +import shared.FixedIterationTrainer as FixedIterationTrainer +import opt.example.TravelingSalesmanEvaluationFunction as TravelingSalesmanEvaluationFunction +import opt.example.TravelingSalesmanRouteEvaluationFunction as TravelingSalesmanRouteEvaluationFunction +import opt.SwapNeighbor as SwapNeighbor +import opt.ga.SwapMutation as SwapMutation +import opt.example.TravelingSalesmanCrossOver as TravelingSalesmanCrossOver +import opt.example.TravelingSalesmanSortEvaluationFunction as TravelingSalesmanSortEvaluationFunction +import shared.Instance as Instance +import util.ABAGAILArrays as ABAGAILArrays + +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:" +path = [] +for x in range(0,N): + path.append(rhc.getOptimal().getDiscrete(x)) +print path + +sa = SimulatedAnnealing(1E12, .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 + + +ga = StandardGeneticAlgorithm(2000, 1500, 250, gap) +fit = FixedIterationTrainer(ga, 1000) +fit.train() +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 + + +# 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(500, 100, pop) +fit = FixedIterationTrainer(mimic, 1000) +fit.train() +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