You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

  1. import gym
  2. from PIL import Image
  3. from copy import deepcopy
  4. import numpy as np
  5. from matplotlib import pyplot as plt
  6. import readchar
  7. import queue
  8. ski_position_queue = queue.Queue()
  9. env = gym.make("ALE/Skiing-v5", render_mode="human")
  10. observation, info = env.reset()
  11. y = 40
  12. standstillcounter = 0
  13. def update_y(y, ski_position):
  14. global standstillcounter
  15. if ski_position in [6,7, 8,9]:
  16. standstillcounter = 0
  17. y_update = 16
  18. elif ski_position in [4,5, 10,11]:
  19. standstillcounter = 0
  20. y_update = 12
  21. elif ski_position in [2,3, 12,13]:
  22. standstillcounter = 0
  23. y_update = 8
  24. elif ski_position in [1, 14] and standstillcounter >= 5:
  25. if standstillcounter >= 8:
  26. print("!!!!!!!!!! no more x updates!!!!!!!!!!!")
  27. y_update = 0
  28. elif ski_position in [1, 14]:
  29. y_update = 4
  30. if ski_position in [1, 14]:
  31. standstillcounter += 1
  32. return y_update
  33. def update_ski_position(ski_position, action):
  34. if action == 0:
  35. return ski_position
  36. elif action == 1:
  37. return min(ski_position+1, 14)
  38. elif action == 2:
  39. return max(ski_position-1, 1)
  40. approx_x_coordinate = 80
  41. ski_position = 8
  42. for _ in range(1000000):
  43. action = env.action_space.sample() # agent policy that uses the observation and info
  44. action = int(repr(readchar.readchar())[1])
  45. ski_position = update_ski_position(ski_position, action)
  46. y_update = update_y(y, ski_position)
  47. y += y_update if y_update else 0
  48. old_x = deepcopy(approx_x_coordinate)
  49. approx_x_coordinate = int(np.mean(np.where(observation[:,:,1] == 92)[1]))
  50. print(f"Action: {action},\tski position: {ski_position},\ty_update: {y_update},\ty: {y},\tx: {approx_x_coordinate},\tx_update:{approx_x_coordinate - old_x}")
  51. observation, reward, terminated, truncated, info = env.step(action)
  52. if terminated or truncated:
  53. observation, info = env.reset()
  54. observation, reward, terminated, truncated, info = env.step(0)
  55. observation, reward, terminated, truncated, info = env.step(0)
  56. observation, reward, terminated, truncated, info = env.step(0)
  57. observation, reward, terminated, truncated, info = env.step(0)
  58. #plt.imshow(observation)
  59. #plt.show()
  60. #im = Image.fromarray(observation)
  61. #im.save("init.png")
  62. env.close()