def ml_loop_for_1P(): # === Here is the execution order of the loop === # # 1. Put the initialization code here. import pickle import numpy as np filename = "C:\\Users\\su021\\OneDrive\\桌面\\sav\\svr_1P.sav" model = pickle.load(open(filename, 'rb')) #print(model) ball_position_history = [] ball_center_record = [] # 2. Inform the game process that ml process is ready comm.ml_ready() vx = 0 vy = 0 ball_going_down = -1 ball_going_x = -1 oldvx = 0 # 3. Start an endless loop. while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_1P[0] #platform length = 40 if len(ball_position_history) > 1: vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] inp_temp = np.array([ scene_info.ball[0], scene_info.ball[1], platform_center_x, vx, vy ]) input = inp_temp[np.newaxis, :] # 3.2. If the game is over or passed, the game process will reset # the scene and wait for ml process doing resetting job. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() continue # 3.3. Put the code here to handle the scene information if (len(ball_position_history) > 1): move = model.predict(input) #print(move) else: move = 0 # 3.4. Send the instruction for this frame to the game process #print(ball_destination) #print(platform_center_x) if move < 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif move > 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT)
def ml_loop(side: str): print("For {}".format(side)) comm.ml_ready() while True: scene_info = comm.get_scene_info() if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() continue comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() print(scene_info) continue # 3.3 Put the code here to handle the scene information # 3.4 Send the instruction for this frame to the game process comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT)
def ml_loop(side: str): init_pygame() print("Invisible joystick is used. " "Press Enter to start the {} ml process.".format(side)) while wait_enter_key(): pass comm.ml_ready() while True: scene_info = comm.get_scene_info() if scene_info.status != GameStatus.GAME_ALIVE: comm.ml_ready() continue key_pressed_list = pygame.key.get_pressed() if key_pressed_list[pygame.K_LEFT]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif key_pressed_list[pygame.K_RIGHT]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) pygame.event.pump()
def ml_loop_for_1P(): ball_last = [101, 101] # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information # print(scene_info.ball) ball_x_end = compute_x_end_1P(scene_info.ball, ball_last) ball_last = scene_info.ball move = (ball_x_end) - (scene_info.platform_1P[0] + 15) # motion direction of ball # compute the location of falling # 3.4. Send the instruction for this frame to the game process if move > 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif move < 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): comm.ml_ready() past_ball_position = [] ball_down = False first = True aid = 100 params = [] while True: scene_info = comm.get_scene_info() if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() first = True continue now_ball_position = scene_info.ball if len(past_ball_position) == 0: past_ball_position = now_ball_position continue else: if (now_ball_position[1] - past_ball_position[1]) > 0: ball_down = True else: ball_down = False m = 0 if now_ball_position[0] - past_ball_position[0] != 0: m = (now_ball_position[1] - past_ball_position[1]) / (now_ball_position[0] - past_ball_position[0]) if ball_down == True: aid = 100 if first: aid = round(now_ball_position[0] - ((now_ball_position[1] - 80) / m)) if aid < 0: aid = -aid elif aid > 195: aid = 200 - (aid - 200) if now_ball_position[1] == 415: first = False parameter = [int(scene_info.ball[0]), round(m, 2), int(scene_info.ball_speed), int(scene_info.frame % 200)] aid = predict(parameter) cc = aid % 5 aid -= cc now_platform_positionX = scene_info.platform_2P[0] + 20 if aid > now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif aid < now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif aid == now_platform_positionX: comm.send_instruction(scene_info.frame, PlatformAction.NONE) past_ball_position = now_ball_position
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here frame_position_history = [] status_position_history = [] ball_position_history = [] ball_speed_position_history = [] platform_1P_position_history = [] platform_2P_position_history = [] ball = [] # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() frame_position_history.append(scene_info.frame) status_position_history.append(scene_info.status) ball_position_history.append(scene_info.ball) ball_speed_position_history.append(scene_info.ball_speed) platform_1P_position_history.append(scene_info.platform_1P) platform_2P_position_history.append(scene_info.platform_2P) #platform_center_x = scene_info.platform[0] + 20 """ if len(ball_position_history)==1: ball_going_down = 0 elif ball_position_history[-1][1]-ball_position_history[-2][1]>0: """ if len(ball_position_history) > 1: #ball_going_down = 1 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] ball_going_down = 0 platform_1P_center_y = platform_1P_position_history[-1][1] - 300 platform_2P_center_y = platform_2P_position_history[-1][1] + 330 platform_1P_center_x = platform_1P_position_history[-1][0] + 20 platform_2P_center_x = platform_2P_position_history[-1][0] + 20 plat_1P = np.array([platform_1P_center_x, platform_1P_center_y]) input_1P = plat_1P[np.newaxis, :] plat_2P = np.array([platform_2P_center_x, platform_2P_center_y]) input_2P = plat_2P[np.newaxis, :] if vy > 0: ball_going_down = 1 else: ball_going_down = 0 #ball=np.array([vx,vy]) #ball.append( (vx,vy)) #print(ball_position_history[-1]) #print(ball) #else: ball_going_down = 0 if side == "1P": if ball_going_down == 1 and ball_position_history[-1][1] >= 120: ball_destination = ball_position_history[-1][0] + (( (415 - ball_position_history[-1][1]) / vy) * vx) #print(input_1P[-1][0]) #pp=3+vx/2 #px=(170//vx) #print((((415-ball_position_history[-1][1])/vy)*vx)) if ball_destination >= 195: ball_destination = 195 - (ball_destination - 195) #print("195") elif ball_destination <= 0: ball_destination = -ball_destination #print("0") #else: # ball_destination=platform_1P_center_x #print(ball_destination)#print("B") if platform_1P_center_x > ball_destination: #print("L") comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_1P_center_x < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #print("R") #######平板位置回中 if ball_going_down == 0: state = "1p-c" #print("1p-c") if input_1P[-1][0] > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif input_1P[-1][0] < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) """ if ball_position_history[-1][1] >350 : if platform_1P_position_history[-1][0]+20 < ball_position_history[-1][0]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_1P_position_history[-1][0]+20 > ball_position_history[-1][0]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif ball_position_history[-1][1] >350 and ball_position_history[-1][0] <30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_position_history[-1][1] >400 and ball_position_history[-1][0] <30: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball[-1][1] < 1 : print("1p-c") if platform_1P_position_history[-1][0]+20 >100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_1P_position_history[-1][0]+20 <100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #######平板位置回中 elif ball[-1] < 1 : if platform_1P_position_history[-1][0]+20 >100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_1P_position_history[-1][0]+20 <100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) return #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) """ else: #######平板位置2####### #print("2P") if ball_going_down == 0 and ball_position_history[-1][1] <= 380: ball_destination = ball_position_history[-1][0] + (( (ball_position_history[-1][1] - 80) / -vy) * vx) # print(input_2P[-1][0]) if ball_destination >= 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination <= 0: ball_destination = -ball_destination #else: # ball_destination=platform_1P_center_x #print((ball_position_history[-1][1]-80)//-vy) if input_2P[-1][0] > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif input_2P[-1][0] < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #######平板位置回中 if ball_going_down == 1: state = "2p-c" #print("2p-c") if platform_2P_center_x > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_center_x < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) """ if ball_position_history[-1][1] <250 : if platform_2P_position_history[-1][0]-40 > ball_position_history[-1][0]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]-20 < ball_position_history[-1][0]: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif ball_position_history[-1][1] <180 and ball_position_history[-1][0] <60: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball[-1][1] > 1 : print("2p-c") if platform_2P_position_history[-1][0]+20 >100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif ball[-1] > 1 : if platform_2P_position_history[-1][0]+20 >100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) print("123") if ball_position_history[-1][1] <120: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_position_history[-1][1] <320: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif ball[-1] < 1 and ball_position_history[-1][0] <20: if ball_position_history[-1][1] >180: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif ball_position_history[-1][0] <20: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #######平板位置5####### elif ball[-1] < 1 and ball_position_history[-1][1] <250 and ball_position_history[-1][0] <100 : return if platform_2P_position_history[-1][0]+20 >180: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <180: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #######平板位置2####### elif ball[-1] < 1 and ball_position_history[-1][1] < 250: if vx > 1 and ball_position_history[-1][0] > 130 and ball_position_history[-1][0] < 180: if platform_2P_position_history[-1][0]+20 >60: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <60: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #return #######平板位置3####### elif ball[-1] < 1 and ball_position_history[-1][1] < 250: if vx > 1 and ball_position_history[-1][0] > 70 and ball_position_history[-1][0] < 130: if platform_2P_position_history[-1][0]+20 >100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #return #######平板位置4####### elif ball[-1] < 1 and ball_position_history[-1][1] < 250: if vx > 1 and ball_position_history[-1][0] > 20 and ball_position_history[-1][0] < 70: if platform_2P_position_history[-1][0]+20 >140: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_position_history[-1][0]+20 <140: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) #return #if vx > 1 and vx < 1 and ball_position_history[-1][0] <20 : """ #######平板位置回中 # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue """ # 3.3 Put the code here to handle the scene information if ball_going_down == 1 and ball_position_history[-1][1]>=0: ball_destination = ball_position_history[-1][0]+ ((395-ball_position_history[-1][1])/vy)*vx if ball_destination >= 195: ball_destination = 195-(ball_destination-195) elif ball_destination <= 0: ball_destination = - ball_destination else: """ # ball_destination = platform_center_x # 3.4 Send the instruction for this frame to the game process """
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_2P[0] + 20 if (len(ball_position_history)) == 1: ball_going_down = 1 vx = 7 vy = 7 elif ball_position_history[-1][1] - ball_position_history[-2][1] < 0: ball_going_down = 0 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] else: ball_going_down = 1 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff if scene_info.status == GameStatus.GAME_2P_WIN: print("紀錄2P玩家") # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information if ball_going_down == 0: ball_destination = ball_position_history[-1][0] - ( (ball_position_history[-1][1] - 80) / vy) * vx if ball_destination > 390: ball_destination = 195 - (585 - ball_destination) elif ball_destination < -195: ball_destination = 390 + ball_destination elif ball_destination > 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination < 0: ball_destination = -ball_destination else: ball_destination = ball_position_history[-1][0] + ( (760 - ball_position_history[-1][1]) / vy) * vx if ball_destination > 585: ball_destination = 780 - ball_destination elif ball_destination < -390: ball_destination = 195 - (585 + ball_destination) elif ball_destination > 390: ball_destination = 195 - (585 - ball_destination) elif ball_destination < -195: ball_destination = 390 + ball_destination elif ball_destination > 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination < 0: ball_destination = -ball_destination #""" # 3.4. Send the instruction for this frame to the game process #ball_destination = ball_destination - ball_destination%5 if ball_going_down == 0: if (platform_center_x) < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (platform_center_x) > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if (platform_center_x) < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (platform_center_x) > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # if ball_going_down == 0 : # if (platform_center_x + 5) < ball_destination : # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) # elif (platform_center_x - 5) > ball_destination : # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) # else: # comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): """ The main loop of the machine learning process This loop is run in a separate process, and communicates with the game process. Note that the game process won't wait for the ml process to generate the GameInstruction. It is possible that the frame of the GameInstruction is behind of the current frame in the game process. Try to decrease the fps to avoid this situation. """ # === Here is the execution order of the loop === # # 1. Put the initialization code here. frame_position_history=[] status_position_history=[] ball_position_history=[] ball_speed_position_history=[] platform_1P_position_history=[] platform_2P_position_history=[] # 2. Inform the game process that ml process is ready before start the loop. filename1 ="svm-1p.sav" model1=pickle.load(open(filename1, 'rb')) filename2 ="svm-2p.sav" model2=pickle.load(open(filename2, 'rb')) comm.ml_ready() # 3. Start an endless loop. while True: # 3.1. Receive the scene information sent from the game process. """scene_info = comm.get_scene_info() platform_center_x = scene_info.platform[0] inp_temp=np.array([scene_info.ball[0], scene_info.ball[1], scene_info.platform[0]]) input=inp_temp[np.newaxis, :]""" scene_info = comm.get_scene_info() frame_position_history.append( scene_info.frame) status_position_history.append( scene_info.status) ball_position_history.append( scene_info.ball) ball_speed_position_history.append( scene_info.ball_speed) platform_1P_position_history.append( scene_info.platform_1P) platform_2P_position_history.append( scene_info.platform_2P) platform_1P_center_x = scene_info.platform_1P[0] + 0 platform_2P_center_x = scene_info.platform_2P[0] + 0 if len(ball_position_history)>1: #ball_going_down = 1 vy=ball_position_history[-1][1]-ball_position_history[-2][1] vx=ball_position_history[-1][0]-ball_position_history[-2][0] #ball=np.array([vx,vy]) inp_temp1=np.array([scene_info.ball[0],scene_info.ball[1],platform_1P_center_x,vx,vy]) input_1P=inp_temp1[np.newaxis, :] inp_temp2=np.array([scene_info.ball[0],scene_info.ball[1],platform_2P_center_x,vx,vy]) input_2P=inp_temp2[np.newaxis, :] #print(len(ball_position_history)) if side == "1P": #print(model1.predict(input_1P)) if(len(ball_position_history) > 1): move1 = model1.predict(input_1P) else: move1 = 0 #move= model.predict(input) #print(move) if vy > 0: #print(move1) if move1 == 1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif move1 == -1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: if platform_1P_center_x >80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_1P_center_x <80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: #print(model2.predict(input_2P)) if(len(ball_position_history) > 1): move2 = model2.predict(input_2P) else: move2 = 0 #move= model.predict(input) #print(move) if vy < 0: #print(move2) if move2 == 1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif move2 == -1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: if platform_2P_center_x >80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif platform_2P_center_x <80: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() continue
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] ball_vx = [] ball_vy = [] list_platform_center_x = [] list_ball_destination = [] #print (time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime())) #file = open('save/'+time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+".pickle",'wb') # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_1P[0] + 20 list_platform_center_x.append(platform_center_x) if (len(ball_position_history)) == 1: ball_going_down = 0 vx = 7 vy = 7 elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0: ball_going_down = 1 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] else: ball_going_down = 0 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] ball_vx.append(vx) ball_vy.append(vy) # 3.3 Put the code here to handle the scene information if ball_going_down == 1: ball_destination = ball_position_history[-1][0] + ( (415 - ball_position_history[-1][1]) / vy) * vx if ball_destination > 390: ball_destination = 195 - (585 - ball_destination) elif ball_destination < -195: ball_destination = 390 + ball_destination elif ball_destination > 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination < 0: ball_destination = -ball_destination # print(ball_destination) ball_destination = round(abs(ball_destination)) list_ball_destination.append(ball_destination) else: ball_destination = ball_position_history[-1][0] - ( (ball_position_history[-1][1] + 260) / vy) * vx if ball_destination > 585: ball_destination = 780 - ball_destination elif ball_destination < -390: ball_destination = 195 - (585 + ball_destination) elif ball_destination > 390: ball_destination = 195 - (585 - ball_destination) elif ball_destination < -195: ball_destination = 390 + ball_destination elif ball_destination > 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination < 0: ball_destination = -ball_destination # print(ball_destination) ball_destination = round(abs(ball_destination)) list_ball_destination.append(ball_destination) #""" # 3.4. Send the instruction for this frame to the game process #ball_destination = ball_destination - ball_destination%5 # print(ball_destination) if ball_going_down == 1: if (platform_center_x) < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (platform_center_x) > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: if (platform_center_x) < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (platform_center_x) > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff if scene_info.status == GameStatus.GAME_1P_WIN: print(len(ball_vx), len(ball_vy), len(list_platform_center_x), len(list_ball_destination), len(ball_position_history)) ##字典 pickle_data_dict = { 'ball_vx': ball_vx, 'ball_vy': ball_vy, 'ball_destination': list_ball_destination, 'platform_center_x': list_platform_center_x, 'ball_position_history': ball_position_history } #print(pickle_data_dict['ball_destination']) print("紀錄1P玩家") ##-----------------------------存成pickle檔-------------------------------------------------## #print('紀錄1P玩家特徵') #file = open('save/'+time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+"_GAME_1P_WIN.pickle",'wb') #pickle.dump(pickle_data_dict, file) #file.close() ball_vx.clear() ball_vy.clear() list_ball_destination.clear() list_platform_center_x.clear() ball_position_history.clear() ##------------------------------------------------------------------------------------------## # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # if ball_going_down == 0 : # if (platform_center_x + 5) < ball_destination : # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) # elif (platform_center_x - 5) > ball_destination : # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) # else: # comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] #global input move = 0 filename = 'SVC_example2.sav' model = pickle.load(open(filename, 'rb')) # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) if len(ball_position_history) > 2: inp_temp = np.array([ ball_position_history[-2][0], ball_position_history[-2][1], ball_position_history[-1][0], ball_position_history[-1][1], scene_info.platform_2P[0] ]) input = inp_temp[np.newaxis, :] print(input) # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information # 3.4 Send the instruction for this frame to the game process if len(ball_position_history) > 2: move = model.predict(input) #print(move) if move > 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif move < 0: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here filename = "C:\\Users\\fghj8\\MLGame-master\\games\\pingpong\\my_tree_1P.sav" model = pickle.load(open(filename, 'rb')) filename = "C:\\Users\\fghj8\\MLGame-master\\games\\pingpong\\my_tree_2P.sav" model_2P = pickle.load(open(filename, 'rb')) ball_location = [0, 0] # 2. Inform the game process that ml process is ready comm.ml_ready() scene_info = comm.get_scene_info() # 3. Start an endless loop if side == "1P": while True: last_ball_location = ball_location ball_location = scene_info.ball scene_info = comm.get_scene_info() if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue if (int(last_ball_location[1]) - int(ball_location[1]) < 0): # go to down if (int(last_ball_location[0]) - int(ball_location[0]) < 0): #go RD LRUP = 2 else: LRUP = 1 #go LD else: #upping if (int(last_ball_location[0]) - int(ball_location[0]) < 0): #go RU LRUP = 4 else: LRUP = 3 #go LU inp_temp = [scene_info.ball[0],scene_info.ball[1],LRUP, \ (200 - int(scene_info.ball[0]))] move = str(model.classify_test(inp_temp)) # print(inp_temp) # print(move) try: ans = move[1:3] ans = int(ans) * 10 except: ans = move[1:2] ans = int(ans) * 10 if (scene_info.platform_1P[0] + 20 > ans): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (scene_info.platform_1P[0] + 20 < ans): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: # 2P # while True: # last_ball_location = ball_location # ball_location = scene_info.ball # scene_info = comm.get_scene_info() # if scene_info.status == GameStatus.GAME_1P_WIN or \ # scene_info.status == GameStatus.GAME_2P_WIN: # # Some updating or reseting code here # comm.ml_ready() # continue # if(int(last_ball_location[1]) - int(ball_location[1]) > 0): # # go to up # # if(int(last_ball_location[0]) - int(ball_location[0]) > 0): # #go LU # LRUP = 3 # else: # LRUP = 4 # #go RU # else: # #down # if(int(last_ball_location[0]) - int(ball_location[0]) > 0): # #go LD # LRUP = 1 # else: # LRUP = 2 # #go RD # # # balltopx = scene_info.platform_2P[0] - scene_info.ball[0] # balltopy = scene_info.platform_2P[1] - scene_info.ball[1] # inp_temp = np.array([scene_info.ball[0],scene_info.ball[1],LRUP, \ # (200 - int(scene_info.ball[0])),balltopx,balltopy \ # ,scene_info.platform_2P[0] + 20, scene_info.ball_speed,scene_info.ball_speed,scene_info.ball_speed]) # # #BALLXY lrup ballto200 balltopx balltopy plat ballspeed # inp_temp = inp_temp[np.newaxis,:] # move = model_2P.prediction(inp_temp) # print(move) # if(move < 0): # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) # elif(move > 0): # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) # else: # comm.send_instruction(scene_info.frame, PlatformAction.NONE) while True: last_ball_location = ball_location scene_info = comm.get_scene_info() ball_location = scene_info.ball if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue if (int(last_ball_location[1]) - int(ball_location[1]) > 0): # go to up if (int(last_ball_location[0]) - int(ball_location[0]) > 0): #go LU LRUP = 1 else: LRUP = 2 #go RU else: #down if (int(last_ball_location[0]) - int(ball_location[0]) > 0): #go LD LRUP = 3 else: LRUP = 4 #go RD inp_temp = [scene_info.ball[0],scene_info.ball[1],LRUP, \ (200 - int(scene_info.ball[0]))] move = str(model_2P.classify_test(inp_temp)) # print(inp_temp) # print(move) try: ans = move[1:3] ans = int(ans) * 10 except: ans = move[1:2] ans = int(ans) * 10 if (ans < 50 and scene_info.ball_speed == 21): # print('move') ans += 10 if (scene_info.platform_2P[0] + 20 > ans): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (scene_info.platform_2P[0] + 20 < ans): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop_for_2P(): ball_position_history = [] ball_center_record = [] # 2. Inform the game process that ml process is ready comm.ml_ready() vx = 0 ball_going_down = -1 ball_going_x = -1 oldvx = 0 mx = 0 # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_2P[0] + 20 platform_center_y = scene_info.platform_2P[1] if (len(ball_position_history)) == 1: ball_going_down = 0 elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0: ball_going_down = 1 #下降 else: ball_going_down = 0 if ball_height >= 390: mx = ball_position_history[-1][0] - ball_position_history[-2][0] #400 球初始 100 #球一步X,Y= 7 平台一步 = 5 ball_center = scene_info.ball[0] #球X位點左上角+2.5為正中央 ball_height = scene_info.ball[1] #球Y位點 if ball_going_down == 0: if ball_height >= 390: oldvx = ball_center if mx > 0: ball_going_x = 1 else: ball_going_x = 0 #左""" if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() continue #scene_info = comm.get_scene_info() # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready # Do some stuff if needed # 3.2.1. Inform the game process that ml process is ready if ball_going_down == 1: if platform_center_x < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if ball_going_x == 1: #右 if oldvx < 80: if platform_center_x < round((-oldvx + 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round((-oldvx + 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < round((oldvx - 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round((oldvx - 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if oldvx > 120: if platform_center_x < round((-oldvx + 320) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round( (-oldvx + 320) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < round((oldvx + 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round((oldvx + 80) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_location = [0, 0] plat_location = [0, 0] # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop if side == "1P": while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() if (scene_info.frame == 0): print(scene_info.ball) # 3.2. If the game is over or passed, the game process will reset # the scene and wait for ml process doing resetting job. last_ball_location = ball_location ball_location = scene_info.ball plat_location = scene_info.platform_1P if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue if (int(ball_location[0]) + int(ball_location[1]) != 0): if (int(ball_location[1]) - int(last_ball_location[1]) > 0): # go to 1P next_x,delta_x,delta_y = predict_1P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],scene_info.ball_speed) else: next_x,delta_x,delta_y = predict_2P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],15) next_x,delta_x,delta_y = predict_1P(next_x + 7*delta_x ,80 + delta_y*-1 \ ,next_x,80,scene_info.ball_speed) if (next_x % 5 > 2.5): next_x = next_x + (5 - next_x % 5) else: next_x = next_x - next_x % 5 # 3.3 Put the code here to handle the scene information # 3.4 Send the instruction for this frame to the game process if (int(plat_location[0]) + 20 > next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (int(plat_location[0]) + 20 < next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else: while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() # 3.2. If the game is over or passed, the game process will reset # the scene and wait for ml process doing resetting job. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue last_ball_location = ball_location ball_location = scene_info.ball plat_location = scene_info.platform_2P if (int(ball_location[0]) + int(ball_location[1]) != 0): if (int(ball_location[1]) - int(last_ball_location[1]) > 0): # go to 1P next_x,delta_x,delta_y = predict_1P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],18) next_x,delta_x,delta_y = predict_2P(next_x + 7*delta_x ,420 + delta_y*-1 \ ,next_x,420,scene_info.ball_speed) else: # go to 2P next_x,delta_x,delta_y = predict_2P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],scene_info.ball_speed) if (next_x % 5 > 2.5): next_x = next_x + (5 - next_x % 5) else: next_x = next_x - next_x % 5 # 3.3 Put the code here to handle the scene information # 3.4 Send the instruction for this frame to the game process if (int(plat_location[0]) + 20 > next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif (int(plat_location[0]) + 20 < next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_location = [0,0] plat_location = [0,0] # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop if side == "1P": filename = "F:\\Documents\\MachineLearning\\MLGame-master\\knn_test.sav" model = pickle.load(open(filename,'rb')) last_ball_x = 0 last_ball_y = 0 comm.ml_ready() scene_info = comm.get_scene_info() while True: last_ball_x = scene_info.ball[0] last_ball_y = scene_info.ball[1] scene_info = comm.get_scene_info() plat_cneter_x = scene_info.platform_1P[0]+20 if(last_ball_x - scene_info.ball[0] > 0): LR = 0 else: LR = 1 if(last_ball_y - scene_info.ball[1] > 0): UP = 1 else: UP = 0 inp_temp = np.array([scene_info.ball[0],scene_info.ball[1],LR,UP,scene_info.platform_1P[0]]) input = inp_temp[np.newaxis,:] if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue move = model.predict(input) if(move>0): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif(move<0): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # # 3.1. Receive the scene information sent from the game process. # scene_info = comm.get_scene_info() # if(scene_info.frame == 0): # print(scene_info.ball) # # 3.2. If the game is over or passed, the game process will reset # # the scene and wait for ml process doing resetting job. # # last_ball_location = ball_location # ball_location = scene_info.ball # plat_location = scene_info.platform_1P # if scene_info.status == GameStatus.GAME_1P_WIN or \ # scene_info.status == GameStatus.GAME_2P_WIN: # # Some updating or reseting code here # comm.ml_ready() # continue # # if(int(ball_location[0]) + int(ball_location[1]) != 0): # if(int(ball_location[1]) - int(last_ball_location[1]) > 0): # # go to 1P # # next_x,delta_x,delta_y = predict_1P(ball_location[0],ball_location[1] \ # ,last_ball_location[0],last_ball_location[1],scene_info.ball_speed) # # else: # next_x,delta_x,delta_y = predict_2P(ball_location[0],ball_location[1] \ # ,last_ball_location[0],last_ball_location[1],15) # next_x,delta_x,delta_y = predict_1P(next_x + 7*delta_x ,80 + delta_y*-1 \ # ,next_x,80,scene_info.ball_speed) # # if( next_x%5 > 2.5): # next_x = next_x + (5 - next_x%5) # else: # next_x = next_x - next_x%5 # # # # 3.3 Put the code here to handle the scene information # # 3.4 Send the instruction for this frame to the game process # if(int(plat_location[0])+20>next_x): # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) # elif(int(plat_location[0])+20<next_x): # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) # else: # comm.send_instruction(scene_info.frame, PlatformAction.NONE) #2P------------------------------------------------------------------------------------------ else: while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() # 3.2. If the game is over or passed, the game process will reset # the scene and wait for ml process doing resetting job. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Some updating or reseting code here comm.ml_ready() continue last_ball_location = ball_location ball_location = scene_info.ball plat_location = scene_info.platform_2P if(int(ball_location[0]) + int(ball_location[1]) != 0): if(int(ball_location[1]) - int(last_ball_location[1]) > 0): # go to 1P next_x,delta_x,delta_y = predict_1P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],18) next_x,delta_x,delta_y = predict_2P(next_x + 7*delta_x ,420 + delta_y*-1 \ ,next_x,420,scene_info.ball_speed) else: # go to 2P next_x,delta_x,delta_y = predict_2P(ball_location[0],ball_location[1] \ ,last_ball_location[0],last_ball_location[1],scene_info.ball_speed) if( next_x%5 > 2.5): next_x = next_x + (5 - next_x%5) else: next_x = next_x - next_x%5 # 3.3 Put the code here to handle the scene information # 3.4 Send the instruction for this frame to the game process if(int(plat_location[0])+20>next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif(int(plat_location[0])+20<next_x): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE)
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] filename1 = "C:\\Users\\BBS\\Desktop\\Machin leaning final project\ \\game\\MLGame-master\\games\\pingpong\\ml\\mlp_model_2P_0552030_V3.sav" model = pickle.load(open(filename1, 'rb')) print(model) # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process. scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_1P[0] + 20 if (len(ball_position_history)) == 1: ball_going_down = 0 vx = 7 vy = 7 elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0: ball_going_down = 1 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] else: ball_going_down = 0 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] # 3.3 Put the code here to handle the scene information inp_temp = np.array([ scene_info.ball[0], scene_info.ball[1], scene_info.platform_2P[0] + 20, scene_info.platform_2P[1] + 30, vx, vy ]) input = inp_temp[np.newaxis, :] #print(input) if (len(ball_position_history) > 1): move = model.predict(input) else: move = 0 #print(move) #""" # 3.4. Send the instruction for this frame to the game process #ball_destination = ball_destination - ball_destination%5 # print(ball_destination) if (move > 0.3): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif (move < -0.3): comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round print(scene_info.ball_speed) comm.ml_ready() continue
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_1P[ 0] + 20 #platform length = 40 #print(platform_center_x) if (len(ball_position_history)) == 1: ball_going_down = 0 elif ball_position_history[-1][1] - ball_position_history[-2][ 1] > 0: #判斷球的方向 ball_going_down = 1 vy = ball_position_history[-1][1] - ball_position_history[-2][1] vx = ball_position_history[-1][0] - ball_position_history[-2][0] else: ball_going_down = 0 # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information if ball_going_down == 1: ball_destination = ball_position_history[-1][0] + ( (420 - ball_position_history[-1][1]) / vy) * vx if ball_destination >= 195: ball_destination = 195 - (ball_destination - 195) elif ball_destination <= 0: ball_destination = -ball_destination else: ball_destination = platform_center_x #print(ball_destination) # 3.4 Send the instruction for this frame to the game process #comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) if (platform_center_x - 5) > ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) print('1P 平板左移') elif (platform_center_x + 5) < ball_destination: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) print('1P 平板右移') else: print('1P 平板不動')
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here filename = 'P2_SVM_parameter_API.sav' model = pickle.load(open(filename, 'rb')) ball_position_history = [] wait_frame = 0 # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information if wait_frame == 1: #------------------ calculate ball Ball = np.asarray(scene_info.ball) # print("Ball : ", Ball) #------------------ calculate ball speed Ball_speed = np.asarray(scene_info.ball_speed) # print("Ball_speed : ", Ball_speed) #------------------ calculate ball move Ball_move = np.asarray(ball_position_history[-1]) - np.asarray( ball_position_history[-2]) # print("Ball_move : ", Ball_move) #------------------ calculate plat Plat_X_P1 = scene_info.platform_1P[0] Plat_X_P2 = scene_info.platform_2P[0] # print("Plat_X_P1 : ", Plat_X_P1) # print("Plat_X_P2 : ", Plat_X_P2) #------------------ calculate Plat Ball distance Plat_P1 = np.asarray(scene_info.platform_1P) Plat_P2 = np.asarray(scene_info.platform_2P) # print("Plat_P1 : ", Plat_P1) # print("Plat_P2 : ", Plat_P2) # Plat_Ball_distance_P1 = np.linalg.norm( Plat_P1 - Ball, axis = 1 ) # Plat_Ball_distance_P2 = np.linalg.norm( Plat_P2 - Ball, axis = 1 ) Plat_Ball_distance_P1 = Plat_P1 - Ball Plat_Ball_distance_P2 = Plat_P2 - Ball # print("Plat_Ball_distance_P1 : ", Plat_Ball_distance_P1) # print("Plat_Ball_distance_P2 : ", Plat_Ball_distance_P2) #------------------ mix data_x = np.hstack( (Ball, Ball_speed, Ball_move, Plat_X_P1, Plat_X_P2, Plat_Ball_distance_P1, Plat_Ball_distance_P2)) input_data_x = data_x[np.newaxis, :] move = model.predict(input_data_x) # 3.4 Send the instruction for this frame to the game process if move == -1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif move == 1: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) # elif Plat_X_P2 > 80: # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) # elif Plat_X_P2 < 80: # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) # comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) else: wait_frame = 1
def ml_loop(side: str): """ The main loop for the machine learning process The `side` parameter can be used for switch the code for either of both sides, so you can write the code for both sides in the same script. Such as: ```python if side == "1P": ml_loop_for_1P() else: ml_loop_for_2P() ``` @param side The side which this script is executed for. Either "1P" or "2P". """ # === Here is the execution order of the loop === # # 1. Put the initialization code here pkl_file = open('P1_SVM_parameter.pkl', 'rb') SVM_dictionary = pickle.load(pkl_file) pkl_file.close() ball_position_history = [] wait_frame = 0 # 2. Inform the game process that ml process is ready comm.ml_ready() # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append( scene_info.ball ) # print( " ball_position_history: ", ball_position_history) # 3.2. If either of two sides wins the game, do the updating or # resetting stuff and inform the game process when the ml process # is ready. if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: # Do some updating or resetting stuff # 3.2.1 Inform the game process that # the ml process is ready for the next round comm.ml_ready() continue # 3.3 Put the code here to handle the scene information if wait_frame ==1 : #------------------ calculate ball Ball = np.asarray( scene_info.ball) # print("Ball : ", Ball) #------------------ calculate ball speed Ball_speed = np.asarray(scene_info.ball_speed) # print("Ball_speed : ", Ball_speed) #------------------ calculate ball move Ball_move = np.asarray(ball_position_history[-1]) - np.asarray( ball_position_history[-2]) # print("Ball_move : ", Ball_move) #------------------ calculate plat Plat_X_P1 = scene_info.platform_1P [0] Plat_X_P2 = scene_info.platform_2P [0] # print("Plat_X_P1 : ", Plat_X_P1) # print("Plat_X_P2 : ", Plat_X_P2) #------------------ calculate Plat Ball distance Plat_P1 = np.asarray( scene_info.platform_1P ) Plat_P2 = np.asarray( scene_info.platform_2P) # print("Plat_P1 : ", Plat_P1) # print("Plat_P2 : ", Plat_P2) Plat_Ball_distance_P1 = Plat_P1 - Ball Plat_Ball_distance_P2 = Plat_P2 - Ball # print("Plat_Ball_distance_P1 : ", Plat_Ball_distance_P1) # print("Plat_Ball_distance_P2 : ", Plat_Ball_distance_P2) #------------------ mix data_x = np.hstack((Ball, Ball_speed, Ball_move, Plat_X_P1, Plat_X_P2, Plat_Ball_distance_P1, Plat_Ball_distance_P2)) # sort_data_x = np.squeeze(np.asarray(data_x)) sort_data_x = np.squeeze(np.asarray(data_x)) # print(np.shape (sort_data_x)) # print(np.shape ( SVM_dictionary['w'])) predict_lenght = np.dot(sort_data_x, SVM_dictionary['w'] ) + SVM_dictionary['b'] if predict_lenght > 0 : predict_y = 1 elif predict_lenght < 0 : predict_y = -1 # 3.4 Send the instruction for this frame to the game process if predict_y == -1 : comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) elif predict_y == 1 : comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) else: comm.send_instruction(scene_info.frame, PlatformAction.NONE) else : wait_frame = 1
def ml_loop_for_1P(): # === Here is the execution order of the loop === # # 1. Put the initialization code here ball_position_history = [] ball_center_record = [] # 2. Inform the game process that ml process is ready comm.ml_ready() vx = 0 ball_going_down = -1 ball_going_x = -1 oldvx = 0 # 3. Start an endless loop while True: # 3.1. Receive the scene information sent from the game process scene_info = comm.get_scene_info() ball_position_history.append(scene_info.ball) platform_center_x = scene_info.platform_1P[0] + 20 platform_center_y = scene_info.platform_1P[1] if (len(ball_position_history)) == 1: ball_going_down = 0 elif ball_position_history[-1][1] - ball_position_history[-2][1] > 0: ball_going_down = 1 #下降 else: ball_going_down = 0 vx = ball_center ball_center = scene_info.ball[0] #球X位點左上角+2.5為正中央 ball_height = scene_info.ball[1] #球Y位點 if ball_going_down == 1: if platform_center_y - ball_height >= 310: oldvx = ball_center if ball_position_history[-1][0] - ball_position_history[-2][ 0] > 0: ball_going_x = 1 else: ball_going_x = 0 #左""" if scene_info.status == GameStatus.GAME_1P_WIN or \ scene_info.status == GameStatus.GAME_2P_WIN: comm.ml_ready() continue if ball_going_down == 0: if platform_center_x < 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > 100: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: #開始計算落下點 if ball_going_x == 1: #右 if oldvx < 90: if platform_center_x < round(abs(oldvx - 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round(abs(oldvx - 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < round((oldvx - 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round((oldvx - 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if oldvx > 110: if platform_center_x < round((-oldvx + 310) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round( (-oldvx + 310) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass else: if platform_center_x < round((oldvx + 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_RIGHT) elif platform_center_x > round((oldvx + 90) / 5.0, 0) * 5: comm.send_instruction(scene_info.frame, PlatformAction.MOVE_LEFT) pass