引用来自ShangtongZhang的代码chapter10/access_control.py
使用访问控制的例子来测试continuing tasks下使用average reward setting训练action value function approximation的效果。
问题描述
有一个永远不会空的customer队列,每位customer都有对应的优先级,每次队列首的customer要求访问server的时候,server可以选择不接受,此时reward=0,也可以选择接受,此时reward根据customer的优先级决定,有1,2,4,8四种,如果server接受了customer的访问请求,那么这台server就判定为busy,不能再接受其余customer的访问请求,每次step每台server都有概率从busy变为free状态;如果没有server自然就会拒绝customer的访问请求。不管队首的customer是否成功访问到server了,本次操作之后customer就从队列剔除,轮到下一位customer来访问server,每个队首的customer的优先级是随机分布的。
这个问题的state由两部分组成:当前可用的server数量和队首的customer的优先级,action对应有接受和拒绝两种。可以使用tiling code来构造特征。
引入模块并定义常量
1 | import numpy as np |
使用了9.5的Tiling Coding来将(s,a)转换成feature,这里没有使用custom的Tiling Coding算法,使用了Richard S. Sutton的tiling-code software
1 | ####################################################################### |
使用Tiling Coding构造特征并建立linear value function
1 | # a wrapper class for differential semi-gradient Sarsa state-action function |
通过建立的optimal policy来指导agent和environment进行交互
1 | # get action based on epsilon greedy policy and @valueFunction |
使用differential semi-gradient Sarsa(0)进行训练
1 | # differential semi-gradient Sarsa |
绘制图表表现differential value和policy
1 | # Figure 10.5, Differential semi-gradient Sarsa on the access-control queuing task |
100%|██████████| 2000000/2000000 [05:48<00:00, 5742.25it/s]
asymptotic average reward: 2.7830264537085903
Frequency of number of free servers:
[1.219660e-01 2.275715e-01 2.717895e-01 2.138460e-01 1.125485e-01
4.154650e-02 9.331000e-03 1.276000e-03 1.150000e-04 8.000000e-06
2.000000e-06]