-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp.py
183 lines (168 loc) · 7.27 KB
/
tsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import sys
import os
import time
from time import clock
from itertools import product
from array import array
sys.path.append(os.path.join(os.path.dirname(__file__), "ABAGAIL/ABAGAIL.jar"))
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
"""
Commandline parameter(s):
none
"""
# set N value. This is the number of points
N = 100
random = Random()
maxIters = 3001
numTrials=5
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()
outfile = './output/tsp/tsp_{}_LOG.txt'
def run_mimic():
fill = [N] * N
ranges = array('i', fill)
odd = DiscreteUniformDistribution(ranges)
for t in range(numTrials):
for samples,keep,m in product([100],[50],[0.1,0.3,0.5,0.7,0.9]):
fname = outfile.format('MIMIC{}_{}_{}'.format(samples,keep,m), str(t+1))
df = DiscreteDependencyTree(m, ranges);
with open(fname,'w') as f:
f.write('iterations,fitness,time\n')
ef = TravelingSalesmanSortEvaluationFunction(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)
score = ef.value(mimic.getOptimal())
st = '{},{},{}\n'.format(i,score,times[-1])
print st
with open(fname,'a') as f:
f.write(st)
def run_rhc():
ef = TravelingSalesmanRouteEvaluationFunction(points)
odd = DiscretePermutationDistribution(N)
nf = SwapNeighbor()
mf = SwapMutation()
cf = TravelingSalesmanCrossOver(ef)
hcp = GenericHillClimbingProblem(ef, odd, nf)
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
for t in range(numTrials):
fname = outfile.format('RHC', str(t+1))
with open(fname,'w') as f:
f.write('iterations,fitness,time\n')
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)
score = ef.value(rhc.getOptimal())
st = '{},{},{}\n'.format(i,score,times[-1])
print st
with open(fname,'a') as f:
f.write(st)
def run_sa():
ef = TravelingSalesmanRouteEvaluationFunction(points)
odd = DiscretePermutationDistribution(N)
nf = SwapNeighbor()
mf = SwapMutation()
cf = TravelingSalesmanCrossOver(ef)
hcp = GenericHillClimbingProblem(ef, odd, nf)
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
for t in range(numTrials):
for CE in [0.15,0.35,0.55,0.75,0.95]:
fname = outfile.format('SA{}'.format(CE), str(t+1))
with open(fname,'w') as f:
f.write('iterations,fitness,time\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)
def run_ga():
ef = TravelingSalesmanRouteEvaluationFunction(points)
odd = DiscretePermutationDistribution(N)
nf = SwapNeighbor()
mf = SwapMutation()
cf = TravelingSalesmanCrossOver(ef)
hcp = GenericHillClimbingProblem(ef, odd, nf)
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
for t in range(numTrials):
for pop,mate,mutate in product([100],[50,30,10],[50,30,10]):
fname = outfile.format('GA{}_{}_{}'.format(pop,mate,mutate), str(t+1))
with open(fname,'w') as f:
f.write('iterations,fitness,time\n')
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)
score = ef.value(ga.getOptimal())
st = '{},{},{}\n'.format(i,score,times[-1])
print st
with open(fname,'a') as f:
f.write(st)
run_mimic()