Connections rule your life. No point denying it. They are all around you. Be it your Facebook friends, your family members, your colleagues. But connections go deeper than that. When you buy something online, you make a connection with the merchant, with the bank and with the seller.
Even machines make a lot of connections. While the promise of the Internet-of-Things is still a little off, machines interact all the time via connections. Your phone connects to a charger. Your laptop connects to the Internet. An aircraft connects to a gate and a rocket connects to the ISS.
Connections are everywhere. They can be social, digital, mechanical, even philosophical.
So they better be part of a simulation tool, right? Good news…
The AnyLogic “Agent-Link”
I have given a talk about “the hidden gems of AnyLogic” and one of the most powerful gems is a timid object that you can’t even see when you create a new model or a new agent. It is only when you pan up a litte that you see our little friend:
While it doesn’t look like much, it gives you tremendous power over your model. You can keep track of agent communication and message passing. You can connect and disconnect agents from each other. You can have uni- and bi-directional connections. But let’s start slow and explore the various options you have at your hand. For that, I uploaded an example model that you can use to follow along:
However, screenshots and explanations below should suffice as well.
A simple connection
Lets create a simple model with an agent-type “Person”. By default, it gets the “connections” object we saw above. However, I typically never use it. I prefer to control connections more tightly by adding my own “Link to Agent” object from the Agent palette. Fortunately, you can have as many as you like so lets create one called “myBestFriend”. In the properties, make sure to select “Single Link” because, obviously, you can only have one best friend :
Note that the “Agent type” specifies what kind of agent your best friend will be. Lets stick with Person for now.
If you run the model now, a very sad connection object will tell you that you do not have a best friend. Lets change that quickly:
You need to use Java code to connect your agent to another and you can find the API in the AnyLogic Help. Here, lets just connect on the model start to a random guy:
We now have a best friend and it is the lucky Person number 21, happy days:
However, if we check Person 21, he or she doesn’t know how lucky she really is. She doesn’t know about you at all!
There is an easy fix in the properties. Click on the button “Make bidirectional…” and select the first option. Now, whenever you connect to someone else, the other agent is also connected to you. This is extremely useful as you do not need to manage connections at both end of the communication. Suppose, we model a supply chain where trucks collect from a storage. If the truck connects to the storage upon arrival, the storage knows about that. When the truck leaves, it disconnects and the storage notices that automatically. No need to tell both explicitly.
If we run our model, lucky person 21 is now your best friend as well:
You can let AnyLogic animate connections (if they live in the same hierarchy level) by clicking the “Draw line connecting agents” tickbox in the properties:
Running the model, you can see how is your best friend, which is very powerful when many connections occur:
You can double-click on single links like the one above and it will take you to the connected agent!
Connecting to other agent types
Note that in our example, we created a bi-directional connection to another Person agent. However, you can (and should) create them between different agent types, such as trucks and storages. Suppose, our person wants to choose a bank to deposit money. Lets create a “Bank” agent and a small population of 10 different banks.
In Person, create a new Link object “myBank” and select the correct agent type that this will connect to:
Then, make it bi-directional. A pop-up will ask you to name the link object that will be created in the Bank agent. Name it “myCustomer”. If you open the Bank agent, you will see it (but you need to pan up again):
Now let’s connect our Person 0 to a random bank at the model start as before:
Often, bi-directional connections are not 1-1 as we have seen so far. You might only have one bank but obviously, the bank has many customers. In order to create a 1-to-many relation, setup a 1-1 relation as before. Next, go the the connection end that will have many connections, in our case to the Bank’s “myCustomer”. Simply change it to a “collection of links” and notice how the symbol changed a little.
Lets connect several persons to bank 5 at the model start to demonstrate:
If you open bank 5 at runtime now, you will see it has several customers, as below:
The great thing is that you can disconnect from either side. A customer person might not want to be a customer anymore:
Or the bank might close and needs to inform all its customers:
In either case, the other end of the connection is managed correctly.
The beauty of agent connections are not in just setting them up. You want to use them, right? Typically, you employ message passing via your connection objects. Say you want to high-five your best friend?
What if your bank sends out a newsletter to all its customers?
But maybe it just wants to let you know that you better get your bills sorted:
banks.get(5).myCustomer.deliver("pay up!!!", people.get(0));
In the object properties, you can also ensure that messages are automatically forwarded to the agent’s state charts. Below, all messages sent from customers to the bank get a response. At the same time, they are forwarded to the state chart, which only executes actions if the bank is waiting for a message:
Remarks and tips
In the old days, you had to manage connections manually via collections. However, the Link-to-Agent object is extremely powerful in managing agent communications. I apply it in every model and use it for all my communications in a model.
I find it helpful to name them coherently as conn_myXXX where XXX is the agent type I connect to. This helps when coding as code-complete after conn_ gives you all possible connections and you can choose the right type.
The only thing I miss is to be able to automatically draw link animations when the agents are not on the same hierarchy level or are of different types. However, this is also possible with a bit of Java magic, but … another day.