Jump to content

Simulation with AnyLogic/Agent-Based Modeling/Step 11. Making people contacts distance-based

From Wikibooks, open books for an open world

Step 11. Making People Contacts Distance-Based

Now people are located randomly in 650x300 miles square area. Let people only contact if they are not more than 25 miles away.

Set up the environment properties

  1. Open the Main class diagram by double-clicking the Main item in the Project view.
  2. Select the environment object.
  3. Open the Advanced' tab of the environments Properties view.
  4. Change the Network type to Distance based and modify the Connection range: 25.

Modify the agent's statechart

  1. Open the Person class diagram by double-clicking the Person item in the Project view.
  2. Click on the internal transition of the Adopter state.
  3. Modify the transition's Action. Substitute the existing string for the following:
    send (“Buy!”, RANDOM_CONNECTED);

    People will now contact only with their neighbors.

Now we will modify our animation a little bit to visualize the network of contacts.

Draw a line to depict people contacts

  1. Open the Person class diagram.
  2. Draw a line like shown in the figure below.

    Choose the Line element from the Presentation page of the Palette view. Then click in the graphical editor at the point (0,0) to place the start point of the line and finally click aside where you want to draw the end point.
  3. Open the Dynamic' tab of the lines Properties view and modify some dynamic properties of the line.
  4. In the Replication field, type:
    getConnectionsNumber(),
    (you can use code completion assist when typing).
    Here we specify the number of contact lines for the agent. We need to draw a line for each connection of this agent. For that, we call the getConnectionsNumber() function, returning the number of agents currently connected to this one in our network.
  5. Define line's location on the animation:
    In the dX field, type:
    getConnectedAgent(index).getX() - getX()
    In the dY field, type:
    getConnectedAgent(index).getY() - getY()
    Here we place the end point of each line to the point with the coordinates of the corresponding connected agent. Connected agents are alternately accessed using the getConnectedAgent(index) function.

    Now those people who contact with each other will be shown on our presentation connected with lines.

We have finished creating the animation. Run the model and watch the dynamics on the network.