def __init__(self, distribution, label):
"""
Creates a new state that contains a distribution of transitions
to other states with a specified label.
:param distribution: an iterable of state - transition probability pairs
:param label: the label of the state
"""
self.prob = {
d[0]: Fraction(d[1]) for d in distribution \
if 0 < Fraction(d[1]) <= 1
}
self.cum_prob = list(accumulate(v for v in self.prob.values()))
if self.cum_prob[-1] != 1:
raise ValueError("Transitions from state " + str(label) +
" do not form a probability distribution")
self.label = label
评论列表
文章目录