-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbayesian_optimization.py
More file actions
72 lines (63 loc) · 2.77 KB
/
bayesian_optimization.py
File metadata and controls
72 lines (63 loc) · 2.77 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
import csv
import signal
from os.path import join
import utils
import hyperopt
import hyperopt.pyll.stochastic
import GenerateSettings
import main
from hyperopt import hp, fmin, tpe, STATUS_OK, Trials
from ResultParser import get_result_data, short_result_to_dict
space = {'ChainAction/ChainDeph': hp.choice('ChainAction/ChainDeph', [1, 2, 3, 4]),
'ChainAction/ChainNodeNumber': hp.choice('ChainAction/ChainNodeNumber',
utils.GenerateSpacedSamples(500, 1500, 100)),
}
i = 0
space_sample = hyperopt.pyll.stochastic.sample(space)
space_keys = space_sample.keys()
signal.signal(signal.SIGINT, main.exit_handler)
def objective(space):
global i
changes_dict = dict()
for key in space:
changes_dict[key] = [space[key]]
all_outputs = GenerateSettings.SettingGenerator(
main.ORIGINAL_BINARY_ADDRESS + main.SETTING_SUBDIR + main.SETTING_NAME,
changes_dict).generate()
all_outputs[0].write_to_file(main.storage_dir, str(i) + '.json')
values = []
for key in space_keys:
values += [space[key]]
setting_dst_address = join(main.TEST_BINARY_ADDRESS + main.SETTING_SUBDIR, main.SETTING_NAME)
print(f"Test values for {i}:{space}")
res = main.test_setting(join(main.storage_dir, f'{i}.json'), setting_dst_address)
short_data = get_result_data(res)
with open(f'./out/{main.TESTNAME}/results/RESULT_{i}', 'w') as res_file:
res_file.write(res)
with open(f'./out/{main.TESTNAME}/short_results_csv.csv', 'a', encoding='UTF8') as f:
writer = csv.writer(f, delimiter=';')
writer.writerow([f'{i}.json', main.TEST_OPPONENT_NAME] + short_data + values)
short_data_dict = short_result_to_dict(short_data)
accuracy = float(short_data_dict['goal_diff'])
i += 1
return {'loss': -accuracy, 'status': STATUS_OK}
main.remove_previous_binary()
main.copy_binary()
main.backup_old_result()
main.make_output_file_and_directories()
values_dict = '\n'.join(space_sample.keys())
with open(f'./out/{main.TESTNAME}/changed_values', 'w') as f:
f.write(values_dict)
with open(f'./out/{main.TESTNAME}/short_results_csv.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f, delimiter=';')
writer.writerow(['Filename', 'Opponent', 'Games Played', 'Invalid Games', 'Goal Difference', 'Goals Scored',
'Goals Conceded', 'Point Difference', 'Left Point', 'Right Point', 'Winrate',
'Expected Winrate'] + list(space_sample.keys()))
trials = Trials()
best = fmin(fn=objective,
space=space,
algo=tpe.suggest,
max_evals=5,
trials=trials, )
print(space['ChainAction/ChainDeph'])
print(hyperopt.space_eval(space, best))