typeerror - "UnboundLocalError: local variable 'reward_n' referenced before assingment" In OpenAI Python Scripts -


i stuck on error after following tutorial online explaining concepts of writing openai scripts in python. complete error is:

typeerror: local variable 'reward_n' referenced before assignment 

this current code:

import gym import random  #reinforcement learning step def determine_turn(turn, observation_n, j, total_sum, prev_total_sum, reward_n):     #for every 15 iterations, sum total observations, , take average     #if lower 0, change direction     #if go 15+ iterations , reward each step, we're doing right     #thats when turn     if(j >= 15):         if(total_sum/ j ) == 0:             turn = true         else:             turn = false          #reset vars         total_sum = 0         j = 0         prev_total_sum = total_sum         total_sum = 0      else:         turn = false     if(observation_n != none):         #increment counter , reward sum         j+=1         total_sum += reward_n     return(turn, j, total_sum, prev_total_sum)  def main():      #init environment     env = gym.make('cartpole-v0')     observation_n = env.reset()      #init variables     # num of game iterations     n = 0     j = 0     #sum of observations     total_sum = 0     prev_total_sum = 0     turn = false      #define our turns or keyboard actions     left = 0     right = 1     forward = env.action_space.sample()      #main logic     while true:         #increment counter number of iterations         n+=1          #if @ least 1 iteration made, check if turn needed         if(n > 1):             #if @ least 1 iteration, check if turn             if(observation_n[0] != none):                 #store reward in previous score                 prev_score = reward_n[0]                  #should turn?                 if(turn):                     #pick random event                     #where turn?                      event = random.choice([left,right])                     #perform action                     action_n = [event ob in observation_n]                     #set turn false                     turn = false          elif(~turn):             #if no turn needed, go straight             action_n = [forward ob in observation_n]           #if there obseravtion, game has started, check if turn needed         if(observation_n[0] != none):             turn, j, total_sum, prev_total_sum = determine_turn(turn, observation_n[0], j, total_sum, prev_total_sum, reward_n[0])          #save new variables each iteration         observation_n, reward_n, done_n, info = env.step(action_n)          env.render()  if __name__ == '__main__':     main()   

the thing not error variables in main() or in determine turn gets variables main(). far knowledge of error , appreciate help.

#if @ least 1 iteration made, check if turn needed     if(n > 1):         #if @ least 1 iteration, check if turn         if(observation_n[0] != none):             #store reward in previous score             prev_score = reward_n[0] 

in part of code trying access reward_n[0], variable has not initialized yet, so, need initialize before first call same way did this:

observation_n = env.reset() 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -