PyTorch深度强化学习中蒙特卡洛策略梯度法在短走廊环境(CartPole-v0)中的实战(超详细 附源码)

news2025/3/3 5:08:45

需要源码请点赞关注收藏后评论区留下QQ~~~

一、策略梯度法

策略梯度法(PG)利用策略函数来选择动作,同时使用值函数来辅助策略函数参数的更新,根据策略类型的不同,可以分为随机策略梯度和确定性策略梯度

策略梯度法与值函数逼近法相比优点如下

1:平滑收敛

在学习过程中,PG法每次更新策略函数,权重参数都会朝着最优值变化,且只发生微小变化,有很强的收敛性,值函数逼近法基于贪心策略对策略进行改进,有些价值函数在后期会一直围绕着最优价值函数持续小的震荡而不收敛 即出现策略退化

2:处理连续动作空间任务

PG法可以直接得到一个策略,而值函数逼近法需要对比状态S中的所有动作的价值

3:学习随机策略

PG法能够输出随机策略,而值函数逼近法基于贪心方法,每次输出的都是确定性函数

PG法缺点如下

1:PG法通常只能收敛到局部最优解

2:PG法的易收敛性和学习过程平滑优势,都是使Agent尝试过多的无效探索,从而造成学习效率低,整体策略方差偏大,以及存在累积错误带来的过高估计问题 

二、蒙特卡洛策略梯度法(REINFORCE)

蒙特卡洛策略梯度法是一种针对情节式问题的,基于MC算法的PG法

REINFORCE算法采用MC算法来计算动作值函数,只考虑Agent在状态S下实际采取的动作,该方法可以从理论上保存策略参数的收敛性

1:实验环境设置

引入短走廊网格世界环境  如下图

每步的收益是-1,对于三个非终止状态都有两个动作可以选择,向左或者向右,特殊的是,第一个状态向左走会保持原地不动,而在第二个状态执行的动作会导致向相反的方向移动 

 

部分超参数列表如下

 2:实验结果分析

每个环境下算法的训练情节数均为1000个情节,这是因为两个环境在1000个情节后都能收敛,如下图所示REINFORCE算法在短走廊环境和CartPole环境的效果整体上都呈现稳步上升,后平稳的学习趋势

迭代一千次后如下  (每隔一千次输出一次可视化图像)

迭代两千次后效果如下 

 

 迭代8000次后如下 发现基本已经在高位收敛

 

三、代码

部分源码如下

# 代38-REINFORCE算法的实验过程


#CartPole环境
import argparse
import gym
import numpy as np
from itertools import count

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
import matplotlib.pyplot as plt

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

parser = argparse.ArgumentParser(description='PyTorch REINFORCE example')
parser.add_argument('--gamma', type=float, default=0.99, metavar='G',
                    help='discount factor (default: 0.99)')
parser.add_argument('--seed', type=int, default=543, metavar='N',
                    help='random seed (default: 543)')
parser.add_argument('--render', action='store_true',
                    help='render the environment')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
                    help='interval between training status logs (default: 10)')
args = parser.parse_args()


env = gym.make('CartPole-v1')
env.seed(args.seed)
torch.manual_seed(args.seed)


class Policy(nn.Module):
    def __init__(self):
        super(Policy, self).__init__()
        self.affine1 = nn.Linear(4, 128)
        self.dropout = nn.Dropout(p=0.6)
        self.affine2 = nn.Linear(128, 2)

        self.saved_log_probs = []
        self.rewards = []

    def forward(self, x):
        x = self.affine1(x)
        x = self.dropout(x)
        x = F.relu(x)
        action_scores = self.affine2(x)
        return F.softmax(action_scores, dim=1)


policy = Policy()
optimizer = optim.Adam(policy.parameters(), lr=1e-2)
eps = np.finfo(np.float32).eps.item()


def select_action(state):
    state = torch.from_numpy(state).float().unsqueeze(0)
    probs = policy(state)
    m = Categorical(probs)
    action = m.sample()
    policy.saved_log_probs.append(m.log_prob(action))
    return action.item()


