class path: pic_path = 'picture\\' my_underwear = pic_path + 'my_underwear.png' destination = pic_path + 'destination.png' screen0 = pic_path + 'screen0.png' human = pic_path + 'human.png' class Info: position = (0, 0) destination = (-1, -1) # info = Info() tt = Time() class Env(): def __init__(self): class_name = 'YYGameMakerYY' wind = Window(class_name) self.wind = wind self.num_frames = arg.num_frames self.press_shift = False # self.action_name = ['left_press', 'left_up', 'right_press', 'right_up', 'shift_press', 'shift_up', 'stop'] self.action_name = ['left', 'right', 'shift_press', 'shift_up', 'stop'] self.N_A = len(self.action_name)
# print('- max_grad: ',tmp) if(arg.plot_net): plot_net(self.eval_net, 0) self.optimizer.step() # print('----> name:{:10} ----> grad_requirs: {} ----> grad_value: {}'.format("name".rjust(25), "parms.requires_grad".rjust(25), "parms.grad.shape".rjust(25))) 1 if __name__ == '__main__': env = Env() model = Model() tt = Time() steps = 0 # arg.MAX_EPISODE # i_episode = 0 for i_episode in range(MAX_EPISODE): try: if(break_flag): break except: break_flag = 0 s, position = env.reset(return_s_pos=1) s = preprocess_state(s, position, env)
# (0.01, 0.001) - 194.7; # (0.01, 0.0012) - 194.7; GLOBAL_RUNNING_R = [] GLOBAL_EP = 0 env = gym.make(GAME) N_S = env.observation_space.shape[0] N_A = env.action_space.n torch.manual_seed(seed) np.random.seed(seed) env.seed(seed) # reproducible #OPT_A = tf.train.RMSPropOptimizer(LR_A, name='RMSPropA') #OPT_C = tf.train.RMSPropOptimizer(LR_C, name='RMSPropC') tt = Time() class ACNet(object): def __init__(self, scope, globalAC=None): if scope == GLOBAL_NET_SCOPE: # get global network #self.a_params, self.c_params = self._build_net(scope)[-2:] pass else: self.s = [torch.FloatTensor([0.] * N_S)] self.a_his = [] self.v_target = np.array([]) self._build_net(scope) self.optimizer_a = torch.optim.Adam(self.a_net.parameters(), lr=LR_A)
""" Shared optimizer, the parameters in the optimizer will shared in the multiprocessors. """ import torch from mycode.my_time import Time tt = Time() class SharedAdam(torch.optim.Adam): def __init__(self, params, lr=1e-3, betas=(0.9, 0.99), eps=1e-8, weight_decay=0): super(SharedAdam, self).__init__(params, lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) # State initialization for group in self.param_groups: for p in group['params']: state = self.state[p] state['step'] = 0 state['exp_avg'] = torch.zeros_like(p.data) state['exp_avg_sq'] = torch.zeros_like(p.data)
dqn = DQN(N_C, arg.h, arg.w, N_A) # device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # if gpu is to be used device = "cpu" LR = 0.01 GAMMA = 0.9 policy_net = DQN(N_C, arg.h, arg.w, N_A).to(device) target_net = DQN(N_C, arg.h, arg.w, N_A).to(device) target_net.load_state_dict(policy_net.state_dict()) target_net.eval() loss_func = nn.MSELoss() optimizer = torch.optim.Adam(policy_net.parameters(), lr=LR) tt = Time() # Transition = namedtuple('Transition', # ('state', 'action', 'next_state', 'reward')) memory = ReplayMemory(1000) # env.step(-1) # s, position = env.reset(return_s_pos=1) # s = preprocess_state(s, position, env) # env.action_name -- ['left', 'right', 'shift_press', 'shift_up', 'stop'] actions = [ 1, 1, 2, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1 ]
screen = env.render(mode='rgb_array').transpose((2, 0, 1)) c, h, w = screen.shape screen = np.ascontiguousarray(screen, dtype=np.float32) / 255 screen = torch.from_numpy(screen) screen = screen.unsqueeze(0) screen_np = screen.cpu().squeeze(0).permute(1, 2, 0).numpy() screen.shape screen_np.shape # import matplotlib.pyplot as plt # # plt.imshow(screen_np) # plt.show() dqn = DQN(h, w, 2) # Height * Width , n_action tt = Time() for i in range(100): dqn.forward(screen) print(tt.now()) screen.shape xx = torch.unsqueeze(screen, 0) torch.cat([screen, screen], -1).shape l_screen = [] for i in range(100): l_screen.append(screen) end = [] for screen0 in l_screen: screen0 = screen0.unsqueeze(0) try:
return processed_img # np.zeros([100,100,3])+255 # find my_location and destination_location def get_loacation(): pass 1 if __name__ == '__main__': from template import match_img tt = Time() class_name = 'YYGameMakerYY' wind = Window(class_name) wind.move_to(0, 0) if (0): # read image # screen0 = wind.grab_screen() # screen_gray = cv2.cvtColor(screen0, cv2.COLOR_BGR2GRAY) # screen_canny = cv2.Canny(screen_gray, threshold1=200, threshold2=300) # # # screen_gray # screen_canny # sg_sc = screen_gray + screen_canny # sg_sc0 = cv2.add(screen_gray, screen_canny)
import win32api, win32gui, win32con, win32process from grabscreen import grab_screen, plt_img, cv_img from mycode.my_time import Time, vk import ctypes tt = Time() class Window(): def __init__(self, class_name = '', title_name = '', hwnd = None ): if(hwnd == None): self.class_name = class_name self.title_name = title_name self.hwnd = win32gui.FindWindow(class_name, None) else: # hwnd= win32gui.FindWindow(class_name, None) self.hwnd = hwnd self.class_name = win32gui.GetClassName(hwnd) self.title_name = win32gui.GetWindowText(hwnd) self.get_rect()
import time #------------------------ win32 api if(1): ######################################## # 前台后台 import win32api, win32con, win32gui import sys, os pre_path = os.getcwd().rsplit('\\', 1)[0] sys.path.append(pre_path + '\\mycode') from mycode.my_time import Time, vk from Env import Env import win32com import win32com.client tt = Time() env = Env() env.wind.hwnd hwnd = 395422 tt.sleep(1) win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') win32gui.SetForegroundWindow(hwnd) tt.sleep(1) win32gui.SetBkMode(hwnd, win32con.TRANSPARENT) SendInput = ctypes.windll.user32.SendInput