机器学习算法完整版见fenghaootong-github
强化学习应用实例
导入模块
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import time
设置参数
#产生伪随机数列
np.random.seed(2)
N_STATES = 6
ACTIONS = ['left', 'right']
EPSILON = 0.9
ALPHA = 0.1
LAMBDA = 0.9
MAX_EPISODES = 13
FRESH_TIME = 0.3 #走一步的时间
构建一个table
def build_Q_tabel(n_states, actions):
table = pd.DataFrame(np.zeros((n_states, len(actions))), columns = actions)
return table
build_Q_tabel(N_STATES, ACTIONS)
left | right | |
---|---|---|
0 | 0.0 | 0.0 |
1 | 0.0 | 0.0 |
2 | 0.0 | 0.0 |
3 | 0.0 | 0.0 |
4 | 0.0 | 0.0 |
5 | 0.0 | 0.0 |
选择行动
def choose_action(state, q_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(S, A):
if A == 'right':
if S == N_STATES - 2:
S_ = 'terminal'
R = 1
else:
S_ = S + 1
R = 0
else:
R = 0
if S == 0:
S_ = S
else:
S_ = S - 1
return S_, R
更新环境
def update_env(S, episode, step_counter):
env_list = ['-']*(N_STATES-1) + ['T']
if S == 'terminal':
interaction = 'Episode %s: total_steps = %s' % (episode+1, step_counter)
print('\r{}'.format(interaction), end='')
time.sleep(2)
print('\r ',end='')
else:
env_list[S] = 'o'
interaction = ''.join(env_list)
print('\r{}'.format(interaction), end='')
time.sleep(FRESH_TIME)
RL过程
def rl():
q_table = build_Q_tabel(N_STATES, ACTIONS)
for episode in range(MAX_EPISODES):
step_counter = 0
S = 0
is_terminated = False
update_env(S, episode, step_counter)
while not is_terminated:
A = choose_action(S, q_table)
S_, R = get_env_feedback(S, A)
q_predict = q_table.loc[S, A]
if S_ != 'terminal':
q_target = R + LAMBDA * q_table.iloc[S_, :].max()
else:
q_target = R
is_terminated = True
q_table.loc[S, A] += ALPHA * (q_target - q_predict)
S = S_
update_env(S, episode, step_counter+1)
step_counter += 1
return q_table
测试
rl()
left | right | |
---|---|---|
0 | 0.000002 | 0.005031 |
1 | 0.000001 | 0.027061 |
2 | 0.000007 | 0.111953 |
3 | 0.000204 | 0.343331 |
4 | 0.000810 | 0.745813 |
5 | 0.000000 | 0.000000 |