mirror of
https://github.com/linyiLYi/street-fighter-ai.git
synced 2025-04-04 23:20:43 +00:00
add recording function to the script
This commit is contained in:
parent
a3ede7dd30
commit
f09e69d05c
3
.gitignore
vendored
3
.gitignore
vendored
@ -6,3 +6,6 @@ archives/
|
|||||||
images/
|
images/
|
||||||
data/
|
data/
|
||||||
main/logs/monitoring/
|
main/logs/monitoring/
|
||||||
|
recordings/
|
||||||
|
|
||||||
|
007*
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
28
main/test.py
28
main/test.py
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import retro
|
import retro
|
||||||
@ -7,9 +8,13 @@ from street_fighter_custom_wrapper import StreetFighterCustomWrapper
|
|||||||
|
|
||||||
RESET_ROUND = False # Reset the round when fight is over.
|
RESET_ROUND = False # Reset the round when fight is over.
|
||||||
RENDERING = True
|
RENDERING = True
|
||||||
|
RECORDING = True
|
||||||
|
|
||||||
RANDOM_ACTION = False
|
RANDOM_ACTION = False
|
||||||
MODEL_PATH = r"trained_models/ppo_ryu_7000000_steps"
|
MODEL_DIR = r"trained_models/"
|
||||||
|
MOVIE_DIR = r"recordings"
|
||||||
|
|
||||||
|
MODEL_NAME = r"ppo_ryu_7000000_steps"
|
||||||
|
|
||||||
def make_env(game, state):
|
def make_env(game, state):
|
||||||
def _init():
|
def _init():
|
||||||
@ -28,8 +33,7 @@ env = make_env(game, state="Champion.Level12.RyuVsBison")()
|
|||||||
# model = PPO("CnnPolicy", env)
|
# model = PPO("CnnPolicy", env)
|
||||||
|
|
||||||
if not RANDOM_ACTION:
|
if not RANDOM_ACTION:
|
||||||
# model.load(MODEL_PATH)
|
model = PPO.load(os.path.join(MODEL_DIR, MODEL_NAME), env=env)
|
||||||
model = PPO.load(MODEL_PATH, env=env)
|
|
||||||
|
|
||||||
# obs = env.reset()
|
# obs = env.reset()
|
||||||
done = False
|
done = False
|
||||||
@ -40,6 +44,13 @@ num_victory = 0
|
|||||||
for _ in range(num_episodes):
|
for _ in range(num_episodes):
|
||||||
done = False
|
done = False
|
||||||
obs = env.reset()
|
obs = env.reset()
|
||||||
|
|
||||||
|
if RECORDING:
|
||||||
|
# Start recording
|
||||||
|
movie_path = os.path.join(MOVIE_DIR, "{}.bk2".format(MODEL_NAME))
|
||||||
|
env.unwrapped.movie = retro.Movie(movie_path, retro.MovieMode.RECORD)
|
||||||
|
env.unwrapped.movie.step()
|
||||||
|
|
||||||
total_reward = 0
|
total_reward = 0
|
||||||
|
|
||||||
while not done:
|
while not done:
|
||||||
@ -51,10 +62,19 @@ for _ in range(num_episodes):
|
|||||||
action, _states = model.predict(obs)
|
action, _states = model.predict(obs)
|
||||||
obs, reward, done, info = env.step(action)
|
obs, reward, done, info = env.step(action)
|
||||||
|
|
||||||
|
if RECORDING:
|
||||||
|
# Record the step
|
||||||
|
env.unwrapped.movie.step()
|
||||||
|
|
||||||
if reward != 0:
|
if reward != 0:
|
||||||
total_reward += reward
|
total_reward += reward
|
||||||
print("Reward: {:.3f}, playerHP: {}, enemyHP:{}".format(reward, info['agent_hp'], info['enemy_hp']))
|
print("Reward: {:.3f}, playerHP: {}, enemyHP:{}".format(reward, info['agent_hp'], info['enemy_hp']))
|
||||||
|
|
||||||
|
if RECORDING:
|
||||||
|
# Stop recording
|
||||||
|
env.unwrapped.movie.close()
|
||||||
|
del env.unwrapped.movie
|
||||||
|
|
||||||
if info['enemy_hp'] < 0:
|
if info['enemy_hp'] < 0:
|
||||||
print("Victory!")
|
print("Victory!")
|
||||||
num_victory += 1
|
num_victory += 1
|
||||||
@ -66,4 +86,4 @@ print("Winning rate: {}".format(1.0 * num_victory / num_episodes))
|
|||||||
if RANDOM_ACTION:
|
if RANDOM_ACTION:
|
||||||
print("Average reward for random action: {}".format(episode_reward_sum/num_episodes))
|
print("Average reward for random action: {}".format(episode_reward_sum/num_episodes))
|
||||||
else:
|
else:
|
||||||
print("Average reward for {}: {}".format(MODEL_PATH, episode_reward_sum/num_episodes))
|
print("Average reward for {}: {}".format(MODEL_NAME, episode_reward_sum/num_episodes))
|
Loading…
Reference in New Issue
Block a user