Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1.  
  2. import gym
  3. import os
  4. import json
  5. import gym_checkers
  6.  
  7. state_array = []
  8. action_array = []
  9.  
  10.  
  11. f = open('data.txt')
  12.  
  13. line = f.readline()
  14.  
  15. #define a conversion array used for the conversion from Directions to moves
  16. DIRECTION_MAP = [[-1,-1], [1,-1], [-1,1], [1,1]]
  17.  
  18. # Load the reward config
  19. with open('config/rewards.json') as fp:
  20. reward_spec = json.load(fp=fp)
  21.  
  22. # Create an environment to train on
  23. env = gym.make("Checkers-v0")
  24. env.set_reward_spec(reward_spec)
  25.  
  26.  
  27. counter = 0
  28.  
  29. while line and counter < 16:
  30. # in python 2+
  31. # print line
  32. # in python 3 print is a builtin function, so
  33. print(line)
  34.  
  35. buffer = ""
  36. select_action_buffer = None
  37. move_action_buffer = []
  38.  
  39. while (line[0] == "[" or line[0] == ' '):
  40. line = f.readline()
  41.  
  42.  
  43. for index, char in enumerate(line):
  44.  
  45. if index == 0 and (char == "[" or char == ' '):
  46. env.reset()
  47.  
  48. if str.isdigit(char) and char != "-":
  49. buffer += char
  50.  
  51. # New Move
  52. if char == ' ' and select_action_buffer != None:
  53.  
  54. # Buffer verarbeiten
  55. env.step_select(select_action_buffer)
  56.  
  57. print("Lenlen",len(move_action_buffer))
  58.  
  59. for index_a, move_action in enumerate(move_action_buffer):
  60.  
  61. if index_a == 0:
  62. sel_x, sel_y = select_action_to_coords(select_action_buffer)
  63. mov_x, mov_y = select_action_to_coords(move_action)
  64. action = move_to_direction(sel_x, sel_y, mov_x, mov_y)
  65. else:
  66. sel_x, sel_y = select_action_to_coords(move_action_buffer[index_a-1])
  67. mov_x, mov_y = select_action_to_coords(move_action)
  68. action = move_to_direction(sel_x, sel_y, mov_x, mov_y)
  69.  
  70. print("Move action", action)
  71. env.step_move(action)
  72.  
  73. env.render()
  74.  
  75. # Empty the buffer
  76. buffer = ""
  77. select_action_buffer = None
  78. move_action_buffer = []
  79.  
  80. elif char == '.':
  81. buffer = ""
  82.  
  83. elif char == 'x' or char == "-":
  84. if select_action_buffer == None:
  85. select_action_buffer = 32 - int(buffer)
  86. buffer = ""
  87. else:
  88. move_action_buffer.append(32 - int(buffer))
  89. buffer = ""
  90.  
  91.  
  92. # use realine() to read next line
  93. line = f.readline()
  94. counter += 1
  95. f.close()
  96.  
  97. def move_to_direction(selected_x,selected_y,move_x,move_y):
  98. """
  99. convert a Move to a field to a direction relationally to the piece
  100. Args:
  101. selected_x(int): x position of the selected field
  102. selected_y(int): y position of the selected field
  103. move_x(int): x position of the desired field
  104. move_y(int): y position of the desired field
  105. Return:
  106. direction(int): used in the board class
  107. """
  108. x = 0
  109. y = 0
  110. if (move_x-selected_x)>0:
  111. x=1
  112. elif (move_x-selected_x)<0:
  113. x=-1
  114. if (move_y-selected_y)>0 :
  115. y=1
  116. elif (move_y-selected_y)<0:
  117. y=-1
  118. dir= [x,y]
  119. for index, direction in enumerate(DIRECTION_MAP):
  120. if dir == direction:
  121. return index
  122.  
  123. return -1
  124.  
  125. def select_action_to_coords(action : int):
  126.  
  127. """
  128. Converts a select action to coordinates.
  129. Args:
  130. action (int) : Select action
  131. Returns:
  132. (int) (int) : corresponding coordinates within the board
  133. """
  134.  
  135. # Convert number to coordinates
  136. pos_x = action % 4
  137. pos_y = int((action-pos_x)/4)
  138.  
  139. # Return the coordinates to the full field
  140. if pos_y % 2 == 1:
  141. pos_x *= 2
  142. else:
  143. pos_x *= 2
  144. pos_x += 1
  145.  
  146. return pos_x, pos_y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement