{'fraction_non_alphanumeric': 0.04900015870496747, 'fraction_numerical': 0.011545786383113791, 'mean_word_length': 4.301851072780816, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Environment Shaping in Reinforcement Learning using State Abstraction

One of the central challenges faced by a reinforcement learning (RL) agent is to effectively learn a (near-)optimal policy in environments with large state spaces having sparse and noisy feedback signals. In real-world applications, an expert with additional domain knowledge can help in speeding up the learning process via \emph{shaping the environment}, i.e., making the environment more learner-friendly. A popular paradigm in literature is \emph{potential-based reward shaping}, where the environment's reward function is augmented with additional local rewards using a potential function. However, the applicability of potential-based reward shaping is limited in settings where (i) the state space is very large, and it is challenging to compute an appropriate potential function, (ii) the feedback signals are noisy, and even with shaped rewards the agent could be trapped in local optima, and (iii) changing the rewards alone is not sufficient, and effective shaping requires changing the dynamics. We address these limitations of potential-based shaping methods and propose a novel framework of \emph{environment shaping using state abstraction}. Our key idea is to compress the environment's large state space with noisy signals to an abstracted space, and to use this abstraction in creating smoother and more effective feedback signals for the agent. We study the theoretical underpinnings of our abstraction-based environment shaping, and show that the agent's policy learnt in the shaped environment preserves near-optimal behavior in the original environment.


Introduction
Recently, Reinforcement Learning (RL) algorithms [1,2] have demonstrated tremendous success in complex games [3,4] and simulation environments [5]. However, in general, their deployment in real-world applications is hindered by high sample complexity especially in the domains with sparse or noisy feedback signals and large or continuous state space. In many applications, an expert with additional domain knowledge (e.g., see [6,7,8,9,10]) can help speed up the learning process by shaping the environment. Here, the expert modifies the original environment such that the new environment is more learner-friendly, i.e., the learning agent can learn an optimal policy that maximizes discounted cumulative reward in a sample efficient manner (or within a practical amount of time). But the expert needs to ensure that the (near-)optimal policy learned in the shaped environment is near-optimal in the original environment as well, i.e., the optimal value function is preserved under the transformation.
Potential-based reward shaping [11,12,6,13,14,15] is a technique where the expert modifies the reward function of the learning environment, using a potential function so that the learning progress of the RL agent is accelerated. Existing works on reward shaping typically rely on an appropriate potential function, such as (approximate) optimal value function V * , which is computationally challenging to estimate in large state spaces. When the feedback signals are noisy, even the shaped rewards cannot help the RL agent to escape the local optimal behaviors. Even though reward shaping is the most widely used technique to manipulate the environment, it can also be accomplished by changing the transition dynamics [7]. And there are instances where specific goals can only be achieved by changing the physics of the problem.
To address the above limitations, we propose a new environment shaping framework, which is scalable to large or continuous state space, and is fundamentally different from the potential-based shaping approaches. In our framework, depending on the application setting, we have the flexibility of shaping: (i) only the reward, (ii) only the transition dynamics, or (iii) both reward and transition dynamics.
Our shaping framework relies on a value-irrelevant (formally defined later) state abstraction φ for the original environment M , and the corresponding (optimal) abstract policy π φ . This still requires domain knowledge; however, this is less knowledge (or maybe easier to acquire) than fully specifying a potential function. Given access to (φ, π φ ), the expert employs the following pipeline: (a) first, shape the reward and/or transition dynamics in the abstract state space and construct an intermediate MDP M φ by leveraging techniques from [16,17] (b) then, from M φ , obtain the final shaped environment by appropriate lifting (formalized later).
Our main contributions include: I We propose a novel framework for environment shaping in the domains with large or continuous state space and sparse or noisy reward signals. II Under mild technical conditions, we prove that the optimal policy in the shaped environment maintains its optimality in the original environment. The optimality guarantee depends on the abstraction quality, and other MDP related quantities. III We demonstrate the advantages of our shaping framework compared to the potential-based reward shaping methods on domains with sparse and noisy reward and large state space.
Our techniques can be extended to another related problem of training-time adversarial attacks in RL, where existing algorithmic efforts are limited to finite state space, and do not scale when state space is continuous or large [16,17,18]. This extension is possible due to the perspective that policy teaching and attacking are mathematically equivalent. Here, the expert modifies the learning environment to induce desirable behavior in the RL agent [19].

Problem Statement
In this section, we formalize the problem of shaping an RL agent's learning environment such that its learning process can be accelerated.
Setup. Consider a learning environment represented by an MDP M = (S, A, T, R, µ 0 , γ). The state and action spaces are denoted by S and A respectively. Here, S is either finite or continuous, but A is finite. The transition function T : S × S × A → [0, 1] is a probability mass function (or density function when S is continuous). The probability of landing in a state s ∈ S (or any state in the region S ⊆ S when S is continuous) by taking action a from state s, induced by T , is given by: respectively for discrete and continuous S. The underlying reward function is given by R : Here γ ∈ (0, 1) is the discounting factor, and µ 0 : S → [0, 1] induces an initial distribution over the state space S (in similar way as Eqs. (1) and (2)).
We denote a policy π : S → ∆ (A) as a mapping from a state to probability distribution over the action space. For any policy π, the state value function V π M and the action value function Q π M in the MDP M are defined as follows respectively: Further the optimal value functions are given by V * M (s) = max π V π M (s) and Q * M (s, a) = max π Q π M (s, a). There always exists a deterministic stationary policy π that achieves the optimal value function simultaneously for all s ∈ S [20], and we denote all such optimal policies by π * M : S → A. We say that a policy π is opt -near optimal policy for the MDP M , if it satisfies the following condition: sup Objective. When the state space is extremely large or continuous, and the reward function is sparse or noisy, learning a near optimal policy is computationally expensive. We aim to accelerate the learning of near optimal behavior, by shaping the reward function and/or the transition dynamics of the underlying MDP M . Formally, we want to construct a new shaped MDP M = S, A, T , R, µ 0 , γ from M such that: 1. any opt -near optimal policy π M : S → ∆ (A) of M satisfies the following condition: for some c > 0, i.e., π M is c · opt -near optimal policy for M , and 2. learning a near-optimal policy π M : S → ∆ (A) in the MDP M is (computationally) easier than learning a near optimal behavior in the original MDP M .

Our Approach
In this section, we introduce our environment shaping framework for easing the learning process of an RL agent. Here, we have the flexibility of modifying both the reward function and the transition dynamics of the learning environment. In Section 3.1, we provide a high-level overview of our shaping framework and the domain knowledge required. Then, in Sections 3.2 and 3.3, we get into the technical details of the pipeline and provide a theoretical guarantee on our overall objective.

High-level Ideas
Here, we provide the high-level ideas of our environment shaping framework. Our approach leverages some of the techniques from state abstraction literature [21,22,23,24]. In particular, we rely on compressed knowledge of the learning environment in the form of a high-level near-optimal policy to facilitate the RL agent's learning process. In contrast to potential-based shaping techniques, which require access to an appropriate potential function such as the exact/approximate optimal value function of the original learning environment, the domain knowledge demanded by our framework is practically feasible to obtain.
To formally explain the key ideas of our abstraction based shaping framework and the domain knowledge required for us, we begin by defining the following notions of approximate abstractions: 24,25]). Let X φ be a finite set. Given an MDP M = (S, A, T, R, µ 0 , γ) and state abstraction φ : S → X φ , we define three types of abstractions as follows: where T (x | s, a) := S T (s | s, a) 1 {φ (s ) = x } ds for x ∈ X φ , s ∈ S, and a ∈ A.
(ii) φ is Q * -approximate Q * -irrelevant if there exists an abstract state-action value function f φ : X φ × A → R such that the following condition holds: exists an abstract policy π φ : X φ → A such that the following condition holds: where the lifted policy Formally, as a prior knowledge, our shaping framework requires access to an V * -approximate , and a corresponding policy π φ : X φ → A that satisfies the condition (7). In Section 4, we discuss the ways of obtaining such an abstraction, policy pair (φ, π φ ). Given this pair as an input, our abstraction-based shaping framework consists of the following two key steps: such that the policy π φ is the unique optimal policy for M φ . In addition, we ensure that π φ is δ better than any other policy in the MDP M φ . This step makes our shaping technique fundamentally different from the potential-based shaping methods. We formalize this construction in Section 3.2.
(ii) Then, using the intermediate MDP M φ , and the abstraction φ, we construct our target MDP M such that the mapping φ is model irrelevant to M . From this construction, we can show that any opt -near optimal policy in M is also near optimal in the original MDP M . This construction is explained in Section 3.3.
Our overall framework is illustrated in Figure 1.

