forked from IBM/BayesLTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltlfunc.py
1172 lines (921 loc) · 40.4 KB
/
ltlfunc.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Module on functions related to Linear Temporal Logic (LTL) - LITE version
# Written by Joseph Kim
import copy
import math
import numpy as np
import os
import random
import re
from itertools import permutations
from scipy.stats import poisson, geom
import sys
##############################
### BEGIN simple functions ###
def nCr(n,r):
""" Returns number of possibilities for n choose r """
f = math.factorial
return f(n) // f(r) // f(n-r)
def rec_replace(l, d):
""" Recursively replace list l with values of dictionary d
ARGUMENTS:
l - list or list of lists
d - dictionary where if an element in l matches d['key'], it is replaced by the value
OUTPUT:
l - list with elements updated
"""
for i in range(len(l)):
if isinstance(l[i], list):
rec_replace(l[i], d)
else:
l[i] = d.get(l[i], l[i])
return l
def to_tuple(lst):
""" Convert an arbitrary nested list to nested tuple (recursively) """
return tuple(to_tuple(i) if isinstance(i, list) else i for i in lst)
### END of simple functions ###
###############################
def computePrior(ltl, lamb = 0.9, conjoin = False):
""" Returns the log-prior of current LTL sample
INPUTS:
ltl - current ltl dict
lamb - lambda parameter for geometric distribution
conjoin - whether or not conjunctions of templates are being considered
OUTPUT:
log P(ltl)
"""
# LTL template prior
log_template = math.log(ltl['prob'])
if conjoin:
# Complexity based on number of conjunctions
num_conjuncts = len(ltl['props_list'])
complexity = geom.pmf(num_conjuncts, 1-lamb)
# complexity = poisson(3).pmf(num_conjuncts)
try:
log_complexity = math.log(complexity)
except ValueError:
log_complexity = -1000
return log_template + log_complexity
else:
return log_template
def samplePrior(templates, vocab, perm_table, lamb = 0.9, conjoin = False, doRandom = False):
""" Samples a new ltl proportional to the prior distribution
INPUTS:
templates - a list of LTL templates
vocab - vocabulary of propositions
perm_table - permutation table of vocab
lamb - lambda parameter for geometric distribution
conjoin - whether or not conjunctions of templates are considered
doRandom - whether or not to sample from pure uniform distribution
OUTPUT:
ltl - a new sampled LTL dict
"""
# Pick a template
t = pickLTLtemplate(templates, usePrior = True)
num_vars = len(t['vars'])
if conjoin:
# Pick number of conjunctions
if not doRandom:
# Geometric distribution
num_conjuncts = geom.rvs(1-lamb)
# Poisson distribution
# num_conjuncts = poisson.rvs(3)
if num_conjuncts == 0:
num_conjuncts = 1
elif num_conjuncts > len(perm_table[num_vars]):
num_conjuncts = len(perm_table[num_vars])
else:
# Uniform over all possibilities
if num_vars > 1:
# Cut off at 100 max conjunctions
num_conjuncts = random.randint(1, 100)
else:
num_conjuncts = random.randint(1, len(perm_table[num_vars]))
# Put cap if it exceeds allotted number of conjunctions
if num_conjuncts > len(perm_table[num_vars]):
num_conjuncts = len(perm_table[num_vars])
# Pick propositions
props_list = random.sample(perm_table[num_vars], num_conjuncts)
ltl = getLTLconjunct(t, vocab, props_list)
return ltl
else:
ltl = instantiateLTL(t, vocab)
return ltl
def computeLikelihood(ltl, X, vocab, params, memory = None, cache = None):
""" Returns the log-likelihood of current ltl with respect to evidence X
INPUTS:
ltl - current ltl dict
X - evidence (cluster1, cluster2) where each trace in cluster is a trace dict
vocab - vocabulary of propositions
params - params dict
memory - memory for checking ltl['str_friendly'] on X
cache - cache for checking LTL on subformulas
conjoin - whether or not this is for conjunction hypothesis space
OUTPUT:
log_likelihood - log P(X | ltl)
cscore - [0-1], contrastive explanation validity score
"""
beta = params['beta']
alpha = params['alpha']
cluster1 = X[0]
cluster2 = X[1]
# Check memory or calculate
if type(memory) is dict and ltl['str_friendly'] in memory:
cscore1, cscore2 = memory[ltl['str_friendly']]
else:
cscore1, cscore2 = checkContrastiveValidity(ltl, cluster1, cluster2, vocab, cache)
if type(memory) is dict:
memory[ltl['str_friendly']] = (cscore1, cscore2)
# cscore = cscore1 * (1-cscore2)
# Likelihood in terms of product over satisfaction/dissatisfaction of traces
num_satisfy_1 = int(cscore1 * len(cluster1))
num_fail_1 = len(cluster1) - num_satisfy_1
num_satisfy_2 = int(cscore2 * len(cluster2))
num_fail_2 = len(cluster2) - num_satisfy_2
# cscore = (total positive entailment + total negative non-entailment) / (total positive and negative traces)
cscore = (num_satisfy_1 + num_fail_2) / float(len(cluster1) + len(cluster2))
log_likelihood = num_satisfy_1 * (1-alpha) + num_fail_1 * alpha + num_satisfy_2 * beta + num_fail_2 * (1-beta)
# ALTERNATIVE LIKELIHOOD - Using flat degree of satisfaction
# # Case 1) c1 SATISFY, c2 NOT
# c1 = cscore1 * (1-cscore2) * (1 - 2 * beta - gamma)
# # Case 2) c1 NOT, c2 NOT
# c2 = (1-cscore1) * (1-cscore2) * beta
# # Case 3) c1 SATISFY, c2 SATISFY
# c3 = cscore1 * cscore2 * beta
# # Case 4) c1 NOT, c2 SATISFY
# c4 = (1-cscore1) * cscore2 * gamma
# likelihood = c1 + c2 + c3 + c4
# log_likelihood = math.log(likelihood)
return log_likelihood, cscore, memory
def computePosterior(ltl, X, vocab, params, memory = None, cache = None, conjoin = False):
""" Returns the log-posterior and the current ltl's validity score on X
INPUTS:
ltl - ltl dict
X - evidence (cluster1, cluster2) where each trace in cluster is a trace dict
vocab - vocab of propositions
params - params dict
memory - memory for checking LTL based on ltl['str_friendly']
cache - cache for checking LTL on subformulas
conjoin - whether or not this is for conjunction space
OUTPUT:
log_posterior - log P(ltl | X)
cscore - contrastive validity score [0-1]
"""
log_prior = computePrior(ltl, params['lambda'], conjoin)
log_likelihood, cscore, memory = computeLikelihood(ltl, X, vocab, params, memory, cache)
log_posterior = log_prior + log_likelihood
return log_posterior, cscore, memory
def checkLTL(f, t, trace, vocab, c = None, v = False):
""" Checks satisfaction of a LTL formula on an execution trace
NOTES:
* This works by using the semantics of LTL and forward progression through recursion
* Note that this does NOT require using any off-the-shelf planner
ARGUMENTS:
f - an LTL formula (must be in TREE format using nested tuples
if you are using LTL dict, then use ltl['str_tree'])
t - time stamp where formula f is evaluated
trace - execution trace (a dict containing:
trace['name']: trace name (have to be unique if calling from a set of traces)
trace['trace']: execution trace (in propositions format)
trace['plan']: plan that generated the trace (unneeded)
vocab - vocabulary of propositions
c - cache for checking LTL on subtrees
v - verbosity
OUTPUT:
satisfaction - true/false indicating ltl satisfaction on the given trace
"""
if v:
print('\nCurrent t = '+str(t))
print('Current f =',f)
###################################################
# Check if first operator is a proposition
if type(f) is str and f in vocab:
return f in trace['trace'][t]
# Check if sub-tree info is available in the cache
key = (f, t, trace['name'])
if c is not None:
if key in c:
if v: print('Found subtree history')
return c[key]
# Check for standard logic operators
if f[0] in ['not', '!']:
value = not checkLTL(f[1], t, trace, vocab, c, v)
elif f[0] in ['and', '&', '&&']:
value = all( ( checkLTL(f[i], t, trace, vocab, c, v) for i in range(1,len(f)) ) )
elif f[0] in ['or', '||']:
value = any( ( checkLTL(f[i], t, trace, vocab, c, v) for i in range(1,len(f)) ) )
elif f[0] in ['imp', '->']:
value = not(checkLTL(f[1], t, trace, vocab, c, v)) or checkLTL(f[2], t, trace, vocab, c, v)
# Check if t is at final time step
elif t == len(trace['trace'])-1:
# Confirm what your interpretation for this should be.
if f[0] in ['X', 'G', 'F']:
value = checkLTL(f[1], t, trace, vocab, c, v) # Confirm what your interpretation here should be
elif f[0] == 'U':
value = checkLTL(f[2], t, trace, vocab, c, v)
elif f[0] == 'W': # weak-until
value = checkLTL(f[2], t, trace, vocab, c, v) or checkLTL(f[1], t, trace, vocab, c, v)
elif f[0] == 'R': # release (weak by default)
value = checkLTL(f[2], t, trace, vocab, c, v)
else:
# Does not exist in vocab, nor any of operators
sys.exit('LTL check - something wrong')
else:
# Forward progression rules
if f[0] == 'X':
value = checkLTL(f[1], t+1, trace, vocab, c, v)
elif f[0] == 'G':
value = checkLTL(f[1], t, trace, vocab, c, v) and checkLTL(('G',f[1]), t+1, trace, vocab, c, v)
elif f[0] == 'F':
value = checkLTL(f[1], t, trace, vocab, c, v) or checkLTL(('F',f[1]), t+1, trace, vocab, c, v)
elif f[0] == 'U':
# Basically enforces f[1] has to occur for f[1] U f[2] to be valid.
if t == 0:
if not checkLTL(f[1], t, trace, vocab, c, v):
value = False
else:
value = checkLTL(f[2], t, trace, vocab, c, v) or (checkLTL(f[1], t, trace, vocab, c, v) and checkLTL(('U',f[1],f[2]), t+1, trace, vocab, c, v))
else:
value = checkLTL(f[2], t, trace, vocab, c, v) or (checkLTL(f[1], t, trace, vocab, c, v) and checkLTL(('U',f[1],f[2]), t+1, trace, vocab, c, v))
elif f[0] == 'W': # weak-until
value = checkLTL(f[2], t, trace, vocab, c, v) or (checkLTL(f[1], t, trace, vocab, c, v) and checkLTL(('W',f[1],f[2]), t+1, trace, vocab, c, v))
elif f[0] == 'R': # release (weak by default)
value = checkLTL(f[2], t, trace, vocab, c, v) and (checkLTL(f[1], t, trace, vocab, c, v) or checkLTL(('R',f[1],f[2]), t+1, trace, vocab, c, v))
else:
# Does not exist in vocab, nor any of operators
sys.exit('LTL check - something wrong')
if v: print('Returned: '+str(value))
# Save result
if c is not None and type(c) is dict:
key = (f, t, trace['name'])
c[key] = value # append
return value
def checkContrastiveValidity(ltl, cluster1, cluster2, vocab, cache = None):
""" Computes constrastive explanation score of current ltl on given pair of traces
Where cluster1 is expected to satisfy ltl and cluster2 to dissatisfy it
Note that with clusters, there exists a degree of satisfaction by the number of
satisfactory/unsatisfactory cases.
ARGUMENTS:
ltl - ltl dict
cluster1, cluster2 - cluster [list] of traces, where each trace is a dict containing
trace['name']: trace name
trace['trace']: execution trace (in propositions format)
trace['plan']: generating plan of a trace (unneeded)
vocab - vocabulary of propositions
cache - cache for checking LTL on subtrees
OUTPUT:
cscore1, cscore2 - validity scores [0-1] for each cluster
"""
ltl_tuple = to_tuple(ltl['str_tree'])
cscore1 = 0
for trace in cluster1:
cscore1 += checkLTL(ltl_tuple, 0, trace, vocab, cache)
cscore1 = 1. * cscore1 / len(cluster1)
cscore2 = 0
for trace in cluster2:
cscore2 += checkLTL(ltl_tuple, 0, trace, vocab, cache)
cscore2 = 1. * cscore2 / len(cluster2)
return cscore1, cscore2
def instantiateLTLvariablePermutate(template, vocab):
""" Instantiate a LTL template with a permutation of propositions.
Returns a list containg all possible permutations
ARGUMENTS:
template - a ltl template
vocab - a set of propositions to draw permutation samples from
OUTPUT:
ltl_list - a list of instantiated LTL dicts over all permutations of vocab
"""
ltl_list = []
num_vars = len(template['vars'])
# Permutation of propositions of length equal to num_vars
props_perm = permutations(vocab, num_vars)
for p_tuple in props_perm:
template_copy = template.copy()
template_copy['props'] = list(p_tuple)
ltl = produceLTLstring(template_copy)
ltl_list.append(ltl)
return ltl_list
def instantiateLTL(template, vocab, props = None):
""" Instantiate a LTL template with a randomly sampled propositions
ARGUMENTS:
template - a ltl template
vocab - a set of propositions to draw samples from if props is None
props - propositions to instantiate with
OUTPUT:
ltl - a dict with following key/values
'fml': uninstantiated formula
'ids': indices where propositions should be inserted
'prob' : prior probability for the LTL template
'props' : instantiated propositions [list]
'str': full instantiated string
"""
ltl = template.copy()
num_vars = len(ltl['vars'])
if props:
assert len(props) == num_vars
ltl['props'] = props
else:
ltl['props'] = random.sample(vocab, num_vars)
ltl = produceLTLstring(ltl) # Fills in ltl['str']
return ltl
def produceLTLStringMeaning(name, props_list):
"""
Produces a natural language explanation of the LTL
:param name: a string specifying the LTL template name
:param props_list: a list of list of strings indicating the propositions
:return: a string of natural language meaning of the LTL
"""
connector_str = ' AND '
template_map = {
"global": '("{}") is true throughout the entire trace',
"eventual": '("{}") eventually occurs (may later become false)',
"stability": '("{}") eventually occurs and stays true forever',
"response": 'If ("{}") occurs, ("{}") eventually follows',
"until": '("{}") has to be true until ("{}") eventually becomes true',
"atmostonce": 'Only one contiguous interval exists where ("{}") is true',
"sometime_before": 'If ("{}") occurs, ("{}") occured in the past'
}
str_template = template_map[name]
ltl_meaning = connector_str.join([str_template.format(*x) for x in props_list])
return ltl_meaning
def produceLTLstring(ltl):
""" Using the current ltl formula, ltl['fml'] and
the current propositions, ltl['props'], fills in the followings:
ltl['str']: formula string
ltl['str_friendly']: formula string in friendly read form
ltl['str_tree']: formula string in tree mode
NOTES:
* Number of props in ltl['props'] must equal to that allowed by current template
ARGUMENTS:
ltl - current LTL dict
OUTPUT:
ltl - updated LTL dict
(ltl['str'] and ltl['str_tree'] and ltl['str_friendly'])
"""
ltl['str'] = ltl['fml'][:]
num_vars = len(ltl['ids'])
num_props = len(ltl['props'])
# Check number of variables
if num_vars != num_props:
print('num_vars = %s' % num_vars)
print('num_props = %s' % num_props)
sys.exit('LTL props does NOT equal to length of variables in template.')
# Fill in the string - ltl['str']
for i, p in enumerate(ltl['props']):
for j in ltl['ids'][i]:
ltl['str'][j] = '"'+p+'"'
ltl['str'] = ' '.join(ltl['str'])
# Friendly string (using name header and current props)
ltl['str_friendly'] = ltl['name'] + ': ' + ' , '.join(ltl['props'])
ltl['str_meaning'] = produceLTLStringMeaning(ltl['name'], [ltl['props']])
# Fill in the string (tree mode) - ltl['str_tree']
rep_dict = dict()
for i in range(num_vars):
rep_dict[ltl['vars'][i]] = ltl['props'][i]
tempcopy = copy.deepcopy(ltl['fml_tree'])
ltl['str_tree'] = rec_replace(tempcopy, rep_dict)
return ltl
def getLTLtemplates(choice = None, user_probs = None):
""" Returns a list of LTL templates (uninstantiated), where each template t
is a dictionary following key/values:
t['name']: LTL pattern name (str)
t['fml']: LTL formula (uninstantiated) --> useful for ltlfond2fond
t['ids']: indices where instantiated proposition(s) should go in t['fml']
t['probs']: prior probability on template t
t['vars']: number of free variables for each primitive LTL template
t['fml_tree']: LTL formula is a tree structure
INPUT:
choice - a list of patterns (str) to include in templates
OUTPUT:
templates - a list of LTL dicts
"""
templates = list()
# Default template priors
probs = dict()
probs['eventual'] = 1.0
probs['eventual_neg'] = 1.0
probs['global'] = 1.0
probs['global_neg'] = 1.0
probs['until'] = 1.0
probs['until_neg'] = 1.0
probs['response'] = 1.0
probs['response_neg'] = 1.0
probs['response_strong'] = 1.0
probs['response_strong_neg'] = 1.0
probs['stability'] = 1.0
probs['stability_strong'] = 1.0
probs['atmostonce_strong'] = 1.0
probs['atmostonce'] = 1.0
probs['sometime_before'] = 1.0
probs['sometime_before_strong'] = 1.0
# Override with user prior probs
if user_probs:
probs.update(user_probs)
# Default choice of templates
if choice is None:
choice = ['eventual', 'global', 'until',
'response', 'stability', 'atmostonce',
'sometime_before']
#### T1: Eventually: v1 becomes true at some point
t = dict()
t['name'] = 'eventual'
t['fml'] = ['F', '"v1"']
t['vars'] = [ 'v1' ]
t['ids'] = [ [1] ]
t['prob'] = probs['eventual']
t['fml_tree'] = ['F', 'v1']
if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['name'] = 'eventual_neg'
# t['fml'] = ['! F', '"v1"']
# t['vars'] = [ 'v1' ]
# t['ids'] = [ [1] ]
# t['prob'] = probs['eventual_neg']
# t['fml_tree'] = ['not', ['F', 'v1']]
# if t['name'] in choice: templates.append(t)
# ####
#### T2: Global: v1 is true always
t = dict()
t['name'] = 'global'
t['fml'] = ['G', '"v1"']
t['vars'] = [ 'v1' ]
t['ids'] = [ [1] ]
t['prob'] = probs['global']
t['fml_tree'] = ['G', 'v1']
if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['name'] = 'global_neg'
# t['fml'] = ['! G', '"v1"']
# t['vars'] = [ 'v1' ]
# t['ids'] = [ [1] ]
# t['prob'] = probs['global_neg']
# t['fml_tree'] = ['not', ['G', 'v1']]
# if t['name'] in choice: templates.append(t)
# ####
#### T3: Until:
# v1 is true until v2 becomes true (v2 has to become true at some point)
# after v2 becomes true, v1 is unrestricted
t = dict()
t['name'] = 'until'
t['fml'] = ['"v1"', 'U', '"v2"']
t['vars'] = [ 'v1', 'v2' ]
t['ids'] = [ [0], [2] ]
t['prob'] = probs['until']
t['fml_tree'] = ['U', 'v1', 'v2']
if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['name'] = 'until_neg'
# t['fml'] = ['!(', '"v1"', 'U', '"v2"', ')']
# t['vars'] = [ 'v1', 'v2' ]
# t['ids'] = [ [1], [3] ]
# t['prob'] = probs['until_neg']
# t['fml_tree'] = ['not', ['U', 'v1', 'v2']]
# if t['name'] in choice: templates.append(t)
# ####
#### T4: Response:
# Globally, if v1 occurs, eventually v2 occurs.
# (If v1 does not ever occur, this is true)
t = dict()
t['name'] = 'response'
t['fml'] = ['G (', '"v1"', '-> X F', '"v2"', ')']
t['vars'] = [ 'v1', 'v2']
t['ids'] = [ [1], [3] ]
t['prob'] = probs['response']
t['fml_tree'] = ['G', ['imp', 'v1', ['X', ['F', 'v2'] ] ] ]
if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['name'] = 'response_neg'
# t['fml'] = ['! ( G (', '"v1"', '-> X F', '"v2"', ') )']
# t['vars'] = [ 'v1', 'v2']
# t['ids'] = [ [1], [3] ]
# t['prob'] = probs['response_neg']
# t['fml_tree'] = ['not', ['G', ['imp', 'v1', ['X', ['F', 'v2'] ] ] ] ]
# if t['name'] in choice: templates.append(t)
# ####
# #### T5: Response (strong):
# # Globally, if v1 occurs, eventually v2 occurs.
# # (Enforces the occurrence of v1)
# t = dict()
# t['name'] = 'response_strong'
# t['fml'] = ['F', '"v1"', '&& G (', '"v1"', '-> X F', '"v2"', ')']
# t['vars'] = [ 'v1', 'v2']
# t['ids'] = [ [1,3], [5] ]
# t['prob'] = probs['response_strong']
# t['fml_tree'] = ['and', ['F', 'v1'] , ['G', ['imp', 'v1', ['X', ['F', 'v2'] ] ] ] ]
# if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['name'] = 'response_strong_neg'
# t['fml'] = ['! ( F', '"v1"', '&& G (', '"v1"', '-> X F', '"v2"', ') )']
# t['vars'] = [ 'v1', 'v2']
# t['ids'] = [ [1,3], [5] ]
# t['prob'] = probs['response_strong_neg']
# t['fml_tree'] = ['not', ['and', ['F', 'v1'] , ['G', ['imp', 'v1', ['X', ['F', 'v2'] ] ] ] ] ]
# if t['name'] in choice: templates.append(t)
# ####
#### T6: Eventually occurs and stays true forever
t = dict()
t['name'] = 'stability'
t['fml'] = ['F G (', '"v1"',')']
t['vars'] = ['v1']
t['ids'] = [ [1] ]
t['prob'] = probs['stability']
t['fml_tree'] = ['F', ['G', 'v1'] ]
if t['name'] in choice: templates.append(t)
# #### T6: Once v1, always v1
# t = dict()
# t['name'] = 'stability'
# t['fml'] = ['G (', '"v1"', '-> G', '"v1"',')']
# t['vars'] = ['v1']
# t['ids'] = [ [1,3] ]
# t['prob'] = probs['stability']
# t['fml_tree'] = ['G', ['imp', 'v1', ['G', 'v1'] ] ]
# if t['name'] in choice: templates.append(t)
# # Negation
# t = dict()
# t['fml'] = ['! ( G (', '"v1"', '-> G', '"v1"',') )']
# t['vars'] = ['v1']
# t['ids'] = [ [1,3] ]
# t['prob'] = probs['stability_neg']
# t['fml_tree'] = ['not', ['G', ['imp', 'v1', ['G', 'v1'] ] ] ]
# if onmode['stability__neg']: templates.append(t)
####
# #### T7: Once v1, always v1 (strong)
# t = dict()
# t['name'] = 'stability_strong'
# t['fml'] = ['F', '"v1"', '&& G (', '"v1"', '-> G', '"v1"',')']
# t['vars'] = ['v1']
# t['ids'] = [ [1,3,5] ]
# t['prob'] = probs['stability_strong']
# t['fml_tree'] = ['and', ['F', 'v1'], ['G', ['imp', 'v1', ['G', 'v1'] ] ] ]
# if t['name'] in choice: templates.append(t)
# ####
#### T8: At most once
# -IF v1 becomes true and then stays true and then (possibly) becomes false and stays false
# -There exists only one interval in the plan over which v1 is true.
t = dict()
t['name'] = 'atmostonce'
t['fml'] = ['G (', '"v1"', '-> (', '"v1"', ' W (G ( !', '"v1"', '))))']
t['vars'] = ['v1']
t['ids'] = [ [1, 3, 5] ]
t['prob'] = probs['atmostonce']
t['fml_tree'] = ['G', ['imp', 'v1',
['W', 'v1',
['G', ['not', 'v1']]
]
]
]
if t['name'] in choice: templates.append(t)
# #### T8: At most once (STRONG)
# # -v1 becomes true and then stays true and then (possibly) becomes false and stays false
# # -There has to exist only one interval in the plan over which v1 is true.
# t = dict()
# t['name'] = 'atmostonce_strong'
# t['fml'] = ['F', '"v1"', '&& G (', '"v1"', '-> (', '"v1"', ' U (G ( !', '"v1"', '))))']
# t['vars'] = ['v1']
# t['ids'] = [ [1, 3, 5, 7] ]
# t['prob'] = probs['atmostonce_strong']
# t['fml_tree'] = ['and',
# ['F', 'v1'],
# ['G', ['imp', 'v1',
# ['U', 'v1',
# ['G', ['not', 'v1']]
# ]
# ]
# ]
# ]
# if t['name'] in choice: templates.append(t)
#### T9: Sometime-before (v1, v2):
# -If v1 occurs, sometime before v1 (no overlap), v2 must have occurred
# -v2 has to occur before A occurs
# -Doesn't enforce v2 or v1 to occur
t = dict()
t['name'] = 'sometime_before'
t['fml'] = ['( ', '"v2"', ' && ! ', '"v1"', ') R ( ! ', '"v1"', ' )']
t['vars'] = ['v1', 'v2']
t['ids'] = [ [3, 5], [1] ]
t['prob'] = probs['sometime_before']
t['fml_tree'] = ['R',
['and', 'v2', ['not', 'v1']],
['not', 'v1']
]
if t['name'] in choice: templates.append(t)
# #### T9: Sometime-before (strong):
# # Same as above but enforces v1 to occur
# t = dict()
# t['name'] = 'sometime_before_strong'
# t['fml'] = ['F ', '"v1"', ' && (( ', '"v2"', ' && ! ', '"v1"', ') R ( ! ', '"v1"', ' ))']
# t['vars'] = ['v1', 'v2']
# t['ids'] = [ [1, 5, 7], [3] ]
# t['prob'] = probs['sometime_before_strong']
# t['fml_tree'] = ['and', ['F', 'v1'],
# ['R',
# ['and', 'v2', ['not', 'v1']],
# ['not', 'v1']
# ]
# ]
# if t['name'] in choice: templates.append(t)
# # Precedence: v1 always precedes v2
# t = dict()
# t['fml'] = ['"v1"', 'R (!', '"v2"', '||', '"v1"', ')']
# t['ids'] = [ [0,4], [2] ]
# t['prob'] = 1.5
# templates.append(t)
# Normalize the prior probabilities
total_prob_mass = sum([t['prob'] for t in templates])
for i in range(len(templates)):
templates[i]['prob'] = 1. * templates[i]['prob'] / total_prob_mass
return templates
def pickLTLtemplate(templates, current = None, change = False, name = None, usePrior = False):
""" Randomly picks a LTL template from the list of templates
ARGUMENTS:
templates - templates (list of LTL dicts)
current - current LTL dict
change - whether or not to pick new template that is different than the current template
name - directly specifying a template to pick
usePrior - pick a template proportional to their prior
OUTPUT:
t - a new LTL template (uninstantiated)
"""
if usePrior:
probs = [t['prob'] for t in templates]
return np.random.choice(templates, 1, p = probs)[0]
if current is None:
if name:
possibles = [t for t in templates if t['name'] == name]
return random.choice(possibles)
else:
return random.choice(templates)
if change is True:
possibles = [t for t in templates if t['name'] != current['name']]
return random.choice(possibles)
def getLTLconjunct(t, vocab, props_list):
""" Returns ltl which is a conjunction of ltl template t instantiated with list from
props_list
INPUTS:
t - ltl template over where conjunction is applied
vocab - full vocabulary
props_list - input list of proposition(s) to include in conjunctions
OUTPUT:
t_conjunct - new ltl dict (conjunct)
NOTES:
* Currently populates everything except ['vars'] and ['ids']
"""
# Check that each value in props matches the number of free vars in template t
assert len(props_list[0]) == len(t['vars'])
# Sort the props_list alphabetically
props_list.sort(key=lambda x: str(' '.join(x)))
# Begin
t_conjunct = dict()
t_conjunct['name'] = t['name']
t_conjunct['prob'] = t['prob']
t_conjunct['str_tree'] = ['and']
t_conjunct['str_friendly'] = t_conjunct['name'] + ': '
t_conjunct['str'] = ''
t_conjunct['str_meaning'] = ''
p_set = set()
# Loop through each conjunction
for i, p in enumerate(props_list):
ltl = instantiateLTL(t, vocab, p)
p_set.update(set(p))
t_conjunct['str_tree'] += [ltl['str_tree']]
if i == 0:
t_conjunct['str_friendly'] += '('+ ','.join(p) + ')'
t_conjunct['str'] += '(' + ltl['str'] + ')'
else:
t_conjunct['str_friendly'] += ', ('+ ','.join(p) + ')'
t_conjunct['str'] += ' && ' + '(' + ltl['str'] + ')'
# Get ['props']
t_conjunct['props'] = list(p_set)
# Create ['props_list']
t_conjunct['props_list'] = props_list
# Construct ['fml']
fml = t_conjunct['str']
for i, p in enumerate(t_conjunct['props']):
# Replace p in 'str'
pattern = '"'+p+'"'
replace = '"v' + str(i+1) +'"'
fml = re.sub(pattern, replace, fml)
t_conjunct['fml'] = fml
# Create LTL meaning
t_conjunct['str_meaning'] = produceLTLStringMeaning(t_conjunct['name'], t_conjunct['props_list'])
return t_conjunct
######################################################################
######################################################################
######################################################################
class MH_sampler():
""" Metropolis-Hastings Sampler """
def __init__(self, ltl_initial, X, vocab, templates, params, perm_table, memory = None, cache = None, conjoin = False):
self.ltl_old = ltl_initial
self.X = X
self.vocab = vocab
self.templates = templates
self.params = params
self.perm_table = perm_table
self.conjoin = conjoin
self.memory = memory
self.cache = cache
self.posterior_dict = dict()
self.cscore_dict = dict()
# Recording
self.ltl_samples = []
self.ltl_log = {}
self.accept_reject_history = []
self.cscore_history = []
self.best_cscore = 0
self.best_cscore_history = []
self.ltl_str_meanings = dict()
# Probabilities
self.log_posterior_old, self.ltl_old['cscore'], self.memory = computePosterior(ltl_initial, X, vocab, params, memory, self.cache, conjoin)
self.posterior_dict[ltl_initial['str_friendly']] = self.log_posterior_old
self.cscore_dict[ltl_initial['str_friendly']] = self.ltl_old['cscore']
def addConjunct(self, t):
num_vars = len(t['vars'])
# Randomly pick a new conjunction to add
possibles = [p for p in self.perm_table[num_vars] if p not in self.ltl_old['props_list']]
p = random.choice(possibles)
# Return new ltl
ltl = getLTLconjunct(t, self.vocab, self.ltl_old['props_list'] + [p] )
return ltl
def removeConjunct(self, t):
# Remove one conjunction
props_list = random.sample(self.ltl_old['props_list'], len(self.ltl_old['props_list']) - 1)
# Return new ltl
ltl = getLTLconjunct(t, self.vocab, props_list)
return ltl
def moveLTLconjoin(self, epsilon = 0.1):
""" Proposal kernel for ltl conjunction space
NOTES:
* Features the following main moves:
-Sample from prior
-Drift from incumbent
-Add a conjunction
-Remove a conjunction
* The two main moves (prior vs drift) is selected based on flat exploration schedule, epsilon
(for future work, consider making it adaptive, e.g. using current validity score)
INPUTS:
epsilon - exploration schedule [0-1] denoting when to sample from prior
instead of using drifting from incumbent
OUTPUTS:
ltl - perturbed ltl
transition_prob - transition ratio, P(old|new) / P(new|old)
"""
# Sample from the prior
if random.random() < epsilon:
ltl = samplePrior(self.templates, self.vocab, self.perm_table, self.params['lambda'], conjoin = True)
transition_forward = 1
transition_backward = 1
# Drift kernel
else:
# Current template
t = pickLTLtemplate(self.templates, name = self.ltl_old['name'])
num_vars = len(t['vars'])
num_conjuncts_incumbent = len(self.ltl_old['props_list'])
num_all_perms = len(self.perm_table[num_vars])
# Case when you can't add anymore
if num_conjuncts_incumbent == num_all_perms:
# Remove
ltl = self.removeConjunct(t)
transition_forward = 1. / num_conjuncts_incumbent
transition_backward = 1
# Case when you can't remove anymore
elif num_conjuncts_incumbent == 1:
# Add
ltl = self.addConjunct(t)
transition_forward = 1. / (num_all_perms - num_conjuncts_incumbent)