forked from aimacode/aima-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planning.py
2005 lines (1679 loc) · 79.2 KB
/
planning.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
"""Planning (Chapters 10-11)
"""
import copy
import itertools
from collections import deque, defaultdict
from functools import reduce as _reduce
import search
from csp import sat_up, NaryCSP, Constraint, ac_search_solver, is_
from logic import FolKB, conjuncts, unify, associate, SAT_plan, cdcl_satisfiable
from search import Node
from utils import Expr, expr, first
class PlanningProblem:
"""
Planning Domain Definition Language (PlanningProblem) used to define a search problem.
It stores states in a knowledge base consisting of first order logic statements.
The conjunction of these logical statements completely defines a state.
"""
def __init__(self, initial, goals, actions, domain=None):
self.initial = self.convert(initial) if domain is None else self.convert(initial) + self.convert(domain)
self.goals = self.convert(goals)
self.actions = actions
self.domain = domain
def convert(self, clauses):
"""Converts strings into exprs"""
if not isinstance(clauses, Expr):
if len(clauses) > 0:
clauses = expr(clauses)
else:
clauses = []
try:
clauses = conjuncts(clauses)
except AttributeError:
pass
new_clauses = []
for clause in clauses:
if clause.op == '~':
new_clauses.append(expr('Not' + str(clause.args[0])))
else:
new_clauses.append(clause)
return new_clauses
def expand_fluents(self, name=None):
kb = None
if self.domain:
kb = FolKB(self.convert(self.domain))
for action in self.actions:
if action.precond:
for fests in set(action.precond).union(action.effect).difference(self.convert(action.domain)):
if fests.op[:3] != 'Not':
kb.tell(expr(str(action.domain) + ' ==> ' + str(fests)))
objects = set(arg for clause in set(self.initial + self.goals) for arg in clause.args)
fluent_list = []
if name is not None:
for fluent in self.initial + self.goals:
if str(fluent) == name:
fluent_list.append(fluent)
break
else:
fluent_list = list(map(lambda fluent: Expr(fluent[0], *fluent[1]),
{fluent.op: fluent.args for fluent in self.initial + self.goals +
[clause for action in self.actions for clause in action.effect if
clause.op[:3] != 'Not']}.items()))
expansions = []
for fluent in fluent_list:
for permutation in itertools.permutations(objects, len(fluent.args)):
new_fluent = Expr(fluent.op, *permutation)
if (self.domain and kb.ask(new_fluent) is not False) or not self.domain:
expansions.append(new_fluent)
return expansions
def expand_actions(self, name=None):
"""Generate all possible actions with variable bindings for precondition selection heuristic"""
has_domains = all(action.domain for action in self.actions if action.precond)
kb = None
if has_domains:
kb = FolKB(self.initial)
for action in self.actions:
if action.precond:
kb.tell(expr(str(action.domain) + ' ==> ' + str(action)))
objects = set(arg for clause in self.initial for arg in clause.args)
expansions = []
action_list = []
if name is not None:
for action in self.actions:
if str(action.name) == name:
action_list.append(action)
break
else:
action_list = self.actions
for action in action_list:
for permutation in itertools.permutations(objects, len(action.args)):
bindings = unify(Expr(action.name, *action.args), Expr(action.name, *permutation))
if bindings is not None:
new_args = []
for arg in action.args:
if arg in bindings:
new_args.append(bindings[arg])
else:
new_args.append(arg)
new_expr = Expr(str(action.name), *new_args)
if (has_domains and kb.ask(new_expr) is not False) or (
has_domains and not action.precond) or not has_domains:
new_preconds = []
for precond in action.precond:
new_precond_args = []
for arg in precond.args:
if arg in bindings:
new_precond_args.append(bindings[arg])
else:
new_precond_args.append(arg)
new_precond = Expr(str(precond.op), *new_precond_args)
new_preconds.append(new_precond)
new_effects = []
for effect in action.effect:
new_effect_args = []
for arg in effect.args:
if arg in bindings:
new_effect_args.append(bindings[arg])
else:
new_effect_args.append(arg)
new_effect = Expr(str(effect.op), *new_effect_args)
new_effects.append(new_effect)
expansions.append(Action(new_expr, new_preconds, new_effects))
return expansions
def is_strips(self):
"""
Returns True if the problem does not contain negative literals in preconditions and goals
"""
return (all(clause.op[:3] != 'Not' for clause in self.goals) and
all(clause.op[:3] != 'Not' for action in self.actions for clause in action.precond))
def goal_test(self):
"""Checks if the goals have been reached"""
return all(goal in self.initial for goal in self.goals)
def act(self, action):
"""
Performs the action given as argument.
Note that action is an Expr like expr('Remove(Glass, Table)') or expr('Eat(Sandwich)')
"""
action_name = action.op
args = action.args
list_action = first(a for a in self.actions if a.name == action_name)
if list_action is None:
raise Exception("Action '{}' not found".format(action_name))
if not list_action.check_precond(self.initial, args):
raise Exception("Action '{}' pre-conditions not satisfied".format(action))
self.initial = list_action(self.initial, args).clauses
class Action:
"""
Defines an action schema using preconditions and effects.
Use this to describe actions in PlanningProblem.
action is an Expr where variables are given as arguments(args).
Precondition and effect are both lists with positive and negative literals.
Negative preconditions and effects are defined by adding a 'Not' before the name of the clause
Example:
precond = [expr("Human(person)"), expr("Hungry(Person)"), expr("NotEaten(food)")]
effect = [expr("Eaten(food)"), expr("Hungry(person)")]
eat = Action(expr("Eat(person, food)"), precond, effect)
"""
def __init__(self, action, precond, effect, domain=None):
if isinstance(action, str):
action = expr(action)
self.name = action.op
self.args = action.args
self.precond = self.convert(precond) if domain is None else self.convert(precond) + self.convert(domain)
self.effect = self.convert(effect)
self.domain = domain
def __call__(self, kb, args):
return self.act(kb, args)
def __repr__(self):
return '{}'.format(Expr(self.name, *self.args))
def convert(self, clauses):
"""Converts strings into Exprs"""
if isinstance(clauses, Expr):
clauses = conjuncts(clauses)
for i in range(len(clauses)):
if clauses[i].op == '~':
clauses[i] = expr('Not' + str(clauses[i].args[0]))
elif isinstance(clauses, str):
clauses = clauses.replace('~', 'Not')
if len(clauses) > 0:
clauses = expr(clauses)
try:
clauses = conjuncts(clauses)
except AttributeError:
pass
return clauses
def relaxed(self):
"""
Removes delete list from the action by removing all negative literals from action's effect
"""
return Action(Expr(self.name, *self.args), self.precond,
list(filter(lambda effect: effect.op[:3] != 'Not', self.effect)))
def substitute(self, e, args):
"""Replaces variables in expression with their respective Propositional symbol"""
new_args = list(e.args)
for num, x in enumerate(e.args):
for i, _ in enumerate(self.args):
if self.args[i] == x:
new_args[num] = args[i]
return Expr(e.op, *new_args)
def check_precond(self, kb, args):
"""Checks if the precondition is satisfied in the current state"""
if isinstance(kb, list):
kb = FolKB(kb)
for clause in self.precond:
if self.substitute(clause, args) not in kb.clauses:
return False
return True
def act(self, kb, args):
"""Executes the action on the state's knowledge base"""
if isinstance(kb, list):
kb = FolKB(kb)
if not self.check_precond(kb, args):
raise Exception('Action pre-conditions not satisfied')
for clause in self.effect:
kb.tell(self.substitute(clause, args))
if clause.op[:3] == 'Not':
new_clause = Expr(clause.op[3:], *clause.args)
if kb.ask(self.substitute(new_clause, args)) is not False:
kb.retract(self.substitute(new_clause, args))
else:
new_clause = Expr('Not' + clause.op, *clause.args)
if kb.ask(self.substitute(new_clause, args)) is not False:
kb.retract(self.substitute(new_clause, args))
return kb
def goal_test(goals, state):
"""Generic goal testing helper function"""
if isinstance(state, list):
kb = FolKB(state)
else:
kb = state
return all(kb.ask(q) is not False for q in goals)
def air_cargo():
"""
[Figure 10.1] AIR-CARGO-PROBLEM
An air-cargo shipment problem for delivering cargo to different locations,
given the starting location and airplanes.
Example:
>>> from planning import *
>>> ac = air_cargo()
>>> ac.goal_test()
False
>>> ac.act(expr('Load(C2, P2, JFK)'))
>>> ac.act(expr('Load(C1, P1, SFO)'))
>>> ac.act(expr('Fly(P1, SFO, JFK)'))
>>> ac.act(expr('Fly(P2, JFK, SFO)'))
>>> ac.act(expr('Unload(C2, P2, SFO)'))
>>> ac.goal_test()
False
>>> ac.act(expr('Unload(C1, P1, JFK)'))
>>> ac.goal_test()
True
>>>
"""
return PlanningProblem(initial='At(C1, SFO) & At(C2, JFK) & At(P1, SFO) & At(P2, JFK)',
goals='At(C1, JFK) & At(C2, SFO)',
actions=[Action('Load(c, p, a)',
precond='At(c, a) & At(p, a)',
effect='In(c, p) & ~At(c, a)',
domain='Cargo(c) & Plane(p) & Airport(a)'),
Action('Unload(c, p, a)',
precond='In(c, p) & At(p, a)',
effect='At(c, a) & ~In(c, p)',
domain='Cargo(c) & Plane(p) & Airport(a)'),
Action('Fly(p, f, to)',
precond='At(p, f)',
effect='At(p, to) & ~At(p, f)',
domain='Plane(p) & Airport(f) & Airport(to)')],
domain='Cargo(C1) & Cargo(C2) & Plane(P1) & Plane(P2) & Airport(SFO) & Airport(JFK)')
def spare_tire():
"""[Figure 10.2] SPARE-TIRE-PROBLEM
A problem involving changing the flat tire of a car
with a spare tire from the trunk.
Example:
>>> from planning import *
>>> st = spare_tire()
>>> st.goal_test()
False
>>> st.act(expr('Remove(Spare, Trunk)'))
>>> st.act(expr('Remove(Flat, Axle)'))
>>> st.goal_test()
False
>>> st.act(expr('PutOn(Spare, Axle)'))
>>> st.goal_test()
True
>>>
"""
return PlanningProblem(initial='At(Flat, Axle) & At(Spare, Trunk)',
goals='At(Spare, Axle) & At(Flat, Ground)',
actions=[Action('Remove(obj, loc)',
precond='At(obj, loc)',
effect='At(obj, Ground) & ~At(obj, loc)',
domain='Tire(obj)'),
Action('PutOn(t, Axle)',
precond='At(t, Ground) & ~At(Flat, Axle)',
effect='At(t, Axle) & ~At(t, Ground)',
domain='Tire(t)'),
Action('LeaveOvernight',
precond='',
effect='~At(Spare, Ground) & ~At(Spare, Axle) & ~At(Spare, Trunk) & \
~At(Flat, Ground) & ~At(Flat, Axle) & ~At(Flat, Trunk)')],
domain='Tire(Flat) & Tire(Spare)')
def three_block_tower():
"""
[Figure 10.3] THREE-BLOCK-TOWER
A blocks-world problem of stacking three blocks in a certain configuration,
also known as the Sussman Anomaly.
Example:
>>> from planning import *
>>> tbt = three_block_tower()
>>> tbt.goal_test()
False
>>> tbt.act(expr('MoveToTable(C, A)'))
>>> tbt.act(expr('Move(B, Table, C)'))
>>> tbt.goal_test()
False
>>> tbt.act(expr('Move(A, Table, B)'))
>>> tbt.goal_test()
True
>>>
"""
return PlanningProblem(initial='On(A, Table) & On(B, Table) & On(C, A) & Clear(B) & Clear(C)',
goals='On(A, B) & On(B, C)',
actions=[Action('Move(b, x, y)',
precond='On(b, x) & Clear(b) & Clear(y)',
effect='On(b, y) & Clear(x) & ~On(b, x) & ~Clear(y)',
domain='Block(b) & Block(y)'),
Action('MoveToTable(b, x)',
precond='On(b, x) & Clear(b)',
effect='On(b, Table) & Clear(x) & ~On(b, x)',
domain='Block(b) & Block(x)')],
domain='Block(A) & Block(B) & Block(C)')
def simple_blocks_world():
"""
SIMPLE-BLOCKS-WORLD
A simplified definition of the Sussman Anomaly problem.
Example:
>>> from planning import *
>>> sbw = simple_blocks_world()
>>> sbw.goal_test()
False
>>> sbw.act(expr('ToTable(A, B)'))
>>> sbw.act(expr('FromTable(B, A)'))
>>> sbw.goal_test()
False
>>> sbw.act(expr('FromTable(C, B)'))
>>> sbw.goal_test()
True
>>>
"""
return PlanningProblem(initial='On(A, B) & Clear(A) & OnTable(B) & OnTable(C) & Clear(C)',
goals='On(B, A) & On(C, B)',
actions=[Action('ToTable(x, y)',
precond='On(x, y) & Clear(x)',
effect='~On(x, y) & Clear(y) & OnTable(x)'),
Action('FromTable(y, x)',
precond='OnTable(y) & Clear(y) & Clear(x)',
effect='~OnTable(y) & ~Clear(x) & On(y, x)')])
def have_cake_and_eat_cake_too():
"""
[Figure 10.7] CAKE-PROBLEM
A problem where we begin with a cake and want to
reach the state of having a cake and having eaten a cake.
The possible actions include baking a cake and eating a cake.
Example:
>>> from planning import *
>>> cp = have_cake_and_eat_cake_too()
>>> cp.goal_test()
False
>>> cp.act(expr('Eat(Cake)'))
>>> cp.goal_test()
False
>>> cp.act(expr('Bake(Cake)'))
>>> cp.goal_test()
True
>>>
"""
return PlanningProblem(initial='Have(Cake)',
goals='Have(Cake) & Eaten(Cake)',
actions=[Action('Eat(Cake)',
precond='Have(Cake)',
effect='Eaten(Cake) & ~Have(Cake)'),
Action('Bake(Cake)',
precond='~Have(Cake)',
effect='Have(Cake)')])
def shopping_problem():
"""
SHOPPING-PROBLEM
A problem of acquiring some items given their availability at certain stores.
Example:
>>> from planning import *
>>> sp = shopping_problem()
>>> sp.goal_test()
False
>>> sp.act(expr('Go(Home, HW)'))
>>> sp.act(expr('Buy(Drill, HW)'))
>>> sp.act(expr('Go(HW, SM)'))
>>> sp.act(expr('Buy(Banana, SM)'))
>>> sp.goal_test()
False
>>> sp.act(expr('Buy(Milk, SM)'))
>>> sp.goal_test()
True
>>>
"""
return PlanningProblem(initial='At(Home) & Sells(SM, Milk) & Sells(SM, Banana) & Sells(HW, Drill)',
goals='Have(Milk) & Have(Banana) & Have(Drill)',
actions=[Action('Buy(x, store)',
precond='At(store) & Sells(store, x)',
effect='Have(x)',
domain='Store(store) & Item(x)'),
Action('Go(x, y)',
precond='At(x)',
effect='At(y) & ~At(x)',
domain='Place(x) & Place(y)')],
domain='Place(Home) & Place(SM) & Place(HW) & Store(SM) & Store(HW) & '
'Item(Milk) & Item(Banana) & Item(Drill)')
def socks_and_shoes():
"""
SOCKS-AND-SHOES-PROBLEM
A task of wearing socks and shoes on both feet
Example:
>>> from planning import *
>>> ss = socks_and_shoes()
>>> ss.goal_test()
False
>>> ss.act(expr('RightSock'))
>>> ss.act(expr('RightShoe'))
>>> ss.act(expr('LeftSock'))
>>> ss.goal_test()
False
>>> ss.act(expr('LeftShoe'))
>>> ss.goal_test()
True
>>>
"""
return PlanningProblem(initial='',
goals='RightShoeOn & LeftShoeOn',
actions=[Action('RightShoe',
precond='RightSockOn',
effect='RightShoeOn'),
Action('RightSock',
precond='',
effect='RightSockOn'),
Action('LeftShoe',
precond='LeftSockOn',
effect='LeftShoeOn'),
Action('LeftSock',
precond='',
effect='LeftSockOn')])
def double_tennis_problem():
"""
[Figure 11.10] DOUBLE-TENNIS-PROBLEM
A multiagent planning problem involving two partner tennis players
trying to return an approaching ball and repositioning around in the court.
Example:
>>> from planning import *
>>> dtp = double_tennis_problem()
>>> goal_test(dtp.goals, dtp.initial)
False
>>> dtp.act(expr('Go(A, RightBaseLine, LeftBaseLine)'))
>>> dtp.act(expr('Hit(A, Ball, RightBaseLine)'))
>>> goal_test(dtp.goals, dtp.initial)
False
>>> dtp.act(expr('Go(A, LeftNet, RightBaseLine)'))
>>> goal_test(dtp.goals, dtp.initial)
True
>>>
"""
return PlanningProblem(
initial='At(A, LeftBaseLine) & At(B, RightNet) & Approaching(Ball, RightBaseLine) & Partner(A, B) & Partner(B, A)',
goals='Returned(Ball) & At(a, LeftNet) & At(a, RightNet)',
actions=[Action('Hit(actor, Ball, loc)',
precond='Approaching(Ball, loc) & At(actor, loc)',
effect='Returned(Ball)'),
Action('Go(actor, to, loc)',
precond='At(actor, loc)',
effect='At(actor, to) & ~At(actor, loc)')])
class ForwardPlan(search.Problem):
"""
Forward state-space search [Section 10.2.1]
"""
def __init__(self, planning_problem):
super().__init__(associate('&', planning_problem.initial), associate('&', planning_problem.goals))
self.planning_problem = planning_problem
self.expanded_actions = self.planning_problem.expand_actions()
def actions(self, state):
return [action for action in self.expanded_actions if all(pre in conjuncts(state) for pre in action.precond)]
def result(self, state, action):
return associate('&', action(conjuncts(state), action.args).clauses)
def goal_test(self, state):
return all(goal in conjuncts(state) for goal in self.planning_problem.goals)
def h(self, state):
"""
Computes ignore delete lists heuristic by creating a relaxed version of the original problem (we can do that
by removing the delete lists from all actions, ie. removing all negative literals from effects) that will be
easier to solve through GraphPlan and where the length of the solution will serve as a good heuristic.
"""
relaxed_planning_problem = PlanningProblem(initial=state.state,
goals=self.goal,
actions=[action.relaxed() for action in
self.planning_problem.actions])
try:
return len(linearize(GraphPlan(relaxed_planning_problem).execute()))
except:
return float('inf')
class BackwardPlan(search.Problem):
"""
Backward relevant-states search [Section 10.2.2]
"""
def __init__(self, planning_problem):
super().__init__(associate('&', planning_problem.goals), associate('&', planning_problem.initial))
self.planning_problem = planning_problem
self.expanded_actions = self.planning_problem.expand_actions()
def actions(self, subgoal):
"""
Returns True if the action is relevant to the subgoal, ie.:
- the action achieves an element of the effects
- the action doesn't delete something that needs to be achieved
- the preconditions are consistent with other subgoals that need to be achieved
"""
def negate_clause(clause):
return Expr(clause.op.replace('Not', ''), *clause.args) if clause.op[:3] == 'Not' else Expr(
'Not' + clause.op, *clause.args)
subgoal = conjuncts(subgoal)
return [action for action in self.expanded_actions if
(any(prop in action.effect for prop in subgoal) and
not any(negate_clause(prop) in subgoal for prop in action.effect) and
not any(negate_clause(prop) in subgoal and negate_clause(prop) not in action.effect
for prop in action.precond))]
def result(self, subgoal, action):
# g' = (g - effects(a)) + preconds(a)
return associate('&', set(set(conjuncts(subgoal)).difference(action.effect)).union(action.precond))
def goal_test(self, subgoal):
return all(goal in conjuncts(self.goal) for goal in conjuncts(subgoal))
def h(self, subgoal):
"""
Computes ignore delete lists heuristic by creating a relaxed version of the original problem (we can do that
by removing the delete lists from all actions, ie. removing all negative literals from effects) that will be
easier to solve through GraphPlan and where the length of the solution will serve as a good heuristic.
"""
relaxed_planning_problem = PlanningProblem(initial=self.goal,
goals=subgoal.state,
actions=[action.relaxed() for action in
self.planning_problem.actions])
try:
return len(linearize(GraphPlan(relaxed_planning_problem).execute()))
except:
return float('inf')
def CSPlan(planning_problem, solution_length, CSP_solver=ac_search_solver, arc_heuristic=sat_up):
"""
Planning as Constraint Satisfaction Problem [Section 10.4.3]
"""
def st(var, stage):
"""Returns a string for the var-stage pair that can be used as a variable"""
return str(var) + "_" + str(stage)
def if_(v1, v2):
"""If the second argument is v2, the first argument must be v1"""
def if_fun(x1, x2):
return x1 == v1 if x2 == v2 else True
if_fun.__name__ = "if the second argument is " + str(v2) + " then the first argument is " + str(v1) + " "
return if_fun
def eq_if_not_in_(actset):
"""First and third arguments are equal if action is not in actset"""
def eq_if_not_in(x1, a, x2):
return x1 == x2 if a not in actset else True
eq_if_not_in.__name__ = "first and third arguments are equal if action is not in " + str(actset) + " "
return eq_if_not_in
expanded_actions = planning_problem.expand_actions()
fluent_values = planning_problem.expand_fluents()
for horizon in range(solution_length):
act_vars = [st('action', stage) for stage in range(horizon + 1)]
domains = {av: list(map(lambda action: expr(str(action)), expanded_actions)) for av in act_vars}
domains.update({st(var, stage): {True, False} for var in fluent_values for stage in range(horizon + 2)})
# initial state constraints
constraints = [Constraint((st(var, 0),), is_(val))
for (var, val) in {expr(str(fluent).replace('Not', '')):
True if fluent.op[:3] != 'Not' else False
for fluent in planning_problem.initial}.items()]
constraints += [Constraint((st(var, 0),), is_(False))
for var in {expr(str(fluent).replace('Not', ''))
for fluent in fluent_values if fluent not in planning_problem.initial}]
# goal state constraints
constraints += [Constraint((st(var, horizon + 1),), is_(val))
for (var, val) in {expr(str(fluent).replace('Not', '')):
True if fluent.op[:3] != 'Not' else False
for fluent in planning_problem.goals}.items()]
# precondition constraints
constraints += [Constraint((st(var, stage), st('action', stage)), if_(val, act))
# st(var, stage) == val if st('action', stage) == act
for act, strps in {expr(str(action)): action for action in expanded_actions}.items()
for var, val in {expr(str(fluent).replace('Not', '')):
True if fluent.op[:3] != 'Not' else False
for fluent in strps.precond}.items()
for stage in range(horizon + 1)]
# effect constraints
constraints += [Constraint((st(var, stage + 1), st('action', stage)), if_(val, act))
# st(var, stage + 1) == val if st('action', stage) == act
for act, strps in {expr(str(action)): action for action in expanded_actions}.items()
for var, val in {expr(str(fluent).replace('Not', '')): True if fluent.op[:3] != 'Not' else False
for fluent in strps.effect}.items()
for stage in range(horizon + 1)]
# frame constraints
constraints += [Constraint((st(var, stage), st('action', stage), st(var, stage + 1)),
eq_if_not_in_(set(map(lambda action: expr(str(action)),
{act for act in expanded_actions if var in act.effect
or Expr('Not' + var.op, *var.args) in act.effect}))))
for var in fluent_values for stage in range(horizon + 1)]
csp = NaryCSP(domains, constraints)
sol = CSP_solver(csp, arc_heuristic=arc_heuristic)
if sol:
return [sol[a] for a in act_vars]
def SATPlan(planning_problem, solution_length, SAT_solver=cdcl_satisfiable):
"""
Planning as Boolean satisfiability [Section 10.4.1]
"""
def expand_transitions(state, actions):
state = sorted(conjuncts(state))
for action in filter(lambda act: act.check_precond(state, act.args), actions):
transition[associate('&', state)].update(
{Expr(action.name, *action.args):
associate('&', sorted(set(filter(lambda clause: clause.op[:3] != 'Not',
action(state, action.args).clauses))))
if planning_problem.is_strips()
else associate('&', sorted(set(action(state, action.args).clauses)))})
for state in transition[associate('&', state)].values():
if state not in transition:
expand_transitions(expr(state), actions)
transition = defaultdict(dict)
expand_transitions(associate('&', planning_problem.initial), planning_problem.expand_actions())
return SAT_plan(associate('&', sorted(planning_problem.initial)), transition,
associate('&', sorted(planning_problem.goals)), solution_length, SAT_solver=SAT_solver)
class Level:
"""
Contains the state of the planning problem
and exhaustive list of actions which use the
states as pre-condition.
"""
def __init__(self, kb):
"""Initializes variables to hold state and action details of a level"""
self.kb = kb
# current state
self.current_state = kb.clauses
# current action to state link
self.current_action_links = {}
# current state to action link
self.current_state_links = {}
# current action to next state link
self.next_action_links = {}
# next state to current action link
self.next_state_links = {}
# mutually exclusive actions
self.mutex = []
def __call__(self, actions, objects):
self.build(actions, objects)
self.find_mutex()
def separate(self, e):
"""Separates an iterable of elements into positive and negative parts"""
positive = []
negative = []
for clause in e:
if clause.op[:3] == 'Not':
negative.append(clause)
else:
positive.append(clause)
return positive, negative
def find_mutex(self):
"""Finds mutually exclusive actions"""
# Inconsistent effects
pos_nsl, neg_nsl = self.separate(self.next_state_links)
for negeff in neg_nsl:
new_negeff = Expr(negeff.op[3:], *negeff.args)
for poseff in pos_nsl:
if new_negeff == poseff:
for a in self.next_state_links[poseff]:
for b in self.next_state_links[negeff]:
if {a, b} not in self.mutex:
self.mutex.append({a, b})
# Interference will be calculated with the last step
pos_csl, neg_csl = self.separate(self.current_state_links)
# Competing needs
for pos_precond in pos_csl:
for neg_precond in neg_csl:
new_neg_precond = Expr(neg_precond.op[3:], *neg_precond.args)
if new_neg_precond == pos_precond:
for a in self.current_state_links[pos_precond]:
for b in self.current_state_links[neg_precond]:
if {a, b} not in self.mutex:
self.mutex.append({a, b})
# Inconsistent support
state_mutex = []
for pair in self.mutex:
next_state_0 = self.next_action_links[list(pair)[0]]
if len(pair) == 2:
next_state_1 = self.next_action_links[list(pair)[1]]
else:
next_state_1 = self.next_action_links[list(pair)[0]]
if (len(next_state_0) == 1) and (len(next_state_1) == 1):
state_mutex.append({next_state_0[0], next_state_1[0]})
self.mutex = self.mutex + state_mutex
def build(self, actions, objects):
"""Populates the lists and dictionaries containing the state action dependencies"""
for clause in self.current_state:
p_expr = Expr('P' + clause.op, *clause.args)
self.current_action_links[p_expr] = [clause]
self.next_action_links[p_expr] = [clause]
self.current_state_links[clause] = [p_expr]
self.next_state_links[clause] = [p_expr]
for a in actions:
num_args = len(a.args)
possible_args = tuple(itertools.permutations(objects, num_args))
for arg in possible_args:
if a.check_precond(self.kb, arg):
for num, symbol in enumerate(a.args):
if not symbol.op.islower():
arg = list(arg)
arg[num] = symbol
arg = tuple(arg)
new_action = a.substitute(Expr(a.name, *a.args), arg)
self.current_action_links[new_action] = []
for clause in a.precond:
new_clause = a.substitute(clause, arg)
self.current_action_links[new_action].append(new_clause)
if new_clause in self.current_state_links:
self.current_state_links[new_clause].append(new_action)
else:
self.current_state_links[new_clause] = [new_action]
self.next_action_links[new_action] = []
for clause in a.effect:
new_clause = a.substitute(clause, arg)
self.next_action_links[new_action].append(new_clause)
if new_clause in self.next_state_links:
self.next_state_links[new_clause].append(new_action)
else:
self.next_state_links[new_clause] = [new_action]
def perform_actions(self):
"""Performs the necessary actions and returns a new Level"""
new_kb = FolKB(list(set(self.next_state_links.keys())))
return Level(new_kb)
class Graph:
"""
Contains levels of state and actions
Used in graph planning algorithm to extract a solution
"""
def __init__(self, planning_problem):
self.planning_problem = planning_problem
self.kb = FolKB(planning_problem.initial)
self.levels = [Level(self.kb)]
self.objects = set(arg for clause in self.kb.clauses for arg in clause.args)
def __call__(self):
self.expand_graph()
def expand_graph(self):
"""Expands the graph by a level"""
last_level = self.levels[-1]
last_level(self.planning_problem.actions, self.objects)
self.levels.append(last_level.perform_actions())
def non_mutex_goals(self, goals, index):
"""Checks whether the goals are mutually exclusive"""
goal_perm = itertools.combinations(goals, 2)
for g in goal_perm:
if set(g) in self.levels[index].mutex:
return False
return True
class GraphPlan:
"""
Class for formulation GraphPlan algorithm
Constructs a graph of state and action space
Returns solution for the planning problem
"""
def __init__(self, planning_problem):
self.graph = Graph(planning_problem)
self.no_goods = []
self.solution = []
def check_leveloff(self):
"""Checks if the graph has levelled off"""
check = (set(self.graph.levels[-1].current_state) == set(self.graph.levels[-2].current_state))
if check:
return True
def extract_solution(self, goals, index):
"""Extracts the solution"""
level = self.graph.levels[index]
if not self.graph.non_mutex_goals(goals, index):
self.no_goods.append((level, goals))
return
level = self.graph.levels[index - 1]
# Create all combinations of actions that satisfy the goal
actions = []
for goal in goals:
actions.append(level.next_state_links[goal])
all_actions = list(itertools.product(*actions))
# Filter out non-mutex actions
non_mutex_actions = []
for action_tuple in all_actions:
action_pairs = itertools.combinations(list(set(action_tuple)), 2)
non_mutex_actions.append(list(set(action_tuple)))
for pair in action_pairs:
if set(pair) in level.mutex:
non_mutex_actions.pop(-1)
break
# Recursion
for action_list in non_mutex_actions:
if [action_list, index] not in self.solution:
self.solution.append([action_list, index])
new_goals = []
for act in set(action_list):
if act in level.current_action_links:
new_goals = new_goals + level.current_action_links[act]
if abs(index) + 1 == len(self.graph.levels):
return
elif (level, new_goals) in self.no_goods:
return
else:
self.extract_solution(new_goals, index - 1)
# Level-Order multiple solutions
solution = []
for item in self.solution:
if item[1] == -1:
solution.append([])
solution[-1].append(item[0])
else:
solution[-1].append(item[0])
for num, item in enumerate(solution):
item.reverse()
solution[num] = item
return solution
def goal_test(self, kb):
return all(kb.ask(q) is not False for q in self.graph.planning_problem.goals)
def execute(self):
"""Executes the GraphPlan algorithm for the given problem"""
while True:
self.graph.expand_graph()
if (self.goal_test(self.graph.levels[-1].kb) and self.graph.non_mutex_goals(
self.graph.planning_problem.goals, -1)):
solution = self.extract_solution(self.graph.planning_problem.goals, -1)
if solution:
return solution