{'fraction_non_alphanumeric': 0.030562313005061098, 'fraction_numerical': 0.014931633252243939, 'mean_word_length': 4.90554821664465, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
PTR-PPO: Proximal Policy Optimization with Prioritized Trajectory Replay

On-policy deep reinforcement learning algorithms have low data utilization and require significant experience for policy improvement. This paper proposes a proximal policy optimization algorithm with prioritized trajectory replay (PTR-PPO) that combines on-policy and off-policy methods to improve sampling efficiency by prioritizing the replay of trajectories generated by old policies. We first design three trajectory priorities based on the characteristics of trajectories: the first two being max and mean trajectory priorities based on one-step empirical generalized advantage estimation (GAE) values and the last being reward trajectory priorities based on normalized undiscounted cumulative reward. Then, we incorporate the prioritized trajectory replay into the PPO algorithm, propose a truncated importance weight method to overcome the high variance caused by large importance weights under multistep experience, and design a policy improvement loss function for PPO under off-policy conditions. We evaluate the performance of PTR-PPO in a set of Atari discrete control tasks, achieving state-of-the-art performance. In addition, by analyzing the heatmap of priority changes at various locations in the priority memory during training, we find that memory size and rollout length can have a significant impact on the distribution of trajectory priorities and, hence, on the performance of the algorithm.


Introduction
Deep reinforcement learning (DRL) enables agents to improve themselves through trial-and-error learning and has achieved good results in fields such as Go , Schrittwieser et al., 2020, games [Mnih et al., 2015, Vinyals et al., 2019, and automatic control [Duan et al., 2020, Liang et al., 2020a. A good simulation environment can generate a large amount of experience for agent learning through rapid high-fidelity simulation, which is one of the key elements for the success of DRL. However, limited by time and space, hardware and software requirements, the real system cannot accelerate and generate a large number of interactions with the agent. It is difficult to generate a large quantity of data for online agent learning, and there is an urgent need to improve the sample efficiency of DRL algorithms. Existing on-policy DRL methods , Schulman et al., 2017, which cannot use past generation data, give up some rare experience that needs to be replayed for learning that will be useful during training. In off-policy DRL methods [Mnih et al., 2015], to enable the agent to remember and reuse past experience and improve sample efficiency, experience replay mechanisms [Mnih et al., 2015, Wang et al., 2016, Lin, 1992 are used to store experience generated from interactions with the environment, and this experience is sampled evenly during training. However, in the vast majority of experience updates, the state values learned by the agent are simply transferred from one initial state value to another, especially in reward-sparse environments, where only those experiences that jump into or out of the state prior to the endpoint goal result in a change in the value estimate [Sutton and Barto, 2018]. In the case of uniform sampling, many useless update calculations are performed before a useful experience sample is collected, which leads to extremely low sample efficiency in an environment with a large state space.
Intuitively, experience can be prioritized according to importance, and this approach increases the replay probability of important experiences, thereby making learning more efficient. Prioritized experience replay (PER) [Schaul et al., 2015, Horgan et al., 2018 makes the agent preferentially select samples for experience replay and frequently replays those experience samples with high expectations that use temporal-difference error [Sutton and Barto, 2018] to measure the level of priority. As training updates increase, the estimated distribution of values will have bias with respect to the true distribution, and PER reduces the influence of the problem on the training results by introducing importance-sample weights (IS weights) to correct the distribution.
PER provides a better empirical convergence guarantee for the convergence of reinforcement learning algorithms, deriving various RL algorithms in combination with PER: PER-DQN [Horgan et al., 2018], PER-DDPG [Hou et al., 2017], PER-HER [Zhao and Tresp, 2018], PER-NAF [Gu et al., 2016], PER-TD3 [Fujimoto et al., 2018], etc. Moreover, the original PER was applied only from the sampling perspective, and some people [Daley andAmato, 2019, Bu andChang, 2020] have used PER in the experience storage phase as well, focusing more on the preservation of important experiences. Figure 1: PTR-PPO Algorithm Architecture. We use the learner-actor architecture to train the agent. The top-left part shows the actor interacting with multiple environments in parallel, generating multiple experiences, aggregating these experiences to obtain the current trajectories, using the trajectory preference calculation module to obtain the priority of each trajectory, inserting them in the sumtree memory in the priority memory according to the trajectory priority, and sampling the experiences from the sumtree memory to obtain sampling trajectories. Afterward, these trajectories are input into the PTR-PPO loss calculation module to obtain PTR-PPO loss; current trajectories are input into the vanilla PPO loss calculation module to obtain the ppo loss. Both losses are backpropagated and the learner parameters are updated, after which the learner passes the parameters to the actor to generate new trajectories.
However, existing DRL algorithms that combine with PER are off-policy methods. On-policy DRL-based policy gradient (PG) methods , Schulman et al., 2017, Gu et al., 2016, Schulman et al., 2015a, which are popular in deep reinforcement learning research, have extremely low sampling efficiency, which severely limits the algorithm performance. In addition, existing PER methods measure the importance of one-step experience. However, updating state values using one-step experience leads to large bias, especially in reward-sparse environments, and using multistep experience for state value evaluation is one of the advantages of on-policy DRL method pairs. How to use the priority replay mechanism of multistep experience is an important factor to improve the sample efficiency of on-policy PG methods. Among PG methods, proximal policy optimization (PPO) [Schulman et al., 2017], which uses multistep experience for value evaluation and makes the policy steadily improve by limiting the gap between the old and new policy, is an advanced deep reinforcement learning algorithm.
In this paper, we propose a new reinforcement learning algorithm, called proximal policy optimization with prioritized trajectory replay (PTR-PPO), to improve the learning speed of the RL algorithm by improving sample efficiency. The main contributions of this paper are as follows: 1) The obtained trajectory performance is analyzed, and two trajectory priority metrics are proposed: the generalized advantage estimator (GAE) [Schulman et al., 2017, 2015b, Kabir, 2021]-based trajectory priority metric and the trajectory priority metric based on the trajectory undiscounted cumulative reward. Compared with the original temporaldifference error, GAE effectively reduces the estimation bias of experiences in trajectory and is a technique used by the current mainstream reinforcement learning algorithms [Schulman et al., 2017]. According to GAE, the trajectory priority based on the max factor of one-step experience advantage bias and the trajectory priority based on the mean factor of one-step experience advantage bias are designed to measure the priority of the trajectory. According to the trajectory undiscounted cumulative reward, the trajectory priority of the normalized undiscounted cumulative reward is designed to improve the replay probability of experience with significant reward signal and overcome the sparse reward.
2) Based on the trajectory priority, the proximal policy optimization with a prioritized trajectory replay algorithm and framework are proposed to reuse the past important trajectories. PTR-PPO combines the general PPO training process with the prioritized trajectory replay method, and the sampled trajectories and current trajectories are used to jointly update the policy, which improves the sample efficiency and performance of the PPO algorithm.
3) Experiments on Atari benchmarks demonstrate that the proposed PTR-PPO algorithm outperforms or matches PPO [Schulman et al., 2017] and ACER [Wang et al., 2016] across all benchmark tasks in terms of the final performance.
The main structure of the paper is as follows. In Section II, we describe some preliminaries of RL. Section III designs three trajectory priority factors. In Section IV, we present the PTR-PPO architecture and algorithm. In Section V, presents experimental results that show the efficacy of PTR-PPO. Section VI concludes this paper.

Deep Reinforcement Learning
Reinforcement learning aims to find the policy with the maximum cumulative reward in a sequential decision task [Liang et al., 2020b]. In a reinforcement learning task, the agent interacts with the environment over time. At each time step t, the agent receives a state s t in state space S and, according to the policy π(·|s t ), selects an action a t from the action space A to interact with the environment, that is the agent's behavior, after which the environment returns a scalar reward value r t and the next state s t+1 according to the dynamics of the environment (reward function R(s, a, s ) and state transfer function P(s t+1 |s t , a t )). A trial is conducted until the agent reaches the termination state, after which it proceeds to the next trial. The return R t = ∞ k=1 γ k−1 r t+k is a cumulative reward with a discount γ ∈ (0, 1], and the agent's goal is to maximize the return expectation for each state. With the development of deep learning, neural networks are widely used to improve the performance of reinforcement learning. Deep Q learning (DQN) [Mnih et al., 2015] uses a convolutional and feed-forward network with parameter θ to learn the action value function Q θ (s, a). The value network accepts a picture representation of the state s t and outputs a vector containing the value of every action a t . DQN interacts with the environment in a greedy manner − greedy by selecting the action with the highest value from the action value estimate and cannot give a preference for actions with similar action values. The asynchronous advantage actor-critic algorithm (A3C)  consists of two networks, actor π(a|s, θ π )and critic V(s, θ v ), which use a similar state encoding network architecture as DQN, with the former outputting the probability of each action and the latter outputting the state value. A3C is an on-policy RL method, that improves the training sample size and reduces the learning time by an asynchronous approach, and uses a multistep empirical estimation value function, which has a high learning efficiency.

Experience Replay
Experience replay[Sutton and Barto, 2018] uniformly samples experience from replay memory, addressing the limitation that an agent learning online must learn strictly in the order of experienced samples. PER is a widely used technique in experience replay that enables an agent to preferentially select samples for experience replay learning by increasing the sampling probability of important samples. PER replays those experience samples with high expectations more frequently and measures the priority by TD error and its variants. The TD-error variant in the PER-DQN algorithm is calculated in Equation1.
(1) where θ and θ represent the value and target value network parameters, respectively.
By adjusting the parameters that combine PER with uniform sampling, the sharpness of the priority can be weakened; then, the sampling probability of a transition becomes: where p i is the priority of sample i, which is greater than 0; the exponential α is a parameter that determines the priority level, and the case α = 0 corresponds to a random sampling method with equal probability. For the definition of the priority value p i , there are two variants: the first is proportional prioritization, calculated as p i = |δ i | + , where is a small positive number, to enable some special marginal samples with a TD-error value of 0 to be sampled; the second is prioritization based on sorting, where p i = 1 rank(i) , where rank(i) is determined according to the rank |δ i |of the sample i in sorting. For both variants, P(i) is monotonic with respect to |δ i |. The second variant, may be more robust due to its insensitivity to outliers. Both variants can accelerate the convergence of the algorithm, but the first variant is most commonly used in practice.

Proximal Policy Optimization
The starting point in the design of many popular on-policy algorithms is the following policy improvement lower bound, which was first developed by Kakade and Langford[Kakade and Langford, 2002] and later refined by Schulman [Schulman et al., 2015a]: Lemma 1. Consider a current policy π k . For any future policy π, we have: where C π,π k = max s∈S |E a∼π(·|s) [A π k (s, a)]| and T V(π, π k )(s) represents the total variation distance between the distributions π(·|s) and π k (·|s) .
The first term of the lower bound in Lemma 1 is the surrogate objective, and the second term is the penalty term. Note that this approach can guarantee policy improvement at every step of the learning process by choosing the next policy π k+1 to maximize this lower bound. Because the expectations in Lemma 1 depend on the current policy π k , we can approximate this lower bound using samples generated by the current policy.
PPO [Schulman et al., 2017], which has become the default on-policy optimization algorithm due to its strong performance and simple implementation, is theoretically motivated by the policy improvement lower bound in Lemma 1.
Based on Lemma 1, the PPO defines the following surrogate objectives: where π θ represents the current policy, π θ old represents the old policy that generated the sample, andÂ t estimates the value of the advantage function of action a t in state s t . Equation 4 aims to maximize the value of the surrogate objective while constraining the distance between the new policy and the old policy. Moreover, PPO trims the above surrogate objective.

Importance Sampling
For most practical applications of probabilistic models, exact inference is not feasible; some form of approximation must be used. Here, we discuss the use of a probability distribution p(z) to find the expected value of some function f (z), where the element z may be a discrete variable, a continuous variable or a combination of both. In the case of a continuous variable we wish to calculate the following expectation: In the case of a discrete variable the integral term is replaced by a summation term.
Importance sampling [Sutton andBarto, 2018, Braverman et al., 2021] provides a framework for approximating expectations, but does not provide a way to sample from the probability distribution p(z). It is assumed that sampling directly from the distribution p(z) is not possible, but for any given value of z, we can easily compute p(z). Importance sampling is based on the use of the proposed distribution q(z). It is relatively easy to sample from the proposed distribution, after which the expectation is expressed in the form of a finite sum of samples {z l } in q(z), as shown in the following equation.
where r l = p(z (l) ) q(z (l) ) is referred to as the importance weight and corrects for the bias introduced by sampling from the wrong probability distribution.
Off-policy reinforcement learning algorithms [Mnih et al., 2015] reuse early experience based on importance sampling, and the return is weighted according to the relative probability of the trajectory occurring in the target and behavior policy. Given a starting state s t , the probability of a subsequent state-action trajectory a t , s t+1 , a t+1 , ..., s T occurring under policy π is: where p denotes the environmental state shift probability function.
The importance sampling ratio of the target π and behavior policy b is: From equation9, it is clear that the importance sampling ratio is related to only the policy and sample sequence data, and not to the dynamic characteristics of the environment.

Trajectory Priority
Prioritized trajectory replay (PTR) is based on the PER method of sampling trajectories according to importance. In the PER-DQN method, the priority of one-step experience is calculated using equation 1. One-step experience TD-error calculations have small variance but excessive bias, whereas trajectories contain multistep experience and have less bias but larger variance in the estimate.
To better weigh the relationship between variance and bias, GAE [Schulman et al., 2015b] considers bias estimates from multistep experience and obtains an estimate of the advantage of the current action value: where γ and λ are hyperparameters We use the GAE algorithm to calculate the bias in the advantage estimate for each experience in the trajectory, and equation 10 defines the bias in the advantage of one-step experiences. The trajectory τ i = {(s i , a i , r i+1 , s i+1 , d i+1 , p i )} contains multiple steps of experience, and the priority p i τ of the trajectory τ i is related to the advantage bias of the same one-step experience. The priority of a trajectory is defined based on the GAE deviation of one-step experience, and two means of calculating the priority of a trajectory are defined. 1) Max trajectory priority: trajectory priority based on the maximum factor of the one-step experience GAE deviation. As with the PER method, experiences with larger deviations should receive more attention, regardless of the positive or negative value. The priority of a trajectory is obtained by applying the max operation to the priority of the one-step experiences contained in the trajectory, using the absolute value |A t | to denote the priority of the one-step experience.
2) Mean trajectory priority: trajectory prioritization based on one-step empirical GAE deviations of the mean factor. As with the max trajectory priority, the priority of the one-step experience is expressed using the absolute value |A t |, but the priority of the trajectory is obtained by taking the mean operation on the priority of one-step experiences. Figure 2: Trajectory priority based on one-step experience GAE deviation. The one-step empirical advantage deviation for each experience in the trajectory is calculated using GAE and the appropriate value is obtained as the trajectory priority using the max and mean operators.
In addition, a third trajectory priority is defined in this paper.
3) Reward trajectory priority. We define a trajectory priority based on normalized undiscounted cumulative reward.
Since the length of a trajectory is finite, the return, that is, the undiscounted cumulative reward, does not lead to divergent results. To better measure the undiscounted cumulative reward, the undiscounted cumulative reward of the current trajectory are normalized based on the historical average and variance.
where R i is the undiscounted cumulative reward for the current trajectory, X t is the mean cumulative reward for all current collected historical trajectories, and σ is the standard deviation.

