From d49ffcdfef723d5449fb7a85c02420156f30b3fa Mon Sep 17 00:00:00 2001 From: Ladislav Dokoupil Date: Sat, 23 Nov 2024 17:58:46 +0100 Subject: [PATCH] change policy tree output format --- paynt/quotient/mdp_family.py | 16 ++++++++-------- paynt/synthesizer/policy_tree.py | 15 ++++++--------- paynt/synthesizer/synthesizer.py | 6 +++--- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/paynt/quotient/mdp_family.py b/paynt/quotient/mdp_family.py index 49792104..bdfa1d8f 100644 --- a/paynt/quotient/mdp_family.py +++ b/paynt/quotient/mdp_family.py @@ -110,16 +110,16 @@ def policy_to_state_valuation_actions(self, policy): ] return state_valuation_to_action - def policy_to_json(self, state_valuation_to_action, indent=""): - import json - json_string = "[\n" + def policy_to_json(self, state_valuation_to_action): + """ DT-Control json format """ + json_whole = [] for index,valuation_action in enumerate(state_valuation_to_action): + json_unit = {} valuation,action = valuation_action - if index > 0: - json_string += ",\n" - json_string += indent + json.dumps(valuation_action) - json_string += "\n" + indent + "]" - return json_string + json_unit["c"] = [{"origin":{"action-label":action}}] + json_unit["s"] = valuation + json_whole.append(json_unit) + return json_whole diff --git a/paynt/synthesizer/policy_tree.py b/paynt/synthesizer/policy_tree.py index a249a2fe..469f9fd8 100644 --- a/paynt/synthesizer/policy_tree.py +++ b/paynt/synthesizer/policy_tree.py @@ -792,23 +792,20 @@ def evaluate_all(self, family, prop, keep_value_only=False): def run(self, optimum_threshold=None): - return self.evaluate(export_filename_base=paynt.synthesizer.synthesizer.Synthesizer.export_synthesis_filename_base) + return self.evaluate() def export_evaluation_result(self, evaluations, export_filename_base): import json policies = self.policy_tree.extract_policies(self.quotient) - policies_string = "{\n" + policies_json = {} for index,key_value in enumerate(policies.items()): policy_id,policy = key_value - if index > 0: - policies_string += ",\n" - policy_json = self.quotient.policy_to_json(policy, indent= " ") + policy_json = self.quotient.policy_to_json(policy) + policies_json[policy_id] = policy_json + policies_string = json.dumps(policies_json, indent=4) - policies_string += f'"{policy_id}" : {policy_json}' - policies_string += "}\n" - - policies_filename = export_filename_base + ".json" + policies_filename = export_filename_base + ".paynt.json" with open(policies_filename, 'w') as file: file.write(policies_string) diff --git a/paynt/synthesizer/synthesizer.py b/paynt/synthesizer/synthesizer.py index dac612a3..822bf012 100644 --- a/paynt/synthesizer/synthesizer.py +++ b/paynt/synthesizer/synthesizer.py @@ -119,7 +119,7 @@ def export_evaluation_result(self, evaluations, export_filename_base): ''' to be overridden ''' pass - def evaluate(self, family=None, prop=None, keep_value_only=False, print_stats=True, export_filename_base=None): + def evaluate(self, family=None, prop=None, keep_value_only=False, print_stats=True): ''' Evaluate each member of the family wrt the given property. :param family if None, then the design space of the quotient will be used @@ -143,8 +143,8 @@ def evaluate(self, family=None, prop=None, keep_value_only=False, print_stats=Tr self.stat.finished_evaluation(evaluations) logger.info("evaluation finished") - if export_filename_base is not None: - self.export_evaluation_result(evaluations, export_filename_base) + if self.export_synthesis_filename_base is not None: + self.export_evaluation_result(evaluations, self.export_synthesis_filename_base) if print_stats: self.stat.print()