Пример #1
0
    def finish_horizon(self, last_val=0):
        """
        TODO: These codes are completly identical to the ones defined in on_policy_trainer.py. Use it.
        """
        samples = self.local_buffer._encode_sample(
            np.arange(self.local_buffer.get_stored_size()))
        rews = np.append(samples["rew"], last_val)
        vals = np.append(samples["val"], last_val)

        # GAE-Lambda advantage calculation
        deltas = rews[:-1] + self._policy.discount * vals[1:] - vals[:-1]
        if self._policy.enable_gae:
            advs = discount_cumsum(deltas,
                                   self._policy.discount * self._policy.lam)
        else:
            advs = deltas

        # Rewards-to-go, to be targets for the value function
        rets = discount_cumsum(rews, self._policy.discount)[:-1]
        self.replay_buffer.add(obs=samples["obs"],
                               act=samples["act"],
                               done=samples["done"],
                               ret=rets,
                               adv=advs,
                               logp=np.squeeze(samples["logp"]))
        self.local_buffer.clear()
Пример #2
0
    def finish_horizon(self, last_val=0):
        """
        Call this at the end of a trajectory, or when one gets cut off
        by an epoch ending. This looks back in the buffer to where the
        trajectory started, and uses rewards and value estimates from
        the whole trajectory to compute advantage estimates with GAE-Lambda,
        as well as compute the rewards-to-go for each state, to use as
        the targets for the value function.
        The "last_val" argument should be 0 if the trajectory ended
        because the agent reached a terminal state (died), and otherwise
        should be V(s_T), the value function estimated for the last state.
        This allows us to bootstrap the reward-to-go calculation to account
        for timesteps beyond the arbitrary episode horizon (or epoch cutoff).
        """
        samples = self.local_buffer._encode_sample(
            np.arange(self.local_buffer.get_stored_size()))
        rews = np.append(samples["rew"], last_val)
        vals = np.append(samples["val"], last_val)

        # GAE-Lambda advantage calculation
        deltas = rews[:-1] + self._policy.discount * vals[1:] - vals[:-1]
        if self._policy.enable_gae:
            advs = discount_cumsum(
                deltas, self._policy.discount * self._policy.lam)
        else:
            advs = deltas

        # Rewards-to-go, to be targets for the value function
        rets = discount_cumsum(rews, self._policy.discount)[:-1]
        self.replay_buffer.add(
            obs=samples["obs"], act=samples["act"], done=samples["done"],
            ret=rets, adv=advs, logp=np.squeeze(samples["logp"]))
        self.local_buffer.clear()
Пример #3
0
 def test_discount_cumsum(self):
     rewards = np.ones(shape=(3, ), dtype=np.float32)
     discount = 0.99
     expected = np.array(
         [1 + discount + discount * discount, 1 + discount, 1],
         dtype=np.float32)
     results = discount_cumsum(rewards, discount)
     np.testing.assert_allclose(expected, results)
Пример #4
0
 def test_discount_cumsum(self):
     x = np.array([1., 1., 1.])
     discount = 0.99
     expected = [
         1. + 1.*discount**1 + 1.*discount**2,
         1. + 1.*discount**1,
         1.]
     results = discount_cumsum(x, discount)
     np.testing.assert_array_equal(results, expected)