用Matlab实现简单的Q-learning算法(学习走出房间)

看到一个简单有趣的Q learning例子,写了段matlab代码实现一下。有兴趣的请先阅读原文 链接

dbstop if error%stop at the error if it happens

%Initialization
episode_num = 100;%Iteration time of exploration
state_num = 6;%Room number (including the hall)
gamma = 0.8;%discount factor

%100: Arrival the hall
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_table = zeros(state_num, state_num);
final_state = 6;

for i = 1:episode_num
    %Randomly start in a room
    current_state = randperm(state_num,1);
    while current_state ~= final_state
        %Get the possible actions based on the current status
        Action_option_list = find(Reward_table(current_state,:)>-1);
        %Randomly choose one
        chosen_action = Action_option_list(randperm(length(Action_option_list),1));
        next_state = chosen_action;
        %Get the possible actions based on the next state
        possible_next_action_list = find(Reward_table(next_state,:)>-1);
        %Get the maximum reward of next state
        max_reward = max(Q_table(next_state,possible_next_action_list));
        %Update the Q table
        Q_table(current_state,chosen_action) =  Reward_table(current_state,chosen_action) + gamma*max_reward;
        %Update: Move to the next state
        current_state = next_state;
    end
    if mod(i,10)==0
        i
        Q_table
    end
end
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Kling 是一款颜值颇高的记忆翻牌游戏,只不过,这次你要寻找的,不是相同的图案,而是相同的声音,记住那个音色,再...
    最美应用阅读 4,542评论 0 4
  • 此文章是为了总结我对《红与黑》的脉络的认识和理解,以便观察以后自己的理解上是否有变化。 维里耶尔,于连出生的地方。...
    王明远大头阅读 5,595评论 0 0
  • 中华博远的文化,融汇入一汪清水中,便成了茶魂。 一题记 从古至今,品茶一...
    五月的柚子茶阅读 3,783评论 0 1
  • 一天又过去了,也快,今天一点都不舒服,晚上睡不好,可怜
    _cc11阅读 1,625评论 0 0

友情链接更多精彩内容