Data from pplapi can be loaded into MASON in several ways:
In the following visualization, n=1500 agents were sampled at random from the world population and their latitude/longitude was plotted. Country outlines were composited to provide context.
If you choose to use live API data, this will result in your simulations using different agent data each time. Presently, it is recommended that you simply download PplApi.java and put it in your MASON project. MASON also requires additional Java libraries from the Apache Foundation In order to function. See Installing Java Libraries for more information.
The following example code is an extension of the MASON SchoolYard tutorial. The example will request 500 agents from pplapi and add them to our simulated world.
import sim.engine.*;
import sim.util.*;
import sim.field.continuous.*;
import sim.field.grid.*;
import sim.field.network.*;
import org.apache.commons.csv.*;
import java.io.*;
import java.net.*;
public class People extends SimState {
// people occupy a 2d environment
public Continuous2D world = new Continuous2D(0.1,360,180);
// use pplapi to recruit agents from Agent Space
PplApi pplapi = new PplApi();
public void start() {
super.start();
// clear the world
world.clear();
// add some people to the world
Iterable<CSVRecord> records = pplapi.getBatchCSV(500);
if (records != null) {
for (CSVRecord record : records) {
Person person = new Person();
person.country_name = record.get("country_name");
person.income = Integer.parseInt(record.get("income"));
person.id = Long.parseLong(record.get("id"));
person.longitude = Double.parseDouble(record.get("longitude"));
person.latitude = Double.parseDouble(record.get("latitude"));
person.age = Integer.parseInt(record.get("age"));
person.internet = Boolean.parseBoolean(record.get("internet").toLowerCase());
// put the person on the globe, using latitude/longitude
Double2D location = new Double2D(180+person.longitude, 90-person.latitude);
world.setObjectLocation(person, location);
schedule.scheduleRepeating(person);
}
}
}
public People(long seed) {
super(seed);
}
public static void main(String[] args) {
doLoop(People.class, args);
System.exit(0);
}
}
In order to connect your MASON simulation to the pplapi.com API, it is helpful to make use of the Apache Foundation’s HTTP and CSV libraries. This section provides some information to get you started.
In this author’s experience, it is easiest to copy any .jar files to the OS X default location instead of trying to manually control the CLASSPATH. The following is a brief example:
cp *.jar ~/Library/Java/Extensions