-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdp.py
68 lines (53 loc) · 1.9 KB
/
mdp.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
#!/usr/bin/env python3
################################################################
# MDP.PY
#
# Specifies all functionality needed for the MDP. Any agent should
# only call functions specified in this file.
################################################################
import numpy as np
class MDPState:
"""A single state within the MDP."""
@property
def i(self):
"""Returns a unique index associated with each state (useful for q table)."""
pass
class MDP:
"""MDP specification and simulator."""
@property
def actions(self):
"""Return iterable of all actions."""
raise NotImplementedError
def actions_at(self, state):
"""Return iterable of all actions at given state."""
raise NotImplementedError
@property
def initial_state(self):
"""Returns initial state (assumed determinstic)."""
raise NotImplementedError
def r(self, s1, s2):
"""Returns the reward for transitioning from s1 to s2. For now, assume it is deterministic."""
raise NotImplementedError
def is_terminal(self, state):
"""Returns true if state s is terminal."""
raise NotImplementedError
def act(self, state, action):
"""Observe a single MDP transition."""
raise NotImplementedError
class FiniteStateMDP:
"""MDP with finite states."""
@property
def num_states(self):
"""Returns the number of states."""
raise NotImplementedError
@property
def states(self):
"""Return iterable of all states. (if discrete)"""
raise NotImplementedError
def p(self, state, action):
"""Return an iterable of state-probability pairs when performing action in state."""
raise NotImplementedError
def act(self, state, action):
x, p = zip(*self.p(state, action))
s2 = np.random.choice(x, p=p)
return s2, self.r(state, s2)