Skip to content

Commit

Permalink
Merge pull request #45 from luinardi/intermediate_doe_write
Browse files Browse the repository at this point in the history
Write to csv as soon as the black-box is evaluated.
  • Loading branch information
arturluis authored Apr 13, 2021
2 parents 07ca253 + c190a4f commit 9689923
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 91 deletions.
56 changes: 18 additions & 38 deletions hypermapper/bo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy
import csv
import datetime
import os
import random
Expand All @@ -23,6 +22,9 @@
compute_data_array_scalarization,
get_single_configuration,
sample_weight_flat,
get_output_data_file,
create_output_data_file,
write_data_array,
)

except ImportError:
Expand Down Expand Up @@ -68,10 +70,13 @@
compute_data_array_scalarization,
get_single_configuration,
sample_weight_flat,
get_output_data_file,
create_output_data_file,
write_data_array,
)


def main(config, black_box_function=None, output_file="", profiling=None):
def main(config, black_box_function=None, profiling=None):
"""
Run design-space exploration using bayesian optimization.
:param config: dictionary containing all the configuration parameters of this optimization.
Expand All @@ -98,6 +103,9 @@ def main(config, black_box_function=None, output_file="", profiling=None):
evaluations_per_optimization_iteration = config[
"evaluations_per_optimization_iteration"
]
output_data_file = get_output_data_file(
config["output_data_file"], run_directory, application_name
)
batch_mode = evaluations_per_optimization_iteration > 1
number_of_cpus = config["number_of_cpus"]
print_importances = config["print_parameter_importance"]
Expand Down Expand Up @@ -166,13 +174,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
for objective in optimization_metrics:
objective_limits[objective] = [float("inf"), float("-inf")]

if output_file == "":
output_data_file = config["output_data_file"]
if output_data_file == "output_samples.csv":
output_data_file = application_name + "_" + output_data_file
else:
output_data_file = output_file

exhaustive_search_data_array = None
exhaustive_search_fast_addressing_of_data_array = None
if hypermapper_mode == "exhaustive":
Expand Down Expand Up @@ -251,6 +252,11 @@ def main(config, black_box_function=None, output_file="", profiling=None):
% absolute_configuration_index
)

create_output_data_file(
output_data_file, param_space.get_input_output_and_timestamp_parameters()
)
if data_array: # if it is not empty
write_data_array(param_space, data_array, output_data_file)
### DoE phase
if absolute_configuration_index < number_of_doe_samples:
configurations = []
Expand Down Expand Up @@ -278,6 +284,7 @@ def main(config, black_box_function=None, output_file="", profiling=None):
hypermapper_mode,
configurations,
beginning_of_time,
output_data_file,
black_box_function,
exhaustive_search_data_array,
exhaustive_search_fast_addressing_of_data_array,
Expand Down Expand Up @@ -312,6 +319,7 @@ def main(config, black_box_function=None, output_file="", profiling=None):
hypermapper_mode,
configurations,
beginning_of_time,
output_data_file,
black_box_function,
exhaustive_search_data_array,
exhaustive_search_fast_addressing_of_data_array,
Expand All @@ -326,20 +334,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
absolute_configuration_index += 1
optimization_iterations -= 1

# Create output file with explored configurations from resumed run and DoE
with open(
deal_with_relative_and_absolute_path(run_directory, output_data_file), "w"
) as f:
w = csv.writer(f)
w.writerow(param_space.get_input_output_and_timestamp_parameters())
tmp_list = [
param_space.convert_types_to_string(j, data_array)
for j in param_space.get_input_output_and_timestamp_parameters()
]
tmp_list = list(zip(*tmp_list))
for i in range(len(data_array[optimization_metrics[0]])):
w.writerow(tmp_list[i])

for objective in optimization_metrics:
lower_bound = min(objective_limits[objective][0], min(data_array[objective]))
upper_bound = max(objective_limits[objective][1], max(data_array[objective]))
Expand Down Expand Up @@ -486,6 +480,7 @@ def main(config, black_box_function=None, output_file="", profiling=None):
hypermapper_mode,
configurations,
beginning_of_time,
output_data_file,
black_box_function,
exhaustive_search_data_array,
exhaustive_search_fast_addressing_of_data_array,
Expand Down Expand Up @@ -520,21 +515,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
for header in configuration:
data_array[header].append(configuration[header])

# and save results
with open(
deal_with_relative_and_absolute_path(run_directory, output_data_file),
"a",
) as f:
w = csv.writer(f)
tmp_list = [
param_space.convert_types_to_string(j, new_data_array)
for j in list(
param_space.get_input_output_and_timestamp_parameters()
)
]
tmp_list = list(zip(*tmp_list))
for i in range(len(new_data_array[optimization_metrics[0]])):
w.writerow(tmp_list[i])
configurations = []
else:
# If we have not selected all points in the batch yet, add the model prediction as a 'liar'
Expand Down
35 changes: 14 additions & 21 deletions hypermapper/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# This script implements a simplification of the evolutionary process proposed by Real et al.: https://arxiv.org/abs/1802.01548v7#
###############################################################################################################################
import copy
import csv
import datetime
import os
import sys
Expand All @@ -20,6 +19,8 @@
get_single_configuration,
concatenate_list_of_dictionaries,
deal_with_relative_and_absolute_path,
get_output_data_file,
create_output_data_file,
)
except ImportError:
if os.getenv("HYPERMAPPER_HOME"): # noqa
Expand Down Expand Up @@ -58,6 +59,8 @@
get_single_configuration,
concatenate_list_of_dictionaries,
deal_with_relative_and_absolute_path,
get_output_data_file,
create_output_data_file,
)


Expand Down Expand Up @@ -115,6 +118,7 @@ def run_objective_function(
hypermapper_mode,
param_space,
beginning_of_time,
output_data_file,
run_directory,
evolution_data_array,
fast_addressing_of_data_array,
Expand Down Expand Up @@ -172,6 +176,7 @@ def run_objective_function(
hypermapper_mode,
new_configurations,
beginning_of_time,
output_data_file,
black_box_function,
run_directory=run_directory,
)
Expand Down Expand Up @@ -385,6 +390,9 @@ def main(config, black_box_function=None, output_file="", profiling=None):
print("Error: the black box function parameter is not callable")
raise SystemExit

output_data_file = get_output_data_file(
config["output_data_file"], run_directory, application_name
)
optimization_metrics = config["optimization_objectives"]
number_of_objectives = len(optimization_metrics)
if number_of_objectives != 1:
Expand Down Expand Up @@ -421,13 +429,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
if hypermapper_mode == "client-server":
sys.stdout.switch_log_only_on_file(True)

if output_file == "":
output_data_file = config["output_data_file"]
if output_data_file == "output_samples.csv":
output_data_file = application_name + "_" + output_data_file
else:
output_data_file = output_file

absolute_configuration_index = 0
fast_addressing_of_data_array = {}
evolution_fast_addressing_of_data_array = {}
Expand All @@ -440,12 +441,17 @@ def main(config, black_box_function=None, output_file="", profiling=None):
optimization_function_parameters["param_space"] = param_space
optimization_function_parameters["beginning_of_time"] = beginning_of_time
optimization_function_parameters["run_directory"] = run_directory
optimization_function_parameters["output_data_file"] = output_data_file
optimization_function_parameters["black_box_function"] = black_box_function
optimization_function_parameters["evolution_data_array"] = evolution_data_array
optimization_function_parameters[
"fast_addressing_of_data_array"
] = evolution_fast_addressing_of_data_array

create_output_data_file(
output_data_file, param_space.get_input_output_and_timestamp_parameters()
)

print("Starting evolution...")
evolution_t0 = datetime.datetime.now()
all_samples = evolution(
Expand Down Expand Up @@ -474,18 +480,5 @@ def main(config, black_box_function=None, output_file="", profiling=None):
)
)

with open(
deal_with_relative_and_absolute_path(run_directory, output_data_file), "w"
) as f:
w = csv.writer(f)
w.writerow(list(evolution_data_array.keys()))
tmp_list = [
param_space.convert_types_to_string(j, evolution_data_array)
for j in list(evolution_data_array.keys())
]
tmp_list = list(zip(*tmp_list))
for i in range(len(evolution_data_array[optimization_metrics[0]])):
w.writerow(tmp_list[i])

print("### End of the evolutionary search")
return evolution_data_array
40 changes: 16 additions & 24 deletions hypermapper/local_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import copy
import csv
import datetime
import os
import sys
Expand Down Expand Up @@ -28,6 +27,8 @@
dict_of_lists_to_numpy,
get_min_configurations,
get_min_feasible_configurations,
get_output_data_file,
create_output_data_file,
)
except ImportError:
if os.getenv("HYPERMAPPER_HOME"): # noqa
Expand Down Expand Up @@ -74,6 +75,8 @@
dict_of_lists_to_numpy,
get_min_configurations,
get_min_feasible_configurations,
get_output_data_file,
create_output_data_file,
)


Expand Down Expand Up @@ -163,6 +166,7 @@ def run_objective_function(
hypermapper_mode,
param_space,
beginning_of_time,
output_data_file,
run_directory,
local_search_data_array,
fast_addressing_of_data_array,
Expand Down Expand Up @@ -227,6 +231,7 @@ def run_objective_function(
hypermapper_mode,
new_configurations,
beginning_of_time,
output_data_file,
black_box_function,
exhaustive_search_data_array,
exhaustive_search_fast_addressing_of_data_array,
Expand Down Expand Up @@ -807,7 +812,7 @@ def parallel_multistart_local_search(
return data_array, best_configuration


def main(config, black_box_function=None, output_file="", profiling=None):
def main(config, black_box_function=None, profiling=None):
"""
Run design-space exploration using random scalarizations.
:param config: dictionary containing all the configuration parameters of this design-space exploration.
Expand All @@ -819,8 +824,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
run_directory = config["run_directory"]
application_name = config["application_name"]
hypermapper_mode = config["hypermapper_mode"]["mode"]
noise = config["noise"]

