-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearner.py
88 lines (80 loc) · 3.65 KB
/
learner.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import copy
import obsTable
from teacher import EQs_new as EQs
import comparator.equivWrapper as equivWrapper
from hypothesis import structDiscreteOTA, structHypothesisOTA
def learnOTA(targetSys, inputs, upperGuard, epsilon, delta, stateNum, comparatorFlag, debugFlag):
mqNum = 0 # number of MQs
eqNum = 0 # number of EQs
testNum = 0 # number of tests
### init Table
table, mqNum = obsTable.initTable(inputs, targetSys, mqNum)
if debugFlag:
print("***************** init-Table_1 is as follow *******************")
table.show()
### learning start
equivalent = False
stableHpy = None # learned model
tNum = 1 # number of table
while not equivalent:
prepared = obsTable.isPrepared(table)
while not prepared:
# make closed
flagClosed, closedMove = obsTable.isClosed(table)
if not flagClosed:
table, mqNum = obsTable.makeClosed(table, inputs, closedMove, targetSys, mqNum)
tNum = tNum + 1
if debugFlag:
print("***************** closed-Table_" + str(tNum) + " is as follow *******************")
table.show()
# make consistent
flagConsistent, consistentAdd = obsTable.isConsistent(table)
if not flagConsistent:
table, mqNum = obsTable.makeConsistent(table, consistentAdd, targetSys, mqNum)
tNum = tNum + 1
if debugFlag:
print("***************** consistent-Table_" + str(tNum) + " is as follow *******************")
table.show()
prepared = obsTable.isPrepared(table)
### build hypothesis
# Discrete OTA
discreteOTA = structDiscreteOTA(table, inputs)
if debugFlag:
print("***************** discreteOTA_" + str(eqNum + 1) + " is as follow. *******************")
discreteOTA.showDiscreteOTA()
# Hypothesis OTA
hypothesisOTA = structHypothesisOTA(discreteOTA)
if debugFlag:
print("***************** Hypothesis_" + str(eqNum + 1) + " is as follow. *******************")
hypothesisOTA.showOTA()
### comparator
if comparatorFlag:
flag, ctx, mqNum = equivWrapper.hpyCompare(stableHpy, hypothesisOTA, upperGuard, targetSys, mqNum)
if flag:
### EQs
equivalent, ctx, testNum = EQs(hypothesisOTA, upperGuard, epsilon, delta, stateNum, targetSys, eqNum, testNum)
eqNum = eqNum + 1
stableHpy = copy.deepcopy(hypothesisOTA)
else:
print("Comparator found a counterexample!!!")
equivalent = False
else:
# without comparator
stableHpy = copy.deepcopy(hypothesisOTA)
### EQs
equivalent, ctx, testNum = EQs(hypothesisOTA, upperGuard, epsilon, delta, stateNum, targetSys, eqNum, testNum)
eqNum = eqNum + 1
if not equivalent:
# show ctx
if debugFlag:
print("***************** counterexample is as follow. *******************")
print([drtw.show() for drtw in ctx])
print("***************** counterexample is as follow. *******************")
print([drtw.show() for drtw in ctx])
# deal with ctx
table, mqNum = obsTable.dealCtx(table, ctx, targetSys, mqNum)
tNum = tNum + 1
if debugFlag:
print("***************** New-Table" + str(tNum) + " is as follow *******************")
table.show()
return stableHpy, mqNum, eqNum, testNum