Q learning演算法實現1(matlab)

2021-08-28 18:17:20 字數 2053 閱讀 5646

演算法偽**:

得到q表後,根據如下演算法選擇最優策略:

以機械人走房間為例,**實現如下:

注:原文中的房間狀態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;

endend %% 選擇最優路徑

%randomly choose a state

currentstate=randperm(state_num,1);

fprintf('initialized state %d\n',currentstate);

%choose action which satisfies q(s,a)=max

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表:

最優策略:

1m網速是什麼意思,1m頻寬是什麼意思

等等,也就是說我們在正常的情況下可以擁有最多不超過64k的專有頻寬。注意是 不高於 那麼也就是說很多時候我們的專有頻寬可能小於64k,那有又是為什麼呢?事實上,中國電信的adsl是執行在atm上面,atm到chinanet邊緣路由器頻寬是155m,每乙個邊緣路由器可以連線3000使用者,如果這些用 ...

1M到底有多大?

很多人買伺服器的時候都會遇到乙個問題,想買大一點的頻寬,最好200m,可是200m 貴上天,學生付不起,有的人付了錢,這個月就沒飯吃了。其實買伺服器只需要1m頻寬 無名的程式設計師 你坑人的是吧?1m怎麼夠用,我每秒有1000多個訪客啊 1m的大小是你無法估量的,有人覺得1t才是無法估量的!其實乙個...

了解阿里雲1M頻寬分析

阿里雲伺服器預設1m頻寬夠不夠用呢?1m對於個人部落格 小 來說每天幾千pv是可以的。這類 併發量也是很小的,跑起來很輕鬆。通常1m頻寬每秒的最大傳輸速度是 128kb,如果乙個網頁 200kb 大小,那就要 1秒多才能載入完成,那麼阿里雲的不同頻寬換算後如下 1mbps 128kb s 2mbps...