Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Copy and paste code:
- 1: Imports:
- import win32api
- import win32con
- import time
- import pymindcore as pm
- import numpy as np
- import cv2
- from mss import mss
- 2: Capture and resize:
- def capture_and_resize(returnBoth=False):
- with mss() as sct:
- monitor = sct.monitors[1]
- sct_img = sct.grab(monitor)
- # Convert to NumPy array
- img = np.array(sct_img)
- # Resize image to 40x40 pixels
- img_resized = cv2.resize(img, (40, 40), interpolation=cv2.INTER_AREA)
- img_resized_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- if returnBoth:
- return img_resized_rgb,img_rgb
- return img_resized_rgb
- 3: Output keys:
- outputkeys = [('hold','w'), ('hold','a'), ('hold','s'), ('hold','d'), ('release', 'w'), ('release', 'a'), ('release', 's'), ('release', 'd'),
- ('press', 'e'), ('hold', 'shift'), ('press', '1'), ('press', '2'), ('press', '3'), ('press', '4'), ('press', '5'), ('press', '6'),
- ('press', '7'), ('press', '8'), ('press', '9'), ('release', 'shift'),
- ('press', 'ctrl'), ('press', 'q'), ('press', 'f5'), ('press', ' '), ('hold', ' '), ('release', ' '), ('mouse_hold_1', 'left'),
- ('mouse_hold_1', 'right'), ('mouse_hold_1', 'up'), ('mouse_hold_1', 'down'), ('mouse_release', 'left'), ('mouse_release', 'right'),
- ('mouse_hold_2', 'left'), ('mouse_hold_2', 'right'), ('mouse_hold_2', 'up'), ('mouse_hold_2', 'down'), ('mouse_click', 'left'),
- ('mouse_hold_3', 'left'), ('mouse_hold_3', 'right'), ('mouse_hold_3', 'up'), ('mouse_hold_3', 'down'), ('mouse_click', 'right'),
- ('mouse_hold_4', 'left'), ('mouse_hold_4', 'right'), ('mouse_hold_4', 'up'), ('mouse_hold_4', 'down'), ('mouse_release', 'up'), ('mouse_release', 'down'),
- ('mouse_click_hold', 'left'), ('mouse_click_hold', 'right'), ('mouse_click_release', 'left'), ('mouse_click_release', 'right'), ('mouse_click', 'left'), ('mouse_click', 'right'),
- ]
- outputs=len(outputkeys)
- nn = pm.Optomised_neural_network(5000,[1600,800,400],outputs,"sig")
- 4: Keycodes and Keyboard action:
- KEY_CODES = {
- 'a': 0x41, 'b': 0x42, 'c': 0x43, 'd': 0x44,
- 'e': 0x45, 'f': 0x46, 'g': 0x47, 'h': 0x48,
- 'i': 0x49, 'j': 0x4A, 'k': 0x4B, 'l': 0x4C,
- 'm': 0x4D, 'n': 0x4E, 'o': 0x4F, 'p': 0x50,
- 'q': 0x51, 'r': 0x52, 's': 0x53, 't': 0x54,
- 'u': 0x55, 'v': 0x56, 'w': 0x57, 'x': 0x58,
- 'y': 0x59, 'z': 0x5A, ' ': 0x20,
- '1': 0x31, '2': 0x32, '3': 0x33, '4': 0x34,
- '5': 0x35, '6': 0x36, '7': 0x37, '8': 0x38, '9': 0x39,
- 'ctrl': 0x11, 'f5': 0x74, 'shift': 0x10
- }
- def keyboard_action(character, action):
- vk_code = KEY_CODES.get(character.lower())
- if vk_code is None:
- return
- if action == "hold" or action == "press":
- win32api.keybd_event(vk_code, 0, 0, 0)
- time.sleep(0.01)
- if action == "release" or action == "press":
- win32api.keybd_event(vk_code, 0, win32con.KEYEVENTF_KEYUP, 0)
- 5: Handle Mouse:
- mouse_hold = False
- click_hold = False
- mouse_hold_direction = None
- def mouse_action(direction, speed, action):
- global mouse_hold
- global click_hold
- global mouse_hold_direction
- if action == "hold":
- mouse_hold = True
- mouse_hold_direction = direction
- elif action == "press":
- mouse_hold = False
- if direction == 'left':
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
- elif direction == 'right':
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
- elif action == "release":
- mouse_hold = False
- elif action == "mouse_click_hold":
- click_hold = False
- if direction == 'left':
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
- elif direction == 'right':
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0)
- elif action == "mouse_click_release":
- click_hold = False
- if direction == 'left':
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
- elif direction == 'right':
- win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0)
- if mouse_hold or action == "press":
- x, y = win32api.GetCursorPos()
- if mouse_hold_direction == "left":
- x -= speed
- elif mouse_hold_direction == "right":
- x += speed
- elif mouse_hold_direction == "up":
- y -= speed
- elif mouse_hold_direction == "down":
- y += speed
- win32api.SetCursorPos((x, y))
- def handle_mouse_input(input_tuple):
- action_map = {
- 'mouse_hold_1': ('hold', 4),
- 'mouse_hold_2': ('hold', 8),
- 'mouse_hold_3': ('hold', 12),
- 'mouse_hold_4': ('hold', 16),
- 'mouse_click': ('press', 1)
- }
- action, direction = input_tuple
- if action.startswith('mouse_hold'):
- mouse_action_cmd = action_map.get(action)[0]
- speed = action_map.get(action)[1]
- mouse_action(direction, speed, mouse_action_cmd)
- elif action.startswith('mouse_click'):
- mouse_action(input_tuple[1], 1, input_tuple[0])
- elif action == 'mouse_release':
- mouse_action(direction, 1, 'release')
- 6: Handle AI output:
- def handle_AI_output(output):
- global outputkeys
- if output>25:
- handle_mouse_input(outputkeys[output])
- else:
- keyboard_action(outputkeys[output][1],outputkeys[output][0])
- 7: Replay Buffer:
- def configure_replay_buffer(replay_buffer,reward_decay=0.99,boosting=1.2):
- x=0
- all_inputs=[]
- ideal_outputs=[]
- terminals=[]
- reverse_buffer=replay_buffer.copy()
- reverse_buffer.reverse()
- for u in replay_buffer:
- inputs, output, outputs, reward, terminal=u
- all_inputs.append(inputs)
- ideal_outputs.append(outputs)
- terminals.append(terminal)
- all_inputs.reverse()
- ideal_outputs.reverse()
- terminals.reverse()
- old_reward=0
- for x in range(len(ideal_outputs)):
- chosen_output=reverse_buffer[x][1]
- if reverse_buffer[x][4]:
- old_reward=0
- for y in range(len(ideal_outputs[x])):
- ideal_outputs[x][y]=max(min(ideal_outputs[x][y]*boosting,1),0)
- old_reward*=reward_decay
- old_reward=max(min(old_reward+reverse_buffer[x][3],abs(reverse_buffer[x][3])),-abs(reverse_buffer[x][3]))
- ideal_outputs[x][chosen_output]=old_reward
- return np.array(all_inputs), np.array(ideal_outputs)
- 8: All together!
- print("In 5 seconds, it will start")
- dyingReward=-1
- LosingBedReward=-7.5
- LosingGameReward=-10
- DyingColourAndPos = [(), ()]
- LosingBedColourAndPos = [(), ()]
- LosingGameColourAndPos = [(), ()]
- replay_buffer=[]
- time.sleep(5)
- for x in range(100):
- terminal=False
- reward=0
- this_replay=[]
- start_time = time.time()
- extra_inputs = np.zeros(200)
- img_array,img_array_2 = capture_and_resize(True)
- img_array=img_array.flatten()
- end_time = time.time()
- print(f"Time taken: {end_time-start_time:.4f} seconds. ")
- inputs = np.concatenate([np.array(img_array),extra_inputs])
- outputs = nn.forward(inputs)
- maximum_output=outputs.max()
- outputKey=int(np.where(outputs==maximum_output)[0][0])
- this_replay.append(inputs)
- this_replay.append(outputKey)
- this_replay.append(outputs)
- print(f"output: {outputkeys[outputKey]}")
- 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]:
- print("The AI has died/lost its bed somehow")
- reward=DyingReward
- this_replay.append(reward)
- this_replay.append(terminal)
- replay_buffer.append(this_replay)
- break
- 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]:
- print("The AI has died/lost its bed somehow")
- reward=LosingBedReward
- this_replay.append(reward)
- this_replay.append(terminal)
- replay_buffer.append(this_replay)
- break
- 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]:
- print("The AI has lost somehow")
- reward=LosingGameReward
- terminal=True
- this_replay.append(reward)
- this_replay.append(terminal)
- replay_buffer.append(this_replay)
- break
- this_replay.append(reward)
- this_replay.append(terminal)
- replay_buffer.append(this_replay)
- x,y=configure_replay_buffer(replay_buffer)
- c=0
- nn.train(x,y,100,0.01,"ADAM")
- nn.save_to_file("Brain test.npy")
Advertisement
Add Comment
Please, Sign In to add comment