Construction of M φ
In this intermediate step, we aim to construct an MDP M φ , defined in the abstract space, that satisfies certain technical conditions formally explained below. These conditions are essential to preserving the optimality behavior between the original learning environment and our final shaped environment. We note that this intermediate step along with these conditions distinguishes our shaping framework from the potential-based shaping methods.
In order to formalize the preferred properties of the MDP M φ , we first introduce some distance measures between the components of the MDP M φ and the original MDP M , for p, q ≥ 0: where T φ , R φ , and (µ 0 ) φ belong to the following sets: respectively. Specifically, we are interested in the case where p → ∞, and q = 1. These distance measures quantify the level of modification done to the original learning environment. Based on the above definitions, we formalize the properties that we seek to ensure while constructing the MDP such that the following conditions holds: , and D p µ 0 , (µ 0 ) φ are minimized, i.e., we enforce the new MDP M φ to not to change too much from the original MDP M . (ii) M φ has a unique deterministic optimal policy π * M φ , and it satisfies: any other deterministic policy in M φ cannot be δ-near optimal in the following sense: Now, we sketch a way of obtaining such an MDP M φ . First, we construct an intermediate MDP After obtaining M φ,(p,q) , we can construct our target M φ by leveraging techniques from [16,17,18].

Construction of M
Now, we turn to our final step of constructing our target MDP M , which can be viewed as a decompression step. In this case, we assume that both the reward and the transition dynamics can be modified. Then, given the abstraction φ, and the MDP M φ constructed in Section 3.2, we construct a shaped MDP M = S, A, T , R, µ 0 , γ as follows: 1. T (s | s, a) = T φ (φ(s )|φ(s),a) S 1{φ(s)=φ(s )}ds , for all s ∈ S, s ∈ S, a ∈ A 2. R (s, a) = R φ (φ (s) , a), for all s ∈ S, a ∈ A 3. µ 0 (s) = (µ 0 ) φ (φ (s)), for all s ∈ S Finally, we want to ensure that our teaching objective, mentioned in Section 2, can be attained by this shaped environment M . To this end, the following theorem shows that the agent's policy learnt in the shaped environment M preserves near-optimal behavior in the original environment M . Then, in Section 5, we empirically demonstrate that our shaping framework indeed helps in accelerating the learning process of the RL agent. Theorem 1. Any opt -near optimal policy π M : S → ∆ (A) of M , satisfies the following condition: When V * ≤ Rmax 1−γ · opt δ , we attain our teaching objective Eq. (4) with c = Rmax 1−γ · 2 δ .
Proof. For the target MDP M , we first show that [π φ ] M is the unique optimal policy. One can easily observe that, by construction, φ is a model-irrelevant abstraction for M ( R = 0, and T = 0 in Eq. (5)). Then, from Lemma 1 (in the Appendix of the supplementary material), φ is also a Q * -irrelevant abstraction for M ( Q * = 0 in Eq. (6)), i.e., we have a) , ∀s ∈ S, a ∈ A.
And since π φ is the unique optimal policy derived from Q * M φ , we can conclude that [π φ ] M is the unique optimal policy for M .
In addition, using the Lemma 5 (in the Appendix of the supplementary material), we can show that any opt -near optimal policy π M : S → ∆ (A) of M , satisfies the following condition: Finally, consider: is by triangle inequality, (iii) is from Eq. (7), (iv) is due to Lemma 2 (in the Appendix of the supplementary material), and (v) is by Eq. (10).

Practical Considerations
In this section, we discuss some practical aspects of our environment shaping framework.
Constructing Abstractions. Here, we briefly discuss the ways of obtaining the abstraction, and high-level policy pair (φ, π φ ), which is required as a prior knowledge to our shaping framework. One way of obtaining such knowledge is via careful feature engineering with the help of a domain expert. For example, when the ground state space is raw pixels acquired through noisy sensors, an expert can focus only on the relevant information that is required for the decision making task. One can also utilize the existing computational approaches, such as [26,27,28], to automatically construct meaningful abstractions.
Reward-only Shaping. In the case of shaping via only modifying the reward function, the shaped MDP M differs from the original MDP M only in its reward function R. In particular, given the abstraction φ, and the MDP M φ constructed in Section 3.2, we construct a reward-only shaped MDP M = S, A, T , R, µ 0 , γ as follows: 1. T (s | s, a) = T (s | s, a), and µ 0 (s) = µ (s), for all s ∈ S, s ∈ S, a ∈ A 2. R (s, a) = R φ (φ (s) , a), for all s ∈ S, a ∈ A The following theorem, similar to Theorem 1, provides a guarantee on preserving the optimality behavior in the reward-only shaped environment.
Theorem 2. We assume the following: Then, any opt -near optimal policy π M : S → ∆ (A) of M , satisfies the following condition: where R max φ = max x,a |R φ (x, a)|.
Transition Dynamics-only Shaping. Here, we shape only the transition dynamics while keeping the reward function of the shaped MDP M is the same as that of the original MDP M . In particular, given the abstraction φ, and the MDP M φ constructed in Section 3.2, we construct a target/shaped MDP M = S, A, T , R, µ 0 , γ as follows: R (s, a), and µ 0 (s) = µ (s), for all s ∈ S, a ∈ A The following theorem, similar to Theorem 1, provides a guarantee on preserving the optimality behavior in the dynamics-only shaped environment.
Theorem 3. We assume the following: Then, any opt -near optimal policy π M : S → ∆ (A) of M , satisfies the following condition: The proofs of Theorems 2 and 3 can be found in the Appendix of the supplementary material.

