-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdialogue.py
877 lines (787 loc) · 36.6 KB
/
dialogue.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
import random
from dialogue_tree import *
from rules import ExecuteAction, AddBelief, RemoveBelief, Perception
# For ease of debugging
action_start = 1
action_end = 30
# Specific moves
class Move:
def __init__(self, parent, player):
self.closed = False
self.parent = parent
self.player = player
def is_closed(self):
return self.closed
def get_player(self):
return self.player
def check_closure(self, position, store):
pass
def perform(self):
if (self.parent.empty):
self.parent.set_move(self)
else:
self.parent.add_child(self)
def move_equals(self, move):
# print(self.player.name())
# print(move.player.name())
if (self.player.name() == move.player.name()):
return True
return False
class WhyNotAction(Move):
def __init__(self, turn, action, i, parent):
Move.__init__(self, parent, turn)
self.action = action
self.trace_point = i
def check_closure(self, position, store):
for node in store.node_list():
move = node.get_move()
if (isinstance(move, WhyAction)):
if move.action == self.action:
if (move.trace_point == self.trace_point):
self.closed = True
break
if (isinstance(move, IDid)):
if move.action == self.action:
if (move.trace_point == self.trace_point):
self.closed = True
break
def __repr__(self):
return str(self.player.name()) + ": Why Not " + str(self.action) + " at " + str(self.trace_point)
def move_equals(self, move):
if (isinstance(move, WhyNotAction)):
if super.move_equals(move):
if self.action == move.action:
if self.trace_point == move.trace_point:
return True
return False
class WhyAction(Move):
def __init__(self, turn, action, i, parent):
Move.__init__(self, parent, turn)
self.action = action
self.trace_point = i
def check_closure(self, position, store):
for node in store.node_list():
move = node.get_move()
if (move.trace_point == self.trace_point - 1):
if (isinstance(move, AssertPi)):
for effect in move.pi.effects:
if (isinstance(effect, ExecuteAction)):
# print (effect.action)
# print (move.action)
if effect.action == self.action:
self.closed = True
break
if (self.closed):
break
if (isinstance(move, IDidnt)):
if move.action == self.action:
if (move.trace_point == self.trace_point):
self.closed = True
break
def move_equals(self, move):
if (isinstance(move, WhyAction)):
if super().move_equals(move):
if self.action == move.action:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": Why " + str(self.action) + " at " + str(self.trace_point)
class IDid(Move):
def __init__(self, turn, action, i, parent):
Move.__init__(self, parent, turn)
self.action = action
self.trace_point = i
self.closed = True
def move_equals(self, move):
if (isinstane(move, IDid)):
if super().move_equals(move):
if self.action == move.action:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": I Did " + str(self.action) + " at " + str(self.trace_point)
class IDidnt(Move):
def __init__(self, turn, action, i, parent):
Move.__init__(self, parent, turn)
self.action = action
self.trace_point = i
self.closed = True
def move_equals(self, move):
if (isinstane(move, IDidnt)):
if super().move_equals(move):
if self.action == move.action:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
if (self.player.name() == "robot"):
return str(self.player.name()) + ": I Didn't " + str(self.action) + " at " + str(self.trace_point)
else:
return str(self.player.name()) + ": I Didn't expect " + str(self.action) + " at " + str(self.trace_point)
class AssertPi(Move):
def __init__(self, turn, pi, i, parent):
Move.__init__(self, parent, turn)
self.pi = pi
self.trace_point = i
self.closed = False
def check_closure(self, position, store):
for node in store.node_list():
move = node.get_move()
if (hasattr(move, 'trace_point')):
if (move.trace_point == self.trace_point):
if (move.get_player() != self.get_player()):
if (isinstance(move, AssertPi)):
node_parent = move.parent
move_parent = node_parent.get_move()
if (move_parent.move_equals(self)):
self.closed = True
break
if (isinstance(move, WhyPi)):
self.closed = True
break
if (isinstance(move, AcceptPi)):
node_parent = move.parent
move_parent = node_parent.get_move()
if (move_parent.move_equals(self)):
self.closed = True
break
else:
if (move.get_player() != self.get_player()):
if (isinstance(move, NotInLibrary)):
if move.pi == self.pi:
self.closed = True
break
if (isinstance(move, Precedence)):
node_parent = move.parent
move_parent = node_parent.get_move()
if (move_parent.move_equals(self)):
self.closed = True
break
def move_equals(self, move):
if (isinstance(move, AssertPi)):
if (super().move_equals(move)):
if self.pi == move.pi:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": Selected " + str(self.pi) + " at " + str(self.trace_point)
class NotInLibrary(Move):
def __init__(self, turn, pi, parent):
Move.__init__(self, parent, turn)
self.pi = pi
self.closed = True
def move_equals(self, move):
if (isinstance(move, NotInLibrary)):
if (super().move_equals(move)):
if self.pi == move.pi:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": " + str(self.pi) + " is not in my plan library"
class Precedence(Move):
def __init__(self, turn, pi, parent):
Move.__init__(self, parent, turn)
self.pi = pi
self.closed = True
def move_equals(self, move):
if (isinstance(move, Precedence)):
if (super.move_equals(move)):
if self.pi == move.pi:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": " + str(self.pi) + " has precedence in my plan library"
class AcceptPi(Move):
def __init__(self, turn, pi, i, parent):
Move.__init__(self, parent, turn)
self.pi = pi
self.trace_point = i
self.closed = True
def move_equals(self, move):
if (isinstance(move, AcceptPi)):
if (super().move_equals(move)):
if self.pi == move.pi:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": I agree that " + str(self.pi) + " was selected at " + str(self.trace_point)
class WhyPi(Move):
def __init__(self, turn, pi, i, parent):
Move.__init__(self, parent, turn)
self.pi = pi
self.trace_point = i
self.closed = False
def check_closure(self, position, store):
for bel in self.pi.beliefs:
bel_literal = AddBelief(bel) # At the moment plans only contain positive beliefs
bl_closed = False
for node in store.node_list():
move = node.get_move()
if (isinstance(move, AssertBelief)):
if (move.belief.effect_equals(bel_literal)):
if (move.trace_point == self.trace_point):
bl_closed = True
break;
if (not bl_closed):
return
self.closed = True
def move_equals(self, move):
if (isinstance(move, WhyPi)):
if (super().move_equals(move)):
if self.pi == move.pi:
if self.trace_point == move.trace_point:
return True
def __repr__(self):
return str(self.player.name()) + ": Why select " + str(self.pi) + " at " + str(self.trace_point)
class AssertBelief(Move):
def __init__(self, turn, belief, i1, i2, parent):
Move.__init__(self, parent, turn)
self.belief = belief
self.lowerbound = i1
self.trace_point = i2
def check_closure(self, position, store):
for node in store.node_list():
move = node.get_move()
if (isinstance(move, WhyBelief)):
if (move.belief == self.belief and move.trace_point == self.lowerbound):
self.closed = True
break
if (isinstance(move, AssertBelief)):
notbelief = move.belief
if (isinstance(self.belief, AddBelief)):
if (isinstance(notbelief, RemoveBelief)):
if (self.belief.belief == notbelief.belief):
if (self.trace_point == move.trace_point):
if (self.lowerbound < move.lowerbound):
self.closed = True
break
if (isinstance(self.belief, RemoveBelief)):
if (isinstance(notbelief, AddBelief)):
if (self.belief.belief == notbelief.belief):
if (self.trace_point == move.trace_point):
if (self.lowerbound < move.lowerbound):
self.closed = True
break
if (isinstance(move, AcceptBelief)):
if (move.belief == self.belief and move.trace_point == self.trace_point and move.lowerbound == self.lowerbound):
self.closed = True
break
def move_equals(self, move):
if (isinstance(move, AssertBelief)):
if super().move_equals(move):
if self.belief.effect_equals(move.belief):
if self.lowerbound == move.lowerbound:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": " + str(self.belief) + " at time " + str(self.lowerbound) + " and it remained so until at least " + str(self.trace_point)
class AcceptBelief(Move):
def __init__(self, turn, belief, i1, i2, parent):
Move.__init__(self, parent, turn)
self.belief = belief
self.lowerbound = i1
self.trace_point = i2
self.closed = True
def move_equals(self, move):
if (isinstance(move, AcceptBelief)):
if super().move_equals(move):
if self.belief.effect_equals(move.belief):
if self.lowerbound == move.lowerbound:
if self.trace_point == move.trace_point:
return True
return False
def __repr__(self):
return str(self.player.name()) + ": I agree " + str(self.belief) + " between " + str(self.lowerbound) + " and " + str(self.trace_point)
class WhyBelief(Move):
def __init__(self, turn, belief, i, parent):
Move.__init__(self, parent, turn)
self.belief = belief
self.trace_point = i
self.closed = False
def check_closure(self, position, store):
for node in store.node_list():
move = node.get_move()
if (isinstance(move, AssertPi)):
if (move.trace_point == self.trace_point - 1):
self.closed = True
if (isinstance(move, Percept)):
if (move.trace_point == self.trace_point):
if (move.belief.effect_equals(self.belief)):
self.closed = True
def move_equals(self, move):
if (isinstance(move, WhyBelief)):
if super().move_equals(move):
if self.belief.effect_equals(move.belief):
if self.trace_point == move.trace_point:
return True
def __repr__(self):
return str(self.player.name()) + ": Why " + str(self.belief) + " at " + str(self.trace_point)
class Percept(Move):
def __init__(self, turn, belief, i, parent):
Move.__init__(self, parent, turn)
self.belief = belief
self.trace_point = i;
self.closed = True
def move_equals(self, move):
if (isinstance(move, Percept)):
if super().move_equals(move):
if self.belief.effect_equals(move.belief):
if self.trace_point == move.trace_point:
return True
def __repr__(self):
return str(self.player.name()) + ": I perceived " + str(self.belief) + " at " + str(self.trace_point)
# Types of move - there may be several of each sort that are legal at any one point
class MoveType:
def legal(self, store, turn, actions):
return []
class WhyNotActionType(MoveType):
def legal(self, store, turn, actions):
move_list = []
if (store.empty):
for action in actions:
for i in range(action_start, action_end):
move_list.append(WhyNotAction(turn, action, i, store))
return move_list
class IDidActionType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move();
if (not move.is_closed()):
if (isinstance(move, WhyNotAction)):
if (move.get_player() != turn):
player_internal_trace_point = turn.trace[move.trace_point]
actions = player_internal_trace_point[4]
if (move.action in actions):
move_list.append(IDid(turn, move.action, move.trace_point, node))
return move_list
class WhyActionType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
for action in actions:
for i in range(action_start, action_end):
move_list.append(WhyAction(turn, action, i, store))
continue
move = node.get_move()
if (not move.is_closed()):
if (isinstance(move, WhyNotAction)):
if (move.get_player() != turn):
move_list.append(WhyAction(turn, move.action, move.trace_point, node))
return move_list
class IDidntType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move();
if (not move.is_closed()):
if (isinstance(move, WhyAction)):
if (move.get_player() != turn):
player_internal_trace_point = turn.trace[move.trace_point]
actions = player_internal_trace_point[4]
#print(player_internal_trace_point)
#print(actions)
#print(move.action)
if (not (move.action in actions)):
move_list.append(IDidnt(turn, move.action, move.trace_point, node))
return move_list
class AssertPiType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, WhyAction)):
# print(move)
player_internal_trace_point = turn.trace[move.trace_point - 1]
rule = player_internal_trace_point[5]
if (rule.effects == set()):
break;
for effect in rule.effects:
# print(effect)
if (isinstance(effect, ExecuteAction)):
# print (effect.action)
# print (move.action)
if effect.action == move.action:
move_list.append(AssertPi(turn, rule, move.trace_point - 1,node))
break;
if (isinstance(move, WhyBelief)):
player_internal_trace_point = turn.trace[move.trace_point - 1]
rule = player_internal_trace_point[5]
if (rule.effects == set()):
break;
for effect in rule.effects:
if (move.belief.effect_equals(effect)):
move_list.append(AssertPi(turn, rule, move.trace_point - 1,node))
break
if (isinstance(move, AssertPi)):
player_internal_trace_point = turn.trace[move.trace_point]
rule = player_internal_trace_point[5]
if (rule.effects == set()):
break;
if (not rule.rule_equals(move.pi)):
potential_new_move = AssertPi(turn, rule, move.trace_point,node)
legal_move = True
for node1 in store.node_list():
if (node1.empty):
continue
move1 = node.get_move();
if (move1.move_equals(potential_new_move)):
legal_move = False
break
if (move1.parent.get_move().move_equals(potential_new_move)):
legal_move = False
break
if (legal_move):
move_list.append(potential_new_move)
return move_list
class NotInLibraryType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, AssertPi)):
if (not move.pi.in_rule_list(turn.rules)):
move_list.append(NotInLibrary(turn, move.pi, node))
return move_list
class PrecedenceType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, AssertPi)):
# print("found assert pi")
# print(move.pi)
# print(turn.rules)
if (move.pi.in_rule_list(turn.rules)):
# print("move pi in rules")
node_parent = move.parent
move_parent = node_parent.get_move()
if (isinstance(move_parent, AssertPi)):
# print("move parent is assert pi")
if (move_parent.get_player() == turn):
# print("move parent is me")
move_list.append(Precedence(turn, move_parent.pi, node))
return move_list
class AcceptPiType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, AssertPi)):
trace_point_num = move.trace_point
trace_point = turn.trace[trace_point_num]
rule = trace_point[5]
if (rule.effects == set()):
break;
if (rule.rule_equals(move.pi)):
move_list.append(AcceptPi(turn, rule, trace_point_num, node))
return move_list
class WhyPiType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, AssertPi)):
move_list.append(WhyPi(turn, move.pi, move.trace_point, node))
return move_list
class AssertBeliefType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, WhyPi)):
pi = move.pi
trace_point = move.trace_point
for belief in pi.beliefs:
# print(belief)
# if (isinstance(belief, AddBelief)):
# print("b")
# bel = belief.belief
for x in range(trace_point, 0, -1):
if (belief in turn.trace[x][2]):
new_move = AssertBelief(turn, AddBelief(belief), x, move.trace_point, node)
# print(new_move)
not_repeat = self.not_repeat(store, new_move)
if not_repeat:
# print("not repeated")
move_list.append(new_move)
break
# if (isinstance(belief, RemoveBelief)):
# print("c")
# bel = belief.belief
# for x in range(trace_point, 0, -1):
# if (bel in turn.trace[x][3]):
# move_list.append(AssertBelief(turn, belief, x, move.trace_point, node))
# break
# if (x == 1):
# move_list.append(AssertBelief(turn, belief, x, move.trace_point, node))
if (isinstance(move, AssertBelief)):
notbelief = move.belief
upperbound = move.trace_point
lowerbound = move.lowerbound
for x in range(upperbound, lowerbound, -1):
if (isinstance(notbelief, AddBelief)):
bel = notbelief.belief
if (bel in turn.trace[x][3]):
new_move = AssertBelief(turn, RemoveBelief(bel), x, move.trace_point, node)
not_repeat = self.not_repeat(store, new_move)
if not_repeat:
move_list.append(new_move)
break
if (isinstance(notbelief, RemoveBelief)):
bel = notbelief.belief
if (bel in turn.trace[x][2]):
new_move = AssertBelief(turn, AddBelief(bel), x, move.upperbound, node)
not_repeat = self.not_repeat(store, new_move)
if not_repeat:
move_list.append(new_move)
break
return move_list
def not_repeat(self, store, new_move):
not_repeat = True
for node in store.node_list():
move1 = node.get_move()
if (move1.move_equals(new_move)):
if (move1.parent.get_move().move_equals(new_move.parent.get_move())):
not_repeat = False
break
return not_repeat
class AcceptBeliefType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, AssertBelief)):
bel_literal = move.belief
if (isinstance(bel_literal, AddBelief)):
belief = bel_literal.belief
lowerbound = move.lowerbound
upperbound = move.trace_point
for x in range(lowerbound, upperbound + 1):
holds = True
if (belief not in turn.trace[x][0]):
holds = False
break
if (holds):
move_list.append(AcceptBelief(turn, bel_literal, lowerbound, upperbound, node))
if (isinstance(bel_literal, RemoveBelief)):
belief = bel_literal.belief
lowerbound = move.lowerbound
upperbound = move.trace_point
for x in range(lowerbound, upperbound + 1):
holds = True
if (belief in turn.trace[x][0]):
holds = False
break
if (holds):
move_list.append(AcceptBelief(turn, bel_literal, lowerbound, upperbound, node))
return move_list
class WhyBeliefType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
# print("why belief" + move.__repr__())
if (not move.is_closed()):
# print("a")
if (move.get_player() != turn):
# print("b")
if (isinstance(move, AssertBelief)):
bel_literal = move.belief
move_list.append(WhyBelief(turn, bel_literal, move.lowerbound, node))
return move_list
class PerceptType(MoveType):
def legal(self, store, turn, actions):
move_list = []
for node in store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
if (move.get_player() != turn):
if (isinstance(move, WhyBelief)):
bel_literal = move.belief
trace_point = move.trace_point
# print(turn.trace[trace_point])
if (isinstance(turn.trace[trace_point][5], Perception)):
if (isinstance(bel_literal, AddBelief)):
for belief in turn.trace[trace_point][2]:
if (belief == bel_literal.belief):
move_list.append(Percept(turn, bel_literal, trace_point, node))
if (isinstance(bel_literal, RemoveBelief)):
for belief in turn.trace[trace_point][3]:
if (belief == bel_literal.belief):
move_list.append(Percept(turn, bel_literal, trace_point, node))
return move_list
# Actual dialogue class
class Dialogue:
def __init__(self, human, robot, actions, public_trace):
# Basic strategy mechanism -- needs to be configurable somehow
# self.strategy = "random"
self.strategy = "public_action_diff"
self.store=DialogueTree()
self.turn = human;
self.initiator = human;
self.responder = robot;
self.move_list = [];
self.can_continue = True;
self.actions = actions;
self.public_trace = public_trace
self.move_list.append(WhyNotActionType())
self.move_list.append(IDidActionType())
self.move_list.append(WhyActionType())
self.move_list.append(IDidntType())
self.move_list.append(AssertPiType())
self.move_list.append(NotInLibraryType())
self.move_list.append(PrecedenceType())
self.move_list.append(AcceptPiType())
self.move_list.append(WhyPiType())
self.move_list.append(AssertBeliefType())
self.move_list.append(AcceptBeliefType())
self.move_list.append(WhyBeliefType())
self.move_list.append(PerceptType())
def move(self):
legal_moves = self.calculate_legal_moves();
if (len(legal_moves) == 0):
# print("no legal moves for " + self.turn.name())
if (self.turn == self.responder):
self.turn = self.initiator
else:
self.turn = self.responder
legal_moves = self.calculate_legal_moves()
if (len(legal_moves) != 0):
next_move = self.choose_next_move(legal_moves);
next_move.perform()
self.propagate_closure()
next_turn = random.randint(0,1);
if (next_turn == 0):
self.turn = self.responder
else:
self.turn = self.initiator
print(next_move)
else:
self.can_continue = False
print("No Legal Moves")
def calculate_legal_moves(self):
legal_move_list = []
for move_type in self.move_list:
for move in move_type.legal(self.store, self.turn, self.actions):
legal_move_list.append(move)
# if len(legal_move_list) == 0:
# self.can_continue = False
return legal_move_list
def choose_next_move(self, move_list):
if (self.strategy == "random"):
return random.choice(move_list)
if (self.strategy == "public_action_diff"):
new_move_list = []
if (self.store.empty):
for move in move_list:
trace_point = move.trace_point
if (trace_point % 3 == 2): # This is a point where an action might have taken place
# print(move)
public_trace_point = (trace_point // 3) # This should be the relevant point in the public trace
# print(public_trace_point)
if (len(self.public_trace[public_trace_point]) > 1): # Someone was expecting an action here.
print("b")
if (len(self.public_trace[public_trace_point]) > 2): # Everyone was expecting an action here.
print(self.public_trace[public_trace_point])
robot_action_string = self.public_trace[public_trace_point][1].split(":")[1]
human_action_string = self.public_trace[public_trace_point][2].split(":")[1]
if (not robot_action_string == human_action_string):
if (isinstance(move, WhyAction)):
if (move.action == robot_action_string):
new_move_list.append(move)
if (isinstance(move, WhyNotAction)):
if (move.action == human_action_string):
new_move_list.append(move)
else:
action_array = self.public_trace[public_trace_point][1].split(":")
actor = action_array[0]
action = action_array[1]
# print("a")
if (actor == "robot"):
if (isinstance(move, WhyAction)):
# print(move)
# print(action)
# print(move.action)
if (move.action == action):
new_move_list.append(move)
else:
if (isinstance(move, WhyNotAction)):
if (move.action == action):
new_move_list.append(move)
# print(self.public_trace[public_trace_point])
# print(self.turn.trace[trace_point])
# public_trace_point == public_trace[trace_point]
return random.choice(new_move_list)
else:
return random.choice(move_list)
def propagate_closure(self):
i = 0
for node in self.store.node_list():
move = node.get_move()
if (not move.is_closed()):
move.check_closure(i, self.store)
i = i + 1
return
def is_closed(self):
if (len(self.store.node_list()) == 0):
return False
for node in self.store.node_list():
if (node.empty):
continue
move = node.get_move()
if (not move.is_closed()):
return False;
return True
def can_proceed(self):
return self.can_continue
def __repr__(self):
string = "Dialogue:\n"
for node in self.store.node_list():
if (node.empty):
continue
move = node.get_move()
string += str(move) + "\n"
return string