Multi-armed bandits

Reinforcement Learning with Gymnasium ใน Python

Fouad Trad

Machine Learning Engineer

Multi-armed bandits

 

  • นักพนันที่ต้องเลือกตู้สล็อต
  • ความท้าทาย → เพิ่มรางวัลให้ได้มากที่สุด
  • วิธีแก้ → การสำรวจและการใช้ประโยชน์

ภาพชายคนหนึ่งยืนอยู่หน้าแถวตู้สล็อต

Reinforcement Learning with Gymnasium ใน Python

ตู้สล็อต

ภาพตู้สล็อต 4 ตู้ที่มีความน่าจะเป็นในการชนะต่างกัน ได้แก่ 45%, 35%, 85% และ 62% ซึ่งผู้ใช้ไม่ทราบค่าเหล่านี้

  • รางวัลจากแขนแต่ละข้างคือ 0 หรือ 1
  • เป้าหมายของ agent → สะสมรางวัลให้ได้มากที่สุด
Reinforcement Learning with Gymnasium ใน Python

การแก้ปัญหา

 

  • Decayed epsilon-greedy
  • Epsilon → สุ่มเลือกตู้

ไดอะแกรมแสดงว่าด้วยความน่าจะเป็น epsilon agent จะสำรวจโดยสุ่มเลือกตู้

Reinforcement Learning with Gymnasium ใน Python

การแก้ปัญหา

 

  • Decayed epsilon-greedy
  • Epsilon → สุ่มเลือกตู้
  • 1 - epsilon → เลือกตู้ที่ดีที่สุดในขณะนั้น
  • Epsilon ลดลงเรื่อย ๆ ตามเวลา

ไดอะแกรมแสดงว่าด้วยความน่าจะเป็น epsilon agent จะสำรวจโดยสุ่มเลือกตู้ และด้วยความน่าจะเป็น 1 - epsilon จะใช้ประโยชน์โดยเลือกตู้ที่ดีที่สุดที่รู้จัก

Reinforcement Learning with Gymnasium ใน Python

การกำหนดค่าเริ่มต้น

n_bandits = 4  
true_bandit_probs = np.random.rand(n_bandits)

n_iterations = 100000 epsilon = 1.0 min_epsilon = 0.01 epsilon_decay = 0.999
counts = np.zeros(n_bandits) # How many times each bandit was played
values = np.zeros(n_bandits) # Estimated winning probability of each bandit
rewards = np.zeros(n_iterations) # Reward history
selected_arms = np.zeros(n_iterations, dtype=int) # Arm selection history
Reinforcement Learning with Gymnasium ใน Python

ลูปการโต้ตอบ

for i in range(n_iterations):
    arm = epsilon_greedy()

reward = np.random.rand() < true_bandit_probs[arm]
rewards[i] = reward selected_arms[i] = arm counts[arm] += 1
values[arm] += (reward - values[arm]) / counts[arm]
epsilon = max(min_epsilon, epsilon * epsilon_decay)
Reinforcement Learning with Gymnasium ใน Python

วิเคราะห์การเลือก

selections_percentage = np.zeros((n_iterations, n_bandits))


ไดอะแกรมแสดงขั้นตอนแรก: อาร์เรย์ขนาด (iterations, n_bandits) ที่เต็มไปด้วยศูนย์

Reinforcement Learning with Gymnasium ใน Python

วิเคราะห์การเลือก

selections_percentage = np.zeros((n_iterations, n_bandits))

for i in range(n_iterations): selections_percentage[i, selected_arms[i]] = 1

ไดอะแกรมแสดงขั้นตอนที่สอง ซึ่งทำเครื่องหมายแขนที่ถูกเลือกในแต่ละรอบด้วยค่า 1 ในอาร์เรย์

Reinforcement Learning with Gymnasium ใน Python

วิเคราะห์การเลือก

selections_percentage = np.zeros((n_iterations, n_bandits))

for i in range(n_iterations): selections_percentage[i, selected_arms[i]] = 1
selections_percentage = np.cumsum(selections_percentage, axis=0) / np.arange(1, n_iterations + 1).reshape(-1, 1)

ไดอะแกรมแสดงขั้นตอนสุดท้าย ซึ่งคำนวณผลรวมสะสมของ bandit ที่ถูกเลือก แล้วหารด้วยจำนวนรอบเพื่อได้เปอร์เซ็นต์การเลือกแต่ละแขนในแต่ละรอบ

Reinforcement Learning with Gymnasium ใน Python

วิเคราะห์การเลือก

  กราฟแสดงเส้น selection_percentage ของแต่ละ bandit ซึ่งแสดงว่าเมื่อจำนวนรอบเพิ่มขึ้น agent มีแนวโน้มเลือก bandit #2 มากกว่าตัวอื่น

for arm in range(n_bandits):
    plt.plot(selections_percentage[:, arm], label=f'Bandit #{arm+1}')
plt.xscale('log')
plt.title('Bandit Action Choices Over Time')
plt.xlabel('Episode Number')
plt.ylabel('Percentage of Bandit Selections (%)')
plt.legend()
plt.show()

for i, prob in enumerate(true_bandit_probs, 1): print(f"Bandit #{i} -> {prob:.2f}")
Bandit #1 -> 0.37
Bandit #2 -> 0.95
Bandit #3 -> 0.73
Bandit #4 -> 0.60
  • agent เรียนรู้ที่จะเลือก bandit ที่มีความน่าจะเป็นสูงที่สุด
Reinforcement Learning with Gymnasium ใน Python

มาฝึกกันเถอะ!

Reinforcement Learning with Gymnasium ใน Python

Preparing Video For Download...