Experimental Evaluation
In this section, we empirically investigate whether our environment shaping approach enables simple reinforcement learning algorithm such as policy gradient, to find (near-)optimal policy under challenging setting with noisy and sparse feedback.

Environment Details
We consider the object gathering game environment, inspired by environments considered in recent works [29,30,31,32], see Figure 2a. In this game, an agent starts from one of the corner cells. The goal of the agent is to collect the objects, so that to maximize its total return. By collecting a "star" object, the agent receives a reward of 1.0, a "plus" object provides 0.04 reward, and collecting a "dot" object provides either 0.1 or −0.1 reward with equal probability. At initialization, the "star" object always appears opposite corner of the agent's position, "plus" appears in the four cells distance from the agent's initial position, and "dot" appears uniformly at random anywhere. After collecting the "plus" object, it appears on the same location, and "dot" object disappears after collecting it. The game's episode ends when the maximum time step H = 30 is reached, or the agent has collected the "star" object. In this game we have two actions {"left", "right"}, i.e., |A| = 2. The state of the system is represented by the position of the agent and the positions of all three objects. Since we have the number of the cells to be 15 and our state representation is described as the concatenation of the binary vector of the position of the agent and the position of each object, it will give us rise to 15 4 total possible states, i.e., |S| = 15 4 . The transition dynamics of the environment is defined as follows: with probability 0.99, the actions succeed in navigating the agent to left or right, as shown with the arrows in Figure 2a; and with probability 0.01, the move to the opposite direction is performed. The rewards are discounted at γ = 0.99. The optimal policy π * is to go towards to the "star" object and collect it.

Experimental Setup
We use the vanilla policy gradient method as the agent's algorithm, where the policy network is implemented as a two-layer neural network with relu activation function and softmax on the last layer. A one-hot vector of length 15, corresponding to the number of cells, represents the agent's location and the positions of all three objects in the environment. Hence the length of the input vector to the neural network is 4 × 15 = 60.  [6]), (iii) training on M shaped using our approach (ABSTRACTION-ENVS), and (iv) optimal policy that follows object "star" (OPT).
For the POTENTIAL-REWS approach, we need to approximate the optimal value function V * M of the original MDP M . To this end, we first run the policy gradient algorithm up to 50, 000 iterations, and then used resulting policy π to approximate V * M by V π M . To estimate the value function of the policy π, we used Monte Carlo (MC) prediction algorithm with 100, 000 samples. Given that we have domain knowledge of our problem, e.g., the information about the agent's position and 'star" object's position is sufficient to learn the optimal behavior, our abstraction φ : S → X φ can be defined as follows: we consider only the position of the agent and position of the "star" object to encode our state space. This abstraction reduces the state-space from 15 4 down to 15 2 , i.e., |X φ | = 15 2 . We construct the MDP M φ by first obtaining M φ, (2,1) and then using the rewards only attack as in [17].