def finish_episode():
    R = 0
    policy_loss = []
    rewards = []
    for r in policy.rewards[::-1]:
        R = r + args.gamma * R
        rewards.insert(0, R)
    rewards = torch.tensor(rewards)
    rewards = (rewards - rewards.mean()) / (rewards.std() + eps)
    for log_prob, reward in zip(policy.saved_log_probs, rewards):
        policy_loss.append(-log_prob * reward)
    optimizer.zero_grad()
    policy_loss = torch.cat(policy_loss).sum()
    policy_loss.backward()
    optimizer.step()
    del policy.rewards[:]
    del policy.saved_log_probs[:]

def plot(epi, run_time):

    plt.title('Training')
    plt.xlabel('Episode')
    plt.ylabel('Run Time')
    plt.plot(epi, run_time, color='red')
    plt.show()

def main():
    running_reward = 10
    running_rewards = []
    i_episodes = []
    for i_episode in range(10000):
        state, ep_reward = env.reset(), 0
        for t in range(1, 10000):  # Don't infinite loop while learning
            action = select_action(state)
            state, reward, done, _ = env.step(action)
            if args.render:
                env.render()
            policy.rewards.append(reward)
            ep_reward += reward
            if done:
                break

        running_reward = 0.05 * ep_reward + (1 - 0.05) * running_reward
        finish_episode()
        running_rewards.append(running_reward)
        i_episodes.append(i_episode)
        if i_episode % args.log_interval == 0:

            print('Episode {}\tLast length: {:2f}\tAverage length: {:.2f}'.format(
                i_episode, ep_reward, running_reward))
        if (i_episode % 1000 == 0):
            plot(i_episodes, running_rewards)
    np.save(f"putu", running_rewards)
if __name__ == '__main__':
    main()



# 短走廊环境
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from tqdm import tqdm

def true_value(p):
    """ True value of the first state
    Args:
        p (float): probability of the action 'right'.
    Returns:
        True value of the first state.
        The expression is obtained by manually solving the easy linear system
        of Bellman equations using known dynamics.
    """
    return (2 * p - 4) / (p * (1 - p))

class ShortCorridor:
    """
    Short corridor environment, see Example 13.1
    """
    def __init__(self):
        self.reset()

    def reset(self):
        self.state = 0

    def step(self, go_right):
        """
        Args:
            go_right (bool): chosen action
        Returns:
            tuple of (reward, episode terminated?)
        """
        if self.state == 0 or self.state == 2:
            if go_right:
                self.state += 1
            else:
                self.state = max(0, self.state - 1)
        else:
            if go_right:
                self.state -= 1
            else:
                self.state += 1

        if self.state == 3:
            # terminal state
            return 0, True
        else:
            return -1, False

def softmax(x):
    t = np.exp(x - np.max(x))
    return t / np.sum(t)

class ReinforceAgent:
    """
    ReinforceAgent that follows algorithm
    'REINFORNCE Monte-Carlo Policy-Gradient Control (episodic)'
    """
    def __init__(self, alpha, gamma):
        # set values such that initial conditions correspond to left-epsilon greedy
        self.theta = np.array([-1.47, 1.47])
        self.alpha = alpha
        self.gamma = gamma
        # first column - left, second - right
        self.x = np.array([[0, 1],
                           [1, 0]])
        self.rewards = []
        self.actions = []

    def get_pi(self):
        h = np.dot(self.theta, self.x)
        t = np.exp(h - np.max(h))
        pmf = t / np.sum(t)
        # never become deterministic,
        # guarantees episode finish
        imin = np.argmin(pmf)
        epsilon = 0.05

        if pmf[imin] < epsilon:
            pmf[:] = 1 - epsilon
            pmf[imin] = epsilon

        return pmf

    def get_p_right(self):
        return self.get_pi()[1]

    def choose_action(self, reward):
        if reward is not None:
            self.rewards.append(reward)

        pmf = self.get_pi()
        go_right = np.random.uniform() <= pmf[1]
        self.actions.append(go_right)

        return go_right

    def episode_end(self, last_reward):
        self.rewards.append(last_reward)

        # learn theta
        G = np.zeros(len(self.rewards))
        G[-1] = self.rewards[-1]

        for i in range(2, len(G) + 1):
            G[-i] = self.gamma * G[-i + 1] + self.rewards[-i]

        gamma_pow = 1

        for i in range(len(G)):
            j = 1 if self.actions[i] else 0
            pmf = self.get_pi()
            grad_ln_pi = self.x[:, j] - np.dot(self.x, pmf)
            update = self.alpha * gamma_pow * G[i] * grad_ln_pi

            self.theta += update
            gamma_pow *= self.gamma

        self.rewards = []
        self.actions = []

