From 082cdd4593c384768cb03eea3a3f7510e55f4e2c Mon Sep 17 00:00:00 2001 From: Michele Date: Fri, 27 Nov 2015 16:49:19 +0100 Subject: [PATCH] working on nondet tic tac toe --- learning/learning.py | 27 +++++++++++++++++++++++---- learning/observationtable.py | 3 ++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/learning/learning.py b/learning/learning.py index cb0c983..c377b3f 100644 --- a/learning/learning.py +++ b/learning/learning.py @@ -80,6 +80,7 @@ class LearningAlgorithm: # this update uses a realistic teacher. If I need an output to happen I # cannot force it to happen. def updateTable(self): + #print("Updating table") # First, try to avoid impossible traces: ask observation query for trace in self._table.getObservableTraces(): observedOutputs = self._table.getOutputs(trace) @@ -89,17 +90,19 @@ class LearningAlgorithm: # For all traces for which we did not observed all possible outputs oTraces = self._table.getObservableTraces() + #print("observable traces: "+ str(len(oTraces))) - trie = th.make_trie(oTraces) + #trie = th.make_trie(oTraces) # Until we tried K times with no results, where K is the number of # observable traces times the number of outputs (including quiescence) - K = len(oTraces) * 35 # (len(self._teacher.getOutputAlphabet()) + 1) # TODO comment from * + K = len(oTraces) * 30 # (len(self._teacher.getOutputAlphabet()) + 1) # TODO comment from * found = 0 tries = 0 while tries < K: tries += 1 oTraces = self._table.getObservableTraces() + #print("observable traces: "+ str(len(oTraces))) trie = th.make_trie(oTraces) subtrie = trie @@ -116,8 +119,14 @@ class LearningAlgorithm: # 1 - observe an output that makes the trace not a prefix # 2 - there is no continuation of that trace in prefixes # We stop when we observed at least an output for each observable + count = 0 while len(oTraces) > len(observations.keys()): - + # TODO: I gave a limit to this inner loop. + # reason on this and find a good estimation for count. + # also, this is NOT GOOD for deterministic systems. It is easier to learn them without this check + count += 1 + #if count > (len(oTraces) * 2): + # break # check if trie contains no traces (but still has a child) children = trie.keys() hasTrace = False @@ -248,6 +257,7 @@ class LearningAlgorithm: observation = self._oracle.observation(trace, observedOutputs) if observation: self._table.updateEntry(trace, observation=observation) + #print("Table updated") def _processInputs(self, consecutiveInputs): if consecutiveInputs != (): @@ -261,6 +271,7 @@ class LearningAlgorithm: def stabilizeTable(self): + #print("Stabilizing table") # While nothing changes, keep closing and making the table consistent closingRows = self._table.isNotGloballyClosed() @@ -270,6 +281,7 @@ class LearningAlgorithm: closingRows = self._table.isNotGloballyClosed() while closingRows: + #print("Closing table") self._logger.debug("Table is not closed") self._logger.debug(closingRows) self._table.promote(closingRows) @@ -283,10 +295,13 @@ class LearningAlgorithm: if not closingRows: if self._makeConsistent(): + #print("Consistencing table") self.updateTable() closingRows = self._table.isNotGloballyClosed() + #print("Done stabilizing") def _makeConsistent(self): + #print("Checking consistency") consistentCheck = self._table.isNotGloballyConsistent() # Table is closed, check for consistency if consistentCheck: @@ -361,7 +376,7 @@ class LearningAlgorithm: if label in self._teacher.getInputAlphabet(): hyp.addTransition(assignments[row], label, hyp.getChaosDelta()) - + elif label in self._table.getPossibleOutputs(row): hyp.addTransition(assignments[row], label, hyp.getChaos()) @@ -423,10 +438,13 @@ class LearningAlgorithm: self._currentLoop = self._currentLoop + 1 self._logger.info("Learning loop number " + str(self._currentLoop)) # Fill the table and make it closed and consistent + #print("Pre update in run") self.updateTable() + #print("Pre stabilize in run") self.stabilizeTable() # Is the table quiescence reducible? If not make it so and # then fill it again, make it closed and consitent + #print("Pre quiescence check in run") newSuffixes = self._table.isNotQuiescenceReducible() while (newSuffixes or not self._table.isStable()): if newSuffixes: @@ -436,6 +454,7 @@ class LearningAlgorithm: if self._logger.isEnabledFor(logging.DEBUG): self._table.printTable(prefix="_Q_") self.updateTable() + #print("Pre stabilize in loop in run") self.stabilizeTable() newSuffixes = self._table.isNotQuiescenceReducible() self._hMinus = self.getHypothesis() diff --git a/learning/observationtable.py b/learning/observationtable.py index 1bedc71..106780b 100644 --- a/learning/observationtable.py +++ b/learning/observationtable.py @@ -441,8 +441,9 @@ class Table: #row2 is in the equivalence class defined by row1 # get all possible (enabled) labels of row1 - inputs = self._possibleInputs(row1) outputs = self.getOutputs(row1) + inputs = self._possibleInputs(row1, outputs) + labels = inputs.union(outputs) for label in labels: -- GitLab