Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when an item in pykov.Chain is None #21

Open
dvircohen opened this issue May 3, 2016 · 1 comment
Open

Error when an item in pykov.Chain is None #21

dvircohen opened this issue May 3, 2016 · 1 comment

Comments

@dvircohen
Copy link

Hey,
first of all this is my first time using github and also i'm very new to python so be gentle with me..
I have a pykov.Chain where one of the items is None and when i'm trying to use the function chain.move(None) i get the following error:
AttributeError: 'OrderedDict' object has no attribute 'choose'
to fix it i did the following changes in the succ function (i only use positive numbers in my project):
from:

def succ(self, key=None):
        try:
            if key is not None:
                return self._succ[key]
            else:
                return self._succ
        except AttributeError:
            self._succ = OrderedDict([(state, Vector()) for state in self.states()])
            for link, probability in six.iteritems(self):
                self._succ[link[0]][link[1]] = probability
            if key is not None:
                return self._succ[key]
            else:
                return self._succ

to:

def succ(self, key=-1): 
        try:
            if key is not -1:
                return self._succ[key]
            else:
                return self._succ
        except AttributeError:
            self._succ = OrderedDict([(state, Vector()) for state in self.states()])
            for link, probability in six.iteritems(self):
                self._succ[link[0]][link[1]] = probability
            if key is not -1:
                return self._succ[key]
            else:
                return self._succ

My solution works for me, but is there a better way?

@riccardoscalco
Copy link
Owner

Hi Dvir, your solution works for you, but it is not suitable for a generic use case.
Think about this case for example:

T = pykov.Chain({(1,-1): .3, (1,1): .7, (-1,1): 1.})

My suggestion is to replace None with a defined value, maybe 'None'. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants