1

My code is as follows:

environment_rows = 3  #1
look_up_tables = [np.zeros((environment_rows, 20)) for _ in range(35)]



def get_starting_look_up_table(): #2
  current_look_up_table = np.random.randint(35)
  return current_look_up_table

def get_next_action(number_of_look_up_table,current_row_index,epsilon):
  if np.random.random() < epsilon:
    result=np.argmax(look_up_tables[number_of_look_up_table][current_row_index])
  else: #choose a random action
    result=np.random.randint(20)
  return result



number_of_look_up_table = get_starting_look_up_table() #3
current_row_index = 0
epsilon = 0.9
action_index = get_next_action(current_row_index,number_of_look_up_table,epsilon)

In the first part, I produce the matrix related to look_up_tables, each of the arrays has three rows and twenty columns. We have a total of 35 of these arrays.

In the second part, using the 'get_starting_look_up_table' function, one of 35 is randomly selected. Also, by using the 'get next action' function, one of the 20 columns of the array selected by the previous function should be selected based on the largest value or randomly.

Finally, the functions are called in the third part, but I get the following error. When I run the line with the error separately, I don't have any problems. Thank you for guiding me in this regard.

IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_28528/1385508929.py in <module>
   2 current_row_index = 0
   3 
 ----> 4 action_index = get_next_action(current_row_index,number_of_look_up_table,epsilon)
   5 
   6 reward = determine_rewards(number_of_look_up_table,current_row_index,action_index)

  ~\AppData\Local\Temp/ipykernel_28528/255015173.py in 
   get_next_action(number_of_look_up_table, current_row_index, epsilon)
  9 def get_next_action(number_of_look_up_table,current_row_index,epsilon):
 10     if np.random.random() < epsilon:
---> 11      return np.argmax(look_up_tables[number_of_look_up_table][current_row_index])
    12     else: #choose a random action
    13      return np.random.randint(20)

  IndexError: index 27 is out of bounds for axis 0 with size 3
Ajay Pun Magar
  • 425
  • 2
  • 13
znb
  • 33
  • 1
  • 8
  • Welcome to StackOverflow. Please take the [tour](https://stackoverflow.com/tour) and learn [How to Ask](https://stackoverflow.com/help/how-to-ask). In order to get help, you will need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). If your question include a pandas dataframe, please provide a [reproducible pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – alec_djinn Sep 26 '22 at 10:51

1 Answers1

0

Your function call get_next_action(current_row_index, number_of_look_up_table, epsilon) has the parameters in the wrong order. You switched current_row_index and number_of_look_up_table.

I think you want this:

action_index = get_next_action(number_of_look_up_table, current_row_index, epsilon)
bitflip
  • 3,436
  • 1
  • 3
  • 22