class ReinforceBaselineAgent(ReinforceAgent):
    def __init__(self, alpha, gamma, alpha_w):
        super(ReinforceBaselineAgent, self).__init__(alpha, gamma)
        self.alpha_w = alpha_w
        self.w = 0

    def episode_end(self, last_reward):
        self.rewards.append(last_reward)

        # learn theta
        G = np.zeros(len(self.rewards))
        G[-1] = self.rewards[-1]

        for i in range(2, len(G) + 1):
            G[-i] = self.gamma * G[-i + 1] + self.rewards[-i]

        gamma_pow = 1

        for i in range(len(G)):
            self.w += self.alpha_w * gamma_pow * (G[i] - self.w)

            j = 1 if self.actions[i] else 0
            pmf = self.get_pi()
            grad_ln_pi = self.x[:, j] - np.dot(self.x, pmf)
            update = self.alpha * gamma_pow * (G[i] - self.w) * grad_ln_pi

            self.theta += update
            gamma_pow *= self.gamma

        self.rewards = []
        self.actions = []

def trial(num_episodes, agent_generator):
    env = ShortCorridor()
    agent = agent_generator()

    rewards = np.zeros(num_episodes)
    for episode_idx in range(num_episodes):
        rewards_sum = 0
        reward = None
        env.reset()

        while True:
            go_right = agent.choose_action(reward)
            reward, episode_end = env.step(go_right)
            rewards_sum += reward

            if episode_end:
                agent.episode_end(reward)
                break

        rewards[episode_idx] = rewards_sum

    return rewards

def example_13_1():
    epsilon = 0.05
    fig, ax = plt.subplots(1, 1)

    # Plot a graph
    p = np.linspace(0.01, 0.99, 100)
    y = true_value(p)
    ax.plot(p, y, color='red')

    # Find a maximum point, can also be done analytically by taking a derivative
    imax = np.argmax(y)
    pmax = p[imax]
    ymax = y[imax]
    ax.plot(pmax, ymax, color='green', marker="*", label="optimal point: f({0:.2f}) = {1:.2f}".format(pmax, ymax))

    # Plot points of two epsilon-greedy policies
    ax.plot(epsilon, true_value(epsilon), color='magenta', marker="o", label="epsilon-greedy left")
    ax.plot(1 - epsilon, true_value(1 - epsilon), color='blue', marker="o", label="epsilon-greedy right")

    ax.set_ylabel("Value of the first state")
    ax.set_xlabel("Probability of the action 'right'")
    ax.set_title("Short corridor with switched actions")
    ax.set_ylim(ymin=-105.0, ymax=5)
    ax.legend()

    plt.savefig('../images/example_13_1.png')
    plt.close()

def figure_13_1():
    num_trials = 100
    num_episodes = 1000
    gamma = 1
    agent_generators = [lambda : ReinforceAgent(alpha=2e-4, gamma=gamma),
                        lambda : ReinforceAgent(alpha=2e-5, gamma=gamma),
                        lambda : ReinforceAgent(alpha=2e-3, gamma=gamma)]
    labels = ['alpha = 2e-4',
              'alpha = 2e-5',
              'alpha = 2e-3']

    rewards = np.zeros((len(agent_generators), num_trials, num_episodes))

    for agent_index, agent_generator in enumerate(agent_generators):
        for i in tqdm(range(num_trials)):
            reward = trial(num_episodes, agent_generator)
            rewards[agent_index, i, :] = reward

    plt.plot(np.arange(num_episodes) + 1, -11.6 * np.ones(num_episodes), ls='dashed', color='red', label='-11.6')
    for i, label in enumerate(labels):
        plt.plot(np.arange(num_episodes) + 1, rewards[i].mean(axis=0), label=label)
    plt.ylabel('total reward on episode')
    plt.xlabel('episode')
    plt.legend(loc='lower right')

    plt.savefig('../images/figure_13_1.png')
    plt.close()

