a Swarm Web Interface for Experimental Economics

What & Why?
A technical introduction
Examples & Tutorials
Docs & FAQ
Download
Staff
Links

A simple Prisoner's Dilemma

The first example of the SWIEE project is an implementation of a Prisoner's Dilemma game. Before starting you can read about it in a lot of Internet sites, or you can try to loose against a Tit for Tat player with this applet.

  1. The code
  2. Starting the experiment
  3. Playing the experiment
  4. Analyzing results immediately

 

There are some basic add-ons to the standard Swarm framework (to learn about it check resources on the Swarm Development Group site: swarm.org), here they are presented.

You need to insert in a standard Swarm model a server managing the RMI connection. This class is called RemoteServer:

import java.rmi.Naming;
import java.rmi.*;

// Remote Server's class easily manages in Swarm simulation
// RMI Java technology

public class RemoteServer {
 Player[] playerArray;
 Timer timer;
 public RemoteServer(Player[] pl, Timer t) {
  playerArray = pl;
  timer = t;
  try {
   RemoteGt1 c = new RemoteGt1Impl(playerArray, timer);
  
// Pay attention to the IP of your host and to the port:
   // default port of RMIRegistry is 1099
  
Naming.rebind("//YOUR_IP:1098/RemoteGt1", c);
   System.out.println("RemoteServer is registered.");
  } catch (Exception e) {
   System.out.println("Trouble in get RemoteGt1: " + e);
  }
 }
}

It is activated by ModelSwarm and it activates RemoteGt1 that is the Stub and Skeleton Interface and Implementation.

The RemoteGt1 Interface declares methods available through RMI link, for example:

import java.rmi.*;

// Interface to Server Methods

public interface RemoteGt1 extends Remote {
 public void setChoice(int i, int v) throws RemoteException;
 public void setExpectation(int i, int v) throws RemoteException;
 public int getOtherChoice(int i) throws RemoteException;
 ....

}

This interface is implemented by RemoteGt1Impl.java:

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

// Implementation of Server's Methods.

public class RemoteGt1Impl extends UnicastRemoteObject 
implements RemoteGt1 {
 public Player[] playerArray;
 public Timer timer;
 public RemoteGt1Impl(Player[] pl, Timer t) throws RemoteException {
  super();
  playerArray = pl;
  timer = t;
 }
 public void setChoice(int i, int v) throws RemoteException {
  int id = i, value = v;
  playerArray[id].setChoice(value);
 }
 public void setExpectation(int i, int v) throws RemoteException {
  int id = i, value = v;
  playerArray[id].setExpectation(value);
 }
 public int getOtherChoice(int i) throws RemoteException {
  int id = i;
  int value = playerArray[id].getOtherChoice2();
 return value;
 }

 ....
}

The players' applet (Gt1Client.java) has to call the server, get the service and assign to it a reference like a normal object (called server):

 try {
  URL hostURL = getCodeBase();
  String host = hostURL.getHost();
  // Change the port number if necessary
  server = (RemoteGt1)Naming.lookup("//"+host+":1098/RemoteGt1");
 } catch (Exception ex) {
  textArea1.setText(ex.toString());
 }

The final important piece of code is Timer.java a not-standard but very simple class that manages the synchronization between simulation and players' steps.

Experimenter starts the simulation (with the instruction "java StartGt1"), then he can set some parameters, like the number of players, the number of cycles and the payoff matrix, via ModelSwarm probes; he can also set a file (and its path) to output data:

The payoff matrix must be symmetric for the two players, or the experimenter has to modify ModelSwarm source code. The standard matrix of this application corresponds to the one described by Tucker (1950, "A two-person dilemma", Stanford University Press):

  Confess Not confess
Confess (c,c) (b,d)
Not confess (d,b) (a,a)

with b>a>c>d and, in this application, a = b2(-1), b = a2(0), c = a1(-3) and d = b1(-6)

Obviously the user can set different payoff values, changing also the structure of equilibria.

The output data file is a simple text file, with different data separated by one white space, like this:

The first row presents headers for the different data, the first column presents the player's identifier. This file can be easily loaded in many useful applications for data managing.

The data writing object is buffered so to not weight the server down: data are physically saved only when every round ends.

 

After the experimenter can push the Start button and players can load their html pages with the java applet, and so they can interact with the simulation.

Player's applet:

With this applet, players can choose and read information about past rounds. In the Messages area they can read information on steps to follow; clicking on the "Game instructions" button appears the Instructions window where they can read a description of the game and where they can read the payoff matrix. Every access to this window is recorded an saved to the last column of the data output file; to get it, the Instructions window is a Java Dialog object and therefore players have to close it if they want to continue playing. 

The Game Instructions window:

 

When all cycles are gone, experimenter can access the ValueGraph of the players' performance:

Or can use other applications to manage output data file, for example:

 

See the Download section to download the software of this experiment and to read installation instructions.