当前位置:   article > 正文

强化学习用于路径规划,以Q-learning解决TSP问题为例

python qlearning路径规划

以下为原创代码,这段代码解决的是TSP问题,采用的思路是强化学习的Q-learning方法。
import numpy as np
epsilon = 0.5
gamma = 0.98
lr = 0.1
distance =np.array([[0,7,6,1,3],[7,0,3,7,8],[6,3,0,12,11],[1,7,12,0,2],[3,8,11,2,0]])
R_table = 11-distance
space = [0,1,2,3,4]
Q_table = np.zeros((5,5))
for i in range(10):
path = [0]
for j in range(4):
#print(path)
s = list(path)[j]
s_row = Q_table[s]
remain = set(space)-set(path)
maxvalue = -1000
for rm in remain:
Q
= Qtable[s, rm]
if Q
>max_value:
maxvalue = Q
a = rm
if np.random.uniform()<epsilon:
a = np.random.choice(np.array(list(set(space)-set(path))))
if j!=3:
Q_table[s,a] = (1-lr)Q_table[s,a]+lr(R_table[s,a]+gammamax_value)
else:
Q_table[s,a] = (1-lr)
Q_table[s,a]+lrR_table[s,a]
path.append(a)
Q_table[a,0] = (1-lr)
Q_table[a,0]+lr*R_table[a,0]
path.append(0)
print(Q_table)
#即可得到最佳的TSP路径的Q表

转载于:https://blog.51cto.com/14213209/2354227

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/791683
推荐阅读
相关标签
  

闽ICP备14008679号