def figure_13_2():
    num_trials = 100
    num_episodes = 1000
    alpha = 2e-4
    gamma = 1
    ageninforce with baseline']

    rewards = np.zeros((len(agent_generators), num_trials, num_episodes))

    for agent_index, agent_generator in enumerate(agent_generators):
        for i in tqdm(range(num_trials)):
            reward = trial(num_episodes, agent_generator)
            rewards[agent_index, i, :] = reward

    plt.plot(np.arange(num_episodes) + 1, -11.6 * np.ones(num_episodes), ls='dashed', color='red', label='-11.6')
    for i, label in enumerate(labels):
        plt.plot(np.arange(num_episodes) + 1, rewards[i].mean(axis=0), label=label)
  _':
    example_13_1()
    figure_13_1()
    figure_13_2()

创作不易  觉得有帮助请点赞关注收藏~~~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/38672.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

1990-2021年全国各省城市化水平数据

1990-2021年全国各省城市化水平数据 1、包括&#xff1a;全国31省 2、时间&#xff1a;1990-2021年 3、指标包括&#xff1a;城镇常住人口数(万人)、常住人口数(万人)、城市化水平 4、指标衡量&#xff1a;城市化率本地区城镇人口/总人口 5、指标解释&#xff1a; 城市化…

C++ 语法基础课5 —— 字符串

文章目录1. 字符与整数的联系 —— ASCII码1.1 练习 — 统计数字和字母个数2. 字符数组(记得多\0)2.1 字符数组的输入输出2.2 字符数组的常用操作2.3 遍历字符数组中的字符3. 标准库类型 string3.1 定义和初始化3.2 string 上的操作3.2.1 string的读写3.2.2 使用getline读取一整…

操作系统之启动过程

本文参考MOOC哈工大操作系统课程&#xff0c;需要有一定的汇编基础 打开电源后&#xff0c;计算机做了什么 此时需要先运行的代码是BIOS x86 PC开机后CPU处于实模式&#xff0c;寻址方式为CS:IP&#xff08;CS左移4位IP&#xff09;开机时&#xff0c;初始化的CS0xFFFF&…

_linux 进程间通信(命名管道)

文章目录1. 创建一个命名管道2. 用命名管道实现server&client通信2.0 log.hpp打印日志信息2.1 comm.hpp(server.cc和client.cc共有文件)2.2 server.cc读取数据2.3 client.cc发送数据2.4 结果展示3. 还可以多个进程接收1. 创建一个命名管道 命名管道可以从命令行上创建&…

微服务介绍微服务环境搭建

一、微服务介绍 从互联网早起到现在&#xff0c;系统架构大体经历了下面几个过程: 单体应用架构--->垂直应用架构--->分布 式架构--->SOA架构--->微服务架构&#xff0c;当然还有悄然兴起的Service Mesh(服务网格化)。 微服务架构 微服务架构在某种程度上是面向…

机械原理复习试题及答案

机械原理 一、填空题&#xff1a; 1.机构具有确定运动的条件是机构的自由度数等于 。 2.同一构件上各点的速度多边形必 于对应点位置组成的多边形。 3.在转子平衡问题中&#xff0c;偏心质量产生的惯性力可以用 相对地表示。 4.机械系统的等效力学模型是具有 &#xff0c;其上作…

Word文档中经常会遇到字体间距突然变得很大,怎么处理?

Word文档中经常会遇到字体间距突然变得很大&#xff0c;怎么处理&#xff1f; 目录 Word文档中经常会遇到字体间距突然变得很大&#xff0c;怎么处理&#xff1f; 1、如下图字体间距突然变大。 可能原因一&#xff1a; 1、选中该段文字&#xff0c;鼠标右键选择【段落 】 …

【882. 细分图中的可到达节点】

来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 描述&#xff1a; 给你一个无向图&#xff08;原始图&#xff09;&#xff0c;图中有 n 个节点&#xff0c;编号从 0 到 n - 1 。你决定将图中的每条边 细分 为一条节点链&#xff0c;每条边之间的新节点数各不相同。 图…

进程替换与复制

目录进程替换基础知识什么是进程替换进程替换函数函数使用execlexeclpexecleexecvexecvpexecve进程替换基础知识 什么是进程替换 进程替换&#xff1a;把当前进程换为其他进程执行。&#xff08;其他进程&#xff1a;创建新进程&#xff0c;生成可执行程序&#xff0c;装在进…

Android 13 Wi-Fi状态机流程及Log分析

