|
@@ -1,14 +1,32 @@
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
-from environment import State, goalTest, actions, result
|
|
|
+from environment import State, goalTest, actions, result, equals
|
|
|
from collections import deque
|
|
|
|
|
|
-# Example of goal-oriented agent.
|
|
|
+class ClosedList:
|
|
|
+ """List with deep membership check"""
|
|
|
+
|
|
|
+ def __init__(self):
|
|
|
+ self.list = []
|
|
|
+
|
|
|
+ def append(self, state):
|
|
|
+ self.list.append(state)
|
|
|
|
|
|
+ def contains(self, state):
|
|
|
+ for elem in self.list:
|
|
|
+ if equals(elem, state):
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+# Example of goal-oriented agent.
|
|
|
frontier = deque() # Initialize the frontier as a queue
|
|
|
-state = State() # Generate the initial state
|
|
|
-while not goalTest(state): # Apply the global test
|
|
|
- for action in actions(state): # Expand the node
|
|
|
- frontier.append(result(state, action))
|
|
|
- state = frontier.popleft()
|
|
|
+closedList = ClosedList() # Initialize the closed list as a list
|
|
|
+node = State() # Generate the initial state
|
|
|
+while not goalTest(node): # Apply the goal test
|
|
|
+ for action in actions(node): # Expand the node
|
|
|
+ frontier.append(result(node, action))
|
|
|
+ node = frontier.popleft()
|
|
|
+ while closedList.contains(node):
|
|
|
+ node = frontier.popleft()
|
|
|
+ closedList.append(node)
|
|
|
state.printState()
|