environment.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 __iter__(self):
  14. self.row = 0
  15. self.col = 0
  16. return self
  17. def __next__(self):
  18. if self.row == MAX_SIZE:
  19. self.row = 0
  20. self.col = self.col+1
  21. if self.col == MAX_SIZE:
  22. raise StopIteration
  23. elem = self.matrix[self.col][self.row]
  24. self.row = self.row + 1
  25. return elem
  26. def printState(self):
  27. for i in range(MAX_SIZE):
  28. print(self.matrix[i])
  29. print("\n")
  30. def up(state):
  31. (x, y) = findZero(state)
  32. (state.matrix[y][x], state.matrix[y-1][x]) = (state.matrix[y-1][x], state.matrix[y][x])
  33. def down(state):
  34. (x, y) = findZero(state)
  35. (state.matrix[y][x], state.matrix[y+1][x]) = (state.matrix[y+1][x], state.matrix[y][x])
  36. def left(state):
  37. (x, y) = findZero(state)
  38. (state.matrix[y][x], state.matrix[y][x-1]) = (state.matrix[y][x-1], state.matrix[y][x])
  39. def right(state):
  40. (x, y) = findZero(state)
  41. (state.matrix[y][x], state.matrix[y][x+1]) = (state.matrix[y][x+1], state.matrix[y][x])
  42. def findZero(state):
  43. for row in state.matrix:
  44. try:
  45. x = row.index(0)
  46. y = state.matrix.index(row)
  47. break
  48. except ValueError:
  49. pass
  50. return (x, y)
  51. def actions(state):
  52. (x, y) = findZero(state)
  53. actions = []
  54. if x > 0:
  55. actions.append(left)
  56. if x < MAX_SIZE-1:
  57. actions.append(right)
  58. if y > 0:
  59. actions.append(up)
  60. if y < MAX_SIZE-1:
  61. actions.append(down)
  62. return actions
  63. def result(state, action):
  64. newState = deepcopy(state)
  65. action(newState)
  66. return newState
  67. def goalTest(state):
  68. el = []
  69. for cell in state:
  70. el.append(cell)
  71. return all(el[i] < el[i+1] for i in range(len(el)-1))
  72. def equals(firstState, secondState):
  73. if firstState == secondState:
  74. return True
  75. return all(x[0]==x[1] for x in zip(firstState, secondState))
  76. if __name__ == "__main__":
  77. pass