CS234 value iteration/policy iteration

这篇博客介绍了在CS234课程作业中,如何使用值迭代和策略迭代解决FrozenLake问题。FrozenLake是一个模拟冬季湖面冰洞挑战的游戏,玩家需谨慎行动避免落入水中。通过值迭代和策略迭代方法,作者展示了如何计算最优策略并得到相应状态的价值矩阵。

CS234 Assignment#1 value iteration/policy iteration

这一部分做的是值迭代和策略迭代的,以一个FrozenLake为例子作为测试。
FrozenLake:
Winter is here. You and your friends were tossing around a frisbee at the park
when you made a wild throw that left the frisbee out in the middle of the lake.
The water is mostly frozen, but there are a few holes where the ice has melted.
If you step into one of those holes, you’ll fall into the freezing water.
At this time, there’s an international frisbee shortage, so it’s absolutely imperative that
you navigate across the lake and retrieve the disc.
However, the ice is slippery, so you won’t always move in the direction you intend.
The surface is described using a grid like the following

    SFFF
    FHFH
    FFFH
    HFFG

S : starting point, safe
F : frozen surface, safe
H : hole, fall to your doom
G : goal, where the frisbee is located

The episode ends when you reach the goal or fall in a hole.
You receive a reward of 1 if you reach the goal, and zero otherwise.

CS234 vi_and_pi.py
附注:
这个例子中
0:← // 1:↓// 2:→// 3:↑
参数P中terminal 为true or false
P中的probability在例子中都为1.0,但实际上在不为1的时候
P[state][action]是这样的:
{(probability1, nextstate1, reward1, terminal1)
(probability2, nextstate2, reward2, terminal2)
…..}
value iteration

def value_iteration(P, nS, nA, gamma=0.9, max_iteration=20, tol=1e-3):
    """
    Learn value function and policy by using value iteration method for a given
    gamma and environment.

    Parameters:
    ----------
    P: dictionary
        It is from gym.core.Environment
        P[state][action] is tuples with (probability, nextstate, reward, terminal)
    nS: int
        number of states
    nA: int
        number of actions
    gamma: float
        Discount factor. Number in range [0, 1)
    max_iteration: int
   
### Value Iteration Networks 的实现与应用 Value Iteration Networks (VINs) 是一种结合了强化学习和卷积神经网络的方法,其核心思想是在深度学习模型中嵌入一个类似于值迭代的过程来解决路径规划问题和其他序列决策任务。这种方法通过模仿经典的动态规划中的值迭代过程,在端到端的学习框架下实现了高效的策略优化。 #### VIN 的基本原理 Value Iteration Networks 将传统的值迭代算法融入到神经网络结构中,使得网络能够自动学习环境的状态转移概率以及奖励分布,并在此基础上推导出最优策略[^1]。具体来说,VIN 使用了一个特殊的模块——称为“值迭代模块”,它模拟了经典值迭代的核心计算逻辑: \[ Q(s, a) \leftarrow R(s, a) + \gamma \sum_{s'} P(s'|s,a)\max_{a} Q(s', a) \] 其中 \(R\) 表示即时奖励,\(P\) 表示状态转移概率矩阵,而 \(\gamma\) 则表示折扣因子。这一模块被设计成可以与其他神经网络层无缝集成的形式,从而允许整个系统以端到端的方式进行训练。 #### 实现方法 在实际实现过程中,通常会采用如下步骤构建 VIN 模型: 1. **输入表示**: 首先定义输入特征向量形式,这可能包括地图布局、障碍物位置以及其他相关信息。 2. **值迭代模块**: 构建专门负责执行多次值迭代操作的部分,这部分可以通过循环展开或者固定层数的方式来完成。 3. **策略提取器**: 基于最终得到的价值函数估计结果,利用 softmax 函数或其他机制生成对应的动作偏好得分。 4. **损失函数设定**: 定义适当的监督目标(如交叉熵),以便指导整体参数调整方向。 下面给出一段 Python 伪代码展示如何搭建简单的 VIN 结构: ```python import tensorflow as tf class ValueIterationNetwork(tf.keras.Model): def __init__(self, num_actions=4, iter_count=20): super(ValueIterationNetwork, self).__init__() # Define convolutional layers for state representation extraction. self.conv_layers = [ tf.keras.layers.Conv2D(filters=150, kernel_size=(3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(pool_size=(2, 2)) ] # Create value iteration module with specified number of iterations. self.value_iteration_module = ValueIterationModule(num_iterations=iter_count) # Final policy head which outputs action probabilities based on computed values. self.policy_head = PolicyHead(output_dim=num_actions) def call(self, inputs): x = inputs # Extract features via convolutions. for layer in self.conv_layers: x = layer(x) # Perform iterative updates over the learned reward map & transition dynamics. q_values = self.value_iteration_module(x) # Derive actionable policies from final estimated Q-values. pi = self.policy_head(q_values) return pi ``` 上述代码片段展示了基于 TensorFlow/Keras 平台创建的一个基础版本的 VIN 类定义方式。值得注意的是,“ValueIterationModule” 和 “PolicyHead” 还需进一步细化才能满足特定需求场景下的性能要求。 #### 应用场景 由于其独特的架构特性,Value Iteration Networks 已经成功应用于多个领域之中,主要包括但不限于以下几个方面: - 自动驾驶汽车导航:帮助车辆理解复杂交通状况并做出合理避让动作决定; - 游戏 AI 开发:增强游戏角色行为模式多样性的同时提高胜率表现水平; - 物流配送路线优化:快速找到货物运输最短时间成本解决方案; 这些案例充分证明了将传统规划技术引入现代机器学习工具箱所带来的巨大潜力和发展空间[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值