เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การนำ SARSA update rule ไปใช้งาน

SARSA เป็น on-policy algorithm ใน RL ที่อัปเดต action-value function โดยอิงจาก action ที่เลือกใช้ในสถานะปัจจุบันและสถานะถัดไป วิธีนี้ช่วยให้โมเดลเรียนรู้ค่าของคู่ state-action ทั้งในปัจจุบันและลำดับถัดไป ซึ่งเป็นแนวทางในการเรียนรู้ policy ที่คำนึงถึง action ในอนาคตด้วย SARSA update rule แสดงไว้ด้านล่าง และโจทย์ของแบบฝึกหัดนี้คือการสร้างฟังก์ชันที่อัปเดต Q-table ตามกฎดังกล่าว

ไลบรารี NumPy ถูก import ให้แล้วในชื่อ np

Image showing the mathematical formula of the SARSA update rule.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Reinforcement Learning with Gymnasium ใน Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ดึง Q-value ปัจจุบันสำหรับคู่ state-action ที่กำหนด
  • หา Q-value สำหรับคู่ state-action ถัดไป
  • อัปเดต Q-value ของคู่ state-action ปัจจุบันโดยใช้สูตร SARSA
  • อัปเดต Q-table Q โดยกำหนดให้ agent เลือก action 0 ใน state 0 ได้รับ reward เป็น 5 จากนั้นย้ายไปยัง state 1 และเลือก action 1

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

def update_q_table(state, action, reward, next_state, next_action):
  	# Get the old value of the current state-action pair
    old_value = ____
    # Get the value of the next state-action pair
    next_value = ____
    # Compute the new value of the current state-action pair
    Q[(state, action)] = ____

alpha = 0.1
gamma  = 0.8
Q = np.array([[10,0],[0,20]], dtype='float32')
# Update the Q-table for the ('state1', 'action1') pair
____
print(Q)
แก้ไขและรันโค้ด