鱼C论坛

 找回密码
 立即注册
查看: 3473|回复: 1

[技术交流] 《用Python动手学习强化学习》【策略迭代】【动态规划】

[复制链接]
发表于 2021-9-20 15:56:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码出处:《用Python动手学习强化学习》第二章:基于动态规划的策略学习(策略迭代)

  1. class PolicyIterationPlanner(Planner):

  2.     def __init__(self, env):
  3.         super().__init__(env)
  4.         self.policy = {}

  5.     def initialize(self):
  6.         super().initialize()
  7.         self.policy = {}
  8.         actions = self.env.actions
  9.         states = self.env.states
  10.         for s in states:
  11.             self.policy[s] = {}
  12.             for a in actions:
  13.                 # 初始化策略
  14.                 # 一开始时各种行动的概率都是一样的
  15.                 self.policy[s][a] = 1 / len(actions)

  16.     def estimate_by_policy(self, gamma, threshold):
  17.         V = {}
  18.         for s in self.env.states:
  19.             # 初始化各种状态的期望奖励
  20.             V[s] = 0

  21.         while True:
  22.             delta = 0
  23.             for s in V:
  24.                 expected_rewards = []
  25.                 for a in self.policy[s]:
  26.                     action_prob = self.policy[s][a]
  27.                     r = 0
  28.                     for prob, next_state, reward in self.transitions_at(s, a):
  29.                         r += action_prob * prob * \
  30.                              (reward + gamma * V[next_state])
  31.                     expected_rewards.append(r)
  32.                 value = sum(expected_rewards)
  33.                 delta = max(delta, abs(value - V[s]))
  34.                 V[s] = value
  35.             if delta < threshold:
  36.                 break

  37.         return V

  38.     def plan(self, gamma=0.9, threshold=0.0001):
  39.         self.initialize()
  40.         states = self.env.states
  41.         actions = self.env.actions

  42.         def take_max_action(action_value_dict):
  43.             return max(action_value_dict, key=action_value_dict.get)

  44.         while True:
  45.             update_stable = True
  46.             # 在当前的策略下估计期望奖励
  47.             V = self.estimate_by_policy(gamma, threshold)
  48.             self.log.append(self.dict_to_grid(V))

  49.             for s in states:
  50.                 # 在当前的策略下得到行动
  51.                 policy_action = take_max_action(self.policy[s])

  52.                 # 与其他行动比较
  53.                 action_rewards = {}
  54.                 for a in actions:
  55.                     r = 0
  56.                     for prob, next_state, reward in self.transitions_at(s, a):
  57.                         r += prob * (reward + gamma * V[next_state])
  58.                     action_rewards[a] = r
  59.                 best_action = take_max_action(action_rewards)
  60.                 if policy_action != best_action:
  61.                     update_stable = False

  62.                 # 更新策略(设置 best_action prob=1, otherwise=0 (贪婪))
  63.                 for a in self.policy[s]:
  64.                     prob = 1 if a == best_action else 0
  65.                     self.policy[s][a] = prob

  66.             if update_stable:
  67.                 # 如果策略没有更新,则停止迭代
  68.                 break

  69.         # 将字典转换为二维数组
  70.         V_grid = self.dict_to_grid(V)
  71.         return V_grid
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-23 16:54:13 | 显示全部楼层
沙发?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 13:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表