Discord
@hash/networks
Behavior
2
13
0
Public

Networks

The Networks Library allows you to define networks of agents who have access to each other as if they were neighbors.

The Networks Library allows you to define networks of agents who have access to each other as if they were neighbors. You can use this library for typical examples of networks, such as social media networks or contagion. You can also use it to connect agents in less typical scenaraios, for instance, linking agents to form pendulums.

Parameters

The Networks Library is built on the the standard HASH neighbors engine. This means that all Network agents must have a position defined, and large enough search radius so that they can always find their network neighbors. Agents using the Networks Library will need to have a large enough search radius defined such that they will always be able to see their network neighbors agents.

Agents define their connection in the network_neighbor_ids field. This should contain an array of agent ids. Any agent ids which do not correspond to an agent in the simulation will automatically be removed after one time step.

Accessing Your neighbors

An agent's network neighbors will be updated each step of the simulation, whenever the get_neighbors.js behavior runs. In most cases, you'll want to place this behavior first, or towards the beginning of an agent's behavior chain.

The neighbor definitions are stored in the network_neighbors field. This contains an array of agent definitions, much like you would find in context.neighbors(). Note that a neighbor's network_neighbors field will not be stored in the definition to avoid exponentially-growing recursion.

Modifying a Network

This behavior library also contains a sample modify_network.js behavior which contains code for adding and removing nodes and edges.

Undirected vs Directed Networks

The default network implementation is for an undirected network, meaning that edges have no assignment of "direction" between nodes. The directed versions of each behavior do distinguish between in and out neighbors. These are the corresponding behaviors or fields:

  • get_neighbors.js -> get_directed_neighbors.js
  • network_neighbor_ids -> network_neighbor_in_ids or network_neighbor_out_ids
  • network_neigbhors -> network_neighbors_in or network_neighbors_out

Generating a Network

Using NetworkX

By using the create_nx_graph.py behavior, you can make use of the powerful NetworkX Python library in hCore. This behavior functions similarly to our other creator behaviors: you will need to pair it with the Create Agents and Remove Self published behaviors to initialize your simulation.

You need to set the nx_graph parameter on the creator agent's state in conjunction with using the behavior. There are three different configurations you can use to generate different types of graphs:

{
  ...
  "nx_graph": {
    "type": "random",
    "n": number // number of nodes
    "p": number // probability of edge creation
    "template": {} // custom template for agents
  }
}
{
  ...
  "nx_graph": {
    "type": "small_world",
    "n": number // number of nodes
    "k": number // initial degree
    "p": number // probability of rewiring
    "template": {} // custom template for agents
  }
}
{
  ...
  "nx_graph": {
    "type": "scale_free",
    "n": number // number of nodes
    "m": number // degree of new nodes
    "template": {} // custom template for agents
  }
}

You can also specify an optional parameter nx_graph.scale which will space out the positions of the generated agents.

See the official documentation for information about the different types of graphs, and the Network Opinion Spread simulation to see how they can be used.

Importing Data

You can also generate networks in HASH by importing data. The import_directed_network.js and import_undirected_network.js will take a properly formatted JSON dataset available in the simulation and generate agents with the appropriate connections. Datasets must be in the form:

{
  "nodes": {
    "<agent_id>": {
      "<agent_property0>": any,
      ...
    },
    ...
  },
  "edges": [
    ["<agent_id0>", "<agent_id1>"],
    ...
  ]
}

You can also look at the example dataset imported into this Library.

In addition the the import behavior, the creator agent must have a "graph_data" property referencing the name of a dataset available in the simulation, and the Create Agents and Remove Self published behaviors.