gpt4 book ai didi

algorithm - Q学习算法问题

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:36 24 4
gpt4 key购买 nike

我正在尝试做一个简单的 Q 学习算法,但无论出于何种原因,它都没有收敛。代理基本上应该从 5x5 网格上的一个点到达目标点。当我运行它时,它似乎找到了最佳方式,但它没有收敛,我也不知道为什么。任何帮助,将不胜感激。我觉得某处有一个小错误,所以我正在寻找一组新的眼睛。

代码:

function Q=ReinforcementLearning

clc;

format short

format compact

% three input: R, alpha and gamma

% immediate reward matrix;

% row and column = states; -Inf = no door between room

R=[-inf,-inf, 0, 0, -inf;

-inf,-inf, 0, 0, -inf;

0, 0, -inf, -inf, 100;

0, 0, -inf, -inf, -inf;

-inf,-inf, 0, -inf, 100];



gamma=0.8; % learning parameter
alpha=0.5;
oldQ = 0;


Q=zeros(size(R)); % initialize Q as zero

q1=ones(size(R))*inf; % initialize previous Q as big number

count=0; % counter


for episode=0:50000

% random initial state

y=randperm(size(R,1));

state=y(1);




% select any action from this state

x=find(R(state,:)>=0); % find possible action of this state

if size(x,1)>0,

x1=RandomPermutation(x); % randomize the possible action

x1=x1(1); % select an action

end


MaxQ=max(Q,[],2);

%Q(state,x1) = R(state,x1) + (gamma * MaxQ(x1)); %old function that works perfectly (converges)

Q(state,x1)= oldQ + alpha * (R(state,x1)+ (gamma * MaxQ(x1)) - oldQ); % new one that I need to implement

oldQ = Q(state,x1);



state=x1; %#ok<NASGU>


%Q = round(Q);


% break if convergence: small deviation on q for 1000 consecutive

if sum(sum(abs(q1-Q)))<5 & sum(sum(Q > 0))

if count>1000,

episode % report last episode

break % for

else

count=count+1; % set counter if deviation of q is small

end

else

q1=Q;

count=0; % reset counter when deviation of q from previous q is large

end

end


%normalize q

g=max(max(Q));

episode

if g>0,

Q=100*Q/g;
roundQ = round(Q);
roundQ

end

最佳答案

编辑:

问题出在这里:

Q(state,x1)=  oldQ + alpha * (R(state,x1)+ (gamma * MaxQ(x1)) - oldQ);

您正在使用 oldQ 值更新每个 Q(state,x1),而该值应该由它自己的先前值更新 Q(state, x1) 在执行此操作之前。你看到了吗?最好有一个 Q 矩阵并保存您可以在此处使用的以前的值。如我所见,您对所有 **Q** 使用相同的 oldQ

实际上,您可以从 q1 加载以前的值并使用它们代替 oldQ

关于algorithm - Q学习算法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22537246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com