@@ -43,9 +43,12 @@ def result(self, state, action):
43
43
44
44
def goal_test (self , state ):
45
45
"""Return True if the state is a goal. The default method compares the
46
- state to self.goal, as specified in the constructor. Override this
46
+ state to self.goal or checks for state in self.goal if it is a list , as specified in the constructor. Override this
47
47
method if checking against a single self.goal is not enough."""
48
- return state == self .goal
48
+ if isinstance (self .goal , list ):
49
+ return is_in (state , self .goal )
50
+ else :
51
+ return state == self .goal
49
52
50
53
def path_cost (self , c , state1 , action , state2 ):
51
54
"""Return the cost of a solution path that arrives at state2 from
@@ -382,10 +385,10 @@ def and_or_graph_search(problem):
382
385
# functions used by and_or_search
383
386
def or_search (state , problem , path ):
384
387
if problem .goal_test (state ):
385
- return {}
388
+ return []
386
389
if state in path :
387
390
return None
388
- for action in problem .action (state ):
391
+ for action in problem .actions (state ):
389
392
plan = and_search (problem .result (state , action ),
390
393
problem , path + [state , ])
391
394
if plan is not None :
@@ -616,6 +619,29 @@ def distance_to_node(n):
616
619
Sibiu = (207 , 457 ), Timisoara = (94 , 410 ), Urziceni = (456 , 350 ),
617
620
Vaslui = (509 , 444 ), Zerind = (108 , 531 ))
618
621
622
+ """
623
+ Eight possible states of the vacumm world
624
+ Each state is represented as "State if the left room" "State of the right room" "Room in which the agent is present"
625
+ 1 Dirty Dirty Left - DDL
626
+ 2 Dirty Dirty Right - DDR
627
+ 3 Dirty Clean Left - DCL
628
+ 4 Dirty Clean Right - DCR
629
+ 5 Clean Dirty Left - CDL
630
+ 6 Clean Dirty Right - CDR
631
+ 7 Clean Clean Left - CCL
632
+ 8 Clean Clean Right - CCR
633
+ """
634
+ Fig [4 , 9 ] = Graph (dict (
635
+ State_1 = dict (Suck = ['State_7' , 'State_5' ], Right = ['State_2' ]),
636
+ State_2 = dict (Suck = ['State_8' , 'State_4' ], Left = ['State_2' ]),
637
+ State_3 = dict (Suck = ['State_7' ], Right = ['State_4' ]),
638
+ State_4 = dict (Suck = ['State_4' , 'State_2' ], Left = ['State_3' ]),
639
+ State_5 = dict (Suck = ['State_5' , 'State_1' ], Right = ['State_6' ]),
640
+ State_6 = dict (Suck = ['State_8' ], Left = ['State_5' ]),
641
+ State_7 = dict (Suck = ['State_7' , 'State_3' ], Right = ['State_8' ]),
642
+ State_8 = dict (Suck = ['State_8' , 'State_6' ], Left = ['State_7' ])
643
+ ))
644
+
619
645
# Principal states and territories of Australia
620
646
Fig [6 , 1 ] = UndirectedGraph (dict (
621
647
T = dict (),
@@ -655,6 +681,20 @@ def h(self, node):
655
681
else :
656
682
return infinity
657
683
684
+ class GraphProblemStochastic (GraphProblem ):
685
+ """
686
+ A version of Graph Problem where an action can lead to undeterministic output i.e. multiple possible states
687
+ Define the graph as dict(A = dict(Action = [[<Result 1>, <Result 2>, ...],<cost>], ...), ...)
688
+ A the dictionary format is different, make sure the graph is created as a directed graph
689
+ """
690
+
691
+ def result (self , state , action ):
692
+ return self .graph .get (state , action )
693
+
694
+ def path_cost ():
695
+ raise NotImplementedError
696
+
697
+
658
698
# ______________________________________________________________________________
659
699
660
700
0 commit comments