PPO with Prioritized Trajectory Replay
In this section, based on the trajectory priority mentioned in the previous section, we combine the trajectory priority with the PPO algorithm and propose the PPO with prioritized trajectory replay (PTR-PPO) algorithm with truncation importance weights. In this paper, we use a network with parameters θ shared by the actor π θ (a|s) and critic V θ (s), and the shared network outputs each action probability and the current state value at the last layer. We now provide the specific procedure for updating the network.  Fig. 1, after which only these two values are relied upon to calculate the priority without updating.

PTR-PPO Policy Evaluation
In this paper, we update the state value function using a general policy evaluation approach, as in the following equation.
where (s, r, s , d, p) denotes the one-step experience in the trajectory and M denotes the trajectory memory, namely, the current trajectory memory and the sampling trajectory memory. If the experience comes from the current trajectory memory, it is an on-policy learning approach and does not require a correction to the value estimate; conversely, if the experience comes from the sampling trajectory memory, the generation of the action a comes from the old policy π θ old , and the calculation of the loss according to Equation 15 is biased and must be learned using an off-policy learning approach, using the importance sampling ratio mentioned in Equation 9 for correction, calculated as: Since we record the probabilities p at the time of the sampled action a, we do not need to store the parameters θ old of the old network.

PTR-PPO Policy Improvement
As with policy evaluation, the experience in policy improvement also comes from two memories, the current trajectories and the sampling trajectories. For the policy improvement objective function in the current trajectories, a combination of Equation 4 and Equation 5 can be used, but the action advantage value A t is obtained using the GAE algorithm calculation described in Equation 10.
Action values from the sampling trajectories, must be corrected them using importance sampling when calculating the action advantage values using the GAE method, as follows: In this paper, multiple environments are used to generate data simultaneously and each environment is simulated cyclically, i.e., a new simulation is started immediately after the end of the previous. With this setup, the done state will appear at an arbitrary position when the trajectory is stored according to a fixed step size. The importance sampling ratio at the end position should not be cumulatively multiplied by the subsequent ratios. Thus, Equation 11 and Equation 17 must be improved to adapt it to any trajectory: When there is no done state in the trajectory or the done state is positioned very far back, the margins of the importance weights in Equation 19 become large, which causes instability in learning. To prevent high variance, we propose truncation importance weights to constrain the bounds of the importance weights. As mentioned in ACER, we split the obtained importance weights into two terms: where [x] + = x if x > 0 and otherwise [x] + = 0. The first term of Equation 20 constrains the upper bound on the importance weights, and the second term ensures that the weight estimate is unbiased, and adjust a large value to 1 when ρ done t:T −1 (1 − marg ). Therefore, the advantage value of GAE under the off-policy is: Thus, the PTR-PPO policy improvement objective is defined by the following equation.
. In the policy gradient class of deep reinforcement learning, a regular term policy entropy loss H(a, θ) is typically added to increase the exploratory nature of the policy, and by observing the loss value of the policy entropy, it is possible to determine the degree of improvement of the policy. Note that, according to the principles of the PPO algorithm, we must record the parameters of the old network θ old to ensure stable improvement in the performance of the policy.

