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