forked from LTL2Action/LTL2Action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltl_progression.py
224 lines (189 loc) · 7.31 KB
/
ltl_progression.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""
This code allows to progress LTL formulas. It requires installing the SPOT library:
- https://spot.lrde.epita.fr/install.html
To encode LTL formulas, we use tuples, e.g.,
(
'and',
('until','True', ('and', 'd', ('until','True','c'))),
('until','True', ('and', 'a', ('until','True', ('and', 'b', ('until','True','c')))))
)
Some notes about the format:
- It supports the following temporal operators: "next", "until", "always", and "eventually".
- It supports the following logical operators: "not", "or", "and".
- Propositions are assume to be one char.
- Negations are always followed by a proposition.
- true and false are encoded as "True" and "False"
"""
from sympy import *
from sympy.logic import simplify_logic
from sympy.logic.boolalg import And, Or, Not
import time, collections, spot
"""
This module contains functions to progress co-safe LTL formulas such as:
(
'and',
('until','True', ('and', 'd', ('until','True','c'))),
('until','True', ('and', 'a', ('until','True', ('and', 'b', ('until','True','c')))))
)
"""
def _is_prop_formula(f):
# returns True if the formula does not contains temporal operators
return 'next' not in str(f) and 'until' not in str(f)
def _subsume_until(f1, f2):
if str(f1) not in str(f2):
return False
while type(f2) != str:
if f1 == f2:
return True
if f2[0] == 'until':
f2 = f2[2]
elif f2[0] == 'and':
if _is_prop_formula(f2[1]) and not _is_prop_formula(f2[2]):
f2 = f2[2]
elif not _is_prop_formula(f2[1]) and _is_prop_formula(f2[2]):
f2 = f2[1]
else:
return False
else:
return False
return False
def _subsume_or(f1, f2):
if str(f1) not in str(f2):
return False
while type(f2) != str:
if f1 == f2:
return True
if f2[0] == 'until':
f2 = f2[2]
elif f2[0] == 'and':
if _is_prop_formula(f2[1]) and not _is_prop_formula(f2[2]):
f2 = f2[2]
elif not _is_prop_formula(f2[1]) and _is_prop_formula(f2[2]):
f2 = f2[1]
else:
return False
else:
return False
return False
def progress_and_clean(ltl_formula, truth_assignment):
ltl = progress(ltl_formula, truth_assignment)
# I am using spot to simplify the resulting ltl formula
ltl_spot = _get_spot_format(ltl)
f = spot.formula(ltl_spot)
f = spot.simplify(f)
ltl_spot = f.__format__("l")
ltl_std,r = _get_std_format(ltl_spot.split(' '))
assert len(r) == 0, "Format error" + str(ltl_std) + " " + str(r)
return ltl_std
def spotify(ltl_formula):
ltl_spot = _get_spot_format(ltl_formula)
f = spot.formula(ltl_spot)
f = spot.simplify(f)
ltl_spot = f.__format__("l")
# return ltl_spot
return f#.to_str('latex')
def _get_spot_format(ltl_std):
ltl_spot = str(ltl_std).replace("(","").replace(")","").replace(",","")
ltl_spot = ltl_spot.replace("'until'","U").replace("'not'","!").replace("'or'","|").replace("'and'","&")
ltl_spot = ltl_spot.replace("'next'","X").replace("'eventually'","F").replace("'always'","G").replace("'True'","t").replace("'False'","f").replace("\'","\"")
return ltl_spot
def _get_std_format(ltl_spot):
s = ltl_spot[0]
r = ltl_spot[1:]
if s in ["X","U","&","|"]:
v1,r1 = _get_std_format(r)
v2,r2 = _get_std_format(r1)
if s == "X": op = 'next'
if s == "U": op = 'until'
if s == "&": op = 'and'
if s == "|": op = 'or'
return (op,v1,v2),r2
if s in ["F","G","!"]:
v1,r1 = _get_std_format(r)
if s == "F": op = 'eventually'
if s == "G": op = 'always'
if s == "!": op = 'not'
return (op,v1),r1
if s == "f":
return 'False', r
if s == "t":
return 'True', r
if s[0] == '"':
return s.replace('"',''), r
assert False, "Format error in spot2std"
def progress(ltl_formula, truth_assignment):
if type(ltl_formula) == str:
# True, False, or proposition
if len(ltl_formula) == 1:
# ltl_formula is a proposition
if ltl_formula in truth_assignment:
return 'True'
else:
return 'False'
return ltl_formula
if ltl_formula[0] == 'not':
# negations should be over propositions only according to the cosafe ltl syntactic restriction
result = progress(ltl_formula[1], truth_assignment)
if result == 'True':
return 'False'
elif result == 'False':
return 'True'
else:
raise NotImplementedError("The following formula doesn't follow the cosafe syntactic restriction: " + str(ltl_formula))
if ltl_formula[0] == 'and':
res1 = progress(ltl_formula[1], truth_assignment)
res2 = progress(ltl_formula[2], truth_assignment)
if res1 == 'True' and res2 == 'True': return 'True'
if res1 == 'False' or res2 == 'False': return 'False'
if res1 == 'True': return res2
if res2 == 'True': return res1
if res1 == res2: return res1
#if _subsume_until(res1, res2): return res2
#if _subsume_until(res2, res1): return res1
return ('and',res1,res2)
if ltl_formula[0] == 'or':
res1 = progress(ltl_formula[1], truth_assignment)
res2 = progress(ltl_formula[2], truth_assignment)
if res1 == 'True' or res2 == 'True' : return 'True'
if res1 == 'False' and res2 == 'False': return 'False'
if res1 == 'False': return res2
if res2 == 'False': return res1
if res1 == res2: return res1
#if _subsume_until(res1, res2): return res1
#if _subsume_until(res2, res1): return res2
return ('or',res1,res2)
if ltl_formula[0] == 'next':
return progress(ltl_formula[1], truth_assignment)
# NOTE: What about release and other temporal operators?
if ltl_formula[0] == 'eventually':
res = progress(ltl_formula[1], truth_assignment)
return ("or", ltl_formula, res)
if ltl_formula[0] == 'always':
res = progress(ltl_formula[1], truth_assignment)
return ("and", ltl_formula, res)
if ltl_formula[0] == 'until':
res1 = progress(ltl_formula[1], truth_assignment)
res2 = progress(ltl_formula[2], truth_assignment)
if res1 == 'False':
f1 = 'False'
elif res1 == 'True':
f1 = ('until', ltl_formula[1], ltl_formula[2])
else:
f1 = ('and', res1, ('until', ltl_formula[1], ltl_formula[2]))
if res2 == 'True':
return 'True'
if res2 == 'False':
return f1
# Returning ('or', res2, f1)
#if _subsume_until(f1, res2): return f1
#if _subsume_until(res2, f1): return res2
return ('or', res2, f1)
if __name__ == '__main__':
#ltl = ('and',('eventually','a'),('and',('eventually','b'),('eventually','c')))
#ltl = ('and',('eventually','a'),('eventually',('and','b',('eventually','c'))))
#ltl = ('until',('not','a'),('and', 'b', ('eventually','d')))
ltl = ('until',('not','a'),('and', 'b', ('until',('not','c'),'d')))
while True:
print(ltl)
props = input()
ltl = progress_and_clean(ltl, props)