<jupyter_start><jupyter_text>No inicio o algoritimo começará com um prêmio maior para testar (exploration), e depois terá uma queda exponencial e o foco será melhorar a pontuação que ele já consegue fazer (exploitation) Treinamento do algoritimo de Q-Learning<jupyter_code># 2 For life or until learning is stopped
for episode in range(total_episodes):
    # Reset the environment
    state = env.reset()
    step = 0
    done = False
    
    for step in range(max_steps):
        # 3. Choose an action a in the current world state (s)
        ## First we randomize a number
        exp_exp_tradeoff = random.uniform(0,1)
        
        ## If this number > greater than epsilon --> exploitation (taking the biggest Q value for this state)
        if exp_exp_tradeoff > epsilon:
            action = np.argmax(qtable[state,:])
        
        # Else doing a random choice --> exploration
        else:
            action = env.action_space.sample()
        
        # Take the action (a) and observe the outcome state(s') and reward (r)
        new_state, reward, done, info = env.step(action)
        # Q-table (“Q” for “quality” of the action)
        # The Action Value Function (or “Q-function”) takes two inputs: “state” and “action.” 
        # It returns the expected future reward of that action at that state.
        # Atualizando a função de valor da ação
        
        qtable[state, action] = qtable[state, action] + learning_rate * (reward + gamma * 
                                    np.max(qtable[new_state, :]) - qtable[state, action])
        
        # New Q value = Current Q value + lr * [Reward + discount_rate * 
        # (highest Q value between possible actions from the new state s’ ) — Current Q value ]
                
            
        # Our new state is state
        state = new_state
        
        # If done : finish episode
        if done == True: 
            break
    
    # Reduce epsilon (because we need less and less exploration)
    epsilon = min_epsilon + (max_epsilon - min_epsilon)*np.exp(-decay_rate*episode)
env.step(action)

env.reset()

rewards = []

for episode in range(total_test_episodes):
    state = env.reset()
    step = 0
    done = False
    total_rewards = 0
    print("*********" * 2)
    print("EPISODE ", episode)

    for step in range(max_steps):
        # UNCOMMENT IT IF YOU WANT TO SEE OUR AGENT PLAYING
        env.render()
        # Take the action (index) that have the maximum expected future reward given that state
        action = np.argmax(qtable[state,:])
        
        new_state, reward, done, info = env.step(action)
        
        total_rewards += reward
        
        if done:
            rewards.append(total_rewards)
            #print ("Score", total_rewards)
            break
        state = new_state
        
env.close()

print ("Score over time: " +  str(sum(rewards)/total_test_episodes))<jupyter_output>******************
EPISODE  0
+---------+
|[35mR[0m: | : :[34;1mG[0m|
| : : : : |
|[43m [0m: : : : |
| | : | : |
|Y| : |B: |
+---------+

+---------+
|[35mR[0m: | : :[34;1mG[0m|
|[43m [0m: : : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (North)
+---------+
|[35mR[0m: | : :[34;1mG[0m|
| :[43m [0m: : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (East)
+---------+
|[35mR[0m: | : :[34;1mG[0m|
| : :[43m [0m: : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (East)
+---------+
|[35mR[0m: |[43m [0m: :[34;1mG[0m|
| : : : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (North)
+---------+
|[35mR[0m: | :[43m [0m:[34;1mG[0m|
| : : : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (East)
+---------+
|[35mR[0m: | : :[34;1m[43mG[0m[0m|
| : : : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (East)
+---------+
|[35mR[0m: | : :[42mG[0m|
| : : : : |
| : : : : |
| | : | : |
|Y| : |B: |
+---------+
  (Pickup)
+--------[...]
{'fraction_non_alphanumeric': 0.20754716981132076, 'fraction_numerical': 0.021672616012238653, 'mean_word_length': 1.9674735249621784, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '532564', 'license': 'MIT', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1421, 'n_words': 338, 'path': 'OpenAI-Taxi.ipynb', 'repo_name': 'kaburelabs/Reinforced-Learning-Projects', 'size': '3922'}