Q Learning 算法伪代码
https://blog.csdn.net/qq_30615903/article/details/80739243
https://morvanzhou.github.io/tutorials/machine-learning/reinforcement-learning/2-1-A-q-learning/
由于Qtable的大小有限制,所以Q Learning比较适合状态空间和动作空间都为离散的情况。
学习资源:莫烦python
import numpy as np
import pandas as pd
import time
# numpy 随机数种子,便于每次运行产生相同的结果
np.random.seed(2)
# 算法参数设置
N_STATES = 6 # 状态数
ACTIONS = ['left', 'right'] # 动作空间
EPSILON = 0.9 # greedy算法的参数,随机数大于该值,则按照 q_table 选取动作,否则随机选取动作
ALPHA = 0.1 # 学习率
GAMMA = 0.9 # 衰减系数
MAX_EPISODES = 13 # 最大训练轮数
FRESH_TIME = 0.3 # 环境更新间隔
# 初始化 q_table
def build_q_table(n_states, actions):
table = pd.DataFrame(
np.zeros((n_states, len(actions