算法伪代码:
得到Q表后,根据如下算法选择最优策略:
以机器人走房间为例,代码实现如下:
原文链接如下:https://www.jianshu.com/p/29db50000e3f
注:原文中的房间状态0-5分别对应代码中1-6
%机器人走房间Q-learning的实现
%% 基本参数
episode=100; %探索的迭代次数
alpha=1;%更新步长
gamma=0.8;%折扣因子
state_num=6;
action_num=6;
final_state=6;%目标房间
Reward_table = [
-1 -1 -1 -1 0 -1; %1
-1 -1 -1 0 -1 100; %2
-1 -1 -1 0 -1 -1; %3
-1 0 0 -1 0 -1; %4
0 -1 -1 0 -1 100; %5
-1 0 -1 -1 0 100 %6
];
%% 更新Q表
%initialize Q(s,a)
Q_table=zeros(state_num,action_num);
for i=1:episode
%randomly choose a state
current_state=randperm(state_num,1);
while current_state~=final_state
%randomly choose an action from current state
op