7

I'm getting the error below when I run my program, which has the function defined below in it. I think it's the

valid_actions = filter(lambda x: x != random.choice(maxQactions)

part that's causing the error. Does anyone see what the issue is, or suggest how to fix it? Thanks.

Error:

choose_action
    action = random.choice(valid_actions)
  File "/Users/UserName/anaconda/lib/python2.7/random.py", line 275, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range

Code:

def choose_action(self, state):


        self.state = state
        self.next_waypoint = self.planner.next_waypoint()

        action_selections = self.Q[state]

        maxQ = max(action_selections.items(), key=lambda x: x[1])[1]

        maxQactions = []
        for action, Q in self.Q[state].items():
            if Q == maxQ:
                maxQactions.append(action)


        if self.learning:
            choose_using_epsilon  = random.random() < 1 - self.epsilon
            if not choose_using_epsilon:
                valid_actions = filter(lambda x: x != random.choice(maxQactions), 
                    Environment.valid_actions)
                action = random.choice(valid_actions)
            else:
                action = random.choice(maxQactions) #maxQaction
        else:
            action = random.choice(Environment.valid_actions)
        return action
user3476463
  • 3,967
  • 22
  • 57
  • 117

1 Answers1

4

Refer to https://docs.python.org/2/library/random.html

random.choice(seq) raises IndexError if the seq is empty. In your case, IndexError is occured at

'action = random.choice(valid_actions)'

I doubt if valid_actions is empty.

Prabhakar Mishra
  • 216
  • 2
  • 10