Evolve ethnocentrism in your spare time

Running an agent based simulation really isn’t that complex. While there’s no shortage of ready-made software packages for ABM (like Repast and NetLogo), all you really need is a good, high-level programming language and a code editor.

As you may have noticed from other blog posts, we have spent quite a bit of time studying agent based models of ethnocentric evolution. To coincide with the publication of our paper (Hartshorn, Kaznatcheev & Shultz, 2013) on the evolution of ethnocentrism in the Journal of Artificial Societies and Social Simulation (JASSS), we thought it would be fun to provide a hands-on tutorial so you can replicate the model yourself. There’s a lot to cover here, so we won’t get into the scientific description of the model itself, but you can read a good synopsis in my executive summary, or Artem’s general overview.

This post assumes no programming background, just a computer, patience, and some curiosity. That being said, you will be compiling a small Java program and modifying its source code, so if words like “compile,” “source code,” and “Java” strike terror in your heart, consider yourself forewarned. It’s actually not that scary. In Estonia they’re teaching kids to program in first grade, and you’re smarter than a first grader…right?!

Getting a Programming Language

Okay, so first thing we need is a programming language. We’re going to use Java because it’s reasonably straightforward and easy to set up. Java is possibly the most popular programming language in the world, and chances are you already have a version of it installed your computer, but not the version we want. In order to get started, we need to download a special version of Java that allows us to write and not just run Java programs. Follow this link and download the version of the Java Development Kit that matches your operating system from the first set of download links.

You’ll notice an x86 and x64 next to the operating systems listed, this refers to the type of OS you’re running. For example, the Windows x86 link is for the 32-bit version of Windows and the Windows x64 link is for the 64-bit version. How do you know which version you’re running? If you’re using Windows click here for instructions. If you’re on Mac click here.

Next, unpack your download and follow the installation instructions, you may need to restart your computer. Go ahead…I’ll wait.

Getting a Code Editor

After you’ve installed Java, the second thing you’ll need is a piece of software for editing and running Java programs. Eclipse is one of the most popular, and it’s free, so we’ll go with that. Go to the following download page and click the download link on the right that corresponds to your operating system. Eclipse doesn’t come with a fancy installation wizard like most programs you’re used to. All you have to do is unpack the file, and drag your folder to whatever directory you want (for example C:\Program Files on Windows). For Mac users, drag the “eclipse” folder to your Applications directory.

Now open it (if it won’t start check out this troubleshooting page). When you open Eclipse for the first time it will ask you to select a workspace directory, this is where you’ll store the simulation we’re about to run.

Next, you’ll see a friendly looking welcome page like the one below:

Eclipse welcome screen

Eclipse welcome screen

Click the right arrow and you’ll be taken to a considerably less friendly looking page. Welcome to Eclipse.

Eclipse user interface

Eclipse user interface

Getting our Simulation

Finally, we’ll need the actual simulation code, which you can download from here (scroll down to where it says “Model Files” and click the “Code” link). Unzip the download, and drag it into your workspace directory.

In Eclipse go to File -> New -> New Project to open the project creation wizard.

Eclipse project creation wizard - Step 1

Eclipse project creation wizard – Step 1. Select “Java Project”.

Select “Java Project” and click Next.

Eclipse project creation wizard - Step 2

Eclipse project creation wizard. Step 2.

Your project name should be exactly the same as the name of the folder you just dragged into your workspace. If you didn’t edit its name, it should be “EMod5_9-28-09”. Leave all other settings the same and click Finish.

Eclipse file explorer

Eclipse file explorer. Navigate to EMod5_9-28-09 -> (default package) to find the files associated with this project.

On the left-hand side of the screen you should now see your project: “EMod_9-28-09”. If you click the expand arrow you will see 2 cryptically named items, “(default package)” and “JRE System Library”. If you expand “(default package)” you will see 6 Java files. This is our code.

Running the Simulation

We’re very close now, I won’t force you to wade too deeply into the muck. We just need to make one small update to the code before we can run it.

  • Now on your desktop create a new folder and name it whatever you like (“results” maybe?). In that folder add a directory and name it “benefit-3x-cost”. The output from the simulation will be written into this directory.
  • Expand “(default package)” and double click on Game_Shell.java to open the file for editing. Note the numbers on the left side of the editor window, those are line numbers.
  • On line 20 of Game_Shell.java, you will see the following text: “/your/path/here/”. In between those quotation marks write out the full file path to the directory you just created on your desktop. (Something like “C:\\Users\\Bill\\Desktop\\Results\\” on Windows, or “/Users/Steve/Desktop/Results/” on Mac). Note how Java on Windows requires a double backslash “\\” while Mac requires a single forward slash “/” to separate directories.
  • Go to File -> Save to save your changes.
  • Click the little green play button at the top left Run button

