Ainsteinx Logo
Ainsteinx
👤
ຍິນດີຕ້ອນຮັບສູ່ Ainsteinx
ເຂົ້າສູ່ລະບົບ

ປ່ຽນພາສາ

ສະກຸນເງິນ

ໜ້າທຳອິດ
ບົດຄວາມ
ບໍລິການ
ບົດ 03 · RL ແບບຕາຕະລາງ

ຊັ່ງເປົ້າໝາຍໄລຍະສັ້ນ ແລະ ໄລຍະຍາວ

Policy, value function ແລະ ການວາງແຜນເມື່ອຮູ້ MDP.

ເປົ້າໝາຍຂອງ agent ແມ່ນເຮັດໃຫ້ expected return (ລາງວັນລວມທີ່ຄາດໄວ້) ສູງສຸດ. ລາງວັນໃນອະນາຄົດຖືກຫຼຸດຄ່າດ້ວຍ γ ເພາະວ່າ "ເງິນມື້ນີ້ມີຄ່າກວ່າເງິນມື້ໜ້າ".

ໂຄດຕົວຢ່າງໃຊ້ Value Iteration ຊອກ policy ທີ່ດີທີ່ສຸດຂອງ FrozenLake. ເຖິງຈະດີທີ່ສຸດແລ້ວ ກໍສຳເລັດພຽງປະມານ 3 ໃນ 4 ເທື່ອ ເພາະນ້ຳກ້ອນລື່ນ: policy ທີ່ດີທີ່ສຸດ ບໍ່ໄດ້ແປວ່າຊະນະທຸກເທື່ອ.

Gₜ = rₜ₊₁ + γ·rₜ₊₂ + γ²·rₜ₊₃ + … V^π(s) = E_π[ Gₜ | sₜ = s ] ຄ່າຂອງ state Q^π(s,a) = E_π[ Gₜ | sₜ = s, aₜ = a ] ຄ່າຂອງ action ໃນ state A^π(s,a) = Q^π(s,a) − V^π(s) ດີກວ່າຄ່າສະເລ່ຍປານໃດ Bellman: V^π(s) = Σₐ π(a|s) Σₛ' T(s'|s,a) [ R + γ·V^π(s') ]
  • Policy (π) ຄື "ແຜນສຳລັບທຸກສະຖານະການ". ມີແບບ deterministic (ຕອບ action ດຽວ) ແລະ stochastic (ຕອບເປັນໂອກາດ)
  • Policy evaluation: ຄິດໄລ່ V ຂອງ policy ທີ່ມີ
  • Policy improvement: ສ້າງ policy ໃໝ່ທີ່ greedy ກັບ V
  • Policy iteration: ສະຫຼັບ 2 ຂັ້ນຕອນຂ້າງເທິງ ຈົນບໍ່ປ່ຽນແປງ
  • Value iteration: ຕັດ evaluation ໃຫ້ເຫຼືອຮອບດຽວ ແລ້ວ improve ເລີຍ (ໄວກວ່າ)
  • Generalized Policy Iteration (GPI): ທຸກວິທີທີ່ຈະຮຽນຕໍ່ໄປ ລ້ວນແຕ່ເປັນ "ປະເມີນ ↔ ປັບປຸງ"

ສິ່ງທີ່ຄວນຈື່

  • V ບອກຄ່າຂອງ state, Q ບອກຄ່າຂອງ action, A ບອກວ່າ action ນັ້ນດີກວ່າຄ່າສະເລ່ຍປານໃດ
  • ຖ້າຮູ້ MDP ໃຊ້ Value/Policy Iteration ຊອກ π* ໄດ້ໂດຍກົງ (planning, ບໍ່ແມ່ນ learning)
deep-rl-ep03-value-iteration.py
import numpy as np
import gymnasium as gym

def value_iteration(P, gamma=0.99, theta=1e-10):
    n_s, n_a = len(P), len(P[0])
    V = np.zeros(n_s)
    while True:
        Q = np.zeros((n_s, n_a))
        for s in range(n_s):
            for a in range(n_a):
                for prob, s2, r, done in P[s][a]:
                    Q[s, a] += prob * (r + gamma * V[s2] * (not done))
        if np.max(np.abs(V - Q.max(axis=1))) < theta:
            break
        V = Q.max(axis=1)                 # Bellman optimality backup
    pi = Q.argmax(axis=1)                 # ນະໂຍບາຍ greedy
    return V, pi

env = gym.make("FrozenLake-v1", is_slippery=True)
V, pi = value_iteration(env.unwrapped.P)
print(np.round(V.reshape(4, 4), 3))
print("π* =", "".join("←↓→↑"[a] for a in pi))

# ທົດສອບນະໂຍບາຍ 1,000 ເກມ
wins = 0
for ep in range(1000):
    s, _ = env.reset(seed=ep)
    done = False
    while not done:
        s, r, term, trunc, _ = env.step(pi[s])
        done = term or trunc
    wins += r
print(f"ອັດຕາສຳເລັດ: {wins / 10:.1f}%")

ຜົນລັບ

[[0.542 0.499 0.471 0.457]
 [0.558 0.    0.358 0.   ]
 [0.592 0.643 0.615 0.   ]
 [0.    0.742 0.863 0.   ]]
π* = ←↑↑↑←←←←↑↓←←←→↓←
ອັດຕາສຳເລັດ: 75.5%
  • #DeepRL
  • #ReinforcementLearning
  • #PythonສຳລັບຄົນLaos
  • #AI
  • #ບອກຕໍ່ຄວາມຮູ້