environment.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. from random import shuffle
  3. from copy import deepcopy
  4. # Example of environment, for the game of fifteen.
  5. # Maximum rows and columns size
  6. MAX_SIZE = 4
  7. class State:
  8. """Represents a state of the environment"""
  9. def __init__(self):
  10. elements = [x for x in range(MAX_SIZE**2)]
  11. shuffle(elements)
  12. self.matrix = [[elements[x+(y*MAX_SIZE)] for x in range(MAX_SIZE)] for y in range(MAX_SIZE)]
  13. def printState(self):
  14. for i in range(MAX_SIZE):
  15. print(self.matrix[i])
  16. print("\n")
  17. def up(state):
  18. (x, y) = findZero(state)
  19. (state.matrix[y][x], state.matrix[y-1][x]) = (state.matrix[y-1][x], state.matrix[y][x])
  20. def down(state):
  21. (x, y) = findZero(state)
  22. (state.matrix[y][x], state.matrix[y+1][x]) = (state.matrix[y+1][x], state.matrix[y][x])
  23. def left(state):
  24. (x, y) = findZero(state)
  25. (state.matrix[y][x], state.matrix[y][x-1]) = (state.matrix[y][x-1], state.matrix[y][x])
  26. def right(state):
  27. (x, y) = findZero(state)
  28. (state.matrix[y][x], state.matrix[y][x+1]) = (state.matrix[y][x+1], state.matrix[y][x])
  29. def findZero(state):
  30. for row in state.matrix:
  31. try:
  32. x = row.index(0)
  33. y = state.matrix.index(row)
  34. break
  35. except ValueError:
  36. pass
  37. return (x, y)
  38. def actions(state):
  39. (x, y) = findZero(state)
  40. actions = []
  41. if x > 0:
  42. actions.append(left)
  43. if x < MAX_SIZE-1:
  44. actions.append(right)
  45. if y > 0:
  46. actions.append(up)
  47. if y < MAX_SIZE-1:
  48. actions.append(down)
  49. return actions
  50. def result(state, action):
  51. newState = deepcopy(state)
  52. action(newState)
  53. return newState
  54. def goalTest(state):
  55. el = []
  56. for row in state.matrix:
  57. el.extend(row)
  58. return all(el[i] < el[i+1] for i in range(len(el)-1))
  59. if __name__ == "__main__":
  60. s0 = State()
  61. for action in actions(s0):
  62. result(s0, action).printState()