-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pong_ai.py
61 lines (54 loc) · 1.92 KB
/
test_pong_ai.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
This is an example on how to use the two player Wimblepong environment with one
agent and the SimpleAI
"""
import matplotlib.pyplot as plt
from random import randint
import pickle
import gym
import numpy as np
import argparse
import wimblepong
parser = argparse.ArgumentParser()
parser.add_argument("--headless", action="store_true", help="Run in headless mode")
parser.add_argument("--housekeeping", action="store_true", help="Plot, player and ball positions and velocities at the end of each episode")
parser.add_argument("--fps", type=int, help="FPS for rendering", default=30)
parser.add_argument("--scale", type=int, help="Scale of the rendered game", default=1)
args = parser.parse_args()
# Make the environment
env = gym.make("WimblepongSimpleAI-v0")
env.unwrapped.scale = args.scale
env.unwrapped.fps = args.fps
# Number of episodes/games to play
episodes = 100000
# Define the player
player_id = 1
# Set up the player here. We used the SimpleAI that does not take actions for now
player = wimblepong.SimpleAi(env, player_id)
# Housekeeping
states = []
win1 = 0
for i in range(0,episodes):
done = False
while not done:
# action1 is zero because in this example no agent is playing as player 0
action1 = 0 #player.get_action()
ob1, rew1, done, info = env.step(action1)
if args.housekeeping:
states.append(ob1)
# Count the wins
if rew1 == 10:
win1 += 1
if not args.headless:
env.render()
if done:
observation= env.reset()
plt.close() # Hides game window
if args.housekeeping:
plt.plot(states)
plt.legend(["Player", "Opponent", "Ball X", "Ball Y", "Ball vx", "Ball vy"])
plt.show()
states.clear()
print("episode {} over. Broken WR: {:.3f}".format(i, win1/(i+1)))
if i % 5 == 4:
env.switch_sides()