Q-learning算法实现1(matlab)

算法伪代码:

得到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
        optional_action=find(Reward_table(current_state,:)>-1);
        chosen_action=optional_action(randperm(length(optional_action),1));
        %take action, observe reward and next state
        r=Reward_table(current_state,chosen_action);
        next_state=chosen_action;
        %update Q-table
        next_possible_action=find(Reward_table(next_state,:)>-1);
        maxQ=max(Q_table(next_state,next_possible_action));
        Q_table(current_state,chosen_action)=Q_table(current_state,chosen_action)+alpha*(r+gamma*maxQ-Q_table(current_state,chosen_action));
        current_state=next_state;
    end
end
 %% 选择最优路径
 %randomly choose a state
currentstate=randperm(state_num,1);
fprintf('Initialized state %d\n',currentstate);
%choose action which satisfies Q(s,a)=max{Q(s,a')}
while currentstate~=final_state
     [maxQtable,index]=max(Q_table(currentstate,:));
     chosenaction=index;
     nextstate=chosenaction;
     fprintf('the robot goes to %d\n',nextstate);
     currentstate=nextstate;
end
        

代码输出:

Q表:

 

最优策略:

 

 

 

  • 14
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值