10:
Store transition tuple (s t , a t , r t , s t+1 , d t , p t ) in buffer T

11:
if d t is True then The PTR-PPO algorithm is detailed in Algorithm 1; Fig.1 shows the architecture of algorithm.

Benchmarks
To evaluate our algorithm, we tested its performance on a set of Atari games with discrete action spaces through the OpenAi Gym environment interface, without modifications to the environment. Fig. 4 illustrates the benchmark tasks used in this paper.

Baselines
We compare our proposed algorithm with the PPO and ACER methods, both mainstream reinforcement learning algorithms in the policy gradient class that have been validated and applied in many challenging domains. Using these algorithms as baseline algorithms allows us to effectively judge the performance of our proposed algorithms. All compared algorithms interacted with the environment using the experience generation process shown in Fig. 1, containing 1 learner and 4 actors; in addition, they all had the same network architecture. The network consists of a convolutional layer with 32 8x8 filters with stride 4 followed by another convolutional layer with 64 4x4 filters with stride 2, a final convolutional layer with 64 3x3 filters with stride 1, and a fully connected layer of size 512. Each of the hidden layers is followed by a nonlinear rectifier. The network outputs a softmax policy and V values. The actor and critic networks share a common set of parameters. The hyperparameter settings for the training process are shown in Table 1.

Performance
: We conducted five training runs for each algorithm using random seeds, each lasting 40,000 steps, for a total of 160,000 experiences. Each execution of 1000 steps was followed by one model test, using 10 environments per test, and the mean of the test results was used as the final value for this test. The learning curve for each algorithm is shown in Fig. 5 and the results are shown in Table 2. The experimental results indicate that the proposed PTR-PPO algorithm outperforms or matches the baseline algorithm in all tasks. Compared to the mainstream PPO and ACER algorithms, our algorithm is substantially ahead in all five tasks, including Altantis-v0, Bowling-v0, NameThisGame-v0, Qbert-v0 and UpNdown-v0. In the task Breakout-v0, our algorithm has similar results to these algorithms, but still has a significant advantage. This result indicates that our algorithms have exceeded the state of the art in these benchmarks.
Figure 5: Comparison of scores of different algorithms on 6 tasks. Red indicates the PPO algorithm, green indicates the ACER algorithm, dark blue indicates the PTR-PPO algorithm for max trajectory priority, black indicates the PTR-PPO algorithm for reward trajectory priority, and bright blue indicates the PTR-PPO algorithm for mean trajectory priority. The PTR-PPO algorithm significantly outperforms the PPO and ACER algorithms in five environments. In the Breakout-v0 environment, these algorithms score similarly, but PTR-PPO still has a small score advantage.

Time Efficiency
Fig. 6 compares the time efficiency of different algorithms. The average wall-clock time consumption per 1000 steps of PTR-PPO is comparable to that of PPO and much lower than that of ACER. This is because the PTR-PPO algorithm maintains the same training times as the PPO algorithm, such as 10 training repetitions for the current trajectories in PPO, while PTR-PPO trains the current trajectories twice and samples 8 priority memory trajectories for training. PTR-PPO takes slightly longer than PPO, mainly for data dumping and format conversion. The ACER algorithm requires the use of a serial approach for one-step empirical computation, which takes longer to process the same length of trajectory.

PTR-PPO
A Preprint Figure 6: Algorithm comparison in terms of time efficiency on the Altantis-v0 benchmark. Each boxplot is drawn based on the execution of 1000 steps, including approximately 62 improvements. All evaluations were performed on server with a 2.5 GHz 48 core Intel Xeon Platinum 8163 CPU.

Ablation Studies
Two hyperparameters are included in the PTR-PPO algorithm: 1) the priority memory size, which determines the replay history of the trajectory, with the empirical memory dropping to 0, the algorithm becomes a general PPO; 2) the trajectory rollout length, which indicates how many empirical steps are used for rollout. The longer the trajectory is, the more egalitarian the preference calculation for the trajectory, degrading to uniform sampling, and an overly short trajectory length will increase the bias of the GAE estimate.

Priority memory size comparison
We examined the performance of the algorithm in the Atlantis-v0 environment with different priority memory sizes (32,256,1024) and a rollout length of 16 with max, mean, and reward trajectory priority. As shown in Fig. 7, the best performance and fastest learning were achieved when the memory size was 256, followed by a memory size of 1024 and a memory size of 32. Furthermore, at 40,000 steps, the final values for memory sizes of 256 and 1024 are were similar, both reaching near 25,000, while memory size 32 only learns to 15,000.
To analyze the causes of these phenomena, we output the heatmaps of priority for different memory sizes, as shown in Fig. 8.
The subplots 8(a)-8(c) and 8(d)-8(f) show heatmaps of the trajectory priority for max and mean trajectory priority with memory sizes of 1024, 256 and 32. The heatmap trend for mean trajectory priority is similar to that of max trajectory priority. For a memory size of 1024, the high-priority trajectories were mostly distributed among the earlier trajectories due to the long-established network updates, while for a memory size of 32, all the recent trajectories were sampled back and forth. Each trajectory would be sampled multiple times in each update step, making the policy learning fall into local adjustment and slow learning. For a memory size of 256, the intensity of sample diversity was greater, avoiding the interference of multiple learning of recent trajectories, thereby preventing the large bias of the early trajectory estimation from interfering with trajectory prioritization. Thus, the memory size of 256 works best.
Subplots 8(g), 8(h) and 8(i) show the heatmaps of the trajectory priority for reward trajectory priority with memory sizes of 1024, 256 and 32. Subplot 8(g) shows the heatmap at a memory size of 1024. In the early stages of training, the bright spots are concentrated on the newly acquired trajectories but do not differ much in brightness from other locations, and the sampling tends to uniform sampling. As the number of training sessions increases, the bright spots become more numerous and evenly distributed over all locations. Subplot 8(h) shows the heatmap at a memory size of 256, with significantly more bright spots than that in 8(g), with more bright spots and a wide distribution at all stages of training. Subplot 8(i) shows the heatmap at a memory size of 32, with significantly more bright spots present at all periods, significantly outperforming and dominating the other trajectories. The heatmap indicates that when the memory size is 1024, the priority on the games in the trajectory is similar and the differentiation is not obvious: the differentiation gradually increases as the training progresses. When the memory size is 256, there are more obvious bright spots with good differentiation at all stages of training; and when the memory size is 32, there are individual bright spots with high brightness at all periods, which are sampled in multiple times for training, thereby reducing the diversity of the trajectories. Thus, the best performance is achieved when the memory size is set to 256.

Trajectory rollout length comparison
Finally, we set the memory size to 1024 and tested the effect of different trajectory rollout lengths (4,8,16) on the PTR-PPO algorithm in the Atlantis-v0 environment. As shown in Fig.9, the algorithm performs best when the rollout length is set to 8, second best when it is set to 16. In reward trajectory priority, the learning speed is similar when the rollout length is set to 4 and 8, and the slowest when the length is 16, but the final value is best for length 8, second best for 16 and worst for length 4.
Again, to analyze the causes of this phenomenon, the historical values of the three priorities were recorded and presented in the form of a heatmap, as shown in Fig.10 .
Comparing 10(a), 10(b) and 10(c), we find that 10(b) has an obvious bright spot demarcation when the vertical coordinate is 247, with a clear distribution of trajectory priority differences in the upper part and weak trajectory priority differences in the lower part. Subplot 10(c) has an obvious demarcation when the vertical coordinate is 190, while subplot 10(a) has no obvious demarcation, with the trajectory priority at all stages being significantly different. These demarcation points correspond to different learning stages in Fig. 9(a), with length 16 always in the climbing stage, while length 8, at 22000 steps, enters a smooth stage; and length 4, at 8000 steps, has a clear climb and then falls into a slow learning improvement stage.
Comparing subplots 10(d), 10(e) and 10(f), we find that subplot 10(e) has a clear bright spot demarcation at a vertical coordinate of 309, with a clear distribution of trajectory priority differences in the upper part and weaker trajectory priority differences in the lower part. Subplot 10(f) has a clear demarcation at a vertical coordinate of 228, and subplot 10(d) has a dark section between 230 and 250. Again, these demarcations correspond to different learning stages in Fig.  9(b), with length 16 entering a plateau at 35,000 steps, length 8 reaching a plateau at 20,000 steps, and length 4 having a clear climb until 10,000 steps and then falling into a slow learning improvement phase.
Subplots 10(a)-10(f) are all GAE-based trajectory priorities with the same learning characteristics, and the degree of weight differentiation of the trajectories in memory determines whether the algorithm is in a learning improvement or a smooth learning phase.
Compared to the six subgraphs above, subgraphs 10(g)-10(i) have different characteristics. As the rollout length is adjusted from 16 to 4, the number of bright spots in the three subplots gradually decreases. Subplot 10(g) has a large number of bright spots at each stage, most of which are significant trajectories. By contrast, subplot 10(i), at different stages, has a small number of unevenly distributed bright spots at different locations, while subplot 10(h) falls in between, with the number of bright spots at each stage at an appropriate value. Thus, in the reward trajectory priority, a rollout length that creates a good degree of differentiation in memory works best.

Conclusion
To improve the sample efficiency of on-policy deep reinforcement learning methods, this paper proposes a novel PPO algorithm with prioritized trajectory replay (PTR-PPO), which improves the performance of the PPO algorithm by reusing trajectories with high priority. We first define three trajectory priorities, max and mean trajectory priorities based on GAE, and reward trajectory priorities based on normalized undiscounted cumulative reward. Then, to overcome the high variance caused by the large importance weights, we propose an importance weight estimation method with truncation to define the policy improvement loss function for PPO under off-policy conditions. We evaluate PTR-PPO and other baseline algorithms in a set of Atari tasks. The experimental results show that the PTR-PPO algorithm outperforms the other baseline algorithms.
The three factors have differences in the trajectory weights in the priority memory but share the same network architecture and can share parameters. In future work, we will design an integrated architecture where the three factors are designed as a hyperparameter to call different priority memories at different stages for policy improvement.