Assignment 5: Artificial Life

Summary
Collaboration
You must work individually on this assignment. You may only discuss this assignment with the course staff.

Goals for this assignment.

  • Design, write, and test a Java program that simulates an artificial life.
  • Practice using appropriate data structures
  • Practice using the git tool.
For this assignment, keep track and record the progress of your project by using Git. 

You will have to submit the output of the git log showing at least 3 commits along with a comment that explains your decision about when to commit your files.

Overview

Artificial Life is a field of computer science that uses the power of computers to gain understanding about the fundamental processes of living systems. Research in Artificial Life (or A-Life) often uses highly complex object-oriented software programs to model properties of a living organism. However, the necessary elements for an artificial life simulation are actually fairly easy.

In this assignment, you will be using your skills in object-oriented programming to make an artificial life simulation of cooperation between bacteria. Bacteria actually cooperate in lots of interesting ways. Because they are so small, however, it is difficult for biologists to figure out exactly what each individual is doing. Instead, we have a good understanding of the large-scale patterns seen in bacterial colonies. This topic is therefore great for a computer simulation, because you can test out hypotheses about what individual organisms are doing and compare your large-scale results to what we already know is happening in biological systems.

Implementing Artificial Life

Fundamentally, an artificial life simulation can consist of only two primary classes:

The Population class is how you represent your virtual petri dish. It keeps track of how long the experiment has been running and what sort of environment your digital bacteria are attempting to live in. It also has methods for collecting statistical information about your experiment. The Population class will hold a list of all of your organisms and at every “time point” or tick it will loop over those organisms and allow them to perform one action, represented by an update method.

The Organism class is how you represent a single individual organism. This project is one where object-oriented programming is a powerful paradigm, because you will have hundreds of instances of Organisms, all with different values for their variables. The Organism class holds methods for determining what each instance is going to do at any given time (in our project, cooperate or not) and instructions for reproduction if that instance has gotten enough resources. Each organism has an “energy level” that measures how much energy that organism has currently. When the organism has enough energy, it is able to reproduce, which uses up all the energy it had saved. You will give each organism one new energy at each update.

Cooperation

You are going to use your artificial life simulation to study the dynamics of cooperation between bacteria of the same species. Your Organism classes will track the likelihood that it cooperates with other organisms, its cooperation probability. When an organism has a chance to act, i.e. when its update method is called. You will use that cooperation probability number to determine if the organism cooperates with its neighbors. In this project, the cooperation probability will be between 1 (100%) or 0 (0%). If the organism’s probability is 1, it will cooperate with organisms around it; if it is 0, it does not cooperate; and if it is somewhere between it cooperates with that probability.

When an organism cooperates, it loses one of its energy units and in exchange it gives eight other organisms an energy unit. (You will randomly select which other organisms to give energy to.)

Classes and Sub-Classes

Note: Create a package for your classes, called "simulator"

The Organism Class

This class will represent individual bacterium in your population. It should have the following constructor and methods:

  • Organism(): construct an Organism, initializing the organism-specific state of this object. By default, an organism starts with zero energy.
  • void update(): updates the given organism. By default, an organism gains one new energy point.
  • int getEnergy(): returns the current energy of this organism.
  • void incrementEnergy(): increments this organism’s energy by 1.
  • void decrementEnergy(): decrements this organism’s energy by 1. An organism’s energy cannot be decremented below 0.
  • abstract String getType(): returns the type of this Organism as a string.
  • abstract Organism reproduce(): called by update when the organism can reproduce. Creates an offspring organism and returns it.
  • abstract double getCooperationProbability(): returns the cooperation probability of this organism.
  • abstract boolean cooperates(): returns whether or not the organism cooperates.

Note that no Organism objects themselves actually exist as they are an abstract concept in this program; you should only create instances of sub-classes of the Organism class described below.

The getType, reproduce, getCooperationProbability, and cooperates methods must be overridden by the subclasses. We should enforce this rule by making those abstract methods.

Organism Sub-Classes

We will use inheritance to differentiate the behavior of the different organisms in our system. In your program, implement the following sub-classes of Organism:

  • Cooperator: a type of organism (“Cooperator”) that always cooperates with its peers.
  • Defector: a type of organism (“Defector”) that never cooperates with its peers.
  • PartialCooperator: a type of organism (“PartialCooperator”) that cooperates with probability 0.5. That is, it acts as if it is cooperator 50% of the time and as a defector 50% of the time.

The Population Class

Your class should have the following constructor and methods:

  • Population(Map<String, Integer> counts): constructs a population of organisms dictated by the given set of pairs that associate (case-sensitive) names of organisms to counts of that type of organism. Organisms not mentioned in the mapping do not appear in the population. Throws an IllegalArgumentException if the mapping mentions organism types that do not exist in the program.
  • void update(): loops through all the organisms in the population and (1) updates them (by calling their update method), (2) checks to see if they cooperate as described above and (3) checks to see if they reproduce. We check to see if an organism reproduces by first checking its energy level. If the organism has at least 10 energy units, then it reproduces and that new organism replaces a random organism in the population.
  • double calculateCooperationMean(): calculates the mean cooperation probability of all the organisms in the population—the average of the cooperation probabilities of all the organisms in the population.
  • Map<String, Integer> getPopulationCounts(): returns the counts of all the organisms in the population.

