-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgent.py
More file actions
390 lines (322 loc) · 12.8 KB
/
Agent.py
File metadata and controls
390 lines (322 loc) · 12.8 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Definition of the Agent object for REAP-like implementations.
The agent possess data and parameters that allows it to score conformations or trajectories.
"""
import sys
from abc import ABC, abstractmethod
import numpy as np
from scipy.optimize import minimize
from scipy.stats import entropy
class Agent(ABC):
"""Abstract class for Agent object.
"""
def __init__(self, arg1):
pass
@abstractmethod
def set_data(self):
"""Assign data to agent.
:return:
"""
pass
@abstractmethod
def train(self):
"""Learn the parameters that the agent will use to score a structure.
:return:
"""
pass
@abstractmethod
def score(self):
"""Score a structure.
:return:
"""
pass
class AgentReap(Agent):
"""Agent class for REAP or MA REAP simulations.
"""
def __init__(self, cv_weights=None, delta=None, data=None, logs=None):
"""Constructor for AgentReap.
:param cv_weights: list[float].
Weights for collective variables.
:param delta: float in [0, 1].
Max change in collective variable weights between rounds.
:param data: list[np.ndarray].
Initial trajectory data for agent. Not necessary.
"""
if logs is None:
assert (np.allclose(sum(cv_weights), 1)) # CV weights must add up to 1
assert (0 <= delta <= 1) # delta must be between 0 and 1
self.cv_weights = cv_weights
self.delta = delta
self.cv_num = len(cv_weights)
self.data = data
self.data_concat = None
self.means = None
self.stdev = None
if self.data is not None:
self.data_concat = np.concatenate(data, axis=0)
self.means = self.data_concat.mean(axis=0)
self.stdev = self.data_concat.std(axis=0)
self.states = None
self.stakes = None
elif isinstance(logs, dict):
self._reload(logs)
def get_log_info(self):
"""Returns a dictionary with certain object attributes for logging purposes.
:return: dict.
Logs.
"""
logs = dict(
cv_weights=self.cv_weights,
delta=self.delta,
cv_num=self.cv_num,
means=self.means,
stdev=self.stdev,
states=self.states,
stakes=self.stakes,
scores=self.score()
)
return logs
def _reload(self, logs):
self.cv_weights = logs['cv_weights']
self.delta = logs['delta']
self.cv_num = logs['cv_num']
self.means = logs['means']
self.stdev = logs['stdev']
self.states = logs['states']
self.stakes = logs['stakes']
def set_data(self, data):
"""Set data (trajectories) for the agent.
:param data: list[np.ndarray].
Trajectory data.
:return: None.
"""
if self.data is None:
self.data = data
else:
self.data.extend(data)
self.data_concat = np.concatenate(self.data, axis=0)
self.means = self.data_concat.mean(axis=0)
self.stdev = self.data_concat.std(axis=0)
def set_states(self, states):
"""Set states that agent can select to restart simulations.
:param states: np.ndarray of shape (n_states, ndim).
Usually, central frames of clusters.
:return: None.
"""
self.states = states
def set_stakes(self, stakes):
"""Set agent stakes. Necessary only for multiagent runs.
:param stakes: np.ndarray.
:return: None.
"""
self.stakes = stakes
def score(self, cv_weights=None):
"""Score takes the role of the reward function. In the case of REAP, this is based on the standardized Euclidean
distance.
:return: np.ndarray of shape (n_states,).
Scores for each state.
"""
if self.stakes is None:
stakes = np.ones(len(self.states))
else:
stakes = self.stakes
if cv_weights is None:
cv_weights = self.cv_weights
epsilon = sys.float_info.epsilon
dist = np.abs(self.states - self.means[:self.cv_num])
distances = (cv_weights * dist / (self.stdev[:self.cv_num] + epsilon)).sum(axis=1)
return stakes * distances
def train(self):
"""Get new CV weights by maximizing REAP score.
:return: None.
"""
weights_prev = self.cv_weights
delta = self.delta
# Create constraints
constraints = [
# Inequality constraints (fun(x, *args) >= 0)
# This constraint makes the weights change by delta (at most)
{
'type': 'ineq',
'fun': lambda weights, weights_prev, delta: delta - np.abs((weights_prev - weights)),
'jac': lambda weights, weights_prev, delta: np.diagflat(np.sign(weights_prev - weights)),
'args': (weights_prev, delta),
},
# This constraint makes the weights be always positive
{
'type': 'ineq',
'fun': lambda weights: weights,
'jac': lambda weights: np.eye(weights.shape[0]),
},
# Equality constraints (fun(x, *args) = 0)
# This constraint makes sure the weights add up to one
{
'type': 'eq',
'fun': lambda weights: weights.sum() - 1,
'jac': lambda weights: np.ones(weights.shape[0]),
}]
def minimize_helper(x):
return -self.score(cv_weights=x).sum()
results = minimize(minimize_helper, weights_prev, method='SLSQP', constraints=constraints)
self.cv_weights = results.x
class AgentVampReap(AgentReap):
"""Agent class for VAMP + MA REAP simulations.
"""
def __init__(self, cv_weights, delta, estimator, data=None, propagation_steps=1):
"""Constructor for AgentVampReap.
:param cv_weights: list[float].
Weights for collective variables.
:param delta: float in [0, 1].
Max change in collective variable weights between rounds.
:param estimator: deeptime.decomposition._vamp.VAMP.
Estimator object for dimensionality reduction.
:param data: list[np.ndarray].
Initial trajectory data for agent. Not necessary.
:param propagation_steps: int, default = 1.
This option allows to apply the Koopman operator propagation_steps times to the input conformation.
"""
# Note that here the term CV is (inaccurately) used to refer to VAMP-reduced coordinates.
# cv_weights must have length equal to the number of coordinates we want to keep, not to the number of total
# features
AgentReap.__init__(self, cv_weights, delta, data)
self.estimator = estimator
self.propagation_steps = propagation_steps
if self.data is not None:
self.transformed_data = self.estimator.transform(self.data_concat)
def get_log_info(self):
"""Returns a dictionary with certain object attributes for logging purposes.
:return: dict.
Logs.
"""
logs = AgentReap.get_log_info(self)
logs |= dict(
estimator=self.estimator,
propagation_steps=self.propagation_steps,
)
return logs
def set_data(self, data):
"""Set data (trajectories) for the agent.
:param data: list[np.ndarray].
Trajectory data.
:return: None.
"""
AgentReap.set_data(self, data)
self.transformed_data = self.estimator.transform(self.data_concat)
self.means = self.transformed_data.mean(axis=0)
self.stdev = self.transformed_data.std(axis=0)
def set_estimator(self, estimator):
"""Set the estimator used by the agent. Needed to update the estimator once it has been fit to new data.
:param estimator: deeptime.decomposition._vamp.VAMP.
Estimator object for dimensionality reduction.
:return: None.
"""
self.estimator = estimator
def set_states(self, states):
"""Set states that agent can select to restart simulations.
:param states: np.ndarray of shape (n_states, ndim).
Usually, central frames of clusters.
:return: None.
"""
self.states = self._propagate_kinetic_model(states)
def _propagate_kinetic_model(self, frames):
"""Propagate the candidate frames using the kinetic model.
:param frames: np.ndarray of shape (n_frames, n_features).
Frames to transform applying Koopman operator.
:return: np.ndarray of shape (n_frames, ndim).
Transformed frames.
"""
model = self.estimator.fetch_model()
inst_obs = model.transform(frames)
propagated = inst_obs @ (model.operator ** self.propagation_steps)[:self.cv_num, :self.cv_num]
# propagated = model._instantaneous_whitening_backwards(propagated) --> The score is calculated in CV space
return propagated
class AgentVampNetReap(AgentVampReap):
"""Agent class for VAMPNet + MA REAP simulations.
Note that the self.estimator attribute will be a deeptime.util.torch.MLP object for this class.
"""
def _propagate_kinetic_model(self, frames):
"""Transform specified simulation frames using the learned VAMPNet.
:param frames: np.ndarray of shape (n_frames, n_features).
Features to transform.
:return: np.ndarray of shape (n_frames, ndim).
Transformed frames.
"""
model = self.estimator.fetch_model()
propagated = model.transform(frames)
return propagated
class AgentVaeReap(AgentVampNetReap):
"""Identical implementation to AgentVampNetReap with a different name.
Note that the self.estimator attribute will be a deeptime.decomposition.deep._tae.TVAE object for this class.
"""
pass
class EntropyBasedAgent(Agent):
"""Agent class for VAMPNet + MaxEnt simulations.
"""
def __init__(self, estimator=None, logs=None, data=None):
"""Constructor for EntropyBasedAgent.
:param estimator: deeptime.util.torch.MLP.
VAMPNet estimator. Note that the output non-linearity must be softmax or similar.
:param data: list[np.ndarray].
Initial trajectory data for agent. Not necessary.
"""
if logs is None:
self.estimator = estimator
self.data = data
self.data_concat = None
self.transformed_data = None
if self.data is not None:
self.data_concat = np.concatenate(data, axis=0)
self.transformed_data = estimator.model.transform(self.data_concat)
self.states = self.transformed_data
elif isinstance(logs, dict):
self._reload(logs)
def get_log_info(self):
"""Returns a dictionary with certain object attributes for logging purposes.
:return: dict.
Logs.
"""
logs = dict(
estimator=self.estimator,
scores=self.score(),
)
return logs
def _reload(self, logs):
self.estimator = logs['estimator']
# Data fields are set by self.set_data()
self.data = None
self.data_concat = None
self.transformed_data = None
def set_data(self, data):
"""Set data (trajectories) for the agent.
:param data: list[np.ndarray].
Trajectory data.
:return: None.
"""
if self.data is None:
self.data = data
else:
self.data.extend(data)
self.data_concat = np.concatenate(self.data, axis=0)
self.transformed_data = self.estimator.model.transform(self.data_concat)
def set_estimator(self, estimator):
"""Set the estimator used by the agent. Needed to update the estimator once it has been fit to new data.
:param estimator: deeptime.util.torch.MLP.
Estimator object for soft discretization.
:return: None.
"""
self.estimator = estimator
def set_states(self, states):
"""Set states that agent can select to restart simulations.
:param states: np.ndarray of shape (n_states, ndim).
Since MaxEnt does not require discretization, any conformation can be considered a state.
:return: None.
"""
self.states = self.estimator.transform(states)
def score(self):
"""Compute score for MaxEnt (Shannon entropy).
:return: np.ndarray of shape (n_states,).
Shannon entropy of the conformations considered as states, potentially all conformations.
"""
return entropy(self.states, axis=1)
def train(self):
pass