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

แก้ปัญหา CliffWalking ด้วยกลยุทธ์ epsilon-greedy

สภาพแวดล้อม CliffWalking เป็น testbed มาตรฐานสำหรับอัลกอริทึม RL โดยเป็น grid world ที่ agent ต้องหาเส้นทางจาก state เริ่มต้นไปยัง state เป้าหมาย โดยหลีกเลี่ยงหน้าผาที่อยู่ตลอดเส้นทาง การใช้กลยุทธ์ epsilon-greedy ช่วยให้ agent สำรวจสภาพแวดล้อมได้อย่างมีประสิทธิภาพ พร้อมกับเรียนรู้การหลีกเลี่ยงหน้าผาเพื่อเพิ่ม cumulative reward ให้สูงสุด ให้แก้ปัญหาสภาพแวดล้อมนี้โดยใช้กลยุทธ์ epsilon-greedy คำนวณรางวัลที่ได้รับในแต่ละ episode ของการฝึก แล้วบันทึกผลลงใน list rewards_eps_greedy

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

Reinforcement Learning with Gymnasium ใน Python

ดูคอร์ส

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

  • ภายในแต่ละ episode ให้เลือก action โดยใช้ฟังก์ชัน epsilon_greedy()
  • สะสม reward ที่ได้รับเข้าสู่ episode_reward
  • หลังจากแต่ละ episode สิ้นสุด ให้เพิ่ม episode_reward รวมทั้งหมดเข้าไปใน list rewards_eps_greedy เพื่อใช้วิเคราะห์ในภายหลัง

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

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

rewards_eps_greedy = []
for episode in range(total_episodes):
    state, info = env.reset()
    episode_reward = 0
    for i in range(max_steps):
      	# Select action with epsilon-greedy strategy
        action = ____
        next_state, reward, terminated, truncated, info = env.step(action)
        # Accumulate reward
        ____        
        update_q_table(state, action, reward, next_state)      
        state = next_state
    # Append the toal reward to the rewards list 
    ____
print("Average reward per episode: ", np.mean(rewards_eps_greedy))
แก้ไขและรันโค้ด