diff --git a/coleman4hcs/environment.py b/coleman4hcs/environment.py index 8981d42..af5ef2e 100644 --- a/coleman4hcs/environment.py +++ b/coleman4hcs/environment.py @@ -44,7 +44,7 @@ class Environment: def __init__(self, agents, scenario_provider, evaluation_metric): self.agents = agents self.scenario_provider = scenario_provider - self.evaluation_metric = evaluation_metric + self.evaluation_metric = evaluation_metric self.reset() def reset(self): @@ -54,7 +54,7 @@ def reset(self): self.reset_agents_memory() # Monitor saves the feedback during the process self.monitor = MonitorCollector() - + self.variant_monitors = {} if isinstance(self.scenario_provider, IndustrialDatasetHCSScenarioProvider) and \ @@ -71,13 +71,13 @@ def reset_agents_memory(self): def run_single(self, experiment, - trials=100, + trials=100, bandit_type: DynamicBandit = EvaluationMetricBandit, restore=True): """ Execute a single simulation experiment :param experiment: Current Experiment - :param trials: The max number of scenarios that will be analyzed + :param trials: The max number of scenarios that will be analyzed :param bandit_type: :param restore: restore the experiment if fail (i.e., energy down) """ @@ -158,7 +158,7 @@ def run_prioritization(self, agent, bandit, bandit_duration, experiment, t, vsc) :param agent: The agent that is being used for the prioritization. :param bandit: The bandit mechanism used for choosing actions. :param bandit_duration: Time taken by the bandit process. - :param experiment: The current experiment number. + :param experiment: The current experiment number. :param t: The current step or iteration of the simulation. :param vsc: The virtual scenario being considered. :return: tuple containing the chosen action by the agent, the ending time of the process, @@ -193,7 +193,7 @@ def run_prioritization(self, agent, bandit, bandit_duration, experiment, t, vsc) # Compute end time end = time.time() - + logging.debug(f"Exp: {experiment} - Ep: {t} - Name: {exp_name} ({str(agent.get_reward_function())}) - " + f"NAPFD/APFDc: {metric.fitness:.4f}/{metric.cost:.4f}") @@ -299,7 +299,7 @@ def run(self, experiments=1, trials=100, bandit_type: DynamicBandit = Evaluation """ Execute a simulation :param experiments: Number of experiments - :param trials: The max number of scenarios that will be analyzed + :param trials: The max number of scenarios that will be analyzed :param bandit_type: :param restore: restore the experiment if fail (i.e., energy down) :return: @@ -312,7 +312,7 @@ def run(self, experiments=1, trials=100, bandit_type: DynamicBandit = Evaluation def create_file(self, name): """ Create a file to store the results obtained during the experiment - """ + """ self.monitor.create_file(name) # If we are working with HCS scenario, we create a file for each variant in a specific directory @@ -331,10 +331,10 @@ def create_file(self, name): def store_experiment(self, csv_file_name): """ Save the results obtained during the experiment - """ + """ # Collect from temp and save a file (backup and easy sharing/auditing) self.monitor.save(csv_file_name) - + if isinstance(self.scenario_provider, IndustrialDatasetHCSScenarioProvider): if self.scenario_provider.get_total_variants() > 0: # Ignore the extension @@ -347,7 +347,7 @@ def store_experiment(self, csv_file_name): # Collect from temp and save a file (backup and easy sharing/auditing) self.variant_monitors[variant].save( f"{name2}/{csv_file_name.split('/')[-1].split('@')[0]}@{variant.replace('/', '-')}.csv") - + def load_experiment(self, experiment): """ Load the backup diff --git a/main.py b/main.py index 24d1a2f..0bc91f7 100644 --- a/main.py +++ b/main.py @@ -77,7 +77,7 @@ def exp_run_industrial_dataset(iteration, trials, env: Environment, experiment_d csv_file_name = f"{experiment_directory}{str(env.scenario_provider)}_{iteration}.csv" env.create_file(csv_file_name) env.run_single(iteration, trials) - env.store_experiment(csv_file_name) + env.store_experiment(csv_file_name) def load_class_from_module(module, class_name: str): @@ -196,10 +196,10 @@ def merge_csv(files, output_file): """ # Merge all CSV files into one DataFrame df = pd.concat([pd.read_csv(file, sep=';') for file in files], ignore_index=True) - + # Save the merged DataFrame to CSV - df.to_csv(output_file, index=False, sep=';', quoting=csv.QUOTE_NONE) - + df.to_csv(output_file, index=False, sep=';', quoting=csv.QUOTE_NONE) + # Optionally, clean up temporary files for file in files: os.remove(file) @@ -232,7 +232,7 @@ def store_experiments(csv_file, scenario): Example usage: >>> store_experiments("experiment_results.csv", my_scenario) """ - # Create/Open a database to store the results + # Create/Open a database to store the results conn = duckdb.connect('experiments.db') # Ensure the tables exist with the appropriate schema @@ -261,12 +261,12 @@ def store_experiments(csv_file, scenario): prioritization_order VARCHAR ); """) - + df = conn.read_csv(csv_file, delimiter=';', quotechar='"', header=True) # Insert the DataFrame into the 'experiments' table conn.execute("INSERT INTO experiments SELECT * FROM df;") - + if isinstance(scenario, IndustrialDatasetHCSScenarioProvider): if scenario.get_total_variants() > 0: # Ignore the extension @@ -275,9 +275,9 @@ def store_experiments(csv_file, scenario): Path(name2).mkdir(parents=True, exist_ok=True) - for variant in scenario.get_all_variants(): + for variant in scenario.get_all_variants(): csv_file_variant = f"{name2}/{csv_file.split('/')[-1].split('@')[0]}@{variant.replace('/', '-')}.csv" - conn.execute( f"COPY experiments FROM '{csv_file_variant}' (HEADER);") + conn.execute( f"COPY experiments FROM '{csv_file_variant}' (HEADER);") df = conn.read_csv(csv_file, delimiter=';', quotechar='"', header=True) # Insert the DataFrame into the 'experiments' table @@ -292,11 +292,11 @@ def store_experiments(csv_file, scenario): # Execution configuration ( parallel_pool_size, - independent_executions, + independent_executions, verbose ) = map(config['execution'].get, [ 'parallel_pool_size', - 'independent_executions', + 'independent_executions', 'verbose' ]) @@ -367,7 +367,7 @@ def store_experiments(csv_file, scenario): logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') else: logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') - + for tr in sched_time_ratio: experiment_directory = os.path.join(experiment_dir, f"time_ratio_{int(tr * 100)}/") @@ -379,12 +379,12 @@ def store_experiments(csv_file, scenario): # Stop conditional trials = scenario.max_builds - + # Prepare the experiment env = Environment(agents, scenario, evaluation_metric) parameters = [(i + 1, trials, env, experiment_directory) for i in range(independent_executions)] - + # Compute time start = time.time() @@ -395,14 +395,14 @@ def store_experiments(csv_file, scenario): for param in parameters: exp_run_industrial_dataset(*param) - end = time.time() + end = time.time() # Read and merge the independent executions - csv_file_names = [f"{experiment_directory}{str(env.scenario_provider)}_{i+1}.csv" for i in range(independent_executions)] + csv_file_names = [f"{experiment_directory}{str(env.scenario_provider)}_{i+1}.csv" for i in range(independent_executions)] csv_file = f"{experiment_directory}{str(env.scenario_provider)}.csv" merge_csv(csv_file_names, csv_file) - + # Store the results in the duckdb database store_experiments(csv_file, scenario) - + logging.info(f"Time expend to run the experiments: {end - start}\n\n")