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

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

作者: 许以诺 | 来源:发表于2018-08-15 19:37 被阅读0次

看到一个简单有趣的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

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/ttcwbftx.html