创建一个多臂老虎机
多臂老虎机问题是强化学习中的经典示例,用来描述这样一种情境:智能体需要在多个动作(或"拉杆")之间进行选择,而并不知道各自的期望回报。随着时间推移,智能体通过探索各个选项来学习哪一个拉杆的回报最高。本练习将搭建用于模拟多臂老虎机问题的基础结构。
numpy 库已按 np 导入。
本练习是课程的一部分
Python 中的 Gymnasium 强化学习
练习说明
- 生成数组
true_bandit_probs,其中包含随机概率,表示每个老虎机的真实成功率。 - 使用全零初始化两个数组
counts和values;counts用于记录每个老虎机被选择的次数,values表示对各老虎机胜率的估计值。 - 创建
rewards和selected_arms数组,用于在每次迭代中存储获得的回报和被选择的拉杆。
交互式实操练习
通过完成这段示例代码来试试这个练习。
def create_multi_armed_bandit(n_bandits):
# Generate the true bandits probabilities
true_bandit_probs = ____
# Create arrays that store the count and value for each bandit
counts = ____
values = ____
# Create arrays that store the rewards and selected arms each episode
rewards = ____
selected_arms = ____
return true_bandit_probs, counts, values, rewards, selected_arms