Results
We evaluate the performance of the four algorithms mentioned above and report the results by averaging across 30 runs and with random initialization for each run. Figure 2b shows the convergence of the total reward of the episode for all the algorithms, where the policy following the "star" object attains the optimal value. As expected, training on the shaped environment outperforms two baselines.
Since the training on the original environment M needs exponentially many episodes to learn optimal policy, it converges to a local minimum [33], e.g., collecting the nearest object "plus". Since our environment provides noisy signals feedback because of the randomness of the "dot" object, it is challenging to compute an appropriate potential function. As a result, the applicability of the potential-based reward shaping is limited in our settings.

Conclusions
In this work, we propose an abstraction based environment shaping framework to accelerate the learning process of an agent. Our framework is fundamentally different from the existing potentialbased shaping methods. We theoretically prove that our environment shaping preserves the optimality behavior between the original and target MDPs, and empirically demonstrate its efficacy on tasks with sparse and noisy feedback signals.

A Auxiliary Lemmas
Lemma 1 (Theorem 2 from [25]). If φ is an ( R , T )-approximate model-irrelevant abstraction, then φ is also an approximate Q * -irrelevant abstraction with approximation error Q * = R 1−γ + γ T Rmax 2(1−γ) 2 . Further, if φ is an Q * -approximate Q * -irrelevant abstraction, then φ is also an approximate V *irrelevant abstraction with approximation error V * = 2 Q * 1−γ . Lemma 2 (Lemma A.1 from [34]). For any two policy π and π (acting in the MDP M ), we have Definition 2 (approximately-equivalent MDPs, adapted from [35]). Suppose we have two MDPs Lemma 3. Suppose we have two (β R , β T ) approximately-equivalent MDPs M 1 and M 2 . Then, we have: sup Proof. Consider the Bellman optimality equation, for any s: Then, for any s, we have: Taking sup over s, and rearranging the terms complete the proof.

B Additional Experiments
In this section, we performed an additional experiment to showcase the efficacy of our algorithm.

B.1 Environment Details
We consider the catcher environment illustrated in Figure 3a. In this game, an agent moves along the x-axis, and two types of objects fall from above the agent, perpendicular to the agent's axis of movement. In this game, the agent has two actions, i.e., A={"left", "right"}. The action taken succeeds in moving the agent in the corresponding direction with a probability of 0.99. And with a probability of 0.01, the movement happens in the opposite direction. The agent starts from the left corner of the environment, and its goal is to move itself to catch the falling objects. The object "plus" always appears close to the agent's initial position, e.g., on the 5 th column, and the object "star" appears before the last column. Intersecting with the object "star" gives the reward of 1.0, and intersecting with the object "plus" gives the reward of 0.04. After catching or missing the objects, they appear in the starting position. The game's episode ends when the maximum time step H = 30 is reached, or the agent has caught the "star" object. Since we have the number of the cells to be 60 and our state representation is described as the concatenation of the binary vector of the position of the agent and the locations of each object, it results in 20 × 60 2 total possible states, i.e., |S| = 20 × 60 2 . The rewards are discounted at γ = 0.99. The optimal policy π * is to catch the "star" object.

B.2 Experimental Setup
We use the vanilla policy gradient method as the agent's algorithm, where the policy network is implemented as a two-layer neural network with relu activation function and softmax on the last layer. A one-hot vector of length 20, corresponding to the number of cells in the x-axis, represents the agent's location. One-hot vectors of length 60, corresponding to the number of cells, represent the positions of all two objects in the environment. Hence the length of the input vector to the neural network is 20 + 2 × 60 = 140.
Methods evaluated. As in Section 5, the following algorithms were considered in the comparison: (i) default baseline with training on original M (DEFAULT), (ii) baseline of potential-based reward shaping of M (POTENTIAL-REWS) (see [6]), (iii) training on M shaped using our approach (ABSTRACTION-ENVS), and (iv) optimal policy that catches object "star" (OPT).
For the POTENTIAL-REWS approach, we need to approximate the optimal value function V * M of the original MDP M . To this end, we first run the policy gradient algorithm up to 50, 000 iterations, and then used resulting policy π to approximate V * M by V π M . To estimate the value function of the policy π, we used Monte Carlo (MC) prediction algorithm with 200, 000 samples. Given that we have domain knowledge of our problem, e.g., the information about the agent's position and "star"