-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbhh.py
307 lines (278 loc) · 7.82 KB
/
bhh.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# The MIT License (MIT)
# =====================
# Copyright 2021 Arné Schreuder
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the “Software”), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
import argparse
import os
import framework as fw
import params
# Globals
DATASET = None
OPTIMISER = None
SEED = None
LOG_LEVEL = None
PARAMS = None
LOG = None
HEURISTIC_POOL = None
POPULATION_SIZE = None
BURN_IN = None
REPLAY = None
RESELECTION = None
REANALYSIS = None
NORMALISE = None
CREDIT = None
DISCOUNTED_REWARDS = None
def parse_bhh_arguments():
global DATASET
global OPTIMISER
global SEED
global LOG_LEVEL
global PARAMS
global LOG
global HEURISTIC_POOL
global POPULATION_SIZE
global BURN_IN
global REPLAY
global RESELECTION
global REANALYSIS
global NORMALISE
global CREDIT
global DISCOUNTED_REWARDS
# Parser
parser = argparse.ArgumentParser(
description="Training Feedforward Neural Networks using Bayesian Hyper-Heuristics"
)
# Basic Params
parser.add_argument(
"--dataset",
type=str,
required=True,
choices=[
"abalone",
"air_quality",
"bank",
"bike",
"car",
"iris",
"diabetic",
"fish_toxicity",
"forest_fires",
"housing",
"mushroom",
"parkinsons",
"student_performance",
"wine_quality",
],
help="The dataset to use",
)
parser.add_argument("--seed", type=int, help="The seed to use")
parser.add_argument("--log-level", type=int, help="The log level to use")
# BHH Params
parser.add_argument(
"--heuristic-pool",
type=str,
default="all",
choices=[
"all",
"gd",
"mh",
],
help="The BHH heuristic pool to use",
)
parser.add_argument(
"--population-size",
type=int,
required=False,
choices=[5, 10, 15, 20, 25],
default=5,
help="The population size to use",
)
parser.add_argument(
"--burn_in",
type=int,
required=False,
choices=[0, 5, 10, 15, 20],
default=0,
help="The burn-in to use",
)
parser.add_argument(
"--replay",
type=int,
required=False,
choices=[1, 5, 10, 15, 20, 250], # Special case
default=10,
help="The replay buffer size to use",
)
parser.add_argument(
"--reselection",
type=int,
required=False,
choices=[1, 5, 10, 15, 20],
default=10,
help="The reselection interval to use",
)
parser.add_argument(
"--reanalysis",
type=int,
required=False,
choices=[1, 5, 10, 15, 20],
default=10,
help="The reanalysis interval to use",
)
parser.add_argument(
"--normalise",
type=bool,
required=False,
default=False,
help="The normalisation flag",
)
parser.add_argument(
"--credit",
type=str,
required=False,
choices=[
"ibest",
"pbest",
"rbest",
"gbest",
"symmetric",
],
default="ibest",
help="The credit assignment strategy to use",
)
parser.add_argument(
"--discounted-rewards",
type=bool,
required=False,
default=False,
help="The credit reward discount flag",
)
# Arguments
args = parser.parse_args()
HEURISTIC_POOL = args.heuristic_pool
DATASET = args.dataset
OPTIMISER = "bhh"
SEED = args.seed or None
POPULATION_SIZE = args.population_size
BURN_IN = args.burn_in
REPLAY = args.replay
RESELECTION = args.reselection
REANALYSIS = args.reanalysis
NORMALISE = args.normalise
CREDIT = args.credit
DISCOUNTED_REWARDS = args.discounted_rewards
print(DISCOUNTED_REWARDS)
LOG_LEVEL = int(os.getenv("LOG_LEVEL")) if os.getenv("LOG_LEVEL") is not None else 0
LOG = "logs/{}/bhh/hp:{}_ps:{}_bi:{}_rp:{}_rs:{}_ra:{}_nm:{}_ct:{}_dr:{}".format(
DATASET,
HEURISTIC_POOL,
POPULATION_SIZE,
BURN_IN,
REPLAY,
RESELECTION,
REANALYSIS,
NORMALISE,
CREDIT,
DISCOUNTED_REWARDS,
)
def print_bhh_banner():
global DATASET
global OPTIMISER
global SEED
global LOG
global LOG_LEVEL
global HEURISTIC_POOL
global POPULATION_SIZE
global BURN_IN
global REPLAY
global RESELECTION
global REANALYSIS
global NORMALISE
global CREDIT
global DISCOUNTED_REWARDS
print("")
print("====================================================================")
print("Training Feedforward Neural Networks using Bayesian Hyper-Heuristics")
print("====================================================================")
print("Dataset: {}".format(DATASET))
print("Optimiser: {}".format(OPTIMISER))
print("Seed: {}".format(SEED))
print("Log: {}".format(LOG))
print("Log Level: {}".format(LOG_LEVEL))
print("Heuristic Pool: {}".format(HEURISTIC_POOL))
print("Population Size: {}".format(POPULATION_SIZE))
print("Burn In: {}".format(BURN_IN))
print("Replay: {}".format(REPLAY))
print("Reselection: {}".format(RESELECTION))
print("Reanalysis: {}".format(REANALYSIS))
print("Normalise: {}".format(NORMALISE))
print("Credit: {}".format(CREDIT))
print("Discounted Rewards: {}".format(DISCOUNTED_REWARDS))
print("====================================================================")
print("")
def bhh_optimiser():
global DATASET
global HEURISTIC_POOL
global POPULATION_SIZE
global BURN_IN
global REPLAY
global RESELECTION
global REANALYSIS
global NORMALISE
global CREDIT
global DISCOUNTED_REWARDS
global SEED
# Extract intances
Experiment = params.params[DATASET]["experiment"]
Optimiser = fw.optimisers.BHH
PARAMS = params.get_bhh_defaults(
heuristic_pool=HEURISTIC_POOL,
experiment=DATASET,
population_size=POPULATION_SIZE,
burn_in=BURN_IN,
replay=REPLAY,
reselection=RESELECTION,
reanalysis=REANALYSIS,
normalise=NORMALISE,
credit=CREDIT,
discounted_rewards=DISCOUNTED_REWARDS,
)
LOG = "logs/{}/bhh/hp:{}_ps:{}_bi:{}_rp:{}_rs:{}_ra:{}_nm:{}_ct:{}_dr:{}/{}".format(
DATASET,
HEURISTIC_POOL,
POPULATION_SIZE,
BURN_IN,
REPLAY,
RESELECTION,
REANALYSIS,
NORMALISE,
CREDIT,
DISCOUNTED_REWARDS,
SEED,
)
experiment = Experiment(optimiser=Optimiser(params=PARAMS), log_dir=LOG, seed=SEED)
experiment.initialise()
experiment()
def main():
parse_bhh_arguments()
print_bhh_banner()
bhh_optimiser()
if __name__ == "__main__":
main()