Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Random;
- public class GeneticDrift_RUN_SIM {
- public static void main(String[] args){
- GeneticDriftSim sim = new GeneticDriftSim();
- for(int i=0; i<5; i++){ // Number of times you wish to run simulation
- sim.go();
- }
- }
- }
- ///////////////////////////////////////////////////
- class GeneticDriftSim {
- public void go() {
- int numOfAnimals = 1000; // total number of males (females will be the same)
- // i.e number you input will be HALF the starting population
- int numOfMatings = 10000; // i.e total number of generations
- ArrayList<Animal> males = new ArrayList<Animal>();
- ArrayList<Animal> females = new ArrayList<Animal>();
- ArrayList<Animal> nextgenerationMales = new ArrayList<Animal>();
- ArrayList<Animal> nextgenerationFemales = new ArrayList<Animal>();
- ArrayList<Animal> lastGeneration = new ArrayList<Animal>();
- for(int i=0; i<numOfAnimals; i++){
- males.add(new Animal('M'));
- females.add(new Animal('F'));
- }
- for(int j=0; j<numOfMatings; j++){
- for(int i=0; i<males.size(); i++){ // male array same size as females anyway
- Animal offspring1 = males.get(i).mate(females.get(i));
- Animal offspring2 = males.get(i).mate(females.get(i));
- nextgenerationMales.add(offspring1); //no need to check for M/F since first
- nextgenerationFemales.add(offspring2); //child always M, second always F
- }
- males.clear();
- females.clear();
- males.addAll(nextgenerationMales);
- females.addAll(nextgenerationFemales);
- nextgenerationMales.clear();
- nextgenerationFemales.clear();
- /*System.out.println(males); // Uncomment this and next section if you
- System.out.println(females); */ // wish to see each generation and the
- Collections.shuffle(males); // phenotypic distribution
- Collections.shuffle(females);
- /*System.out.println();
- System.out.println("-----NEXT GEN-----");
- System.out.println(); */
- }
- lastGeneration.addAll(males);
- lastGeneration.addAll(females);
- int totalA1Count = 0;
- int totalA2Count = 0;
- for(Animal a : lastGeneration){
- if(a.allele1 == "A1"){
- totalA1Count++;
- } else {
- totalA2Count++;
- }
- if(a.allele2 == "A1"){
- totalA1Count++;
- } else {
- totalA2Count++;
- }
- } //closes for loop
- System.out.println("totalA1Count: "+totalA1Count +" totalA2Count: " +totalA2Count);
- }
- }
- ///////////////////////////////////////////////
- class Animal {
- public String allele1;
- public String allele2;
- private char gender;
- private int numOfChildren;
- public Animal(char gender){
- this.gender = gender; // Use no-arg constructor to make the
- this.allele1 = "A1"; // starting Animals so equal frequencies of A1 & A2
- this.allele2 = "A2";
- this.numOfChildren = 0; // all A1/A2 heterozygotes
- }
- public Animal(char gender, String allele1, String allele2){
- this.gender = gender;
- this.allele1 = allele1; // Use to create all future Animals once
- this.allele2 = allele2; // program has started
- this.numOfChildren = 0;
- }
- public Animal mate(Animal parentTwo){
- char genderToPassOn;
- if(this.numOfChildren == 0){
- genderToPassOn = 'M';
- } else if(this.numOfChildren == 1){
- genderToPassOn = 'F';
- } else {
- genderToPassOn = 'N'; // N = neutral. But shouldn't be possible anyway
- } // since not allowed to have >2 children
- String parent1AlleleToPassOn;
- String parent2AlleleToPassOn;
- Random rand = new Random();
- int y = rand.nextInt(2);
- if(y ==0){
- parent1AlleleToPassOn = this.allele1;
- } else {
- parent1AlleleToPassOn = this.allele2;
- }
- int z = rand.nextInt(2);
- if(z ==0){
- parent2AlleleToPassOn = parentTwo.allele1;
- } else {
- parent2AlleleToPassOn = parentTwo.allele2;
- }
- this.numOfChildren++;
- return new Animal(genderToPassOn, parent1AlleleToPassOn, parent2AlleleToPassOn);
- }
- @Override
- public String toString(){
- return this.allele1 + this.allele2 +"("+this.gender+")";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement