PPO算法实战

原理简介

PPO是一种on-policy算法,具有较好的性能,其前身是TRPO算法,也是policy gradient算法的一种,它是现在 OpenAI 默认的强化学习算法,具体原理可参考PPO算法讲解。PPO算法主要有两个变种,一个是结合KL penalty的,一个是用了clip方法,本文实现的是后者即PPO-clip

伪代码

要实现必先了解伪代码,伪代码如下:
在这里插入图片描述
这是谷歌找到的一张比较适合的图,本人比较懒就没有修改,上面的k就是第k个episode,第六步是用随机梯度下降的方法优化,这里的损失函数(即argmax后面的部分)可能有点难理解,可参考PPO paper,如下:
在这里插入图片描述
第七步就是一个平方损失函数,即实际回报与期望回报的差平方。

代码实战

点击查看完整代码

PPOmemory

首先第三步需要搜集一条轨迹信息,我们可以定义一个PPOmemory来存储相关信息:

class PPOMemory:
    def __init__(self, batch_size):
        self.states = []
        self.probs = []
        self.vals = []
        self.actions = []
        self.rewards = []
        self.dones = []
        self.batch_size = batch_size
    def sample(self):
        batch_step = np.arange(0, len(self.states), self.batch_size)
        indices = np.arange(len(self.states), dtype=np.int64)
        np.random.shuffle(indices)
        batches = [indices[i:i+self.batch_size] for i in batch_step]
        return np.array(self.states),\
                np.array(self.actions),\
                np.array(self.probs),\
                np.array(self.vals),\
                np.array(self.rewards),\
                np.array(self.dones),\
                batches

    def push(self, state, action, probs, vals, reward, done):
        self.states.append(state)
        self.actions.append(action)
        self.probs.append(probs)
        self.vals.append(vals)
        self.rewards.append(reward)
        self.dones.append(done)

    def clear(self):
        self.states = []
        self.probs = []
        self.actions = []
        self.rewards = []
        self.dones = []
        self.vals = []

这里的push函数就是将得到的相关量放入memory中,sample就是随机采样出来,方便第六步的随机梯度下降。

PPO model

model就是actor和critic两个网络了:

import torch.nn as nn
from torch.distributions.categorical import Categorical
class Actor(nn.Module):
    def __init__(self,state_dim, action_dim,
            hidden_dim=256):
        super(Actor, self).__init__()

        self.actor = nn.Sequential(
                nn.Linear(state_dim, hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, action_dim),
                nn.Softmax(dim=-1)
        )
    def forward(self, state):
        dist = self.actor(state)
        dist = Categorical(dist)
        return dist

class Critic(nn.Module):
    def __init__(self, state_dim,hidden_dim=256):
        super(Critic, self).__init__()
        self.critic = nn.Sequential(
                nn.Linear(state_dim, hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, hidden_dim),
                nn.ReLU(),
                nn.Linear(hidden_dim, 1)
        )
    def forward(self, state):
        value = self.critic(state)
        return value

这里Actor就是得到一个概率分布(Categorica,也可以是别的分布,可以搜索torch distributionsl),critc根据当前状态得到一个值,这里的输入维度可以是state_dim+action_dim,即将action信息也纳入critic网络中,这样会更好一些,感兴趣的小伙伴可以试试。

PPO update

定义一个update函数主要实现伪代码中的第六步和第七步:

def update(self):
    for _ in range(self.n_epochs):
        state_arr, action_arr, old_prob_arr, vals_arr,\
        reward_arr, dones_arr, batches = \
                self.memory.sample()
        values = vals_arr
        ### compute advantage ###
        advantage = np.zeros(len(reward_arr), dtype=np.float32)
        for t in range(len(reward_arr)-1):
            discount = 1
            a_t = 0
            for k in range(t, len(reward_arr)-1):
                a_t += discount*(reward_arr[k] + self.gamma*values[k+1]*\
                        (1-int(dones_arr[k])) - values[k])
                discount *= self.gamma*self.gae_lambda
            advantage[t] = a_t
        advantage = torch.tensor(advantage).to(self.device)
        ### SGD ###
        values = torch.tensor(values).to(self.device)
        for batch in batches:
            states = torch.tensor(state_arr[batch], dtype=torch.float).to(self.device)
            old_probs = torch.tensor(old_prob_arr[batch]).to(self.device)
            actions = torch.tensor(action_arr[batch]).to(self.device)
            dist = self.actor(states)
            critic_value = self.critic(states)
            critic_value = torch.squeeze(critic_value)
            new_probs = dist.log_prob(actions)
            prob_ratio = new_probs.exp() / old_probs.exp()
            weighted_probs = advantage[batch] * prob_ratio
            weighted_clipped_probs = torch.clamp(prob_ratio, 1-self.policy_clip,
                    1+self.policy_clip)*advantage[batch]
            actor_loss = -torch.min(weighted_probs, weighted_clipped_probs).mean()
            returns = advantage[batch] + values[batch]
            critic_loss = (returns-critic_value)**2
            critic_loss = critic_loss.mean()
            total_loss = actor_loss + 0.5*critic_loss
            self.actor_optimizer.zero_grad()
            self.critic_optimizer.zero_grad()
            total_loss.backward()
            self.actor_optimizer.step()
            self.critic_optimizer.step()
    self.memory.clear()

该部分首先从memory中提取搜集到的轨迹信息,然后计算gae,即advantage,接着使用随机梯度下降更新网络,最后清除memory以便搜集下一条轨迹信息。

最后实现效果如下:
在这里插入图片描述

  • 11
    点赞
  • 82
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
Floyd算法是一种用于解决任意两点之间最短路径问题的动态规划算法。它可以在存在负权边和环的情况下仍然保证正确性。然而,Floyd算法本身并不直接用于避障问题,它更多地用于寻找最短路径。 如果你想要使用Python实现避障算法,可以考虑使用其他算法,如A*算法或Dijkstra算法。这些算法更适合用于避障问题,因为它们可以根据给定的启发式函数或权重来找到最优路径。 以下是一个使用A*算法实现避障的Python示例: ```python import heapq def astar(start, goal, obstacles): open_list = [(0, start)] closed_set = set() g_score = {start: 0} f_score = {start: heuristic(start, goal)} while open_list: current = heapq.heappop(open_list)[1] if current == goal: return reconstruct_path(goal) closed_set.add(current) for neighbor in get_neighbors(current): if neighbor in closed_set: continue tentative_g_score = g_score[current] + distance(current, neighbor) if neighbor not in g_score or tentative_g_score < g_score[neighbor]: g_score[neighbor] = tentative_g_score f_score[neighbor] = tentative_g_score + heuristic(neighbor, goal) heapq.heappush(open_list, (f_score[neighbor], neighbor)) return None def heuristic(node, goal): # 计算启发式函数的值 return abs(node[0] - goal[0]) + abs(node[1] - goal[1]) def distance(node1, node2): # 计算两个节点之间的距离 return abs(node1[0] - node2[0]) + abs(node1[1] - node2[1]) def get_neighbors(node): # 获取一个节点的邻居节点 neighbors = [] # 在这里添加获取邻居节点的逻辑 return neighbors def reconstruct_path(goal): # 重构路径 path = [] # 在这里添加重构路径的逻辑 return path # 示例使用 start = (0, 0) goal = (5, 5) obstacles = [(2, 2), (3, 3), (4, 4)] path = astar(start, goal, obstacles) print("Path:", path) ``` 这个示例使用A*算法来寻找从起点到目标点的最优路径,并避开了给定的障碍物。你可以根据实际情况修改`get_neighbors`函数来获取节点的邻居节点,以及修改`heuristic`和`distance`函数来计算启发式函数的值和节点之间的距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值