本文基于Android 13源码解读,对Wi-Fi状态机调用流程进行梳理,并结合Log进行分析,便于大家理解Wi-Fi模块调用流程。 梳理出Wi-Fi状态机共有如下几种状态: mConnectableState mConnectingOrConnectedState mL2ConnectingState mL2ConnectedState mL3ProvisioningState …

【机器学习项目实战10例目录】项目详解 + 数据集 + 完整源码

前言 大家好&#xff0c;我是阿光。 本专栏整理了《机器学习项目实战10例》&#xff0c;内包含了各种不同的入门级机器学习项目&#xff0c;包含项目原理以及源码&#xff0c;每一个项目实例都附带有完整的代码数据集。 正在更新中~ ✨ &#x1f6a8; 我的项目环境&#xff…

《计算机体系结构量化研究方法第6版》1.4 技术趋势

引入 1、以下五种技术是现代计算机实现所不可或缺的。 &#xff08;1&#xff09;集成电路逻辑技术。晶体管密度每年大约增加35%&#xff0c;相当于每4年翻两番。晶片大小的增长速度比较难以预测&#xff0c;也慢一些&#xff0c;增速为每年10%~ 20%。两者综合起来&#xff0c…

【Linux】CentOS 7安装 MySQL

1. 更改Centos YUM 源 1&#xff09; 备份 yum 源配置文件 &#xff08;在根目录下执行&#xff09; mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak2&#xff09;将官方的 yum 源换成阿里的yum源 wget -O /etc/yum.repos.d/CentOS-Base.repo…

Day12--渲染二级和三级分类列表

1.动态渲染右侧的二级分类列表 我的操作&#xff1a; 1》在cate.vue中&#xff1a; 2》效果图&#xff1a; *********************************** *********************************** *********************************** 2.循环渲染右侧二级分类列表的 UI 结构&#xff1…

RabbitMQ快速入门

中间件&消息队列 中间件概述 中间件&#xff08;Middleware&#xff09;是处于操作系统和应用程序之间的软件&#xff0c;也有人认为它应该属于操作系统中的一部分。人们在使用中间件时&#xff0c;往往是一组中间件集成在一起&#xff0c;构成一个平台&#xff08;包括开…

你不能错过的【Python爬虫】测试3(爬取所有内容 + 完整源代码 + 架构 + 结果)

目录 一、主要工具包 以及 版本二、架构展示三、各部分code3.1 yjs.py (重要)3.2 items.py3.3 middlewares.py3.4 pipelines.py3.5 settings.py3.6 start.py四、结果展示一、主要工具包 以及 版本 scrapy:2.7.1版本(这里主要用到的工具包) 二、架构展示 三、各部分code 3…

MySQL体系-日志与MVCC(源码层面)

MySQL 本身具备生产binlog日志的功能&#xff0c;在InnoDB存储引擎中&#xff0c;为了持久性有了redo log,为了原子性和隔离性有了undo log&#xff0c;最终通过redo log undo log 保证了一致性&#xff1b; 我先画一个InnoDB操作流程&#xff0c;先简单的了解下它们的工作机制…

基于S2SH的保险业务管理系统【数据库设计、源码、开题报告】

数据库脚本下载地址&#xff1a; https://download.csdn.net/download/itrjxxs_com/86467452 主要使用技术 SpringStruts2HibernateJSPJSCSSMysql 功能介绍 本系统旨在为当今的保险行业提供一套综合性的管理系统业务&#xff0c;系统的主要用户为保险的购买者以及系统的管理…

10.实用调试技巧

一、调试 1.调试的定义 调试&#xff08;英语&#xff1a;Debugging / Debug&#xff09;&#xff0c;又称除错&#xff0c;是发现和减少计算机程序或电子仪器设备中程序 错误的一个过程。 2.调试的基本步骤 发现程序错误的存在 以隔离、消除等方式对错误进行定位 确定错误产…

Linux系统编程(三)——Linux下的进程

第一篇中总结了系统的环境搭建&#xff0c;第二篇中学习了系统的一些IO函数&#xff0c;接下来就深入到了Linux下的进程线程的实现。 目录 0x01 进程概述 一、进程的信息 二、程序与进程 三、并行与并发 四、进程控制块PCB 0x02 进程状态转换 一、进程的状态 二、进程相…