Guest User

AI thingies

a guest
Jan 1st, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. Copy and paste code:
  2. 1: Imports:
  3. import win32api
  4. import win32con
  5. import time
  6. import pymindcore as pm
  7. import numpy as np
  8. import cv2
  9. from mss import mss
  10.  
  11.  
  12. 2: Capture and resize:
  13. def capture_and_resize(returnBoth=False):
  14. with mss() as sct:
  15. monitor = sct.monitors[1]
  16. sct_img = sct.grab(monitor)
  17.  
  18. # Convert to NumPy array
  19. img = np.array(sct_img)
  20.  
  21. # Resize image to 40x40 pixels
  22. img_resized = cv2.resize(img, (40, 40), interpolation=cv2.INTER_AREA)
  23.  
  24. img_resized_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
  25. img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  26. if returnBoth:
  27. return img_resized_rgb,img_rgb
  28. return img_resized_rgb
  29.  
  30.  
  31. 3: Output keys:
  32. outputkeys = [('hold','w'), ('hold','a'), ('hold','s'), ('hold','d'), ('release', 'w'), ('release', 'a'), ('release', 's'), ('release', 'd'),
  33. ('press', 'e'), ('hold', 'shift'), ('press', '1'), ('press', '2'), ('press', '3'), ('press', '4'), ('press', '5'), ('press', '6'),
  34. ('press', '7'), ('press', '8'), ('press', '9'), ('release', 'shift'),
  35. ('press', 'ctrl'), ('press', 'q'), ('press', 'f5'), ('press', ' '), ('hold', ' '), ('release', ' '), ('mouse_hold_1', 'left'),
  36. ('mouse_hold_1', 'right'), ('mouse_hold_1', 'up'), ('mouse_hold_1', 'down'), ('mouse_release', 'left'), ('mouse_release', 'right'),
  37. ('mouse_hold_2', 'left'), ('mouse_hold_2', 'right'), ('mouse_hold_2', 'up'), ('mouse_hold_2', 'down'), ('mouse_click', 'left'),
  38. ('mouse_hold_3', 'left'), ('mouse_hold_3', 'right'), ('mouse_hold_3', 'up'), ('mouse_hold_3', 'down'), ('mouse_click', 'right'),
  39. ('mouse_hold_4', 'left'), ('mouse_hold_4', 'right'), ('mouse_hold_4', 'up'), ('mouse_hold_4', 'down'), ('mouse_release', 'up'), ('mouse_release', 'down'),
  40. ('mouse_click_hold', 'left'), ('mouse_click_hold', 'right'), ('mouse_click_release', 'left'), ('mouse_click_release', 'right'), ('mouse_click', 'left'), ('mouse_click', 'right'),
  41. ]
  42.  
  43. outputs=len(outputkeys)
  44. nn = pm.Optomised_neural_network(5000,[1600,800,400],outputs,"sig")
  45.  
  46.  
  47. 4: Keycodes and Keyboard action:
  48. KEY_CODES = {
  49. 'a': 0x41, 'b': 0x42, 'c': 0x43, 'd': 0x44,
  50. 'e': 0x45, 'f': 0x46, 'g': 0x47, 'h': 0x48,
  51. 'i': 0x49, 'j': 0x4A, 'k': 0x4B, 'l': 0x4C,
  52. 'm': 0x4D, 'n': 0x4E, 'o': 0x4F, 'p': 0x50,
  53. 'q': 0x51, 'r': 0x52, 's': 0x53, 't': 0x54,
  54. 'u': 0x55, 'v': 0x56, 'w': 0x57, 'x': 0x58,
  55. 'y': 0x59, 'z': 0x5A, ' ': 0x20,
  56. '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34,
  57. '5': 0x35, '6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39,
  58. 'ctrl': 0x11, 'f5': 0x74, 'shift': 0x10
  59. }
  60.  
  61.  
  62. def keyboard_action(character, action):
  63. vk_code = KEY_CODES.get(character.lower())
  64. if vk_code is None:
  65. return
  66.  
  67. if action == "hold" or action == "press":
  68. win32api.keybd_event(vk_code, 0, 0, 0)
  69. time.sleep(0.01)
  70. if action == "release" or action == "press":
  71. win32api.keybd_event(vk_code, 0, win32con.KEYEVENTF_KEYUP, 0)
  72.  
  73.  
  74.  
  75. 5: Handle Mouse:
  76. mouse_hold = False
  77. click_hold = False
  78. mouse_hold_direction = None
  79.  
  80. def mouse_action(direction, speed, action):
  81. global mouse_hold
  82. global click_hold
  83. global mouse_hold_direction
  84.  
  85. if action == "hold":
  86. mouse_hold = True
  87. mouse_hold_direction = direction
  88. elif action == "press":
  89. mouse_hold = False
  90. if direction == 'left':
  91. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
  92. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
  93. elif direction == 'right':
  94. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
  95. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
  96. elif action == "release":
  97. mouse_hold = False
  98. elif action == "mouse_click_hold":
  99. click_hold = False
  100. if direction == 'left':
  101. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
  102. elif direction == 'right':
  103. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
  104. elif action == "mouse_click_release":
  105. click_hold = False
  106. if direction == 'left':
  107. win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
  108. elif direction == 'right':
  109. win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
  110.  
  111. if mouse_hold or action == "press":
  112. x, y = win32api.GetCursorPos()
  113. if mouse_hold_direction == "left":
  114. x -= speed
  115. elif mouse_hold_direction == "right":
  116. x += speed
  117. elif mouse_hold_direction == "up":
  118. y -= speed
  119. elif mouse_hold_direction == "down":
  120. y += speed
  121. win32api.SetCursorPos((x, y))
  122.  
  123. def handle_mouse_input(input_tuple):
  124. action_map = {
  125. 'mouse_hold_1': ('hold', 4),
  126. 'mouse_hold_2': ('hold', 8),
  127. 'mouse_hold_3': ('hold', 12),
  128. 'mouse_hold_4': ('hold', 16),
  129. 'mouse_click': ('press', 1)
  130. }
  131.  
  132. action, direction = input_tuple
  133. if action.startswith('mouse_hold'):
  134. mouse_action_cmd = action_map.get(action)[0]
  135. speed = action_map.get(action)[1]
  136. mouse_action(direction, speed, mouse_action_cmd)
  137. elif action.startswith('mouse_click'):
  138. mouse_action(input_tuple[1], 1, input_tuple[0])
  139. elif action == 'mouse_release':
  140. mouse_action(direction, 1, 'release')
  141.  
  142.  
  143. 6: Handle AI output:
  144. def handle_AI_output(output):
  145. global outputkeys
  146. if output>25:
  147. handle_mouse_input(outputkeys[output])
  148. else:
  149. keyboard_action(outputkeys[output][1],outputkeys[output][0])
  150.  
  151.  
  152. 7: Replay Buffer:
  153. def configure_replay_buffer(replay_buffer,reward_decay=0.99,boosting=1.2):
  154. x=0
  155. all_inputs=[]
  156. ideal_outputs=[]
  157. terminals=[]
  158. reverse_buffer=replay_buffer.copy()
  159. reverse_buffer.reverse()
  160. for u in replay_buffer:
  161. inputs, output, outputs, reward, terminal=u
  162. all_inputs.append(inputs)
  163. ideal_outputs.append(outputs)
  164. terminals.append(terminal)
  165.  
  166. all_inputs.reverse()
  167. ideal_outputs.reverse()
  168. terminals.reverse()
  169.  
  170. old_reward=0
  171. for x in range(len(ideal_outputs)):
  172. chosen_output=reverse_buffer[x][1]
  173. if reverse_buffer[x][4]:
  174. old_reward=0
  175. for y in range(len(ideal_outputs[x])):
  176. ideal_outputs[x][y]=max(min(ideal_outputs[x][y]*boosting,1),0)
  177.  
  178. old_reward*=reward_decay
  179. old_reward=max(min(old_reward+reverse_buffer[x][3],abs(reverse_buffer[x][3])),-abs(reverse_buffer[x][3]))
  180. ideal_outputs[x][chosen_output]=old_reward
  181. return np.array(all_inputs), np.array(ideal_outputs)
  182.  
  183.  
  184.  
  185. 8: All together!
  186. print("In 5 seconds, it will start")
  187.  
  188. dyingReward=-1
  189. LosingBedReward=-7.5
  190. LosingGameReward=-10
  191.  
  192. DyingColourAndPos = [(), ()]
  193. LosingBedColourAndPos = [(), ()]
  194. LosingGameColourAndPos = [(), ()]
  195.  
  196. replay_buffer=[]
  197.  
  198.  
  199. time.sleep(5)
  200. for x in range(100):
  201. terminal=False
  202. reward=0
  203.  
  204. this_replay=[]
  205. start_time = time.time()
  206.  
  207. extra_inputs = np.zeros(200)
  208. img_array,img_array_2 = capture_and_resize(True)
  209. img_array=img_array.flatten()
  210.  
  211.  
  212. end_time = time.time()
  213. print(f"Time taken: {end_time-start_time:.4f} seconds. ")
  214. inputs = np.concatenate([np.array(img_array),extra_inputs])
  215. outputs = nn.forward(inputs)
  216. maximum_output=outputs.max()
  217. outputKey=int(np.where(outputs==maximum_output)[0][0])
  218. this_replay.append(inputs)
  219. this_replay.append(outputKey)
  220. this_replay.append(outputs)
  221.  
  222. print(f"output: {outputkeys[outputKey]}")
  223.  
  224. if img_array_2[DyingColourAndPos[1][0]][DyingColourAndPos[1][1]][0]==DyingColourAndPos[0][0] and img_array_2[DyingColourAndPos[1][0]][DyingColourAndPos[1][1]][1]==DyingColourAndPos[0][1] and img_array_2[DyingColourAndPos[1][0]][DyingColourAndPos[1][1]][2]==DyingColourAndPos[0][2]:
  225. print("The AI has died/lost its bed somehow")
  226. reward=DyingReward
  227. this_replay.append(reward)
  228. this_replay.append(terminal)
  229. replay_buffer.append(this_replay)
  230. break
  231. if img_array_2[LosingBedColourAndPos[1][0]][LosingBedColourAndPos[1][1]][0]==LosingBedColourAndPos[0][0] and img_array_2[LosingBedColourAndPos[1][0]][LosingBedColourAndPos[1][1]][1]==LosingBedColourAndPos[0][1] and img_array_2[LosingBedColourAndPos[1][0]][LosingBedColourAndPos[1][1]][2]==LosingBedColourAndPos[0][2]:
  232. print("The AI has died/lost its bed somehow")
  233. reward=LosingBedReward
  234. this_replay.append(reward)
  235. this_replay.append(terminal)
  236. replay_buffer.append(this_replay)
  237. break
  238. if img_array_2[LosingGameColourAndPos[1][0]][LosingGameColourAndPos[1][1]][0]==LosingGameColourAndPos[0][0] and img_array_2[LosingGameColourAndPos[1][0]][LosingGameColourAndPos[1][1]][1]==LosingGameColourAndPos[0][1] and img_array_2[LosingGameColourAndPos[1][0]][LosingGameColourAndPos[1][1]][2]==LosingGameColourAndPos[0][2]:
  239. print("The AI has lost somehow")
  240. reward=LosingGameReward
  241. terminal=True
  242. this_replay.append(reward)
  243. this_replay.append(terminal)
  244. replay_buffer.append(this_replay)
  245. break
  246. this_replay.append(reward)
  247. this_replay.append(terminal)
  248. replay_buffer.append(this_replay)
  249. x,y=configure_replay_buffer(replay_buffer)
  250. c=0
  251. nn.train(x,y,100,0.01,"ADAM")
  252. nn.save_to_file("Brain test.npy")
Advertisement
Add Comment
Please, Sign In to add comment