Reinforcement learning is a type of machine learning in which an agent learns from its experience in an environment to maximize some cumulative reward(Sutton and Barto, 1998; Mnih et al., 2015). The environment is a crucial part of any reinforcement learning setting, as the environment defines the dynamics of the world in which the agent is embedded and hence it determines (possibly stochastically) the feedback to the agent’s actions. This feedback includes the next observation state for the agent and its external rewards.
Large part of creating a reinforcement learning task consists of programming the environment or world in which the agent will be immersed and interacting with other elements. This includes the properties of all possible items in the environment, their positioning, the set of events triggering rewards and punishments, and the computation of the next observation state. The latter can be complicated by the perceptual properties with which we would like to endow our agent. In particular, more realistic agents typically do not observe directly the environment state but just a function or partial state of it. For example, simulating the finite field of vision of an agent in an environment with occluders is a common situation leading to such partial observations of states.
In this work we present APES (Artificial Primate Environment Simulator), a Python tool to create 2D grid-world environments for reinforcement learning tasks. The name derives from the use of our environment for simulating real experiments with real apes competing for a reward in a controlled environment (Hare et al., 2000). APES allows the user to quickly build 2D environments for reinforcement learning problems by allowing the user to add layers of different types of pre-defined or new items, set any rewarding scheme, and interface an agent with the environment (i.e. compute the agent’s next observation state and perform the agent’s action). Moreover, the agent can be equipped with different types of vision with customizable parameters. Importantly, APES also supports the interaction of multiple agents in the same environment, and hence could be used to train and test multiagent reinforcement learning algorithms (Busoniu et al., 2008; Schwartz, 2014; Tampuu et al., 2017; Foerster et al., 2016). The full code can be found at https://github.com/aqeel13932/APES.
In the next section we describe some alternative software solutions. The rest of the paper is the core of this contribution and it explains the organization and capabilities of APES as well as an example of its use to simulate a simple reinforcement task.
1.1. Related Work
There are other solutions to simulate simple environments for reinforcement learning tasks. Here, we focus on MazeBase, OpenAI gym, and VGDL and note their main differences with APES. MazeBase (Sukhbaatar et al., 2015) is a 2D grid-world environment to create simple games programmable by the user. MazeBase can also have a non-visual world representation. Although MazeBase offers a variety of types and co-existing elements on same grid cell, the agents have limited vision options. The wrapper OpenAI gym (Brockman et al., 2016) offers numerous games but lacks the option to customize the environment, and most games are fully observed environments. VGDL(Schaul, 2013) allows to define different kinds of games in declarative language, yet it lacks the control of some vision properties such as partial observability in 2D.
APES is organized around two basic structures: the World and the agents’ interface with the world. The first defines the characteristics and dynamics of the environment, while the second describes the interaction of one or multiple agents with the environment. Each agent can be controlled with any algorithm external to APES (say your favourite implementation of deep reinforcement learning) and the agent solely interacts with APES by receiving an observation state and reward, and sending an action to be performed in the world. See Figure 1 for a diagram illustrating the APES environment within the framework of reinforcement learning. Next we describe the elements of these structures. We begin with the world.
The world arena is a 2D grid where all elements are located and interact. In Figure 2 we illustrate a snapshot of a simple environment created with APES. To define the properties of the environment and the elements therein the user can specify each of the following features:
World size: dimensions of the 2D grid defining the arena. Current version supports only square-shaped environments.
End of episode: the world has the attribute Terminated to identify when an episode ends. The user can specify an episode to end in any of these cases (or whichever occurs first):
the maximum number of time steps has been reached (specified by Maximum steps).
all rewards have been consumed.
After the flag Terminated has being raised the environment will be regenerated.
Items: elements to be located at the cells of the 2D grid. There are different types of pre-defined items:
Food: item reachable by agents. Agents can occupy the same cell as the food item and collect it, making it disappear from the environment. Although typically associated with a positive reward, it can be associated with any type or reward or punishment according to the Rewarding scheme defined by the user.
Obstacles: items responsible for stopping agents from moving freely in the World. An obstacle element can be chosen from the following list.
Wall: obstacles that cannot be traversed and impede visibility through them.
Water: obstacles that cannot be traversed but they allow visibility through them.
Each obstacle can be associated with a Shape matrix determining the geometric shape of the obstacle. For example, adding a ”T”-shaped obstacle can be done by passing 33 array with first row and middle column as ones and the rest as zeros.
Agent: interactive elements in the environment which are controlled by external algorithms. They interact with the items in the World by executing actions and receiving observation states and rewards. Each agent can be associated a Power level to define the interaction with other agents. For example, in case of collision, agents of lower level can be set to receive some punishment. Each agent also has an attribute Transparency to make the agent act as a vision barrier or as a transparent element for other agents’ vision.
Probability Distribution Matrix (PDM): matrix of same size as the world grid used to distribute elements across it. The matrix contains the probability of an element to be located at each position at the start of each episode. Figure 3 illustrates a graphical representation of PDMs used to distribute several elements randomly within some defined locations. PDM values will be automatically normalized and multiple elements can have the same PDM. If no PDM is given, elements are placed randomly across the entire grid by default. When two elements spawn in the same position the placing priority is: 1) agent, 2) food, and 3) obstacle. For obstacles with more than one block, only the center is guaranteed to appear. Extensions from the center of the obstacle will be overruled by other elements if such conflict occurred.
Block size: size of the images of each element. This feature is used only for visualization of the environment as pictures or video.
2.2. Agents’ Interface
The agents’ interface defines the information communicated between the environment and each agent. It consists of the observation state and reward received by the agent at every time step, as well as the execution of the action chosen. Next we describe the content of each of these streams per agent and how they are computed.
Observation: determines what the agent obtains as observation depending on the environment state and vision algorithm. At each time step the observation fed back to the agent contains:
Observability: a binary 2D array of same size as the world with each cell marked as if the corresponding cell location is observable by the agent.
Food: a binary 2D array of the same size as the world with each cell marked as if a food item is observed at the corresponding cell location.
Agent position: a binary 2D array of same size as the world size with a in the cell currently occupied by the focal agent.
one hot vector of size 4 (North, South, East, West) with aindicating the current orientation of the focal agent.
Other agent’s position: a binary 2D array per agent (other than the focal agent) of same size as the world and with a in the cell currently occupied the agent. If the agent is not currently observed by the focal agent the array will be all zeros.
Other agent’s orientations: one hot vector of size 4 (North, South, East, West) per agent (other than the focal agent) with a indicating the current orientation of the agent. If the agent is not currently observed by the focal agent the array will be all zeros.
Agent Vision: it determines how and which parts of the environment state are transformed to produce an observation state for the agent. APES simulates two modes of agent’s vision.
Allocentric: the agent receives a bird’s eye view of the environment. As the agent executes actions and moves across the environment the agent’s position in the visual field will change depending on its location as shown in Figure 4 where the black area represents the unobserved area.
Egocentric: provides a more natural type of vision in which the field of view is centered at agent’s location. Hence the agent will always be in the center of the view and the surrounding will change depending on its location and direction. Figure 5 illustrates the environment state from this point of view.
For both modes APES uses a shadow-casting algorithm to determine the agent’s field of vision based on the elements populating the 2D grid (Bergström, 2016). Each of the two modes of vision can be tuned by parameters such as:
Vision Angle: the angle spanning the agent’s field of view. The value should be between 0 to 360. Left panel in Figure6 shows the effect of the vision angle on an agent’s field of view.
Vision Limit: extent of the agent’s field of view. It is the maximum distance the agent can observe. If set to value -1 the agent obtains an infinite vision limit. Right panel in Figure6 shows the effect of the vision range on an agent’s field of view.
Reward: provides feedback to agent’s actions by specifying the rewards and punishments obtained in case of different events occurring in the environment. The default option is a rewarding scheme given the vector [-10, 10, -0.1] indicating a value of -10 for the event of colliding with another agent, +10 for finding food item, and -0.1 for every time step. Any rewarding scheme can be specified for any set of events defined by the user by passing a new reward function to World class instance.
Actions: defines how the agent interacts and influences the environment, including other agents. The actions are selected according to the agent’s controller which is usually an algorithm external to APES (although see the section on Autopilots for some automated agents provided by APES). The space of actions supported by APES and their ordering is given as follows:
Set of actions: there are two sets of primitive actions for each agent, 4 actions for looking in different directions (North, South, East, West) and another 4 actions for moving. Lastly, there is a no-operation action to complete a set of 9 primitive actions. These primitives can be concatenated to create different single complex actions, for example ”Look left, Move Left” as one action which can be executed in one time step.
Actions Order: actions of different agents are executed in temporal order according to an ordered array of the agents’ identifiers.
The design of the agents’ controllers is a task separated from the APES environment. Any agent controller can interface with APES environment by reading the next observation state and reward and outputting an action within the space of actions described above. How the controllers make use of the environment state and reward to select the next action is upon the user’s preferred reinforcement learning algorithm. However, to ease the interaction with some simple behaving agents APES includes the following autopilots:
Random Walker: this autopilot randomly assigns actions to the agent regardless of its position or vision. For example, it can select the action ”Move Left” although there is a wall on the left.
Algorithmic Agent: this autopilot has two properties: it has incremental vision (retains within its field of view all elements once seen) and navigates with a smart pathfinding algorithm. In particular, this agent randomly explores the unobserved area but once a food item is spotted the agent takes the shortest path to it according to the A* pathfinding algorithm (Hart et al., 1968).
The performance in executing updates of the environment is a crucial issue since if not properly optimized this time can become a bottleneck in the training and testing of reinforcement learning algorithms. The speed depends on the number of the agents and the size of the world. For two agents and a world of size 1111 it can perform around 450 steps per second in a Intel Core i5-5200U CPU.
Here we illustrate how APES can be set up to define and simulate a simple reinforcement learning task. Figure 8 shows the code used to define and update the environment illustrated in Fig.2. In this case at the beginning of each episode several elements (obstacles, agents, food) will be located randomly within some range of cells of the 2D-grid given by the probability distribution matrices. Given the rewarding scheme used in the example code the two agents (red and green) will compete for the food reward and will be punished every time step with a small penalty. Executing ”world.Step()” after assigning each agent an action will update the environment for one time step. The regeneration of the environment (new episode) after the termination of the current episode is achieved by executing ”Generate World”.
In this article we presented the most important features in APES to design and simulate reinforcement learning tasks in 2D grid-worlds. In particular, APES handles the properties of the 2D arena and the elements therein. APES also provides the feedback for the agents and executing their actions in the environment. Importantly, APES supports different vision algorithms and multiple agents, which can make it attractive for studying multiagent reinforcement learning problems with partial observability. It is our hope that APES can ease the creation and simulation of simple environments so that users can focus on the design and testing of better reinforcement learning algorithms.
Acknowledgements.A.L. was funded by the European Regional Development Fund and IT Academy. All authors thank the financial support from the Estonian Research Council through the personal research grant PUT1476. This work was also supported by the Estonian Centre of Excellence in IT (EXCITE), funded by the European Regional Development Fund. The authors are also indebted to Daniel Majoral and Axel Harpe for taking time in working with the environment and reporting bugs.
- Bergström (2016) Björn Bergström. 2016. FOV using recursive shadowcasting. http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting. (2016). [Online; accessed 12-Jan-2017].
- Brockman et al. (2016) Greg Brockman, Vicki Cheung, Ludwig Pettersson, Jonas Schneider, John Schulman, Jie Tang, and Wojciech Zaremba. 2016. OpenAI Gym. CoRR abs/1606.01540 (2016). http://arxiv.org/abs/1606.01540
- Busoniu et al. (2008) Lucian Busoniu, Robert Babuska, and Bart De Schutter. 2008. A comprehensive survey of multiagent reinforcement learning. IEEE Trans. Systems, Man, and Cybernetics, Part C 38, 2 (2008), 156–172.
- Foerster et al. (2016) Jakob Foerster, Ioannis Alexandros Assael, Nando de Freitas, and Shimon Whiteson. 2016. Learning to communicate with deep multi-agent reinforcement learning. In Advances in Neural Information Processing Systems. 2137–2145.
- Hare et al. (2000) Brian Hare, Josep Call, Bryan Agnetta, and Michael Tomasello. 2000. Chimpanzees know what conspecifics do and do not see. Animal Behaviour 59, 4 (2000), 771–785.
et al. (1968)
Peter E Hart, Nils J
Nilsson, and Bertram Raphael.
A formal basis for the heuristic determination of minimum cost paths.IEEE transactions on Systems Science and Cybernetics 4, 2 (1968), 100–107.
- Mnih et al. (2015) Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Andrei A Rusu, Joel Veness, Marc G Bellemare, Alex Graves, Martin Riedmiller, Andreas K Fidjeland, Georg Ostrovski, and others. 2015. Human-level control through deep reinforcement learning. Nature 518, 7540 (2015), 529.
- Schaul (2013) Tom Schaul. 2013. A Video Game Description Language for Model-based or Interactive Learning. In Proceedings of the IEEE Conference on Computational Intelligence in Games. IEEE Press, Niagara Falls.
- Schwartz (2014) Howard M Schwartz. 2014. Multi-agent machine learning: A reinforcement approach. John Wiley & Sons.
- Sukhbaatar et al. (2015) Sainbayar Sukhbaatar, Arthur Szlam, Gabriel Synnaeve, Soumith Chintala, and Rob Fergus. 2015. MazeBase: A Sandbox for Learning from Games. CoRR abs/1511.07401 (2015). http://arxiv.org/abs/1511.07401
- Sutton and Barto (1998) Richard S Sutton and Andrew G Barto. 1998. Reinforcement learning: An introduction. Vol. 1. MIT press Cambridge, 3.
- Tampuu et al. (2017) Ardi Tampuu, Tambet Matiisen, Dorian Kodelja, Ilya Kuzovkin, Kristjan Korjus, Juhan Aru, Jaan Aru, and Raul Vicente. 2017. Multiagent cooperation and competition with deep reinforcement learning. PloS one 12, 4 (2017), e0172395.