Sutton and Barto 书籍的算法实现

这个仓库包含了对 Sutton 和 Barto 的强化学习书籍中算法的实现。

仓库地址: ivanbelenky/RL

描述:

R.L. 方法和技术。

License

MIT license

47 stars 5 forks

项目导航:

目录结构:

仓库包含以下目录和文件:

Reinforcement Learning

License: MIT Python 3.8

安装

setup.py

使用 setup.py 安装:

$ python setup.py install

概述

本仓库包含的代码实现了 Sutton 的强化学习书籍中的算法和模型。《Reinforcement Learning: An Introduction》这本书是该领域的经典教材,对该主题进行了全面的介绍。

仓库中的代码被组织成几个模块,每个模块涵盖不同的主题。

方法

所有无模型的求解器都可以通过定义 statesactionstrasition 函数来工作。Transitions 被定义为一个函数,该函数接受一个状态和一个动作,并返回一个由下一个状态和奖励组成的元组。transition 函数还会返回一个布尔值,指示 episode 是否已终止。

states: Sequence[Any]
actions: Sequence[Any]
transtion: Callable[[Any, Any], Tuple[Tuple[Any, float], bool]]

示例

Single State Infinite Variance Example 5.5

from mypyrl import off_policy_mc, ModelFreePolicy
states = [0]
actions = ['left', 'right']
def single_state_transition(state, action):
  if action == 'right':
    return (state, 0), True
  if action == 'left':
    threshold = np.random.random()
    if threshold > 0.9:
      return (state, 1), True
    else:
      return (state, 0), False
b = ModelFreePolicy(actions, states) #by default equiprobable
pi = ModelFreePolicy(actions, states)
pi.pi[0] = np.array([1, 0])
# calculate ordinary and weighted samples state value functions
vqpi_ord, samples_ord = off_policy_mc(states, actions, single_state_transition,
  policy=pi, b=b, ordinary=True, first_visit=True, gamma=1., n_episodes=1E4)
vqpi_w, samples_w = off_policy_mc(states, actions, single_state_transition, 
  policy=pi, b=b, ordinary=False, first_visit=True, gamma=1., n_episodes=1E4)

Monte Carlo Tree Search 迷宫求解图

s = START_XY
budget = 500
cp = 1/np.sqrt(2)
end = False
max_steps = 50
while not end:
  action, tree = mcts(s, cp, budget, obstacle_maze, action_map, max_steps, eps=1)
  (s, _), end = obstacle_maze(s, action)
tree.plot()

贡献

虽然此包中的代码提供了书中算法的基本实现,但它不一定是最高效或编写最好的。 如果您有改进代码的建议,请随时提出 issue。

总的来说,这个包为任何有兴趣学习强化学习并从头开始实现算法的人提供了宝贵的资源。 但绝不是生产就绪的。

主题

reinforcement-learning qlearning q-learning markov sarsa gridworld markov-decision-processes tabular-methods

贡献者

编程语言