There you have it, your program should begin to running like—umm—a running program (us computer people ain’t too good with metaphors). But how do you know it’s working?

At the bottom of the Eclipse window you will see something called “Console”. This is essentially your window into the inner life of your program. If all is going well, it should look something like this:

Eclipse console

Eclipse console. It should look something like this.

If Eclipse encounters an error, here’s where it will let you know. Maybe you didn’t enter your path correctly on line 20. Maybe you didn’t create the “benefit-3x-cost” directory.

Eclipse console after error

Your console will show an error like this if you did not create an output folder.

The simulation may take a good 15 – 20 seconds to run. Seem long? We’re actually running 10 separate simulations in sequence and computing aggregate statistics. You’ll know it’s finished when the little red box Stop button at the top of your Console disappears. Now, go back into the directory you created on your desktop and click on the “benefit-3x-cost” directory. You should see a bunch of text files that weren’t there before. These are your results.

Interpreting your Results

1) Individual Simulation Summary (e.g. “b=0.03 – EHST – simulation1.txt”)

In your “benefit-3x-cost” directory you should see 10 files of the type “b=0.03 – EHST – simulation1.txt”. Recall that we ran a set of simulations, not just one. Each simulation produces its own text output file, which can be imported into a spreadsheet or statistical analysis program like Excel or SPSS as a CSV file. Each row in the file corresponds to a generation in the simulation, the columns are as follows:

  • H – the current frequency of humanitarian agents: a strategy of indiscriminate cooperation with all agents
  • T – the current frequency of traitorous agents: a strategy of cooperation with one’s out-group but not in-group
  • S – the current frequency of selfish agents: a strategy of constant non-cooperation
  • E – the current frequency of ethnocentric agents: a strategy of preferential cooperation with one’s in-group
  • Chi – a chi square test to determine if any one strategy dominates
  • i – the current simulation generation (1 – 2000)

2) Simulation Set Summary (e.g. “b=0.03 – EHST – summary.txt”)

Each set of 10 identical simulations produces its own summary file, which records the overall mean frequency, standard deviation, and percentage of each strategy over the last 100 cycles. Also recorded are the percentage of in-group and out-group cooperators, and the overall percentage of cooperative behavior over the last 100 cycles.

Don’t worry about the other file: “overall summary b=0.03.txt”. It’s not relevant for our purposes.

Varying the Cost to Benefit Ratio

The simulation set we just ran uses a cost to benefit (c/b) ratio of 1/3. That is to say the cost of cooperating with one’s neighbor is 1/3 the benefit of being cooperated with. This is a pretty good ratio for ensuring the evolution of cooperation. We can vary the c/b ratio to model more competitive evolutionary environments where the benefit of receiving cooperation is closer to the cost of giving it. We can then examine the impact of the new c/b ratio on the evolution of ethnocentrism and cooperation in general. Here’s how:

  • In the results folder you created on your desktop add the following folders (in addition to the “benefit-3x-cost” folder that’s already there): “benefit-1.5x-cost”, “benefit-2x-cost”, “benefit-2.5x-cost”, “benefit-6x-cost”.
  • Go back to Game_Shell.java. On the start of line 25 you’ll see a forward slash followed by an asterix “/*”. At the end of line 31 you’ll see an asterix followed by a forward slash “*/”. These are known as comment markers. Basically any text between a “/*” and a “*/” is skipped over by the program when it executes. It’s as if the text wasn’t even there in the first place. Programmers often use comment markers to add descriptive comments to their code so other programmers aren’t left scratching (or more realistically yanking the hair out of) their heads trying to figure out how the code works. People also use comment markers to prevent blocks of code from executing, which is exactly what we’ve done here. Deleting the “/*” on line 25, and the “*/” on line 31 will let the other c/b ratio simulations to run.
  • Go to File -> Save to save your changes.
  • Click the little green play button at the top left Run button.

It will take a little longer to run this time, as you’re running 5 simulation sets of 10 simulations each. If all goes well, the results should populate the folders you just created. You’ll have plenty of data to analyze so I’ll leave you to it. If you hit a snag along the way or if want me to explain something more clearly, please comment on this post. I will do my best to update my instructions so they are error proof. If you are literally at the pulling-your-hair-out-smashing-your-cup-of-coffee-at-the-wall phase, email me at max [dot] hartshorn [at] gmail [dot] com and I will try and steer you in the right direction.

ResearchBlogging.orgHartshorn, M., Kaznatcheev, A., & Shultz, T. (2013). The Evolutionary Dominance of Ethnocentric Cooperation. Journal of Artificial Societies and Social Simulation, 16 (3)

3 Responses to Evolve ethnocentrism in your spare time

  1. Pingback: Stats 101: an update on readership | Theory, Evolution, and Games Group

  2. Pingback: Cataloging a year of blogging: applications of evolutionary game theory | Theory, Evolution, and Games Group

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: