concept agent in category reinforcement learning

appears as: agent, An agent, gent, gents, The agent, Agents, n agent, agents, n agents
Grokking Deep Reinforcement Learning

This is an excerpt from Manning's book Grokking Deep Reinforcement Learning.

In DRL, these computer programs are called agents. An agent is a decision-maker only and nothing else. That means if you are training a robot to pick up objects, the robot arm is not part of the agent. Only the code that makes decisions is referred to as the agent.

The two core components in RL are the agent and the environment. The agent is the decision maker, is the solution to a problem. The environment is the representation of a problem. One of the fundamental distinctions between RL from other ML approaches is that the agent and the environment interact; the agent attempts to influence the environment through actions, and the environment reacts to the agent’s actions.

Notice that what we are trying to represent here is the fact that the environment “reacts” to the agent’s actions in some way, perhaps even by ignoring the agent’s actions. But at the end of the day, there is an internal process that is uncertain (except in this and next chapter). To represent the ability to interact with an environment in an MDP we need states, observations, actions, a transition and a reward function.

Figure 2.7 Process the environment goes through as a consequence of agent’s actions
02_07
Deep Reinforcement Learning in Action

This is an excerpt from Manning's book Deep Reinforcement Learning in Action.

  • The agent is the focus of any RL problem. It is the part of the RL algorithm that processes input to determine which action to take. In this book we are primarily focused on agents implemented as deep neural networks.
  • The environment is the potentially dynamic conditions in which the agent operates. More generally, the environment is whatever process generates the input data for the agent. For example, we might have an agent flying a plane in a flight simulator, so the simulator would be the environment.
  • The state is a snapshot of the environment that the agent has access to and uses to make decisions. The environment is often a set of constantly changing conditions, but we can sample from the environment, and these samples at particular times are the state information of the environment we give to the agent.
  • An action is a decision made by an agent that produces a change in its environment. Moving a particular chess piece is an action, and so is pressing the gas pedal in a car.
  • A reward is a positive or negative signal given to an agent by the environment after it takes an action. The rewards are the only learning signals the agent is given. The objective of an RL algorithm (i.e., the agent) is to maximize rewards.
  • The general pipeline for an RL algorithm is a loop in which the agent receives input data (the state of the environment), the agent evaluates that data and takes an action from a set of possible actions given its current state, the action changes the environment, and the environment then sends a reward signal and new state information to the agent. Then the cycle repeats. When the agent is implemented as a deep neural network, each iteration evaluates a loss function based on the reward signal and backpropagates to improve the performance of the agent.
  • The second function in listing 9.3 is our reward function. It accepts a list, s, of binary digits, and a single binary digit, a, and then compares how many values in s match a. If all of the values match, the reward is maximal, and if none of them match, the reward is negative. The input s will be the list of neighbors. In this case, we will use the two nearest neighbors, so for a given agent its neighbors will be the agents to its left and right on the grid. If an agent is at the end of the grid, its right neighbor will be the first element in the grid, so we wrap around to the beginning. This makes the grid into a circular grid.

    Each element in the grid (either 1 or 0) represents an electron being spin-up or spin-down. In reinforcement learning jargon, the electrons are individual agents in the environment. Agents need to have value functions and policies, so they cannot merely be a binary number. The binary number on the grid represents the action of the agent, choosing to be either spin-up or spin-down. Hence, we need to model our agents using a neural network. We will use a Q-learning approach rather than the policy gradient method. In listing 9.4 we define a function that will create parameter vectors to be used in a neural network.

    Listing 9.4. The 1D Ising model: Generate neural network parameters
    def gen_params(N,size):         #1
        ret = []
        for i in range(N):
            vec = torch.randn(size) / 10.
            vec.requires_grad = True
            ret.append(vec)
        return ret

    Since we will be using a neural network to model the Q function, we need to generate the parameters for it. In our case, we will use a separate neural network for each agent, although this is unnecessary; each agent has the same policy, so we could re-use the same neural network. We’ll do this just to show how it works; for the later examples we will use a shared Q function for agents with identical policies.

    If you think of the Ising model as a multiplayer game, it would be considered a pure cooperative multiplayer game, since all the agents have the same objective and their reward is maximized when they work together to all align in the same direction. In contrast, chess would be a pure competitive game, because when one player is winning the other player is losing; it is zero-sum. Team-based games, like basketball or football, are called mixed cooperative-competitive games, since the agents on the same team need to cooperate in order to maximize their rewards, but when one team as a whole is winning, the other team must be losing, so at the team-to-team level it is a competitive game.

    In this section we are going to use an open source Gridworld-based game that is specially designed for testing multi-agent reinforcement learning algorithms in cooperative, competitive, or mixed cooperative-competitive scenarios (figure 9.23). In our case, we will set up a mixed cooperative-competitive scenario with two teams of Gridworld agents that can move around in the grid and can also attack other agents on the opposing team. Each agent starts with 1 “health point” (HP), and when they’re attacked the HP decreases little by little until it gets to 0, at which point the agent dies and is cleared off the grid. Agents get rewards for attacking and killing agents on the opposing team.

    Figure 9.23. Screenshot from the MAgent multiplayer Gridworld game with two opposing teams of Gridworld agents. The objective is for each team to kill the other.

    Since all the agents on one team share the same objective and hence the optimal policy, we can use a single DQN to control all the agents on one team, and a different DQN to control the agents on the other team. It’s basically a battle between two DQNs, so this would be a perfect opportunity to try out different kinds of neural networks and see which is better. To keep things simple, though, we’ll use the same DQN for each team.

    We’re going to use the same technique to solve this environment as we did for the 2D Ising model, but with two DQNs. We’ll use a softmax policy and experience replay buffer. Things will get a bit complicated because the number of agents changes over training, since agents can die and be removed from the grid.

    With the Ising model, the state of the environment was the joint actions; there was no additional state information. In MAgent we additionally have the positions and health points of agents as state information. The Q function will be Qj(st,a–j) where a–j is the mean field for the agents within agent j’s field of view (FOV) or neighborhood. By default, each agent has a FOV of the 13 × 13 grid around itself. Thus, each agent will have a state of this binary 13 × 13 FOV grid that shows a 1 where there are other agents. However, MAgent separates the FOV matrix by teams, so each agent has two 13 × 13 FOV grids: one for its own team and one for the other team. We will need to combine these into a single state vector by flattening and concatenating them together. MAgent also provides the health points of the agents in the FOV, but for simplicity we will not use these.

    We’ve initialized the environment, but we haven’t initialized the agents on the grid. We now have to decide how many agents and where to place them on the grid for each team.

    Listing 9.14. Adding the agents
    hid_layer = 25
    in_size = 359
    act_space = 21
    layers = [(in_size,hid_layer),(hid_layer,act_space)]
    params = gen_params(2,in_size*hid_layer+hid_layer*act_space)               #1
    map_size = 30
    width = height = map_size
    n1 = n2 = 16                                                               #2
    gap = 1                                                                    #3
    epochs = 100
    replay_size = 70
    batch_size = 25
     
    side1 = int(math.sqrt(n1)) * 2
    pos1 = []
    for x in range(width//2 - gap - side1, width//2 - gap - side1 + side1, 2): #4
        for y in range((height - side1)//2, (height - side1)//2 + side1, 2):
            pos1.append([x, y, 0])
     
    side2 = int(math.sqrt(n2)) * 2
    pos2 = []
    for x in range(width//2 + gap, width//2 + gap + side2, 2):                 #5
        for y in range((height - side2)//2, (height - side2)//2 + side2, 2):
            pos2.append([x, y, 0])
            
    env.reset()
    env.add_agents(team1, method="custom", pos=pos1)                           #6
    env.add_agents(team2, method="custom", pos=pos2)