資源簡介
強化學習中Qlearning的簡單實現,維度為1,通過Qlearning自主找到最優的Q表
代碼片段和文件信息
import?numpy?as?np
import?pandas?as?pd
import?time
N_STATES?=?6???#1維世界的寬度
ACTIONS?=?[‘left‘‘right‘]??#動作
EPSILON?=?0.9???#貪婪都
ALPHA?=?0.1??#學習率
GAMMA?=?0.9??#獎勵遞減值
MAX_ROUND?=?13??#最大回合數
FRESH_TIME?=?0.01?#移動間隔時間
def?build_q_table(n_statesactions):
????table?=?pd.Dataframe(np.zeros((n_stateslen(actions)))columns=actions)
????return?table
def?choose_action(stateq_table):
????state_actions?=?q_table.iloc[state:]
????if(np.random.uniform()>EPSILON)?or?(state_actions.all()==0):
????????action_name?=?np.random.choice(ACTIONS)
????else:
????????action_name?=?state_actions.argmax()
????return?action_name
def?get_env_feedback(SA):
????if?A?==?‘right‘:
????????if?S?==?N_STATES?-2:
????????????S_?=?‘terminal‘
????????????R?=?1
????????else:
????????????S_?=?
評論
共有 條評論