-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplay.py
69 lines (58 loc) · 1.98 KB
/
play.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
61
62
63
64
65
66
67
68
69
import gym
import numpy as np
import utils
from config import args
from models import create_model
def game_config(args):
if args.game == 'pong':
actions = [0, 2, 3]
meanings = ['NOOP', 'UP', 'DOWN']
enviroment = gym.make('Pong-v0')
elif args.game == 'breakout':
actions = [0, 1, 2, 3]
meanings = ['NOOP', 'FIRE', 'RIGTH', 'LEFT']
enviroment = gym.make('Breakout-v0')
elif args.game == 'space-invaders':
actions = [0, 1, 2, 3]
meanings = ['NOOP', 'FIRE', 'RIGTH', 'LEFT']
enviroment = gym.make('SpaceInvaders-v0')
else:
raise Exception('Unknown game')
shape = enviroment.observation_space.shape
screen = args.nb_frame_state, shape[0]//2, shape[1]//2
return {
'actions': actions,
'meanings': meanings,
'enviroment': enviroment,
'state_shape': screen,
'preprocessing': utils.preprocessing,
}
def play(args, game_config):
state_shape = game_config['state_shape']
env = game_config['enviroment']
preprocessing = game_config['preprocessing']
actions = game_config['actions']
# Initialize action value function with random with random weights
model = create_model(args, game_config)
# keep track variables
t = 0
epsilon = 0.05
done = False
obs = np.zeros(state_shape, dtype=np.int8)
while not done:
if (t % args.frame_skip) == 0:
if np.random.rand() < epsilon:
action_idx = np.random.randint(low=0, high=len(actions))
else:
qval = model.predict(np.array([obs]), verbose=0)
action_idx = qval.argmax()
ob, reward, done, info = env.step(actions[action_idx])
if (t % args.frame_skip) == 0:
# update state
obs[1:] = obs[:-1]
obs[0] = preprocessing(ob)
t += 1
if args.render: env.render()
if __name__ == '__main__':
game_config = game_config(args)
play(args, game_config)