-
Notifications
You must be signed in to change notification settings - Fork 262
/
random.py
54 lines (45 loc) · 1.36 KB
/
random.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
# The random agent algorithm
# For basic dev purpose
from slm_lab.agent.algorithm.base import Algorithm
from slm_lab.lib import logger
from slm_lab.lib.decorator import lab_api
import numpy as np
logger = logger.get_logger(__name__)
class Random(Algorithm):
'''
Example Random agent that works in both discrete and continuous envs
'''
@lab_api
def init_algorithm_params(self):
'''Initialize other algorithm parameters'''
self.to_train = 0
self.training_frequency = 1
self.training_start_step = 0
@lab_api
def init_nets(self, global_nets=None):
'''Initialize the neural network from the spec'''
self.net_names = []
@lab_api
def act(self, state):
'''Random action'''
body = self.body
if body.env.is_venv:
action = np.array([body.action_space.sample() for _ in range(body.env.num_envs)])
else:
action = body.action_space.sample()
return action
@lab_api
def sample(self):
self.body.memory.sample()
batch = np.nan
return batch
@lab_api
def train(self):
self.sample()
self.body.env.clock.tick('opt_step') # to simulate metrics calc
loss = np.nan
return loss
@lab_api
def update(self):
self.body.explore_var = np.nan
return self.body.explore_var