diff --git a/up_ac/AC_interface.py b/up_ac/AC_interface.py index eb1c22a..0e7bad5 100644 --- a/up_ac/AC_interface.py +++ b/up_ac/AC_interface.py @@ -21,6 +21,7 @@ def __init__(self): self.engine_param_spaces = {} self.engine_param_types = {} self.treader = treader(raise_on_error=True) + self.verbose = True def get_available_engines(self): """Get planning engines installed in up.""" @@ -142,32 +143,37 @@ def run_engine_config(self, config, metric, engine, if (result.status == up.engines.PlanGenerationResultStatus. SOLVED_SATISFICING): - print("Result found.\n") + if self.verbose: + print("Result found.\n") else: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = self.get_feedback(engine, metric, result) except: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = None else: with OneshotPlanner(name=engine, params=config) as planner: - print(config) + if self.verbose: + print(config) # result = planner.solve(problem) try: result = planner.solve(problem) - print('RESULT', result) - print(result.log_messages) if (result.status == up.engines.PlanGenerationResultStatus. SOLVED_SATISFICING): - print("Result found.\n") + if self.verbose: + print("Result found.\n") else: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = self.get_feedback(engine, metric, result) except: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = None elif plantype == 'AnytimePlanner': @@ -181,12 +187,15 @@ def run_engine_config(self, config, metric, engine, if (result.status == up.engines.PlanGenerationResultStatus. SOLVED_SATISFICING): - print("Result found.\n") + if self.verbose: + print("Result found.\n") else: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = self.get_feedback(engine, metric, result) except: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = None else: with AnytimePlanner(name=engine, @@ -196,12 +205,15 @@ def run_engine_config(self, config, metric, engine, if (result.status == up.engines.PlanGenerationResultStatus. SOLVED_SATISFICING): - print("Result found.\n") + if self.verbose: + print("Result found.\n") else: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = self.get_feedback(engine, metric, result) except: - print("No plan found.\n") + if self.verbose: + print("No plan found.\n") feedback = None return feedback diff --git a/up_ac/Irace_configurator.py b/up_ac/Irace_configurator.py index dc0a0e1..d5ce594 100644 --- a/up_ac/Irace_configurator.py +++ b/up_ac/Irace_configurator.py @@ -82,7 +82,8 @@ def solve(config, metric, engine, except (AssertionError, NotImplementedError, UPProblemDefinitionError, UPException, UnicodeDecodeError) as err: - print('\n** Error in planning engine!', err) + if self.verbose: + print('\n** Error in planning engine!', err) if metric == 'runtime': feedback = self.planner_timelimit elif metric == 'quality': @@ -121,8 +122,9 @@ def solve(config, metric, engine, return planner_feedback else: - print(f'Algorithm Configuration for {metric} of {engine} in' + - f' {mode} is not supported.') + if self.verbose: + print(f'Algorithm Configuration for {metric} of {engine} in' + + f' {mode} is not supported.') return None def set_scenario(self, engine, param_space, gaci, @@ -209,7 +211,8 @@ def set_scenario(self, engine, param_space, gaci, self.irace_param_space = gaci.irace_param_space - print('\nIrace scenario is set.\n') + if self.verbose: + print('\nIrace scenario is set.\n') self.scenario = scenario @@ -230,7 +233,8 @@ def optimize(self, feedback_function=None, gray_box=False): if feedback_function is not None: - print('\nStarting Parameter optimization\n') + if self.verbose: + print('\nStarting Parameter optimization\n') ac = irace(self.scenario, self.irace_param_space, feedback_function) @@ -238,8 +242,9 @@ def optimize(self, feedback_function=None, gray_box=False): self.incumbent = self.incumbent.to_dict(orient='records')[0] - print('\nBest Configuration found is:\n', - self.incumbent) + if self.verbose: + print('\nBest Configuration found is:\n', + self.incumbent) return self.incumbent, None else: diff --git a/up_ac/OAT_configurator.py b/up_ac/OAT_configurator.py index 283fccf..af2d561 100644 --- a/up_ac/OAT_configurator.py +++ b/up_ac/OAT_configurator.py @@ -128,7 +128,8 @@ def planner_thread(gb_out, problem, res, except: output = None if output is not None and len(output) not in (0, 1): - print('gray box:', output) + if self.verbose: + print('gray box:', output) if not res.empty(): thread.join() @@ -166,7 +167,8 @@ def solve(config, metric, engine, except (AssertionError, NotImplementedError, UPProblemDefinitionError): - print('\n** Error in planning engine!') + if self.verbose: + print('\n** Error in planning engine!') if metric == 'runtime': feedback = self.planner_timelimit elif metric == 'quality': @@ -208,8 +210,9 @@ def solve(config, metric, engine, return planner_feedback else: - print(f'Algorithm Configuration for {metric} of {engine} in' + \ - ' {mode} is not supported.') + if self.verbose: + print(f'Algorithm Configuration for {metric} of {engine} in' + \ + ' {mode} is not supported.') return None def set_scenario(self, engine, param_space, gaci, @@ -296,7 +299,8 @@ def optimize(self, feedback_function=None, gray_box=False): """ if feedback_function is not None: - print('\nStarting Parameter optimization\n') + if self.verbose: + print('\nStarting Parameter optimization\n') if self.scenario['metric'] == 'quality': tunefor = ' --byValue ' @@ -335,12 +339,14 @@ def optimize(self, feedback_function=None, gray_box=False): while p.poll() is None: line = p.stdout.readline() - print(line.decode('utf-8')) + if self.verbose: + print(line.decode('utf-8')) self.incumbent = self.get_OAT_incumbent() - print('\nBest Configuration found is:\n', - self.incumbent) + if self.verbose: + print('\nBest Configuration found is:\n', + self.incumbent) return self.incumbent, None else: diff --git a/up_ac/Smac_configurator.py b/up_ac/Smac_configurator.py index b1c337c..4b6eb76 100644 --- a/up_ac/Smac_configurator.py +++ b/up_ac/Smac_configurator.py @@ -67,7 +67,8 @@ def planner_feedback(config, instance, seed, reader): except (AssertionError, NotImplementedError, UPProblemDefinitionError, UPException, UnicodeDecodeError) as err: - print('\n** Error in planning engine!', err) + if self.verbose: + print('\n** Error in planning engine!', err) if metric == 'runtime': feedback = self.planner_timelimit elif metric == 'quality': @@ -148,8 +149,9 @@ def solve(config, metric, engine, return planner_feedback else: - print(f'Algorithm Configuration for {metric} of {engine}' + \ - f' in {mode} is not supported.') + if self.verbose: + print(f'Algorithm Configuration for {metric} of {engine}' + \ + f' in {mode} is not supported.') return None def set_scenario(self, engine, param_space, gaci, @@ -202,7 +204,8 @@ def set_scenario(self, engine, param_space, gaci, instances=instances, # List of training instances instance_features=instance_features # Dict of instance features ) - print('\nSMAC scenario is set.\n') + if self.verbose: + print('\nSMAC scenario is set.\n') self.scenario = scenario @@ -227,7 +230,8 @@ def optimize(self, feedback_function=None, gray_box=False): sys.path.append(r"{}".format(path)) from load_smac_feedback import get_feedback - print('\nStarting Parameter optimization\n') + if self.verbose: + print('\nStarting Parameter optimization\n') ac = AlgorithmConfigurationFacade( self.scenario, @@ -238,8 +242,9 @@ def optimize(self, feedback_function=None, gray_box=False): self.incumbent = self.incumbent.get_dictionary() - print('\nBest Configuration found is:\n', - self.incumbent) + if self.verbose: + print('\nBest Configuration found is:\n', + self.incumbent) return self.incumbent, None else: diff --git a/up_ac/configurators.py b/up_ac/configurators.py index 1cd5fde..42a932e 100644 --- a/up_ac/configurators.py +++ b/up_ac/configurators.py @@ -31,6 +31,7 @@ def __init__(self): self.metric = None self.crash_cost = 0 self.ac = None + self.verbose = True def print_feedback(self, engine, instance, feedback): """ @@ -41,8 +42,9 @@ def print_feedback(self, engine, instance, feedback): instance (str): Name of the instance. feedback: Feedback from the engine. """ - print(f'** Feedback of {engine} on instance\n**' + - f' {instance}\n** is {feedback}\n\n') + if self.verbose: + print(f'** Feedback of {engine} on instance\n**' + + f' {instance}\n** is {feedback}\n\n') def get_instance_features(self, instance_features=None): """ @@ -52,7 +54,8 @@ def get_instance_features(self, instance_features=None): instance_features (dict): Instance names and their features in lists. """ self.instance_features = instance_features - print('\nSetting instance features.\n') + if self.verbose: + print('\nSetting instance features.\n') def set_training_instance_set(self, train_set): """ @@ -62,7 +65,8 @@ def set_training_instance_set(self, train_set): train_set (list): List of instance paths. """ self.train_set = train_set - print('\nSetting training instance set.\n') + if self.verbose: + print('\nSetting training instance set.\n') def set_test_instance_set(self, test_set): """ @@ -72,7 +76,8 @@ def set_test_instance_set(self, test_set): test_set (list): List of instance paths. """ self.test_set = test_set - print('\nSetting testing instance set.\n') + if self.verbose: + print('\nSetting testing instance set.\n') def get_feedback_function(self, gaci, engine, metric, mode, gray_box=False): @@ -96,8 +101,9 @@ def get_feedback_function(self, gaci, engine, metric, mode, return planner_feedback else: - print(f'Algorithm Configuration for {metric} of {engine}' + \ - f' in {mode} is not supported.') + if self.verbose: + print(f'Algorithm Configuration for {metric} of {engine}' + \ + f' in {mode} is not supported.') return None def set_scenario(self, engine, param_space, gaci, @@ -208,7 +214,8 @@ def solve(incumbent, metric, engine, except (AssertionError, NotImplementedError, UPProblemDefinitionError): - print('\n** Error in planning engine!') + if self.verbose: + print('\n** Error in planning engine!') if metric == 'runtime': f = planner_timelimit elif metric == 'quality': @@ -229,24 +236,31 @@ def solve(incumbent, metric, engine, else: avg_f += self.crash_cost if metric == 'runtime': - print(f'\nFeedback on instance {inst}:\n\n', f, '\n') + if self.verbose: + print(f'\nFeedback on instance {inst}:\n\n', f, '\n') elif metric == 'quality': if f is not None: - print(f'\nFeedback on instance {inst}:\n\n', -f, '\n') + if self.verbose: + print(f'\nFeedback on instance {inst}:\n\n', -f, + '\n') else: - print(f'\nFeedback on instance {inst}:\n\n', None, - '\n') + if self.verbose: + print(f'\nFeedback on instance {inst}:\n\n', None, + '\n') if nr_inst != 0: avg_f = avg_f / nr_inst if metric == 'runtime': - print(f'\nAverage performance on {nr_inst} instances:', - avg_f, '\n') + if self.verbose: + print(f'\nAverage performance on {nr_inst} instances:', + avg_f, '\n') if metric == 'quality': - print(f'\nAverage performance on {nr_inst} instances:', - -avg_f, '\n') + if self.verbose: + print(f'\nAverage performance on {nr_inst} instances:', + -avg_f, '\n') return avg_f else: - print('\nPerformance could not be evaluated. No plans found.') + if self.verbose: + print('\nPerformance could not be evaluated. No plans found.') return None else: return None @@ -265,7 +279,9 @@ def save_config(self, path, config, gaci, engine): config = gaci.transform_conf_from_ac(engine, config) with open(f'{path}/incumbent_{engine}.json', 'w') as f: json.dump(config, f) - print('\nSaved best configuration in ' + - f'{path}/incumbent_{engine}.json\n') + if self.verbose: + print('\nSaved best configuration in ' + + f'{path}/incumbent_{engine}.json\n') else: - print(f'No configuration was saved. It was {config}') + if self.verbose: + print(f'No configuration was saved. It was {config}') diff --git a/up_ac/tests/test_lpg_OAT.py b/up_ac/tests/test_lpg_OAT.py index 5165d89..3104388 100644 --- a/up_ac/tests/test_lpg_OAT.py +++ b/up_ac/tests/test_lpg_OAT.py @@ -45,7 +45,7 @@ class TestOatLpgOnQuality(unittest.TestCase): configuration_time=30, n_trials=30, crash_cost=0, planner_timelimit=15, n_workers=3, instance_features=None, popSize=5, metric=metric, - evlaLimit=1) + evalLimit=1) OAC_fb_func = OAC.get_feedback_function(ogaci, engine[0], metric, 'OneshotPlanner') @@ -92,7 +92,7 @@ class TestOatLpgOnRuntime(unittest.TestCase): configuration_time=30, n_trials=30, crash_cost=0, planner_timelimit=15, n_workers=3, instance_features=None, popSize=5, metric=metric, - evlaLimit=1) + evalLimit=1) OAC_fb_func = OAC.get_feedback_function(ogaci, engine[0], metric, 'OneshotPlanner')