I'm trying to create a stochastic environment for a custom RL algorithm the purpose of this code is to take an ordered dictionary (example: OrderedDict([(0,1),(1,0), (2,0),(3,0)]) first number in tuples is indx second is probability) and return the new state at random weighted by the probability that the state occurred as defined in the ordered dictionary (in the example above there is a 100% chance that it enters state 0)
The problem I am having is that for some reason when indx is 0( for the above example input), probability is also 0. I expected probability to be 1.
in this same case pcloud[0] == 1 which is what I want. which means that there is something I am mistaken about in how I use enumerate but I don't know what it is.
def collapse(pcloud):
randomnum = Random.uniform(0,1)
threshold = 0
for indx , probability in enumerate(pcloud):
threshold += probability
if randomnum <= threshold:
return indx
raise ValueError("For some reason the probabilities can't be compared with the <= operator.")
#it should never get here.
return
to run the code create an ordered dictionary.
from collections import OrderedDict
import random as Random
#all probabilities should sum to 1
pcloud = OrderedDict()
pcloud[0] = 1
pcloud[1] = 0
pcloud[2] = 0
pcloud[3] = 0
#then run the function
print collapse(pcloud)
You shouldn't be using enumerate
just because there are indices involved. The indices are already present as your OrderedDict's keys. You need to iterate through your OrderedDict's key-value pairs, so you should be iterating over its items()
, or iteritems()
on Python 2 (since items()
builds an unnecessary list on Python 2):
for index, probability in pcloud.items():
...