agent.py 931 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. from environment import State, goalTest, actions, result, equals
  3. from collections import deque
  4. class ClosedList:
  5. """List with deep membership check"""
  6. def __init__(self):
  7. self.list = []
  8. def append(self, state):
  9. self.list.append(state)
  10. def contains(self, state):
  11. for elem in self.list:
  12. if equals(elem, state):
  13. return True
  14. return False
  15. # Example of goal-oriented agent.
  16. frontier = deque() # Initialize the frontier as a queue
  17. closedList = ClosedList() # Initialize the closed list as a list
  18. node = State() # Generate the initial state
  19. while not goalTest(node): # Apply the goal test
  20. for action in actions(node): # Expand the node
  21. frontier.append(result(node, action))
  22. node = frontier.popleft()
  23. while closedList.contains(node):
  24. node = frontier.popleft()
  25. closedList.append(node)
  26. state.printState()