-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract.py
213 lines (171 loc) · 6.49 KB
/
abstract.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from abc import ABC, abstractmethod, abstractproperty
from matplotlib import pyplot as plt
from numpy import inf
from random import choice, random
class Algorithm(ABC):
"""Algorithm - abstraction for reinforced learning algorithms."""
def __init__(self, environment, lambd: float, epsilon: float, gamma: float,
alpha: float, *args, **kwargs):
self.environment = environment
self.actions = list(range(len(self.environment.actions)))
self.lambd = lambd
self.epsilon = epsilon
self.gamma = gamma
self.alpha = alpha
self.steps_per_episode = []
@abstractmethod
def get_greedy_actions(self, environment_state) -> list:
pass
@abstractmethod
def run_learning_episode(self, render=False):
pass
@property
def episodes(self):
return len(self.steps_per_episode)
@property
def name(self) -> str:
if self.lambd > 0.0:
return self.__class__.__name__ + '(lambda)'
return self.__class__.__name__ + '(0)'
def get_action(self, epsilon_greedy=True) -> int:
if epsilon_greedy and random() < self.epsilon:
return choice(self.actions)
else:
return choice(self.get_greedy_actions())
def learn(self, n_episodes=1, stop_when_learned=False, spe_lte=0,
spe_gte=inf, wsize=1, print_status=True, render=False):
for i in range(n_episodes):
self.environment.clear()
if print_status:
print(f"environment: {self.environment.__class__.__name__}\n" +
f"algorithm: {self.name}\n" +
f"episode: {self.episodes + 1}\n")
self.run_learning_episode(render=render)
self.steps_per_episode.append(len(self.environment.steps))
if stop_when_learned and self.is_learned(spe_lte, spe_gte, wsize):
break
return self.steps_per_episode, self.environment
def is_learned(self, steps_per_episode_lte=0, steps_per_episode_gte=inf,
window_size=1):
if len(self.steps_per_episode) < window_size:
return False
return all([
steps_per_episode_gte <= n_steps or n_steps <= steps_per_episode_lte
for n_steps in self.steps_per_episode[-window_size:]
])
class Approximator(ABC):
"""Approximator - state approximator for environments with continuous
state variables."""
def __init__(self, n_state_variables: int, state_variables_ranges: list,
*args, **kwargs):
self.n_state_variables = n_state_variables
self.state_variables_ranges = state_variables_ranges
@abstractproperty
def possible_states(self):
pass
@abstractmethod
def approximate_state(self, observation: tuple):
pass
@property
def n_state_variables(self):
return self._n_state_variables
@n_state_variables.setter
def n_state_variables(self, value: int):
if value <= 0:
raise ValueError("cannot approximate when there's no variables" +
" to approximate")
self._n_state_variables = value
@property
def state_variables_ranges(self):
return self._state_variables_ranges
@state_variables_ranges.setter
def state_variables_ranges(self, value: list):
if len(value) != self.n_state_variables or \
any(map(lambda r: not isinstance(r, list), value)):
raise ValueError("You must specify ranges list for each of state" +
" variables. Use empty list, if you don't want" +
" to take into account specific state variable")
self._state_variables_ranges = value
class Model(ABC):
"""Model - abstraction of object (or set of objects) being a base of
environment for training AI algorithms."""
def __init__(self, timestep=0.01, *args, **kwargs):
self.timestep = timestep
self.viewer = None
def close(self):
if self.viewer is not None:
plt.close(self.viewer)
self.viewer = None
@abstractproperty
def observation(self) -> tuple:
pass
@abstractmethod
def render(self):
pass
@abstractmethod
def reset(self):
pass
@abstractmethod
def step(self, control: float or None) -> tuple:
pass
class Environment(ABC):
"""Environment - abstraction of ready-to-use learning environment"""
model = None
state_variables_ranges = []
max_steps = 100000
def __init__(self, max_steps=None, state_variables_ranges=None,
*args, **kwargs):
self.approximator = None
self.model = self.model(*args, **kwargs)
self.max_steps = max_steps or self.max_steps
self.state_variables_ranges = state_variables_ranges or \
self.state_variables_ranges
self.clear()
@abstractproperty
def actions(self) -> list:
pass
@abstractproperty
def reward(self) -> float:
pass
@abstractmethod
def is_state_absorbing(self) -> bool:
pass
@property
def done(self) -> bool:
return self.is_state_absorbing() or len(self.steps) >= self.max_steps
@property
def name(self) -> str:
return self.__class__.__name__
@property
def states(self):
if self.approximator is not None:
return self.approximator.possible_states
raise AttributeError("It's impossible to specify list of possible" +
" states for continuous non-approximated" +
" environment")
def approximate_with(self, approximator, *args, **kwargs):
self.approximator = approximator(
len(self.model.observation),
self.state_variables_ranges,
*args, **kwargs
)
self.state = self.get_state()
return self
def clear(self):
self.model.reset()
self.state = self.get_state()
self.steps = []
def close(self):
self.model.close()
def do_action(self, action_index):
action = self.actions[action_index]
self.model.step(action)
self.state = self.get_state()
self.steps.append(self.state)
return self.state
def get_state(self):
if self.approximator is not None:
return self.approximator.approximate_state(self.model.observation)
return self.model.observation
def render(self):
self.model.render()