-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_extractor.py
87 lines (68 loc) · 2.7 KB
/
feature_extractor.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'''
Feature extractor to get features from an environment.
Currently only works with tabular MDPs.
author: [email protected]
'''
import numpy as np
from environment import TabularMDP
#-------------------------------------------------------------------------------
class FeatureExtract(object):
'''Get features out of an environment'''
def __init__(self, epLen, nState, nAction, nFeat):
self.epLen = epLen
self.nState = nState
self.nAction = nAction
self.nFeat = nFeat
def get_feat(self, env):
'''Get the features out of the environment'''
def check_env(self, env):
''' Check if a feature extractor is compatible with an environment '''
assert(self.epLen == env.epLen)
assert(self.nState == env.nState)
assert(self.nAction == env.nAction)
def check_agent(self, agent):
''' Check if a feature extractor is compatible with an environment '''
assert(self.epLen == agent.epLen)
assert(self.nFeat == agent.nFeat)
assert(self.nAction == agent.nAction)
#----------------------------------------------------------------------------
class FeatureTrueState(FeatureExtract):
'''Trivial feature extractor which just gives the state'''
def get_feat(self, env):
'''
Args:
env - TabularMDP
Returns:
timestep - int - timestep within episode
state - int - state of the environment
'''
return env.timestep, env.state
#----------------------------------------------------------------------------
class LookupFeatureExtract(FeatureExtract):
'''Simple lookup phi feature extractor'''
def __init__(self, epLen, nState, nAction, nFeat):
'''Very simple implementation, lookup Phi for now'''
self.epLen = epLen
self.nState = nState
self.nAction = nAction
self.nFeat = nFeat
self.phi = np.zeros((epLen, nState, nAction, nFeat))
def get_feat(self, env):
'''
Get all the state features for an environment.
Args:
env - Tabular MDP environment
Returns:
phi(h, s, :, :) - nAction x nFeat - array of features
'''
return self.phi[env.timestep, env.state, :, :]
def check_env(self, env):
''' Check if a feature extractor is compatible with an environment '''
assert(self.epLen == env.epLen)
assert(self.nState == env.nState)
assert(self.nAction == env.nAction)
def check_agent(self, agent):
''' Check if a feature extractor is compatible with an environment '''
assert(self.epLen == agent.epLen)
assert(self.nFeat == agent.nFeat)
assert(self.nAction == agent.nAction)