eaplatanios / jelly-bean-world

A framework for experimenting with never-ending learning
Apache License 2.0
73 stars 17 forks source link

Adding an agent is very slow #13

Closed finbarrtimbers closed 2 years ago

finbarrtimbers commented 2 years ago

Everytime we call simulator._add_agent(agent), it takes ~40s. Is this expected? Is there anyway to speed this up?

asaparov commented 2 years ago

Wow that is not expected. Does this happen when the world is large? (i.e. been heavily explored) Does the world have a lot of agents?

finbarrtimbers commented 2 years ago

No, the world only has a single agent and has not been explored at all. We create a Simulator and then immediately add the agent to it.

In case it helps with diagnosing, this is our agent class- it doesn't do anything interesting:

class _DummyAgent(jbw.agent.Agent):
  """Minimal implementation of the agent interface."""

  def __init__(self, simulator, load_filepath=None):  # pylint: disable=useless-super-delegation
    super().__init__(simulator, load_filepath)

  def do_next_action(self):
    raise NotImplementedError()

  def save(self, filepath):
    # This method is called by the simulator when saving.
    with open(filepath, 'wb') as f:
      pickle.dump((self._id, self._position, self._direction, self._scent,
                   self._vision, self._items), f, pickle.HIGHEST_PROTOCOL)

  def _load(self, filepath):
    # This method is called by the simulator when loading.
    with open(filepath, 'rb') as f:
      (self._id, self._position, self._direction, self._scent, self._vision,
       self._items) = pickle.load(f)

Simulator config:

'agent_field_of_view': 6.283185307179586, 
'allowed_movement_directions': [<ActionPolicy.ALLOWED: 0>, <ActionPolicy.ALLOWED: 0>, <ActionPolicy.ALLOWED: 0>, <ActionPolicy.ALLOWED: 0>], 
'allowed_turn_directions': [<ActionPolicy.DISALLOWED: 1>, <ActionPolicy.DISALLOWED: 1>, <ActionPolicy.DISALLOWED: 1>, <ActionPolicy.DISALLOWED: 1>]
, 
'collision_policy': <MovementConflictPolicy.FIRST_COME_FIRST_SERVED: 1>, 
'decay_param': 0.4, 
'deleted_item_lifetime': 2000, 
'diffusion_param': 0.14, 
'max_steps_per_movement': 1, 
'mcmc_num_iter': 10000, 
'no_op_allowed': False, 
'patch_size': 64, 
'vision_range': 8, 
'seed': 84}

Callback:

def _on_step(self):
  self._t += 1

We're then doing:

simulator = jbw.Simulator(
    sim_config=simulator_config,
    on_step_callback=self._on_step)
agent = _DummyAgent(self._simulator)

The slowdown is in the call to sim_handle->add_agent.

asaparov commented 2 years ago

Oh this is actually expected. The simulator doesn't actually start generating the world until the first agent is added. In addition, when a new agent is added, the simulator performs 10 times more MCMC iterations to make sure the initial patches of the world are well-mixed (since there are no previous patches to provide a good initialization).

Adding agents in the future will be much faster since we currently only support adding them at the position (0,0), which will have been already explored.

finbarrtimbers commented 2 years ago

Ok- thanks for clarifying. That makes sense- all of the time is spent in get_fixed_neighborhood.