ql
- ql 推荐度:
- 相关推荐
ql
import numpy as np
Q 矩阵初始化为0
q = np.matrix(np.zeros([6, 6]))
Reward 矩阵为提前定义好的。 类似与HMM的生成矩阵。-1表示无相连接的边
r = np.matrix([[-1, -1, -1, -1, 0, -1],
[-1, -1, -1, 0, -1, 100],
[-1, -1, -1, 0, -1, -1],
[-1, 0, 0, -1, 0, -1],
[ 0, -1, -1, 0, -1, 100],
[-1, 0, -1, -1, 0, 100]])
hyperparameter
#折扣因子
gamma = 0.8
#是否选择最后策略的概率
epsilon = 0.4
the main training loop
for episode in range(101):
# random initial state
state = np.random.randint(0, 6)
# 如果不是最终转态
while (state != 5):
# 选择可能的动作
# Even in random case, we cannot choose actions whose r[state, action] = -1.
possible_actions = []
possible_q = []
for action in range(6):
if r[state, action] >= 0:
possible_actions.append(action)
possible_q.append(q[state, action])
# Step next state, here we use epsilon-greedy algorithm.action = -1if np.random.random() < epsilon:# choose random actionaction = possible_actions[np.random.randint(0, len(possible_actions))]else:# greedyaction = possible_actions[np.argmax(possible_q)]# Update Q valueq[state, action] = r[state, action] + gamma * q[action].max()# Go to the next statestate = action# Display training progress
if episode % 10 == 0:print("------------------------------------------------")print("Training episode: %d" % episode)print(q)
最新文章
- 请仔细品味
- mybatis实战:一、mybatis入门(配置、一些问题的解决)
- 林达华
- Window Server 2008虚拟机安装
- MinGW和MSYS简介
- 关于使用ComponentName连接俩个Activity运行闪退的问题
- 语音识别之HTK入门(四)——HCompV源码解析
- 电话银行
- php开源cms系统比较好,最受欢迎免费开源CMS建站系统排行榜
- void* 指针有什么用
- nofollow标签使用方法
- 对前途迷茫时多看看
- html5 调用手机摄像头详解
- itoa函数
- extern 详细用法
- 主引导扇区(MBR),分区表(DPT)及活动分区(DBR)
- gcc开启C99或C11标准支持
- Python爬虫爬取动态网页
- gets函数用法
- 【十一届蓝桥杯】