-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
28 lines (22 loc) · 886 Bytes
/
agent.py
File metadata and controls
28 lines (22 loc) · 886 Bytes
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
import random
import numpy as np
class Agent:
def __init__(self, model, epsilon, ball_obs_dims, car_obs_dims):
self.model = model
self.epsilon = epsilon
self.action_space = (0, 1, 2)
self.ball_obs_dims = ball_obs_dims
self.car_obs_dims = car_obs_dims
ball = np.zeros((1, ) + self.ball_obs_dims)
car = np.zeros((1, ) + self.car_obs_dims)
self.model.predict([ball, car]) # Initializes model
def get_action(self, obs):
ball, car = obs
ball = ball.reshape((1, ) + self.ball_obs_dims)
car = car.reshape((1, ) + self.car_obs_dims)
pred = self.model.predict([ball, car])[0]
print('\r', '{:>6.2f} {:>6.2f} {:>6.2f}'.format(*pred))
if random.random() < self.epsilon:
return random.choice(self.action_space)
else:
return np.argmax(pred)