From 6e926426f49035c70c7b5a8173c9c2eb04076c62 Mon Sep 17 00:00:00 2001
From: dmt <>
Date: Sun, 3 Nov 2019 20:43:58 +0100
Subject: [PATCH] Save pragmatic model only if all 4 deconstruction strategies
 failed.

---
 cml/domain/deconstruction.py | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/cml/domain/deconstruction.py b/cml/domain/deconstruction.py
index b4f3b74..02d23c8 100644
--- a/cml/domain/deconstruction.py
+++ b/cml/domain/deconstruction.py
@@ -272,13 +272,12 @@ class Deconstructor:
         self.source = source
         self.logger = None
         self.reconstructor = None
-        self.free_id = None
 
     def _strategies(self, block):
-        yield (("T", "Sigma"), partial(self.deconstruct_time_sigma, block=block))
-        yield (("T", "Z"), partial(self.deconstruct_time_zeta, block=block))
-        yield (("Sigma", "Z"), partial(self.deconstruct_sigma_zeta, block=block))
         yield (("complete", ), partial(self.deconstruct_complete, block=block))
+        yield (("Sigma", "Z"), partial(self.deconstruct_sigma_zeta, block=block))
+        yield (("T", "Sigma"), partial(self.deconstruct_time_sigma, block=block))
+        # yield (("T", "Z"), partial(self.deconstruct_time_zeta, block=block))
 
     @log
     def deconstruct(self,
@@ -286,6 +285,7 @@ class Deconstructor:
                     prag_model: PragmaticMachineLearningModel,
                     learnblock) -> None:
 
+        success = False
         ###################################################################
         self.logger.protocol(self.settings)
         self.logger.protocol("{:<20}: {}".format("Pragmatic", str(prag_model)))
@@ -305,31 +305,29 @@ class Deconstructor:
 
                 try:
                     strategy(tier, prag_model, relative)
-                    ###########################################################
-                    self.logger.protocol("Deconstruction SUCCESS!")
-                    ###########################################################
+                    success = True
                     if self.settings.deconst_mode == "minimal": return
 
                 except NoModelReconstructedError:
                     continue
 
                 except DeconstructionFailed:
-                    ###########################################################
-                    self.logger.protocol("Deconstruction FAILED!")
-                    ###########################################################
                     continue
 
                 except Exception as error:
                     print(error.with_traceback())
 
+        if not success:
+            # All deconstructions failed, so save the pragmatic model
+            self.knowledge_database.insert(prag_model)
+
     def deconstruct_time_sigma(self,
                                tier: int,
                                p_model: PragmaticMachineLearningModel,
                                r_model: PragmaticMachineLearningModel,
                                block):
         success = False
-        self.knowledge_database.insert(p_model)
-        if r_model and p_model.tier < HIGHEST_TIER-1:
+        if r_model and p_model.tier < self.settings.highest_tier-1:
 
             # Get learnblock that trained relative model
             second_block = r_model.trained_with(self.source)
@@ -459,7 +457,6 @@ class Deconstructor:
                             self.knowledge_database.insert(r_model)
 
         if not success:
-            self.knowledge_database.insert(p_model)
             raise DeconstructionFailed()
 
     def time_constraint(self,
-- 
GitLab