6from skopt
import Optimizer
7from operator
import itemgetter
9algorithm_to_tune =
"cone_coloring"
12base_dir = os.path.dirname(os.path.abspath(__file__))
15full_path = os.path.join(base_dir, algorithm_to_tune)
18if os.path.exists(os.path.join(full_path,
"adapt.py")):
20 sys.path.append(full_path)
23 module = importlib.import_module(
"adapt")
25 print(f
"adapt.py not found in {algorithm_to_tune}")
28executable_path =
"./" + algorithm_to_tune +
"/bayesian_opt"
29export_path = algorithm_to_tune +
"/parameters_rank.txt"
35 param_str_list = list(map(str, params))
39 result = subprocess.run(
50 return float(result.stdout.strip())
51 except subprocess.CalledProcessError
as e:
53 print(
"Error while running C++ program:", e)
67best_value = float(
"inf")
68best_params = module.parameters_list[0]
73 top_5.append((params, value))
76 top_5.sort(key=itemgetter(1))
85 with open(export_path,
"a")
as f:
86 f.write(
"Top 5 parameter sets and their objective values:\n")
87 for i, (params, value)
in enumerate(top_5, 1):
88 f.write(f
"Rank {i}: Parameters = {params}, Objective Value = {value}\n")
89 print(
"\nTop 5 parameters have been saved to 'parameters_rank.txt'.")
94 print(
"\nTermination signal received. Exporting top 5 parameters...")
100signal.signal(signal.SIGINT, signal_handler)
104optimizer = Optimizer(module.param_space,
"GP", n_initial_points=10, random_state=
None)
106for params
in module.parameters_list:
108 optimizer.tell(params, result)
110 if result < best_value:
114 print(f
"New best parameters: {best_params}")
115 print(f
"New best objective value (to minimize): {best_value}")
120 next_params = optimizer.ask()
126 optimizer.tell(next_params, current_value)
129 if current_value < best_value:
130 best_value = current_value
131 best_params = next_params
133 print(f
"New best parameters: {best_params}")
134 print(f
"New best objective value (to minimize): {best_value}")
update_top_5(params, value)
signal_handler(sig, frame)
objective_function(params)