From ac77db5391830e8b454f44ae40db23a340abf00a Mon Sep 17 00:00:00 2001 From: Michele Date: Fri, 13 Nov 2015 18:14:39 +0100 Subject: [PATCH] added placeholder for ALL OUTPUTS in table --- examples/tictactoe/learn.py | 21 +++++++++++++-------- examples/tictactoe/tictacoracle.py | 29 +++++++++++++++++++++++++++-- examples/tictactoe/tictacpurpose.py | 11 ++++++++--- examples/tictactoe/tictactoe.py | 2 +- learning/learning.py | 2 ++ testing/randomtesting.py | 2 ++ 6 files changed, 53 insertions(+), 14 deletions(-) diff --git a/examples/tictactoe/learn.py b/examples/tictactoe/learn.py index 120bfb2..b208fd7 100644 --- a/examples/tictactoe/learn.py +++ b/examples/tictactoe/learn.py @@ -34,10 +34,11 @@ import itertools from tictacteacher import TicTacToeTeacher from tictacoracle import TicTacToeOracle -from tictacpurpose import InputPurpose, OutputPurpose +from tictacpurpose import TicTacToeInputPurpose, TicTacToeOutputPurpose from learning.learning import LearningAlgorithm from testing.randomtesting import RandomTester +from completetesting import CompleteTicTacToeTester logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -45,17 +46,21 @@ logger = logging.getLogger(__name__) HOST = 'localhost' PORT = 29000 # Arbitrary non-privileged port +outputExpert = TicTacToeOutputPurpose() +inputExpert = TicTacToeInputPurpose() + inputs = set(['0','1','2','3','4','5','6','7','8']) -outputs = set(itertools.product('XO_', repeat=9)) -quiescence = 'delta' +# Use a placeholder for outputs +outputs = outputExpert.allOutputs() +#outputs = set(itertools.product('XO_', repeat=9)) -outputExpert = OutputPurpose() -inputExpert = InputPurpose() +quiescence = 'delta' T1 = TicTacToeTeacher(HOST, PORT) -O1 = TicTacToeOracle() +O1 = TicTacToeOracle(inputs, quiescence) -tester = RandomTester(T1, 100, 10) +#tester = RandomTester(T1, 50000, 100) +tester = CompleteTicTacToeTester(T1) currentdir = os.path.dirname(os.path.abspath( inspect.getfile(inspect.currentframe()))) @@ -67,7 +72,7 @@ print("Starting learning...") #print(T1.oneOutput(('1'))) L = LearningAlgorithm(T1, O1, printPath=path, maxLoops=4, - tablePreciseness=10000, logger=logger, tester=tester, outputPurpose=outputExpert, + tablePreciseness=100000, logger=logger, tester=tester, outputPurpose=outputExpert, inputPurpose=inputExpert) minus, plus = L.run() diff --git a/examples/tictactoe/tictacoracle.py b/examples/tictactoe/tictacoracle.py index 5f751b2..34ae34f 100644 --- a/examples/tictactoe/tictacoracle.py +++ b/examples/tictactoe/tictacoracle.py @@ -31,13 +31,38 @@ import random # TODO for the moment we keep SUL deterministic, simple oracle. class TicTacToeOracle(AbstractOracle): - def __init__(self): - pass + def __init__(self, inputs, quiescence): + self._inputs = inputs.copy() + self._quiescence = quiescence # Reply to an observation query # trace is a list of inputs and or outputs # outputs is the set of outputs observed so far (after trace) def observation(self, trace, outputs): + # If we already observed some outputs, then return True (determinism) if len(outputs) > 0: return True + else: + # Check trace, if it has more outputs or more imputs in a row, + # then return True + if trace == (): + return False + # 0 = input, 1 = output + latest = 1 + if trace[0] in self._inputs: + latest = 0 + for i in range(1,len(trace)): + if trace[i] == self._quiescence: + continue + if trace[i] in self._inputs: + if latest == 0: + return True + else: + latest = 0 + else: + if latest == 1: + return True + else: + latest = 1 + return False diff --git a/examples/tictactoe/tictacpurpose.py b/examples/tictactoe/tictacpurpose.py index fa474c5..05de8ab 100644 --- a/examples/tictactoe/tictacpurpose.py +++ b/examples/tictactoe/tictacpurpose.py @@ -28,7 +28,7 @@ sys.path.append(currentdir) from systems.basepurpose import Purpose import itertools -class InputPurpose(Purpose): +class TicTacToeInputPurpose(Purpose): def __init__(self): pass @@ -45,7 +45,7 @@ class InputPurpose(Purpose): else: return inputs -class OutputPurpose(Purpose): +class TicTacToeOutputPurpose(Purpose): def __init__(self): pass @@ -61,4 +61,9 @@ class OutputPurpose(Purpose): if (outputs != None and len(outputs) > 0): return outputs else: - return set(itertools.product('XO_', repeat=9)) + # In this SUL we have many outputs (>4000) + # for this reason we use a placeholder + return 1 + + def allOutputs(self): + return set(itertools.product('XO_', repeat=9)) diff --git a/examples/tictactoe/tictactoe.py b/examples/tictactoe/tictactoe.py index 99363c1..ec9a346 100644 --- a/examples/tictactoe/tictactoe.py +++ b/examples/tictactoe/tictactoe.py @@ -422,7 +422,7 @@ if __name__ == "__main__": board = in_line_board() if winner != 0: - logger.debug("Sending: END ") + logger.debug("We have a winner!") board = board + " END" conn.sendall(bytes(board, 'UTF-8')) newGame() diff --git a/learning/learning.py b/learning/learning.py index a9c12ab..3b7d9aa 100644 --- a/learning/learning.py +++ b/learning/learning.py @@ -39,6 +39,8 @@ class LearningAlgorithm: self._inputPurpose = inputPurpose self._outputPurpose = outputPurpose + # If input (output) purpose is not defined, then add one that + # returns always all inputs (outputs) if self._inputPurpose == None: self._inputPurpose = InputPurpose(teacher.getInputAlphabet().copy()) if self._outputPurpose == None: diff --git a/testing/randomtesting.py b/testing/randomtesting.py index c470d8a..ae9b255 100644 --- a/testing/randomtesting.py +++ b/testing/randomtesting.py @@ -95,6 +95,8 @@ class RandomTester(AbstractTester): model.reset() # return counterexample trace and output obtained by # testing + self._logger.info(ce) + self._logger.info(output) return ce, output # reset self._teacher.reset() -- GitLab