-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuouspeaks.py
More file actions
158 lines (147 loc) · 5.84 KB
/
Copy pathcontinuouspeaks.py
File metadata and controls
158 lines (147 loc) · 5.84 KB
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
import time
import sys
sys.path.append("./ABAGAIL/ABAGAIL.jar")
import dist.DiscreteDependencyTree as DiscreteDependencyTree
import dist.DiscreteUniformDistribution as DiscreteUniformDistribution
import opt.DiscreteChangeOneNeighbor as DiscreteChangeOneNeighbor
import opt.GenericHillClimbingProblem as GenericHillClimbingProblem
import opt.RandomizedHillClimbing as RandomizedHillClimbing
import opt.SimulatedAnnealing as SimulatedAnnealing
import opt.ga.SingleCrossOver as SingleCrossOver
import opt.ga.DiscreteChangeOneMutation as DiscreteChangeOneMutation
import opt.ga.GenericGeneticAlgorithmProblem as GenericGeneticAlgorithmProblem
import opt.ga.StandardGeneticAlgorithm as StandardGeneticAlgorithm
import opt.prob.GenericProbabilisticOptimizationProblem as GenericProbabilisticOptimizationProblem
import opt.prob.MIMIC as MIMIC
import shared.FixedIterationTrainer as FixedIterationTrainer
import opt.example.ContinuousPeaksEvaluationFunction as ContinuousPeaksEvaluationFunction
from array import array
from time import clock
from itertools import product
from base import *
# Adapted from https://github.com/JonathanTay/CS-7641-assignment-2/blob/master/continuouspeaks.py
"""
Commandline parameter(s):
none
"""
N = 100
T = 29
maxIters = 5001
numTrials = 5
fill = [2] * N
ranges = array('i', fill)
outfile = OUTPUT_DIRECTORY + '/CONTPEAKS/CONTPEAKS_{}_{}_LOG.csv'
ef = ContinuousPeaksEvaluationFunction(T)
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
mf = DiscreteChangeOneMutation(ranges)
cf = SingleCrossOver()
df = DiscreteDependencyTree(.1, ranges)
hcp = GenericHillClimbingProblem(ef, odd, nf)
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
pop = GenericProbabilisticOptimizationProblem(ef, odd, df)
# RHC
for t in range(numTrials):
fname = outfile.format('RHC', str(t + 1))
with open(fname, 'w') as f:
f.write('iterations,fitness,time,fevals\n')
ef = ContinuousPeaksEvaluationFunction(T)
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
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
with open(fname, 'a') as f:
f.write(st)
# SA
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,fevals\n')
ef = ContinuousPeaksEvaluationFunction(T)
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
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
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], [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,fevals\n')
ef = ContinuousPeaksEvaluationFunction(T)
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
mf = DiscreteChangeOneMutation(ranges)
cf = SingleCrossOver()
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
with open(fname, 'a') as f:
f.write(st)
# MIMIC
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))
with open(fname, 'w') as f:
f.write('iterations,fitness,time,fevals\n')
ef = ContinuousPeaksEvaluationFunction(T)
odd = DiscreteUniformDistribution(ranges)
nf = DiscreteChangeOneNeighbor(ranges)
mf = DiscreteChangeOneMutation(ranges)
cf = SingleCrossOver()
gap = GenericGeneticAlgorithmProblem(ef, odd, mf, cf)
df = DiscreteDependencyTree(m, ranges)
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
with open(fname, 'a') as f:
f.write(st)