Module multi_agent_example.handcrafted_agents.cleanup

Handcrafted agents for the cleanup task.

These are some simple hand-crafted agents that are useful for playing against in the demo.

These are intended for example purposes only, and are necessary to play the demo on ../configs/cleanup.py.

Classes

class FickleAgent (*agents, steps_per_agent=25)
Expand source code
class FickleAgent():
    """This agent switches regularly between a given set of strategies."""

    def __init__(self, *agents, steps_per_agent=25):
        """Constructor.

        Example usage to produce an agent that switches between being selfish
        and being selfless every 100 steps:
            ```python
            FickleAgent(
                SelfishAgent(name='agent_layer_name'),
                SelflessAgent(name='agent_layer_name'),
                steps_per_agent=100)
            ```

        Args:
            agents: Iterable of agents. These are the agents that this
                FickleAgent mimics.
        """
        self._agents = agents
        self._num_agents = len(self._agents)
        self._steps_per_agent = steps_per_agent
        self._current_agent = 1
        self.switch()

    def switch(self):
        self._steps_until_switch = self._steps_per_agent
        self._current_agent = (self._current_agent + 1) % self._num_agents

    def step(self, observation):
        if self._steps_until_switch == 0:
            self.switch()
        self._steps_until_switch -= 1
        return self._agents[self._current_agent].step(observation)

This agent switches regularly between a given set of strategies.

Constructor.

Example usage to produce an agent that switches between being selfish and being selfless every 100 steps: python FickleAgent( SelfishAgent(name='agent_layer_name'), SelflessAgent(name='agent_layer_name'), steps_per_agent=100)

Args

agents
Iterable of agents. These are the agents that this FickleAgent mimics.

Methods

def step(self, observation)
Expand source code
def step(self, observation):
    if self._steps_until_switch == 0:
        self.switch()
    self._steps_until_switch -= 1
    return self._agents[self._current_agent].step(observation)
def switch(self)
Expand source code
def switch(self):
    self._steps_until_switch = self._steps_per_agent
    self._current_agent = (self._current_agent + 1) % self._num_agents
class RandomAgent (name)
Expand source code
class RandomAgent():
    """Agent takes random action in [0, 1] x [0, 1], regardless of the state."""

    def __init__(self, name):
        self._name = name

    def step(self, observation):
        del observation
        return np.random.uniform(-1., 1., size=(2,))

Agent takes random action in [0, 1] x [0, 1], regardless of the state.

Methods

def step(self, observation)
Expand source code
def step(self, observation):
    del observation
    return np.random.uniform(-1., 1., size=(2,))
class SelfishAgent (name)
Expand source code
class SelfishAgent():
    """This agent is a free-rider, only collecting ripe fruit."""

    def __init__(self, name):
        self._name = name

    def step(self, observation):
        return _ripe_fruit_homing(observation['state'], self._name)

This agent is a free-rider, only collecting ripe fruit.

Methods

def step(self, observation)
Expand source code
def step(self, observation):
    return _ripe_fruit_homing(observation['state'], self._name)
class SelflessAgent (name)
Expand source code
class SelflessAgent():
    """This agent only cleans poisonous fountains and never collects fruit."""

    def __init__(self, name):
        self._name = name

    def step(self, observation):
        return _poison_fountain_homing(observation['state'], self._name)

This agent only cleans poisonous fountains and never collects fruit.

Methods

def step(self, observation)
Expand source code
def step(self, observation):
    return _poison_fountain_homing(observation['state'], self._name)