aimacode / aima-python

Python implementation of algorithms from Russell And Norvig's "Artificial Intelligence - A Modern Approach"
MIT License
7.79k stars 3.65k forks source link

MDP actions handles dict and list, but not set #1259

Open ptoche opened 1 year ago

ptoche commented 1 year ago

In mdp4e.py, there is the following:

        if isinstance(actlist, list):
            # if actlist is a list, all states have the same actions
            self.actlist = actlist

        elif isinstance(actlist, dict):
            # if actlist is a dict, different actions for each state
            self.actlist = actlist

In test_mdp4e.py, there is this:

actlist={"plan1", "plan2", "plan3"}

This is a set, not a dictionary, and is not handled by the code above. And Perhaps an error handling was intended after the elif?

Moreover (unrelatedly) I found the notation actlist confusing (since the object can be a dictionary or a set) and inconsistent with the textbook notation. I suggest replacing every instance of actions with A (consistent with the Rand T notation) and every instance of actlist with actions. Below I think is closer to the intention of the conditional above:


        if isinstance(actions, list):
            # if actions is a list, all states have the same actions
            self.actions = actions

        elif isinstance(actions, dict):
            # if actions is a dict, different actions for each state
            self.actions = {k: v for k,v in actions.items()}

        elif isinstance(actions, set):  # This line added
            # if actions is a set, convert to list
            self.actions = list(actions)

        else:
        # throw an error