Initial Agents

Where simulated life begins

HASH simulations start life in the init.json file in your editor. In this file we generate the starting state, or initial conditions of the simulated world. There are two ways to populate this. You can:

  1. Explicitly define the agents that will be in your model; or

  2. You can define "creator" agents with behaviors that will generate more complex initial states. You can do this with published behaviors, or create your own.

Here's what explicitly defining your agents might look like:

Defining five agents in init.json

With "creator" agents you can initialize more dynamic models.

If you want to jump right into code you can take a look at our Initialization Demo which demos creator agents.

For example, by accessing published behaviors, we can very easily generate common agent placements. These behaviors can be found in the lower left corner; click on them to add them to your simulation:

  • Create Grids: copy an agent to every unit within the topology bounds

  • Create Scatters: copy an agent to random locations within the topology bounds

  • Create Stacks: copy an agent multiple times to the same location

Take a look at how we can use published behaviors in the following example, where rabbits forage for food and reproduce, while grass and weeds grow around them:

There's a singly agent, named creator, that has a set of behaviors that will reference the "templates" we attached as properties on the creator agent.

Create Grids looks at the agent templates in the "grid_templates" array, in this case the "ground". We're copying it to fill the space defined in the bounds of our "topology" field inglobals.json:

Next, Create Scatters distributes the "rabbits" across the environment. Each one is placed in a random location within the bounds specified in the topology.

Now we want to make a few adjustments to the agents we've generated which requires a bit more logic. Luckily for us, HASH behaviors are composable. Create Grids and Create Scatters have created "agent" objects in our creator and filled them. We access those agents by using the "template_name" as a key:

Here we've randomly assigned the color of our "ground" agents, and given each of the "rabbits" a random starting amount of energy.

Our creator then runs two more published behaviors. Create Agents sends messages to the engine to generate every agent in the "agents" object, and Remove Self gets rid of the "creator" agent, since it's finished all it needs to do. Again, these behaviors can be found in the lower left sidebar.

You can create new agents during your simulation by sending a message to the reserved hash keyword.

JavaScript
Python
JavaScript
state.addMessage("hash", "create_agent", {
...agent_details
})
Python
state.add_message("hash", "create_agent", {
...agent_details
})

If you'd like to explore another simple example that uses these published behaviors, take a look at the Wildfires or Rock, Paper, Scissors simulations.

If you ever feel like you might be "re-inventing the wheel," make sure to check out HASH Index in case someone else has shared a similar behavior.