reinit
This commit is contained in:
70
TankMan/ml/Group_1/ml_play_1.py
Normal file
70
TankMan/ml/Group_1/ml_play_1.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""
|
||||
The template of the main script of the machine learning process
|
||||
"""
|
||||
import random
|
||||
import pygame
|
||||
|
||||
from src.env import IS_DEBUG
|
||||
|
||||
|
||||
class MLPlay:
|
||||
def __init__(self, ai_name, *args, **kwargs):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@param side A string "1P" or "2P" indicates that the `MLPlay` is used by
|
||||
which side.
|
||||
"""
|
||||
self.side = ai_name
|
||||
print(f"Initial Game {ai_name} ml script")
|
||||
self.time = 0
|
||||
|
||||
def update(self, scene_info: dict, keyboard=[], *args, **kwargs):
|
||||
"""
|
||||
Generate the command according to the received scene information
|
||||
"""
|
||||
# print(keyboard)
|
||||
|
||||
if scene_info["status"] != "GAME_ALIVE":
|
||||
# print(scene_info)
|
||||
return "RESET"
|
||||
move_act = random.randrange(5)
|
||||
aim_act = random.randrange(3)
|
||||
shoot_cd = random.randrange(15, 31)
|
||||
|
||||
is_shoot = 0
|
||||
if scene_info["used_frame"] % shoot_cd == 0:
|
||||
is_shoot = random.randrange(2)
|
||||
|
||||
command = []
|
||||
if move_act == 1:
|
||||
command.append("TURN_RIGHT")
|
||||
elif move_act == 2:
|
||||
command.append("TURN_LEFT")
|
||||
elif move_act == 3:
|
||||
command.append("FORWARD")
|
||||
elif move_act == 4:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if aim_act == 1:
|
||||
command.append("AIM_LEFT")
|
||||
elif aim_act == 2:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if is_shoot and not IS_DEBUG:
|
||||
command.append("SHOOT")
|
||||
|
||||
if self.side == "1P":
|
||||
if pygame.K_b in keyboard:
|
||||
command.append("DEBUG")
|
||||
|
||||
if not command:
|
||||
command.append("NONE")
|
||||
|
||||
return command
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the status
|
||||
"""
|
||||
print(f"reset Game {self.side}")
|
70
TankMan/ml/Group_1/ml_play_2.py
Normal file
70
TankMan/ml/Group_1/ml_play_2.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""
|
||||
The template of the main script of the machine learning process
|
||||
"""
|
||||
import random
|
||||
import pygame
|
||||
|
||||
from src.env import IS_DEBUG
|
||||
|
||||
|
||||
class MLPlay:
|
||||
def __init__(self, ai_name, *args, **kwargs):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@param side A string "1P" or "2P" indicates that the `MLPlay` is used by
|
||||
which side.
|
||||
"""
|
||||
self.side = ai_name
|
||||
print(f"Initial Game {ai_name} ml script")
|
||||
self.time = 0
|
||||
|
||||
def update(self, scene_info: dict, keyboard=[], *args, **kwargs):
|
||||
"""
|
||||
Generate the command according to the received scene information
|
||||
"""
|
||||
# print(keyboard)
|
||||
|
||||
if scene_info["status"] != "GAME_ALIVE":
|
||||
# print(scene_info)
|
||||
return "RESET"
|
||||
move_act = random.randrange(5)
|
||||
aim_act = random.randrange(3)
|
||||
shoot_cd = random.randrange(15, 31)
|
||||
|
||||
is_shoot = 0
|
||||
if scene_info["used_frame"] % shoot_cd == 0:
|
||||
is_shoot = random.randrange(2)
|
||||
|
||||
command = []
|
||||
if move_act == 1:
|
||||
command.append("TURN_RIGHT")
|
||||
elif move_act == 2:
|
||||
command.append("TURN_LEFT")
|
||||
elif move_act == 3:
|
||||
command.append("FORWARD")
|
||||
elif move_act == 4:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if aim_act == 1:
|
||||
command.append("AIM_LEFT")
|
||||
elif aim_act == 2:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if is_shoot and not IS_DEBUG:
|
||||
command.append("SHOOT")
|
||||
|
||||
if self.side == "1P":
|
||||
if pygame.K_b in keyboard:
|
||||
command.append("DEBUG")
|
||||
|
||||
if not command:
|
||||
command.append("NONE")
|
||||
|
||||
return command
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the status
|
||||
"""
|
||||
print(f"reset Game {self.side}")
|
70
TankMan/ml/Group_1/ml_play_3.py
Normal file
70
TankMan/ml/Group_1/ml_play_3.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""
|
||||
The template of the main script of the machine learning process
|
||||
"""
|
||||
import random
|
||||
import pygame
|
||||
|
||||
from src.env import IS_DEBUG
|
||||
|
||||
|
||||
class MLPlay:
|
||||
def __init__(self, ai_name, *args, **kwargs):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@param side A string "1P" or "2P" indicates that the `MLPlay` is used by
|
||||
which side.
|
||||
"""
|
||||
self.side = ai_name
|
||||
print(f"Initial Game {ai_name} ml script")
|
||||
self.time = 0
|
||||
|
||||
def update(self, scene_info: dict, keyboard=[], *args, **kwargs):
|
||||
"""
|
||||
Generate the command according to the received scene information
|
||||
"""
|
||||
# print(keyboard)
|
||||
|
||||
if scene_info["status"] != "GAME_ALIVE":
|
||||
# print(scene_info)
|
||||
return "RESET"
|
||||
move_act = random.randrange(5)
|
||||
aim_act = random.randrange(3)
|
||||
shoot_cd = random.randrange(15, 31)
|
||||
|
||||
is_shoot = 0
|
||||
if scene_info["used_frame"] % shoot_cd == 0:
|
||||
is_shoot = random.randrange(2)
|
||||
|
||||
command = []
|
||||
if move_act == 1:
|
||||
command.append("TURN_RIGHT")
|
||||
elif move_act == 2:
|
||||
command.append("TURN_LEFT")
|
||||
elif move_act == 3:
|
||||
command.append("FORWARD")
|
||||
elif move_act == 4:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if aim_act == 1:
|
||||
command.append("AIM_LEFT")
|
||||
elif aim_act == 2:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if is_shoot and not IS_DEBUG:
|
||||
command.append("SHOOT")
|
||||
|
||||
if self.side == "1P":
|
||||
if pygame.K_b in keyboard:
|
||||
command.append("DEBUG")
|
||||
|
||||
if not command:
|
||||
command.append("NONE")
|
||||
|
||||
return command
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the status
|
||||
"""
|
||||
print(f"reset Game {self.side}")
|
39
TankMan/ml/QT.py
Normal file
39
TankMan/ml/QT.py
Normal file
@ -0,0 +1,39 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
class QLearningTable:
|
||||
def __init__(self,actions,learning_rate=0.05,reward_decay=0.9,e_greedy=0.1):
|
||||
|
||||
self.actions=actions
|
||||
self.lr=learning_rate
|
||||
self.gamma=reward_decay
|
||||
self.epsilon=e_greedy
|
||||
|
||||
self.q_table=pd.DataFrame(columns=self.actions,dtype=np.float64)
|
||||
|
||||
def choose_action(self,observation):
|
||||
self.check_state_exist(observation)
|
||||
|
||||
#action selection
|
||||
if np.random.uniform()>self.epsilon:
|
||||
state_action =self.q_table.loc[observation,:]
|
||||
action =np.random.choice(state_action[state_action==np.max(state_action)].index)
|
||||
else:
|
||||
action = np.random.choice(self.actions)
|
||||
|
||||
return action
|
||||
|
||||
def learn(self,s,a,r,s_):
|
||||
self.check_state_exist(s)
|
||||
self.check_state_exist(s_)
|
||||
q_predict=self.q_table.loc[s,a]
|
||||
if s_!='Game_over' or s_!='Game_pass':
|
||||
q_target =r+self.gamma*self.q_table.loc[s_,:].max()
|
||||
else:
|
||||
q_target=r
|
||||
self.q_table.loc[s,a]+=self.lr*(q_target-q_predict)
|
||||
|
||||
def check_state_exist(self,state):
|
||||
if state not in list(self.q_table.index):
|
||||
new_row = pd.Series([0]*len(self.actions), index=self.q_table.columns, name=state)
|
||||
self.q_table = pd.concat([self.q_table, pd.DataFrame(new_row).T])
|
0
TankMan/ml/__init__.py
Normal file
0
TankMan/ml/__init__.py
Normal file
70
TankMan/ml/ml_play.py
Normal file
70
TankMan/ml/ml_play.py
Normal file
@ -0,0 +1,70 @@
|
||||
"""
|
||||
The template of the main script of the machine learning process
|
||||
"""
|
||||
import random
|
||||
import pygame
|
||||
|
||||
from src.env import IS_DEBUG
|
||||
|
||||
|
||||
class MLPlay:
|
||||
def __init__(self, ai_name, *args, **kwargs):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@param side A string "1P" or "2P" indicates that the `MLPlay` is used by
|
||||
which side.
|
||||
"""
|
||||
self.side = ai_name
|
||||
print(f"Initial Game {ai_name} ml script")
|
||||
self.time = 0
|
||||
|
||||
def update(self, scene_info: dict, keyboard=[], *args, **kwargs):
|
||||
"""
|
||||
Generate the command according to the received scene information
|
||||
"""
|
||||
# print(keyboard)
|
||||
|
||||
if scene_info["status"] != "GAME_ALIVE":
|
||||
# print(scene_info)
|
||||
return "RESET"
|
||||
move_act = random.randrange(5)
|
||||
aim_act = random.randrange(3)
|
||||
shoot_cd = random.randrange(15, 31)
|
||||
|
||||
is_shoot = 0
|
||||
if scene_info["used_frame"] % shoot_cd == 0:
|
||||
is_shoot = random.randrange(2)
|
||||
|
||||
command = []
|
||||
if move_act == 1:
|
||||
command.append("TURN_RIGHT")
|
||||
elif move_act == 2:
|
||||
command.append("TURN_LEFT")
|
||||
elif move_act == 3:
|
||||
command.append("FORWARD")
|
||||
elif move_act == 4:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if aim_act == 1:
|
||||
command.append("AIM_LEFT")
|
||||
elif aim_act == 2:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if is_shoot and not IS_DEBUG:
|
||||
command.append("SHOOT")
|
||||
|
||||
if self.side == "1P":
|
||||
if pygame.K_b in keyboard:
|
||||
command.append("DEBUG")
|
||||
|
||||
if not command:
|
||||
command.append("NONE")
|
||||
|
||||
return command
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the status
|
||||
"""
|
||||
print(f"reset Game {self.side}")
|
79
TankMan/ml/ml_play_manual.py
Normal file
79
TankMan/ml/ml_play_manual.py
Normal file
@ -0,0 +1,79 @@
|
||||
"""
|
||||
The template of the main script of the machine learning process
|
||||
"""
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
class MLPlay:
|
||||
def __init__(self, ai_name, *args, **kwargs):
|
||||
"""
|
||||
Constructor
|
||||
|
||||
@param ai_name A string "1P" or "2P" indicates that the `MLPlay` is used by
|
||||
which side.
|
||||
"""
|
||||
self.side = ai_name
|
||||
print(f"Initial Game {ai_name} ml script")
|
||||
self.time = 0
|
||||
|
||||
def update(self, scene_info: dict, keyboard=[], *args, **kwargs):
|
||||
"""
|
||||
Generate the command according to the received scene information
|
||||
"""
|
||||
if scene_info["status"] != "GAME_ALIVE":
|
||||
# print(scene_info)
|
||||
return "RESET"
|
||||
command = []
|
||||
if self.side == "1P":
|
||||
print(scene_info["oil"])
|
||||
if pygame.K_RIGHT in keyboard:
|
||||
command.append("TURN_RIGHT")
|
||||
elif pygame.K_LEFT in keyboard:
|
||||
command.append("TURN_LEFT")
|
||||
elif pygame.K_UP in keyboard:
|
||||
command.append("FORWARD")
|
||||
elif pygame.K_DOWN in keyboard:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if pygame.K_z in keyboard:
|
||||
command.append("AIM_LEFT")
|
||||
elif pygame.K_x in keyboard:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if pygame.K_m in keyboard:
|
||||
command.append("SHOOT")
|
||||
# debug
|
||||
if pygame.K_b in keyboard:
|
||||
command.append("DEBUG")
|
||||
# paused
|
||||
if pygame.K_t in keyboard:
|
||||
command.append("PAUSED")
|
||||
elif self.side == "2P":
|
||||
if pygame.K_d in keyboard:
|
||||
command.append("TURN_RIGHT")
|
||||
elif pygame.K_a in keyboard:
|
||||
command.append("TURN_LEFT")
|
||||
elif pygame.K_w in keyboard:
|
||||
command.append("FORWARD")
|
||||
elif pygame.K_s in keyboard:
|
||||
command.append("BACKWARD")
|
||||
|
||||
if pygame.K_q in keyboard:
|
||||
command.append("AIM_LEFT")
|
||||
elif pygame.K_e in keyboard:
|
||||
command.append("AIM_RIGHT")
|
||||
|
||||
if pygame.K_f in keyboard:
|
||||
command.append("SHOOT")
|
||||
|
||||
if not command:
|
||||
command.append("NONE")
|
||||
|
||||
return command
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset the status
|
||||
"""
|
||||
print(f"reset Game {self.side}")
|
Reference in New Issue
Block a user