# An introduction to Q-Learning: reinforcement learning (Part1) [toc]       :::info - What is Q-Learning ? - Mathematics behind Q-Learning - Implementation using python :::   ## Q-Learning — a simplistic overview 問題的情境 : 機器人必須越過迷宮並到達終點。機器人一次只能移動一格,如果機器人踩到地雷的話,機器人就死了。機器人必須在盡可能短的時間內到達終點。      得分/獎勵 (scoring/reward) 機制如下 : 1. 機器人每走一格都會失去 1 分,這樣是為了使機器人採用最短路徑並儘可能快地到達目標。 2. 如果機器人踩到地雷,則損失 100 分並且遊戲結束。 3. 如果機器人獲得能量 ⚡️ ,它會獲得 1 分。 4. 如果機器人到達終點 (End),則機器人獲得 100 分。   我們如何訓練機器人以最短的路徑到達終點,而且不會踩到地雷 ?   ## Introducing the Q-Table      每個非邊緣格子 (non-edge) 將有四個可能的行動。機器人可以向上或向下或向右或向左移動。     model this environment in Q-Table :      columns : actions rows : states     - Q-Table 是一個 lookup table,我們計算每個狀態的最大預期未來獎勵。基本上,這張表將使我們在每個狀態都採取最佳的行動。 - 每個 Q-table score 將是機器人在該狀態下採取該行動時將獲得的最大預期未來獎勵。 - 這是一個迭代的過程,我們需要在每次迭代時改進 Q-Table。     :::danger - 我們如何計算 Q-Table 的值? :::     ## Mathematics: the Q-Learning algorithm **Q-function** The Q-function takes two inputs: state (s) and action (a).  目標就是找出累計獎勵最大的策略,即 Q-function的最大化     現在,讓我們了解更新是如何進行的。 ### Introducing the Q-learning algorithm process        #### Step 1: initialize the Q-Table   首先建立一個 Q-table,n 個 columns 代表 n 個 actions,m 個 rows 代表 m 個狀態,然後將所有的值都初始化為 0。           我們的例子中有 5 個狀態(s=5) 和 4 個動作(a=4),所以我們建立的 Q-table 會有 5 個 rows 和 4 個 columns     #### Steps 2 and 3: choose and perform an action **epsilon greedy strategy :**   Exploration and exploitation trade off    老鼠要在碰到貓咪或是電擊以前,吃到盡可能多的乳酪。小的乳酪值 1 分,大的乳酪值 100 分。   - Exploitation : 找到某一條獎勵不錯的路線,並堅持走那條路線。( 只專注於附近的獎勵 )   - Exploration : 對不同的路線進行探索,將更有可能使獎勵最大化,但也要承擔走到差的路線帶來的風險。 ( 吃到大乳酪,被貓咪吃掉或是被電死的機率增加 )   **epsilon rates : 執行探索 (exploration) 的機率**        在機器人對環境一無所知的時候, epsilon rates 會最高,機器人將探索環境並隨機選擇動作。 隨著機器人探索環境, epsilon rates 會降低,機器人開始利用已知的環境。 在探索過程中,機器人逐漸變得更有信心估計Q值。          #### Steps 4 and 5: evaluate   採取行動,並觀察結果和獎勵,然後更新 function Q (s,a)          我們將一次又一次地重複這一過程,直到學習停止。通過這種方式, Q-Table 將會更新。       :::info - 使用 Q function 找到最佳的動作選擇策略。我們的目標是最大化 Q function。 - Q table 幫助我們找到每個狀態的最佳行動。通過選擇所有可能的最佳行動,有助於最大化預期的獎勵。 - 最初,我們探索環境並更新 Q-Table 。當 Q-Table 準備就緒時,機器人將開始利用環境並採取更好的行動。 :::       參考資料 https://en.wikipedia.org/wiki/Greedy_algorithm https://blog.csdn.net/Allenalex/article/details/78220926 https://blog.csdn.net/qq_30615903/article/details/80739243 https://www.youtube.com/watch?v=o_g9JUMw1Oc
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up