-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrozenLake.py
More file actions
61 lines (46 loc) · 1.7 KB
/
frozenLake.py
File metadata and controls
61 lines (46 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gym
import numpy as np
import matplotlib.pyplot as plt
from gym.envs.registration import register
import random as pr
def rargmax(vector): # https://gist.github.com/stober/1943451
"""Argmax that chooses randomly among eligible maximum indices."""
m = np.amax(vector)
indices = np.nonzero(vector == m)[0]
return pr.choice(indices)
register(
id='FrozenLake-v3',
entry_point='gym.envs.toy_text:FrozenLakeEnv',
kwargs={'map_name': '4x4', 'is_slippery': False}
)
env = gym.make('FrozenLake-v3') # 환경 생성
# Initialize table with all zeros
Q = np.zeros([env.observation_space.n, env.action_space.n])
# Set learning parameters
num_episodes = 3
# Create lists to contain total rewards and steps per episode
rList = []
for i in range(num_episodes):
# Reset environment and get first new observation
state= env.reset()
rAll = 0
terminated = False
truncated = False
# The Q-Table learning algorithm
while not (terminated or truncated): # 종료 조건 수정
# Choose an action by greedily picking from Q table
action = rargmax(Q[state, :])
# Get new state and reward from environment
new_state, reward, terminated, truncated, _ = env.step(action)
# Update Q-Table with new knowledge
Q[state, action] = reward + np.max(Q[new_state, :])
rAll += reward
state = new_state
rList.append(rAll)
print("Success rate: " + str(sum(rList) / num_episodes))
print("Final Q-Table Values")
print("LEFT DOWN RIGHT UP")
print(Q)
plt.bar(range(len(rList)), rList, color="blue")
plt.savefig("result_graph.png") # 그래프를 PNG 파일로 저장
plt.show()