Note that when implementing update() that you should respect encapsulation of data between the population and organism class. Furthermore, you should also be aware of ConcurrentModificationExceptions in your code. Such an exception occurs whenever you add or remove from a list that you are currently iterating over with an iterator. You will need to be careful with how you design your iteration pattern in update to avoid this problem.

The ALifeSim Class

Finally, write a driver (main) class ALifeSim that creates a simulation with parameters provided from the command-line and runs that simulation for a particular number of iterations. Your ALifeSim class should contain a main method that accepts command-line arguments of the following form:

  java ALifeSim <#/iterations> <#/cooperators> <#/defectors> <#/partial cooperators> 

Your ALifeSim simulates the given population for the given number of iterations of the simulation. It then reports the final counts of organisms and mean cooperation probability for the population in the following format:

After <#/iterations> ticks:
Cooperators = <final #/cooperators>
Defectors   = <final #/defectors> 
Partial     = <final #/partial cooperators>  

Mean Cooperation Probability = <mean cooperation probability> 

Assignment Report

You will use your program to perform a number of experiments with artificial life and write a short essay on the results.

You should use the following settings:

  1. Run all experiments for 100 updates
  2. Test population sizes of 100 - note that the size of the population does NOT change during the simulation
  3. Test starting populations of different configurations:
    1. with 1 cooperator and the rest defectors,
    2. with 1 defector and the rest cooperators,
    3. with 1 cooperator, 1 defector, and the rest partial cooperators,
    4. with 1/3 cooperators, 1/3 defectors and 1/3 partial cooperators,
    5. with 50 cooperators, 25 defectors, and 25 partial cooperators.
  4. Run all configurations 10 times to see the trend.
  5. For all experiments you will be reporting the average cooperation value of the population at the end of the experiment.

For each experiment you should write a short paragraph reporting:

  • A prediction of what types of organism will thrive and why you have that prediction
  • The average cooperation mean for each type of organism and the average of averages.
  • Whether or not the results support your prediction and why.

Finally, you should write a concluding paragraph that summarizes the results you found. What insights can you make regarding what lets cooperators succeed in this simulation versus go extinct?

All together, this report should be at least a page in length. You may want to test other population sizes and starting proportions to get a better idea of what dynamics are happening.

Submission and Grading

You will have to submit the output of the git log –showing at least 3 commits– along with a comment that explain your decision about when to commit your project/files, and the source files.

You MUST include an Academic Honesty Statement in your Program (the file with the main method). If you do not, your submission will be assessed a 10% penalty. Code that does not compile or have corrupted files will received a zero (0). This is the last assignment, so there is no opportunity to resubmit your work.

You will submit this assignment via Gradescope. You must upload the source files, the assignment report, the git log, and the compressed file containing all the aforementioned files. The compressed file must be a tar.gz file (see Lab 1 for instructions on how to create this file from your directory). Files that are in a .zip format will lose one point. Others types of compressed files will not be graded.

This assignment is worth up to 25 points based on the following:

Good practices

  • [1 point] Well documented code (use of Javadoc comments). Points will be deducted if your code is difficult to read.
  • [1 point] Use of version control system software to maintain multiple versions of software under development.
  • [1/2 point] Avoided spelling literal constants in code (“magic numbers”).

Meet Requirement specification

  • [4 points] Population class methods meet specifications: constructor, update, calculateCooperationMean, and getPopulationCounts
  • [5 points] Organism class methods meet specifications: constructor, incrementEnergy, decrementEnergy, update, getEnergy, abstract reproduce, abstract getType, abstract getCooperationProbability, and abstract cooperates
  • [3 points] ALifeSim class: sets up the simulation using the command-line parameters and reports the results
  • [6 points] Report includes data from 5 experiments (could be in a spreadsheet) and reporting the average cooperation value of the population as a whole.
  • [3 points] Report your prediction of which type of organism would be most plentiful at the end of the simulation. Were you correct?

Apply object-oriented design principles to the creation of Java programs

  • [1/2 point] Fields and methods’ visibility demonstrate good OOP design practices.

Handling errors

  • [1 point] Handles possible exceptions (e.g., program handles errors such as errors in command-line arguments)

Acknowledgements

The original version of this assignment was written by Anya Vostinar and is available at: https://github.com/anyaevostinar/alife-assignment

Extra

For your assignment to be considered for extra points, the Artificial Life Simulator must work without errors, as detailed in the assignment requirements. You can create a different tester for the extra section.

  • Create a visual representation of the Artificial Life Simulator. (The visual representation should communicate the environment (population and organisms) clearly and effectively by using graphics and text/labels)
  • In the report, add a set of screenshoots of the simulation and describe them.

The following may or not be helpful.

Basic Animation

To render text in an animated way, we can use a loop but in each iteration of the loop, delay execution by causing the program to sleep. To do this, we’ll use the sleep(milliseconds) function of the Thread class:

public static void printStaggered() throws InterruptedException {
   while(true) {
     System.out.println("Hello World!");
     Thread.sleep(2000);
   } // while
 } // printStaggered

This snippet of code constantly prints Hello World to the console but in two second intervals. The argument to Thread.sleep is the amount of time the program should wait in milliseconds. Note that Thread.sleep throws the checked exception InterruptedException. We use a throws clause here to avoid handling the exception explicitly.

You can use the information above and what you learned about Graphics and DrawingPanel in the Drawing and the Java GUI Lab to create a visual representation of the Artificial Life Simulator.

This section is worth 5 extra points. (No partial points will be given).