Commit 5478cf36 authored by Michele Volpato's avatar Michele Volpato

Merge branch 'development' into 'master'

Merging Development into Master



See merge request !2
parents 1b365d1e 5dabd427
......@@ -3,6 +3,22 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased][unreleased]
### Added
-
-
### Changed
-
-
## [v0.3.0] - 2016-01-10
### Added
- Case study Tic Tac Toe
- LearnLib lib for Tic Tac Toe
### Changed
- License
- Name: new name is Alnos
## [v0.2.0] - 2015-10-27
### Added
......@@ -24,5 +40,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Test classes
- Simple Examples
[unreleased]: https://gitlab.science.ru.nl/mvolpato/active-learning-nondeterministic-systems/compare/v0.1.0...HEAD
[unreleased]: https://gitlab.science.ru.nl/mvolpato/active-learning-nondeterministic-systems/compare/v0.3.0...HEAD
[v0.1.0]: https://gitlab.science.ru.nl/mvolpato/active-learning-nondeterministic-systems/compare/f7f05033cf5e002a45a67632e60b311892ca0850...v0.1.0
[v0.2.0]: https://gitlab.science.ru.nl/mvolpato/active-learning-nondeterministic-systems/compare/v0.1.0...v0.2.0
[v0.3.0]: https://gitlab.science.ru.nl/mvolpato/active-learning-nondeterministic-systems/compare/v0.2.0...v0.3.0
\ No newline at end of file
## Synopsis
The active-learning-nondeterministic-systems is an implementation of an
**Alnos** is an implementation of an
adaptation of
[L*](http://www.cs.berkeley.edu/~dawnsong/teaching/s10/papers/angluin87.pdf) to
nondeterministic systems. The code is based on these scientific papers:
......@@ -14,7 +14,6 @@ simulation, or model checking.
The project is coded in Python3 and tested using Python3.4.
## Included Libraries
[NumPy](https://github.com/numpy/numpy)
......@@ -31,35 +30,10 @@ This code exists as a support implementation to the papers mentioned previously.
Clone the repository. Then you can modify any file in [examples](examples/),
or create your own.
If you are using a real system under testing (an actual running black box
software), you need to write your own adapters to connect it to the
learning tool. The adapters should inherit from `AbstractTeacher` in
[baseteacher.py](teachers/baseteacher.py) and from `AbstractOracle` in
[baseoracle.py](teachers/baseoracle.py).
In particular, the adapters should implement the abstract methods in
`AbstractTeacher` and `AbstractOracle`. Those method are used by the
learner to ask so called **output** and **observation** queries.
Then you can start learning your system:
```python
teacher = YourOwnAdapterTeacher()
oracle = YourOwnAdapterOracle()
underModel, overModel = LearningAlgorithm(teacher, oracle, maxLoops=10,
tablePreciseness = 10000, modelPreciseness = 0.1,
tester=tester)
```
where `underModel` and `overModel` are the under and over approximations
of your system, respectively, `maxLoops` is the limit of learning loops
when the learned models are not changing any more, `tablePreciseness` and
`modelPreciseness` are the levels of preciseness you would like to reach
before stopping. Tester is a testing algorithm.
The learning process stops when either the learned model does not change for
`maxLoops` loops, or when both the preciseness levels are met.
[Tic Tac Toe](examples/tictactoe/) uses a real *black box* system under learning.
There is also an example for learning a model of it using another learning
tool: [LearnLib](https://github.com/LearnLib/learnlib).
## Contributors
......@@ -68,4 +42,6 @@ checking [my contact details](https://gitlab.science.ru.nl/u/mvolpato).
## License
See [LICENSE](./LICENSE)
[This license](./LICENSE) applies to most of the files. Some files may have a different license. If so, the license can be found at the top of the source code.
If no license is found at the top of the source, [this license](./LICENSE) is applied.
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# This file is used to learn the IOTS depicted in Figure 2.5 of my PhD thesis.
import random
seed = output = random.sample(range(99999999), 1)[0]
print(seed)
random.seed(81077353) # 81077353
import os, inspect, sys
# Include project dir in path
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
from learning.learning import LearningAlgorithm
from teachers.ltsteachers import InputOutputTeacher
from systems.implementations import InputOutputLTS
from teachers.ltsoracles import InputOutputPowerOracle
import logging
import helpers.bisimulation as bi
from testing.randomtesting import RandomTester
from systems.iopurpose import InputPurpose, OutputPurpose
import helpers.graphhelper as gh
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
inputs = set(['b'])
outputs = set(['t','c'])
quiescence = 'd'
I1=InputOutputLTS(8, inputs, outputs, quiescence)
I1.addTransition(0,'b',1)
I1.addTransition(0,'b',2)
I1.addTransition(1,'t',3)
I1.addTransition(1,'c',3)
I1.addTransition(1,'b',6)
I1.addTransition(2,'b',4)
I1.addTransition(3,'t',0)
I1.addTransition(3,'c',0)
I1.addTransition(3,'b',6)
I1.addTransition(4,'c',5)
I1.addTransition(4,'b',6)
I1.addTransition(5,'b',6)
I1.addTransition(5,'t',0)
I1.addTransition(5,'c',0)
# Chaos
I1.addTransition(6,'b',6)
I1.addTransition(6,'t',6)
I1.addTransition(6,'c',6)
I1.addTransition(6,'d',7)
I1.addTransition(7,'b',6)
I1.makeInputEnabled()
T1 = InputOutputTeacher(I1)
O1 = InputOutputPowerOracle(I1)
outputExpert = OutputPurpose(set(['t','c', quiescence]))
inputExpert = InputPurpose(set(['b']))
tester = RandomTester(T1, 10000, 50)
currentdir = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
path = os.path.join(currentdir, "dotFiles")
gh.createDOTFile(I1, path + "figure2-5", "pdf")
print("Starting learning...")
# change printPath=None to printPath=path for dot files
L2 = LearningAlgorithm(T1, O1, printPath=path, maxLoops=4,
tablePreciseness=10000, logger=logger, tester=tester, outputPurpose=outputExpert,
inputPurpose=inputExpert)
minus, plus = L2.run()
print("Models learned. Check language equivalence...")
print("hMinus bisimilar to target: " + str(bi.bisimilar(I1,minus)))
print("hPlus bisimilar to target: " + str(bi.bisimilar(I1,plus)))
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os,sys,inspect
# Include project dir in path
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
......
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import random
seed = output = random.sample(range(99999999), 1)[0]
print(seed)
random.seed(81077353) # 81077353
import os, inspect, sys
# Include project dir in path
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
......@@ -10,8 +36,10 @@ from teachers.ltsoracles import InputOutputPowerOracle
import logging
import helpers.bisimulation as bi
from testing.randomtesting import RandomTester
from systems.iopurpose import InputPurpose, OutputPurpose
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
inputs = set(['a','b'])
......@@ -48,7 +76,10 @@ I1.makeInputEnabled()
T1 = InputOutputTeacher(I1)
O1 = InputOutputPowerOracle(I1)
tester = RandomTester(T1, 10000, 20)
outputExpert = OutputPurpose(set(['x','y', quiescence]))
inputExpert = InputPurpose(set(['a','b']))
tester = RandomTester(T1, 10000, 50)
currentdir = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
......@@ -58,7 +89,9 @@ path = os.path.join(currentdir, "dotFiles")
print("Starting learning...")
# change printPath=None to printPath=path for dot files
L2 = LearningAlgorithm(T1, O1, printPath=None, maxLoops=4, tablePreciseness=10000, logger=logger, tester=tester)
L2 = LearningAlgorithm(T1, O1, printPath=None, maxLoops=4,
tablePreciseness=10000, logger=logger, tester=tester, outputPurpose=outputExpert,
inputPurpose=inputExpert)
minus, plus = L2.run()
print("Models learned. Check language equivalence...")
......
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from testing.basetesting import AbstractTester
import logging
import itertools
# Complete tester.
class CompleteTicTacToeTester(AbstractTester):
def __init__(self, teacher, logger=None):
# The tester will run test using the teacher
self._teacher = teacher
# upper bound: 9! (possible plays, also invalid ones)
# according to wikipedia
# Reduced to games of length 5 (maximum number of X in a bard)
self._all_games = set(itertools.permutations(['0','1','2','3','4','5','6','7','8'],5))
self._logger = logger or logging.getLogger(__name__)
# Search a counterexample to teacher ioco model
def findCounterexample(self, model):
self._teacher.reset()
model.reset()
ce = ()
i = 0
for i in self._all_games:
ce = ()
# Next game
self._teacher.reset()
model.reset()
# play this game, if we receive END, then go to next game.
for action in i:
ce = ce + (action,)
output = self._processInputs((action,))
model.move(action)
if output not in model.outputs():
self._logger.info("Found a counterexample: "
+ str(ce) + " output: "+str(output))
self._teacher.reset()
model.reset()
# return counterexample trace and output obtained by
# testing
return ce, output
#elif 'END' in output:
# continue
else:
model.move(output)
ce = ce + (output,)
return None, None
def _processInputs(self, consecutiveInputs):
if consecutiveInputs != ():
output = self._teacher.oneOutputForTesting(consecutiveInputs)
if output == None:
return None
return output
return self._teacher.output()
# Copyright (c) 2015 Michele Volpato
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import random
random.seed(100)
import os, inspect, sys
# Include project dir in path
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
sys.path.append(currentdir)
import socket
import logging
import itertools
from tictacteacher import TicTacToeTeacher
from tictacoracle import TicTacToeOracle
from tictacpurpose import TicTacToeInputPurpose, TicTacToeOutputPurpose
from learning.learning import LearningAlgorithm
from testing.randomtesting import RandomTester
from completetesting import CompleteTicTacToeTester
from systems.implementations import SuspensionAutomaton
import helpers.bisimulation as bi
import csv
logging.basicConfig(level=logging.INFO)
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'])
# Use a placeholder for outputs
outputs = outputExpert.allOutputs()
#outputs = set(itertools.product('XO_', repeat=9))
quiescence = 'delta'
T1 = TicTacToeTeacher(HOST, PORT)
O1 = TicTacToeOracle(inputs, quiescence)
#tester = RandomTester(T1, 50000, 100)
tester = CompleteTicTacToeTester(T1)
currentdir = os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe())))
path = os.path.join(currentdir, "dotFiles")
print("Starting learning...")
#print(T1.oneOutput(('1')))
L = LearningAlgorithm(T1, O1, printPath=path, maxLoops=4,
tablePreciseness=100000, logger=logger, tester=tester, outputPurpose=outputExpert,
inputPurpose=inputExpert)
minus, plus = L.run()
print("Models learned.")
print("Number of inputs sent to the SUL: " + str(T1.getInputCounter()))
T1.close()
#######################################################################
# If there is a model learned by LearnLib, load it and run a bisimulation
# check with minus
# The file must be converted with learnlib_dot2jtorx_aut.py
with open("/home/mic/repo/learnLTS/examples/tictactoe/learnLib/TicTacToe.aut", 'r') as csvfile:
first = True
reader = csv.reader(csvfile, delimiter=';',
quoting=csv.QUOTE_MINIMAL)
for row in reader:
if first:
first = False
tup = row[0][3:] # remove 'des' from the first line
tuple_row = eval(tup)
learnLibmodel = SuspensionAutomaton(tuple_row[2],
inputs.copy(),
outputs.copy(),
quiescence,
False)
else:
tuple_row = eval(row[0])
#try:
# label = eval(tuple_row[1])
#except NameError:
label = tuple_row[1]
learnLibmodel.addTransition(tuple_row[0], label,
tuple_row[2])
print("Models learned. Check language equivalence...")
print("minus bisimilar to LearnLib model: " + str(bi.bisimilar(learnLibmodel,minus,startState1=0, startState2=0, noDelta=True)))
########################################################################
This is the code used for comparing my tool with learnLib version 0.9.1.
I use this version because of some errors introduced in learnLib by newer versions.
LearnLib code, readme, license and other relevent information can be found at https://github.com/LearnLib/learnlib
How to setup this example:
In your favorite IDE create a project including the content of the src folder and adding to the path
the LearnLib library (the content of the lib folder). Everything has been tested in Eclipse Lunawith JavaSE-1.7.
Adapt the paths to your specific case (around line 178 of TicTacToeLearner.java) and run it to
learn the SUT. You might need to install Graphviz for the dot program.
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python
"""
generates aut for jtorx from dot file learned with learnlib
'?COIN_1_1'
'!TEA_0_1'
note: dot file uses I for input instead of ? and O for output instead of !
"""
# Author: Harco Kuppens
# MOdification by Michele Volpato: removed ? and ! from inputs and outputs
import sys, re, pprint # modules from standard lib (python 2.6 and later)
def get_lts_from_dotfile(dot_file):
""" Get labeled transition system from graphviz dot file
The dot file:
- describes a digraph with labels
- encodes the start state with the color='red' attribute
note: this corresponds with the highlighted state in learnlib API
Returns: [start_state,transions]
Where :
- start_state: start state label
- transitions: list of transitions
"""
start_state='unknown'
f=file(dot_file)
lines=f.readlines()
# find start state
# line in dot: __start0 -> s0;
for line in lines:
if line.find('->') != -1:
if line.find('__start') != -1:
start_state=line[line.find('->')+2:].strip(" ;\t\n")
break
# get transitions
# line in dot: s5 -> s5 [label="ARTREG 20013226 / 531"];
transitions=[]
for line in lines:
if line.find('__start') != -1:
continue
if line.find('->') != -1:
transitions.append(line)
# throw away transitions with the keywords : quiescence or inconsistency or undefined
#transitions = [ t for t in transitions if ( 'quiescence' not in t ) and ( 'inconsistency' not in t ) and ( 'undefined' not in t )]
trans_out=[]
regexpr_transition=re.compile(r'\s*(\w*)\s*-\>\s*(\w*)\s*\[label=\"(.*)\"\]')
regexpr_tag=re.compile(r'<[^>]+>')
for transition in transitions:
match=regexpr_transition.match(transition)
if match:
match=match.groups()
label=regexpr_tag.sub('',match[2])
trans_out.append({
'source' : match[0],
'target' : match[1],
'label': label
})
states=set()
for t in trans_out:
states.add(t['source'])
states.add(t['target'])
return [start_state,states,trans_out]
def parse_labels_of_mealy_lts(transitions):
"""Parse labels of labeled transition system
"""
trans_out=[]
for t in transitions:
label=t['label']
[inputstr,outputstr]=label.split('/')
trans_out.append({
'source' : t['source'],
'target' : t['target'],
'input': inputstr,
'output': outputstr,
})
return trans_out
def split_io_transitions_in_separate_input_and_output_transition(io_transitions,nr_states):
"""Split transitions with both an input and output event into two transitions
Makes two sequential transitions with a dummy state in between:
- dummy state <midstate> is labeled :
m_<counter>
- first transition :
<source> -> <midstate> for <input>
- second transition :
<midstate> -> <target> for <output>
"""
trans_out=[]
id=nr_states
for t in io_transitions:
midstate= 'm' + str(id)
trans_out.append({
'source': t['source'],
'target': midstate,
'label' : t['input'].strip(),
})
trans_out.append({
'source': midstate,
'target': t['target'],
'label' : t['output'].strip(),
})
id=id+1
states=set()
for t in trans_out:
states.add(t['source'])