Commit 8e465d47 authored by Michele's avatar Michele
Browse files

added testing to learning, commented out other ways of increasing preciseness,...

added testing to learning, commented out other ways of increasing preciseness, CE handling still missing
parent 5075ac4c
......@@ -7,14 +7,15 @@ import logging
class LearningAlgorithm:
def __init__(self, teacher, oracle , tablePreciseness = 1000,
def __init__(self, teacher, oracle, tester, tablePreciseness = 1000,
modelPreciseness = 0.1, closeStrategy = None,
printPath = None, maxLoops=10, logger=None):
printPath = None, maxLoops=10, logger=None,):
self._logger = logger or logging.getLogger(__name__)
self._teacher = teacher
self._oracle = oracle
self.tester = tester
self._tablePreciseness = tablePreciseness
self._modelPreciseness = modelPreciseness
......@@ -276,6 +277,8 @@ class LearningAlgorithm:
samples.add('update')
choice = random.sample(samples, 1)[0]
# TODO: forcing testing: removed
choice = 'testing'
if choice == 'extendTable':
self._logger.info("Improve preciseness method: extend the table")
# create a set of suffix closed suffixes and add them
......@@ -318,8 +321,8 @@ class LearningAlgorithm:
self.updateTable()
elif choice == 'testing':
self._logger.info("Improve preciseness method: testing")
# TODO testing
pass
ce = self.tester.findCounterexample(self._hMinus)
# TODO: handle counterexample
continue
else:
self._logger.info("Requested table preciseness: " +
......
......@@ -5,5 +5,5 @@ class AbstractTester(metaclass=ABCMeta):
# Run a test suite
@abstractmethod
def findCounterexample(self, model, limit, expectedLength):
def findCounterexample(self, model):
pass
......@@ -5,23 +5,26 @@ import logging
# Random tester.
class RandomTester(AbstractTester):
def __init__(self, teacher, logger=None):
def __init__(self, teacher, limit=1000, expectedLength=20, logger=None):
# The tester will run test using the teacher
self._teacher = teacher
self._expectedLength = expectedLength
self._limit = limit
self._logger = logger or logging.getLogger(__name__)
# Search a counterexample to teacher ioco model using at most limit actions
# provide an expectedLength
def findCounterexample(self, model, limit, expectedLength):
def findCounterexample(self, model):
self._teacher.reset()
model.reset()
ce = ()
i = 0
while i <= limit:
while i <= self._limit:
#choice to reset or not:
#(roll dice with expectedLength faces, if 0
# I reset, otherwise I continue with one more input or output)
reset = random.sample(range(expectedLength),1)[0]
reset = random.sample(range(self._expectedLength),1)[0]
if reset != 0:
# do not Reset
typeAction = random.sample(set(['input', 'output']),1)[0]
......@@ -43,13 +46,18 @@ class RandomTester(AbstractTester):
else:
# wait for output and check if it is also in model
output = self._teacher.output()
ce = ce + (output,)
if output not in model.outputs():
self._logger.info("Found a counterexample after "
+ str(i) + " actions.")
print(ce)
print(output)
print(model.outputs())
self._teacher.reset()
model.reset()
return ce
else:
model.move(output)
ce = ce + (output,)
# waiting for outputs does not cost actions
else:
# before resetting
......
......@@ -6,6 +6,7 @@ from systems.implementations import InputOutputLTS
from teachers.ltsoracles import InputOutputPowerOracle
import helpers.graphhelper as gh
import helpers.bisimulation as bi
from testing.randomtesting import RandomTester
class TestLearningAlgorithm:
......@@ -35,7 +36,9 @@ class TestLearningAlgorithm:
self.T1 = InputOutputTeacher(self.I1)
self.O1 = InputOutputPowerOracle(self.I1)
self.L1 = LearningAlgorithm(self.T1, self.O1)
self.tester = RandomTester(self.T1, 1000, 20)
self.L1 = LearningAlgorithm(self.T1, self.O1, self.tester)
def creation_test(self):
......@@ -144,7 +147,9 @@ class TestLearningAlgorithm:
pathI3 = os.path.join(path, "I3")
gh.createDOTFile(I3, pathI3, "pdf")
L3 = LearningAlgorithm(T3, O3, printPath=path, maxLoops=3)
tester = RandomTester(T3, 10000, 20)
L3 = LearningAlgorithm(T3, O3, tester, printPath=path, maxLoops=3)
Hminus, Hplus = L3.run()
assert_set_equal(L3._table._isNotConsistent(), set())
......@@ -189,7 +194,9 @@ class TestLearningAlgorithm:
parentdir = os.path.dirname(currentdir)
path = os.path.join(parentdir, "tests", "test_reducibility_check")
L3 = LearningAlgorithm(T3, O3, printPath=path, maxLoops=10,
tester = RandomTester(T3, 10000, 20)
L3 = LearningAlgorithm(T3, O3, tester, printPath=path, maxLoops=10,
tablePreciseness = 10000, modelPreciseness = 0.8)
Hminus, Hplus = L3.run()
......@@ -227,7 +234,9 @@ class TestLearningAlgorithm:
parentdir = os.path.dirname(currentdir)
path = os.path.join(parentdir, "tests", "test_deterministic")
L3 = LearningAlgorithm(T3, O3, printPath=path, maxLoops=5)
tester = RandomTester(T3, 1000, 20)
L3 = LearningAlgorithm(T3, O3, tester, printPath=path, maxLoops=5)
Hminus, Hplus = L3.run()
assert_equal(bi.bisimilar(I3,Hminus), True)
......
......@@ -30,7 +30,7 @@ class TestRandomTester:
self.T1 = InputOutputTeacher(self.I1)
self.tester = RandomTester(self.T1)
self.tester = RandomTester(self.T1, 1000, 20)
def test_simple_tester(self):
inputs = set(['a','b'])
......@@ -57,6 +57,6 @@ class TestRandomTester:
assert_equal(model.isValid(), True)
ce = self.tester.findCounterexample(model, 1000, 20)
ce = self.tester.findCounterexample(model)
assert_equal(ce, None)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment