-
Notifications
You must be signed in to change notification settings - Fork 8
/
gym.py
52 lines (39 loc) · 1.36 KB
/
gym.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
import numpy as np
from .abstate import StateBuilder
class FrozenLakeState(StateBuilder):
def build_state(self, obs):
if obs is not None:
index = obs
obs = np.zeros((1, 16))
obs[0][index] = 1
return obs
else:
return None
def get_state_dim(self):
return 16
class PureState(StateBuilder):
def __init__(self, state_dim):
# you can get observation_space from gym_env.env_instance.observation_space.shape[0]
self.state_dim = state_dim
def preprocess_obs(self, obs):
if type(obs) == int:
new_obs = np.array(obs)
new_obs = new_obs.reshape(1, self.get_state_dim())
return new_obs
else:
return obs.reshape(1, self.get_state_dim())
def build_state(self, obs):
# self.preprocess_obs(obs)
obs = self.preprocess_obs(obs)
return obs
def get_state_dim(self):
return self.state_dim
class GymState(StateBuilder):
def __init__(self, state_dim):
# you can get observation_space from gym_env.env_instance.observation_space.shape
self.state_dim = state_dim
def build_state(self, obs):
state = obs[np.newaxis, :]
return state
def get_state_dim(self):
return self.state_dim