Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e7479db

Browse files
committed
Resolved conflicts
1 parent fcead67 commit e7479db

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

search.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ def result(self, state, action):
4343

4444
def goal_test(self, state):
4545
"""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
4747
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
4952

5053
def path_cost(self, c, state1, action, state2):
5154
"""Return the cost of a solution path that arrives at state2 from
@@ -382,10 +385,10 @@ def and_or_graph_search(problem):
382385
# functions used by and_or_search
383386
def or_search(state, problem, path):
384387
if problem.goal_test(state):
385-
return {}
388+
return []
386389
if state in path:
387390
return None
388-
for action in problem.action(state):
391+
for action in problem.actions(state):
389392
plan = and_search(problem.result(state, action),
390393
problem, path + [state, ])
391394
if plan is not None:
@@ -616,6 +619,29 @@ def distance_to_node(n):
616619
Sibiu=(207, 457), Timisoara=(94, 410), Urziceni=(456, 350),
617620
Vaslui=(509, 444), Zerind=(108, 531))
618621

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+
619645
# Principal states and territories of Australia
620646
Fig[6, 1] = UndirectedGraph(dict(
621647
T=dict(),
@@ -655,6 +681,20 @@ def h(self, node):
655681
else:
656682
return infinity
657683

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+
658698
# ______________________________________________________________________________
659699

660700

tests/test_search.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import pytest
22
from search import * # noqa
3+
from random import choice #noqa
34

45

56
romania = GraphProblem('Arad', 'Bucharest', Fig[3, 2])
7+
vacumm_world = GraphProblemStochastic('State_1', ['State_7', 'State_8'], Fig[4, 9])
68

79

810
def test_breadth_first_tree_search():
@@ -31,5 +33,17 @@ def test_iterative_deepening_search():
3133
'Fagaras',
3234
'Bucharest']
3335

36+
def test_and_or_graph_search():
37+
def run_plan(state, problem, plan):
38+
if problem.goal_test(state):
39+
return True
40+
if len(plan) is not 2:
41+
return False
42+
next_state = choice(problem.result(state, plan[0]))
43+
return run_plan(next_state, problem, plan[1][next_state])
44+
plan = and_or_graph_search(vacumm_world)
45+
assert run_plan('State_1', vacumm_world, plan)
46+
47+
3448
if __name__ == '__main__':
3549
pytest.main()

0 commit comments

Comments
 (0)