if hypermapper_mode == "default":
if black_box_function == None:
print("Error: the black box function must be provided")
Expand All @@ -829,6 +832,10 @@ def main(config, black_box_function=None, output_file="", profiling=None):
print("Error: the black box function parameter is not callable")
raise SystemExit

noise = config["noise"]
output_data_file = get_output_data_file(
config["output_data_file"], run_directory, application_name
)
optimization_metrics = config["optimization_objectives"]
number_of_objectives = len(optimization_metrics)
# local search will not produce reasonable output if run in parallel - it is therefore disabled
Expand Down Expand Up @@ -895,13 +902,6 @@ def main(config, black_box_function=None, output_file="", profiling=None):
if hypermapper_mode == "client-server":
sys.stdout.switch_log_only_on_file(True)

if output_file == "":
output_data_file = config["output_data_file"]
if output_data_file == "output_samples.csv":
output_data_file = application_name + "_" + output_data_file
else:
output_data_file = output_file

absolute_configuration_index = 0
fast_addressing_of_data_array = {}
local_search_fast_addressing_of_data_array = {}
Expand All @@ -914,6 +914,7 @@ def main(config, black_box_function=None, output_file="", profiling=None):
optimization_function_parameters["param_space"] = param_space
optimization_function_parameters["beginning_of_time"] = beginning_of_time
optimization_function_parameters["run_directory"] = run_directory
optimization_function_parameters["output_data_file"] = output_data_file
optimization_function_parameters[
"exhaustive_search_data_array"
] = exhaustive_search_data_array
Expand All @@ -936,6 +937,10 @@ def main(config, black_box_function=None, output_file="", profiling=None):
"enable_feasible_predictor"
] = enable_feasible_predictor

create_output_data_file(
output_data_file, param_space.get_input_output_and_timestamp_parameters()
)

print("Starting local search...")
local_search_t0 = datetime.datetime.now()
all_samples, best_configuration = local_search(
Expand All @@ -957,18 +962,5 @@ def main(config, black_box_function=None, output_file="", profiling=None):
% (len(local_search_data_array[optimization_metrics[0]]))
)

with open(
deal_with_relative_and_absolute_path(run_directory, output_data_file), "w"
) as f:
w = csv.writer(f)
w.writerow(list(local_search_data_array.keys()))
tmp_list = [
param_space.convert_types_to_string(j, local_search_data_array)
for j in list(local_search_data_array.keys())
]
tmp_list = list(zip(*tmp_list))
for i in range(len(local_search_data_array[optimization_metrics[0]])):
w.writerow(tmp_list[i])

print("### End of the local search.")
return local_search_data_array
Loading

0 comments on commit 9689923

Please sign in to comment.