learn.py 4.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

33
34
import itertools

35
36
from tictacteacher import TicTacToeTeacher
from tictacoracle import TicTacToeOracle
37
from tictacpurpose import TicTacToeInputPurpose, TicTacToeOutputPurpose
38
39

from learning.learning import LearningAlgorithm
40
from testing.randomtesting import RandomTester
41
from completetesting import CompleteTicTacToeTester
42
43
44
45
46
from systems.implementations import SuspensionAutomaton

import helpers.bisimulation as bi

import csv
47

48
logging.basicConfig(level=logging.INFO)
49
50
51
logger = logging.getLogger(__name__)

HOST = 'localhost'
52
PORT = 29000 # Arbitrary non-privileged port
53

54
55
56
outputExpert = TicTacToeOutputPurpose()
inputExpert = TicTacToeInputPurpose()

57
inputs = set(['0','1','2','3','4','5','6','7','8'])
58
59
60
# Use a placeholder for outputs
outputs = outputExpert.allOutputs()
#outputs = set(itertools.product('XO_', repeat=9))
61

62
quiescence = 'delta'
63

64
T1 = TicTacToeTeacher(HOST, PORT)
65
O1 = TicTacToeOracle(inputs, quiescence)
66

67
68
#tester = RandomTester(T1, 50000, 100)
tester = CompleteTicTacToeTester(T1)
69
70
71
72
73
74
75
76

currentdir = os.path.dirname(os.path.abspath(
                inspect.getfile(inspect.currentframe())))

path = os.path.join(currentdir, "dotFiles")

print("Starting learning...")

77
78
#print(T1.oneOutput(('1')))

79
L = LearningAlgorithm(T1, O1, printPath=path, maxLoops=4,
80
    tablePreciseness=100000, logger=logger, tester=tester, outputPurpose=outputExpert,
81
82
83
84
    inputPurpose=inputExpert)
minus, plus = L.run()

print("Models learned.")
85
print("Number of inputs sent to the SUL: " + str(T1.getInputCounter()))
86

87
88
T1.close()

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#######################################################################
# 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)))

########################################################################