for i in range(num_episodes): episode = [] s,_ = env.reset() while True: a = pi(s) # hit or stay s_, r, terminated, truncated, _ = env.step(a) episode.append((s, a, r)) if terminated or truncated: if r == 1: win_cnt += 1 elif r == -1: lose_cnt += 1 else: draw_cnt += 1 break s = s_ G = 0 visited_states = [] for s, a, r in episode[::-1]: G = GAMMA*G + r if s not in visited_states: Returns[s].append(G) V[s] = np.mean(Returns[s]) visited_states.append(s) if i % 5000 == 0: print(f"episode = {i}/{num_episodes} completed")print("Policy : stick threshold = {}".format(stick_threshold))print("win ratio = {:2f}%".format(100*win_cnt/num_episodes))print("lose ratio = {:2f}%".format(100*lose_cnt/num_episodes))print("draw ratio = {:2f}%".format(100*draw_cnt/num_episodes))
#시각화X, Y = np.meshgrid( np.arange(12, 22), # player가 가진 카드 합계 (12~21) np.arange(1, 11)) # dealer가 공개한 카드 (1~10)#V[(player의 hand 합계, dealer 공개 카드, 사용 가능한 에이스 보유)]no_usable_ace = np.apply_along_axis(lambda idx: V[(idx[0], idx[1], False)], 2, np.dstack([X, Y]))usable_ace = np.apply_along_axis(lambda idx: V[(idx[0], idx[1], True)], 2, np.dstack([X, Y]))fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 4), subplot_kw={'projection': '3d'})ax0.plot_surface(X, Y, no_usable_ace, cmap=plt.cm.YlGnBu_r)ax0.set_xlabel('Dealer open Cards')ax0.set_ylabel('Player Cards')ax0.set_zlabel('MC Estimated Value')ax0.set_title('No Useable Ace')ax1.plot_surface(X, Y, usable_ace, cmap=plt.cm.YlGnBu_r)ax1.set_xlabel('Dealer open Cards')ax1.set_ylabel('Player Cards')ax1.set_zlabel('MC Estimated Value')ax1.set_title('Useable Ace')plt.show()
for i in range(num_episodes): episode = [] s, _ = env.reset() while True: P = pi[s] a = np.random.choice(np.arange(len(P)), p = P) s_, r, terminated, truncated, _ = env.step(a) episode.append((s, a, r)) if terminated or truncated: if r == 1: win_cnt += 1 elif r == -1: lose_cnt += 1 else: draw_cnt += 1 break s = s_ G = 0 visited_state_action_pair = [] for s, a, r in episode[::-1]: G = GAMMMA*G + r if (s, a) not in visited_state_action_pair: Returns[(s, a)].append(G) Q[s][a] = np.mean(Returns[(s, a)]) visited_state_action_pair.append((s, a)) A_star = np.argmax(Q[s]) for a in range(num_actions): if a == A_star: pi[s][a] = 1 - e + e/num_actions else: pi[s][a] = e/num_actions if i % 1000 == 0: print(f"{i}/{num_episodes} episode completed")
Optimal action = 1
State value = -0.33860045146726864
#시각화X, Y = np.meshgrid( np.arange(12, 22), # player가 가진 카드 합계 (12~21) np.arange(1, 11)) # dealer가 공개한 카드 (1~10)#V[(player의 hand 합계, dealer 공개 카드, 사용 가능한 에이스 보유)]no_usable_ace = np.apply_along_axis(lambda idx: V[(idx[0], idx[1], False)], 2, np.dstack([X, Y]))usable_ace = np.apply_along_axis(lambda idx: V[(idx[0], idx[1], True)], 2, np.dstack([X, Y]))fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(12, 4), subplot_kw={'projection': '3d'})ax0.plot_surface(X, Y, no_usable_ace, cmap=plt.cm.YlGnBu_r)ax0.set_xlabel('Dealer open Cards')ax0.set_ylabel('Player Cards')ax0.set_zlabel('MC Estimated Value')ax0.set_title('No Useable Ace')ax1.plot_surface(X, Y, usable_ace, cmap=plt.cm.YlGnBu_r)ax1.set_xlabel('Dealer open Cards')ax1.set_ylabel('Player Cards')ax1.set_zlabel('MC Estimated Value')ax1.set_title('Useable Ace')plt.show()