In [14]:
import gym
import numpy as np
import matplotlib.pyplot as plt
from gym.envs.registration import register
import random as pr

In [2]:
register(
    id = 'FrozenLake-v3',
    entry_point = 'gym.envs.toy_text:FrozenLakeEnv',
    kwargs = {'map_name':"4x4",'is_slippery':False}
)
env = gym.make('FrozenLake-v3')


[2017-04-03 05:02:09,943] Making new env: FrozenLake-v3

In [19]:
Q = np.zeros([env.observation_space.n,env.action_space.n])
dis = .99
num_episodes = 6000

rList = []
total_move_count = []
for i in range(num_episodes):
    state = env.reset()
    rAll = 0
    done = False
    e = 1./((i//100)+1)
    while not done: 
        if np.random.rand(1) < e:
            action = env.action_space.sample()
        else:
            if np.random.rand(1) < e:
                action = np.argmax(Q[state, :] + np.random.randn(1,env.action_space.n) / (i + 1))
            else:
                action = np.argmax(Q[state, :])
        new_state, reward, done, _ = env.step(action)
        Q[state, action] = reward + dis * np.max(Q[new_state, :])
        state = new_state
        rAll += reward
    rList.append(rAll)

In [20]:
print("Score over time: " + str(sum(rList) / num_episodes))
print("Final Q-Table Values")
print("LEFT DOWN RIGHT UP")
print(Q)
plt.bar(range(len(rList)), rList, color="red",edgecolor='none')
plt.show()


Score over time: 0.9146666666666666
Final Q-Table Values
LEFT DOWN RIGHT UP
[[ 0.94148015  0.95099005  0.95099005  0.94148015]
 [ 0.94148015  0.          0.96059601  0.95099005]
 [ 0.95099005  0.970299    0.95099005  0.96059601]
 [ 0.96059601  0.          0.95099005  0.95099005]
 [ 0.95099005  0.96059601  0.          0.94148015]
 [ 0.          0.          0.          0.        ]
 [ 0.          0.9801      0.          0.96059601]
 [ 0.          0.          0.          0.        ]
 [ 0.96059601  0.          0.970299    0.95099005]
 [ 0.96059601  0.9801      0.9801      0.        ]
 [ 0.970299    0.99        0.          0.970299  ]
 [ 0.          0.          0.          0.        ]
 [ 0.          0.          0.          0.        ]
 [ 0.          0.9801      0.99        0.970299  ]
 [ 0.9801      0.99        1.          0.9801    ]
 [ 0.          0.          0.          0.        ]]

In [ ]: