Skip navigation.
Home

Interaction between Repast Simphony Agents and Python

1. Introduction

In this technical note, we do an experiment on the interaction between a simple agent-based model implemented by Repast Simphony and a simple model written in Python. This experiment has been motivated by our need to construct a computational interface between agent-based socio-economic model and a Python-based biophysical model.

2. Brief Description of Experiment Model

We design two simple models to experiment their interaction. One model is implemented using Repast Simphony, the other is written in Python.

2.1. Repast Simphony Model

We establish an agent model using Repast Simphony. There is one agent that represents a client in the model. An account assigned to the agent is also established in Python. The agent can do some operations to its account, such as deposit money, withdraw money, and get balance from it. At each time step, the agent will choose one operation to act, like depositing money, withdrawing money, getting balance, or no action. Some relative operations are shown as Fig. 1 and Fig. 2.

2.2. The Model Written in Python

Account part is written in Python. It provides some operations to deal with the agent’s account, such as adding and subtracting. If the account is overdrawn, the account will send a notification to the agent and let it manage the overdraft forcibly. Above operations are shown as Fig. 1 and Fig. 2.


Fig.1. UML Model for Interaction Process


Fig.2. Sequence Diagram for Interaction Model

3. Interaction between the Two Models

From the model description in Section 2, we can see there are two interactions between two models. Firstly, if the agent (representing a client) in Repast Simphony model deposits money into its account, add() method in Python will be invoked. If the agent withdraws money from its account, subtract() method in Python will be invoked. These parts need to call Python method from Repast Simphony model. Secondly, If the account is overdrawn, the account will invoke sendNotification() method to call the agent’s manageOverDraft() method forcibly. This part needs to call method in Repast Simphony model from Python Model.

Since a Repast Simphony modeler can create model pieces, as needed, in the form of Java objects. So we can shift the interaction issue between the Repast Simphony model and Python's account model to the interaction between Java and Python.

3.1. Jython

We chose Jython as a bridge to facilitate the interaction between the Repast Simphony model and the model in Python. This is shown as Fig. 3. It is an implementation of the high-level, dynamic, object-oriented language Python seamlessly integrated with the Java platform. Java programmers can add the Jython libraries to their system to allow end users to write simple or complicated scripts that add functionality to the application.


Fig.3. Jython as A Bridge to Facilitate the Interaction between Two Models

3.2. Call Python Method from Repast Simphony Model

There are several options for embedding Jython in a Java application. Sometimes the nicest approach is to make a real Java class out of a Python class and then just use that Python class from Java code. The simplest approach to embedding Jython is to use the PythonInterpreter object. Using it, a Java model will call some operations to the account, such as the add(money) method in a Python file named “Balance.py”. Fig. 4 shows a Java code segment to call Python methods. The methods are shown in Fig. 5.


Fig.4. Java Code Segment to Call A Method in Python

3.3. Call Java Method in Repast Simphony Model from Python Model

In Python model, required Java classes are imported. Then, Python code can call required Java code. For example, after the Python model calls the manageOverDraft() method of the agent to make the agent deal with its overdraft issue. Now the manageOverDraft() method just prints "Oh, overdraft! I have to work hard to make money". The code segment is shown as Fig. 5.


Fig.5. Python's Code Segment to Call Java Methods

4. Results

When the demo runs, at each time step, the interaction processes between Repast Simphony model and Python model will be executed once. A part of output is shown in Fig. 6.


Fig.6. A part of output of Interaction Model

The source codes are attached as follows

AttachmentSize
Source Code in Java.zip2.26 KB
Source Code in Python.zip370 bytes

thank you

thank you for this article!It is very helpful!

Great article.

Great article.

Latest image