-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyoptuna_newPar_nomlu_nbarsEvents_newBarFlags.py
More file actions
133 lines (89 loc) · 3.33 KB
/
myoptuna_newPar_nomlu_nbarsEvents_newBarFlags.py
File metadata and controls
133 lines (89 loc) · 3.33 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
#!/usr/bin/env python
# coding: utf-8
"""
Optimization with various strategies:
For illustration, currently limited to tree ensemble classifiers
- Optuna: tree-structured Parzen estimator (TPE). Not limited to sklearn
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import json
import subprocess
import logging
FORMAT = '%(asctime)-15s- %(levelname)s - %(name)s -%(message)s'
logging.basicConfig(format=FORMAT, level=logging.INFO)
logger = logging.getLogger(__name__)
# optuna
import optuna
# gpyopt
#import GPyOpt
#from GPyOpt.methods import BayesianOptimization
def objective(trial):
mydict = {
#"mlu_scale":[0.,1.,False],
"top_gain":[0.1,1.5,False],
"top_spread":[0.001,0.3,True],
"qE_top":[0.001,0.5,True],
"kE_top":[0.001,0.5,True],
"bot_gain":[0.1,1.5,False],
"bot_spread":[0.001,0.3,True],
"qE_bot":[0.001,0.5,True],
"kE_bot":[0.001,0.5,True],
"gain_c":[0.001,2.5,True],
"lambda":[0.7,2,False],
"c_Ej":[0.5e8,2e8,False],
"t_smearing_top":[300e-12,1e-9,True],
"t_smearing_bot":[300e-12,1e-9,True]
# "t_offset_a":[-5e-9,5e-9,False],
# "t_walk_b_top":[0,5e-9,False],
# "t_walk_b_bot":[0,5e-9,False]
}
params=''
for key in mydict:
val = trial.suggest_float(key,mydict[key][0],mydict[key][1],log=mydict[key][2]) #log se è true
params += str(val) + ','
params=params[:-1]
command = "root -l -b -q macro_newPar_nomlu_nbarsEvents_newBarFlags.C+\(-1,"+ params +",104\) | grep double | awk '{print $2}' "
print(command)
out = subprocess.run(command,shell=True,capture_output=True)
x = float(out.stdout.decode())
return x
def optuna_mc(n_trials=100, timeout=600): #quando fermare ottimizzazione
"""
https://arxiv.org/pdf/1907.10902.pdf
https://optuna.org/
"""
SEED = 4005
logger.info("OPTUNA")
print("hello")
study = optuna.create_study(
direction="minimize",
sampler=optuna.samplers.TPESampler(seed=SEED),
pruner=optuna.pruners.MedianPruner(n_warmup_steps=10),
)
study.optimize(objective, n_trials=n_trials, timeout=timeout)
# fig = optuna.visualization.plot_intermediate_values(study) #non funzia
# fig.show()
#######################################################
print("\n\nThis is the end!!!!!\n\n")
logger.info(study.best_trial)
logger.info(study.best_value)
logger.info(study.best_params)
bestpardict = study.best_params
bestpar = study.best_params.values()
bestpar_str = str(bestpar)[13:-2].replace(" ","")
print("\n")
command = "root -l -b -q macro_newPar_nomlu_nbarsEvents_newBarFlags.C+\(-1,"+ bestpar_str +",104\) | grep double | awk '{print $2}' "
print(command)
subprocess.run(command,shell=True,capture_output=True)
print("\n")
file = open("../results/Tuning_ParametriComuni/TuningResults_NewPar/newPar_nomlu_nbarsEvents-1_run104_BestPars.txt", "w")
for key in bestpardict:
val = bestpardict[key]
file.write(key+" "+f"{val}\n")
file.close()
return study.best_trial
#https://www.blopig.com/blog/wp-content/uploads/2019/10/GPyOpt-Tutorial1.html
#def obj_func(x):
# return(out)