Agent-Based Modeling – Connecting Agents

After we create agents, change their state and move them, it’s time also to connect agents. In some scenarios, connections can be beneficial to understand and resolve certain realities depicted in a model.

This post takes into account that you read and exercises all my previous posts in InsightMaker. You can find a reference to all of them here (https://ongalaxies.com/2020/01/27/moving-agents-in-agent-based-modeling-abm-using-insightmaker/). I’ll just cover in detail new settings and relevant code.

Create a new model and name it. You need to create two folders set as agents and two agent populations. On folder for an associate, with an “A Connected” state and “# of A connections” stock. The other folder is for Vendor, with a “B Connected” state and “# of B connections“ stock. Set the state in both folders to be false (StartActive). 

Create two Agent Populations. One for Associates and the other for Vendors. Set them to the right folders. Set both Pupulations Size to 0, and the Placement Method to Network.

Add an Action and linked the two Agent Populations to the action. The model should look like that:

Image 1 – The Model

This time we are going to use the action outside of an agent, so it will run per running cycle. 

First, we need to add five associates and 3 vendors. To do that, we will add the Add from Agent Functions and the general Repeat function. To prevent this code from running every run cycle, we need to add a simple condition:

if [Associates].PopulationSize() = 0 then

  Repeat([Vendors].Add(),3)

  Repeat([Associates].Add(),5)

end if

Running the simulation now. You need to see five associates and three vendors:

Image 2 – Creating Agents

Now we can add lines between the agents. To connect two agents all we need is to use the Connect function of Agent. This function required the target agent and the weight of the link. The weight can be queried and used for analysis. To use dynamic agents, we need to use the FindIndex function. FindIndex required an index, but in the first run, there aren’t any agents. Therefore, we need to use the following logic to prevent the model from crashing in the first run. If you have a better alternative, please share it!

if [Associates].PopulationSize() > 0 then

The second condition needed to prevent the code from creating links any run cycle. That is why we set the states to false. We need to set them to true after we add connections:

if [Associates].FindIndex(1).Value([A Connected]) = false then

    [Associates].SetValue([A Connected], true)

    [Vendors].SetValue([B Connected], true)

The rest of the code just creates any links you want. This is the entire code in Action primitive Action property:

if [Associates].PopulationSize() = 0 then

  Repeat([Vendors].Add(),3)

  Repeat([Associates].Add(),5)

end if

if [Associates].PopulationSize() > 0 then

  if [Associates].FindIndex(1).Value([A Connected]) = false then

    [Associates].SetValue([A Connected], true)

    [Vendors].SetValue([B Connected], true)

    [Associates].FindIndex(1).connect([Associates].FindIndex(2),5)

    [Associates].FindIndex(1).connect([Associates].FindIndex(3),5)

    [Associates].FindIndex(1).connect([Associates].FindIndex(5),5)

    [Associates].FindIndex(1).connect([Vendors].FindIndex(3),1)

    [Associates].FindIndex(2).connect([Associates].FindIndex(1),5)

    [Associates].FindIndex(2).connect([Associates].FindIndex(3),5)

    [Associates].FindIndex(2).connect([Vendors].FindIndex(1),1)

    [Associates].FindIndex(3).connect([Associates].FindIndex(2),5)

    [Associates].FindIndex(3).connect([Associates].FindIndex(5),5)

    [Associates].FindIndex(3).connect([Vendors].FindIndex(1),1)

    [Associates].FindIndex(4).connect([Associates].FindIndex(5),5)

    [Associates].FindIndex(4).connect([Associates].FindIndex(1),5)

    [Associates].FindIndex(4).connect([Vendors].FindIndex(2),1)

    [Associates].FindIndex(5).connect([Associates].FindIndex(3),5)

    [Associates].FindIndex(5).connect([Associates].FindIndex(1),5)

    [Associates].FindIndex(5).connect([Vendors].FindIndex(3),1)

    [Vendors].FindIndex(1).connect([Vendors].FindIndex(3),2)

  end if

end if

If you’ll run the model, you should see something like that:

Image 3 – Connected Agents

Image 3

If you’ll add a default Action primitive to one of the folders and run the model, you can see connection and movement.

That’s all for ABM. The next post will show how to use InsightMaker API to create a model using code. 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: