Skip to content

Commit

Permalink
Added verbosity setting
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriWeiss committed Oct 24, 2023
1 parent 3f18d39 commit a42959d
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 59 deletions.
42 changes: 27 additions & 15 deletions up_ac/AC_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand 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':
Expand All @@ -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,
Expand All @@ -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
19 changes: 12 additions & 7 deletions up_ac/Irace_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -230,16 +233,18 @@ 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)
self.incumbent = ac.run()

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:
Expand Down
22 changes: 14 additions & 8 deletions up_ac/OAT_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 '
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 12 additions & 7 deletions up_ac/Smac_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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:
Expand Down
56 changes: 36 additions & 20 deletions up_ac/configurators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
Expand All @@ -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,
Expand Down Expand Up @@ -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':
Expand All @@ -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
Expand All @@ -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}')
Loading

0 comments on commit a42959d

Please sign in to comment.