-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathengine.py
More file actions
81 lines (72 loc) · 2.87 KB
/
Copy pathengine.py
File metadata and controls
81 lines (72 loc) · 2.87 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
import numpy as np
import sys, traceback
class MoveEngine(object):
"""MoveEngine provides perturbation functions for the context during the NCMC
simulation.
Ex.
from blues.ncmc import MoveEngine
probabilities = [0.25, 0.75]
#Where move is a list of two Move objects
mover = MoveEngine(move, probabilities)
#Get the dictionary of proposed moves
mover.moves
"""
def __init__(self, moves, probabilities=None):
"""Initialize the MovePropsal object that contains functions to perturb
the context in the NCMC simulation.
Parameters
----------
moves : blues.ncmc.Model object or list of n blues.ncmc.Model-like objects
Specifies the possible moves to be performed.
probabilities: list of floats, optional, default=None
A list of n probabilities,
where probabilities[i] corresponds to the probaility of moves[i]
being selected to perform its associated move() method.
If None, uniform probabilities are assigned.
"""
#make a list from moves if not a list
if isinstance(moves,list):
self.moves = moves
else:
self.moves = [moves]
#normalize probabilities
if probabilities is None:
single_prob = 1. / len(self.moves)
self.probs = [single_prob for x in (self.moves)]
else:
prob_sum = float(sum(probabilities))
self.probs = [x/prob_sum for x in probabilities]
#if move and probabilitiy lists are different lengths throw error
if len(self.moves) != len(self.probs):
print('moves and probability list lengths need to match')
raise IndexError
#use index in selecting move
self.selected_move = None
def selectMove(self):
"""chooses the move which will be selected for a given NCMC
iteration
"""
rand_num = np.random.choice(len(self.probs), p=self.probs)
self.selected_move = rand_num
def runEngine(self, context):
"""Selects a random Move object based on its
assigned probability and and performs its move() function
on a context.
Parameters
----------
context : openmm.context object
OpenMM context whose positions should be moved.
"""
try:
new_context = self.moves[self.selected_move].move(context)
except Exception as e:
#In case the move isn't properly implemented, print out useful info
print('Error: move not implemented correctly, printing traceback:')
ex_type, ex, tb = sys.exc_info()
traceback.print_tb(tb)
print(e)
raise SystemExit
return new_context
def resetAcceptanceRatios(self):
for move in self.moves:
move.acceptance_ratio = 1.0