"""
Simple Reinforcement Learning agent resolving a deterministic problem
"""
import numpy as np
import pandas as pd
import time


np.random.seed(2)  # reproducible results

# Environment config
N_STATES = 6  # length of the one-dimensional world
ACTIONS = ['left', 'right']  # available actions
FRESH_TIME = 0.3

# Hyperparameters
epsilon = 0.9  # greedy policy
alpha = 0.1  # learning rate
gamma = 0.9  # discount rate
MAX_EPISODES = 10  # maximum episodes in a episode


def create_q_table(n_states, actions):
    table = pd.DataFrame(
        np.zeros((n_states, len(actions))),
        columns=actions
    )
    return table


def update_environment(s, episode, step_counter):
    env_list = ['-'] * (N_STATES -1) + ['T']
    if s == "terminal":
        print("terminal")
    else:
        env_list[s] = "o"
        interaction = "".join(env_list)
        print(f"\r{interaction}")
        time.sleep(FRESH_TIME)


def learn(): 
    q = create_q_table(N_STATES, ACTIONS)
    for episode in range(MAX_EPISODES):
        step_counter = 0
        s = 0
        done = False
        update_environment(s, episode, step_counter)

if __name__ == "__main__":
    learn()

@@ -329,15 +329,15 @@
 ers%0A
-epsilon
+EPSILON
  = 0
@@ -360,13 +360,13 @@
 icy%0A
-alpha
+ALPHA
  = 0
@@ -389,13 +389,13 @@
 ate%0A
-gamma
+GAMMA
  = 0
@@ -466,17 +466,16 @@
 pisode%0A%0A
-%0A
 def crea
@@ -628,34 +628,22 @@
 e%0A%0A%0Adef 
-update_environment
+render
 (s, epis
@@ -743,24 +743,222 @@
     
-print(%22terminal%22
+interaction = 'Episode %25s: total_steps = %25s' %25 (%0A        episode + 1, step_counter)%0A        print('%5Cr%7B%7D'.format(interaction), end='')%0A        time.sleep(2)%0A        print('%5Cr                                ', end=''
 )%0A  
@@ -1049,16 +1049,31 @@
 int(f%22%5Cr
+%7Bstep_counter%7D 
 %7Binterac
@@ -1121,16 +1121,950 @@
 def 
+reset():%0A    return 0%0A%0A%0Adef draw():%0A    return np.random.choice(ACTIONS)%0A%0A%0Adef choose_action(s, q_table):%0A    state_actions = q_table.iloc%5Bs, :%5D%0A    if np.random.uniform() %3E EPSILON or state_actions.all() == 0:%0A        action_name = draw()  # act non-greedy or state-action%0A    else:%0A        action_name = state_actions.argmax()  # act greedy%0A    return action_name%0A%0A%0Adef step(action, state):%0A    %22%22%22Run one timestep of the environment's dynamic%0A    :param action: an action provided by the environment%0A    :return: (observation, reward, done)%0A    %22%22%22%0A    observation = state%0A    reward = 0%0A    done = False%0A    if action == %22right%22:%0A        if state == N_STATES - 2:%0A            observation = %22terminal%22%0A            reward = 1%0A            done = True%0A        else:%0A            observation = state + 1%0A    else:%0A        # move left%0A        if state != 0:%0A            observation = state - 1%0A    return observation, reward, done%0A%0A%0Adef 
 learn():
  %0A  
@@ -2059,17 +2059,16 @@
 learn():
- 
 %0A    q =
@@ -2176,20 +2176,30 @@
        s
- = 0
+tate = reset()
 %0A       
@@ -2224,52 +2224,600 @@
     
-update_environment(s, episode, step_counter)
+render(state, episode, step_counter)%0A        while not done:%0A            action = choose_action(state, q)%0A            observation, reward, done = step(action, state)%0A            if observation != %22terminal%22:%0A                q_target = reward + GAMMA * q.iloc%5Bobservation, :%5D.max()%0A            else:%0A                q_target = reward  # next state is terminal%0A            q_predict = q.ix%5Bstate, action%5D%0A            q.ix%5Bstate, action%5D += ALPHA * (q_target - q_predict)%0A            state = observation%0A            render(state, episode, step_counter)%0A            step_counter += 1%0A    return q
 %0A%0Aif
@@ -2844,16 +2844,20 @@
 __%22:%0A   
+ q =
  learn()
@@ -2857,8 +2857,49 @@
 learn()%0A
+    print('%5Cr%5CnQ-table:%5Cn')%0A    print(q)%0A

RL hello world - complete