View Javadoc
1   package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2   
3   
4   import lombok.EqualsAndHashCode;
5   import lombok.Getter;
6   import lombok.ToString;
7   import lombok.extern.log4j.Log4j2;
8   import org.woehlke.computer.kurzweil.commons.tabs.TabModel;
9   import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.Cell;
10  import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.SimulatedEvolutionParameter;
11  import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.SimulatedEvolutionWorldLattice;
12  import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.WorldPoint;
13  import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.population.SimulatedEvolutionPopulationContainer;
14  
15  import java.io.Serializable;
16  import java.util.ArrayList;
17  import java.util.Date;
18  import java.util.List;
19  import java.util.Random;
20  
21  /**
22   * The World contains Water, Cells and Food.
23   * It is the Data Model of the Simulation in a MVC Pattern.
24   *
25   * @see Cell
26   * @see SimulatedEvolutionWorldLattice
27   *
28   * © 2006 - 2008 Thomas Woehlke.
29   * http://thomas-woehlke.de/p/simulated-evolution/
30   * @author Thomas Woehlke
31   * User: thomas
32   * Date: 04.02.2006
33   * Time: 19:06:20
34   */
35  @Log4j2
36  @ToString(exclude = {"random"})
37  @EqualsAndHashCode(exclude = {"random"})
38  public class SimulatedEvolutionModel implements Serializable, TabModel {
39  
40      static final long serialVersionUID = 242L;
41  
42      /**
43       * List of the Simulated Bacteria Cells.
44       */
45      private List<Cell> cells;
46  
47      /**
48       * Start with 20 Cells.
49       */
50      private final int INITIAL_POPULATION = 20;
51  
52      /**
53       * Random Generator used for Bacteria Motion.
54       */
55      private Random random;
56  
57      /**
58       * Definition of the World's Size in Pixel Width and Height.
59       */
60      private WorldPoint worldDimensions;
61  
62      /**
63       * Map of the World monitoring growth and eating food.
64       */
65      private SimulatedEvolutionWorldLattice simulatedEvolutionWorldLattice;
66  
67      //@Getter
68      //private SimulatedEvolutionPopulationContainer simulatedEvolutionPopulationContainer;
69  
70      @Getter
71      private SimulatedEvolutionParameter simulatedEvolutionParameter;
72  
73      public SimulatedEvolutionModel(WorldPoint worldDimensions) {
74          long seed = new Date().getTime();
75          random = new Random(seed);
76          this.worldDimensions = worldDimensions;
77          simulatedEvolutionWorldLattice = new SimulatedEvolutionWorldLattice(this.worldDimensions,random);
78          createPopulation();
79          simulatedEvolutionParameter = new SimulatedEvolutionParameter();
80          //simulatedEvolutionPopulationContainer = new SimulatedEvolutionPopulationContainer(tabCtx);
81      }
82  
83      /**
84       * Create the initial Population of Bacteria Cells and give them their position in the World.
85       */
86      private void createPopulation() {
87          cells = new ArrayList<Cell>();
88          for (int i = 0; i < INITIAL_POPULATION; i++) {
89              int x = random.nextInt(worldDimensions.getX());
90              int y = random.nextInt(worldDimensions.getY());
91              if (x < 0) {
92                  x *= -1;
93              }
94              if (y < 0) {
95                  y *= -1;
96              }
97              WorldPointrzweil/tabs/simulatedevolution/model/WorldPoint.html#WorldPoint">WorldPoint pos = new WorldPoint(x, y);
98              Cell cell = new Cell(worldDimensions, pos, random);
99              cells.add(cell);
100         }
101     }
102 
103     /**
104      * One Step of Time in the World in which the Population of Bacteria Cell perform Life:
105      * Every Cell moves, eats, dies of hunger, and it has sex: splitting into two children with changed DNA.
106      */
107     public void letLivePopulation() {
108         simulatedEvolutionWorldLattice.letFoodGrow();
109         WorldPoint pos;
110         List<Cell> children = new ArrayList<Cell>();
111         List<Cell> died = new ArrayList<Cell>();
112         for (Cell cell:cells) {
113             cell.move();
114             if(cell.died()){
115                 died.add(cell);
116             } else {
117                 pos = cell.getPosition();
118                 int food = simulatedEvolutionWorldLattice.eat(pos);
119                 cell.eat(food);
120                 if (cell.isPregnant()) {
121                     Cell child = cell.performReproductionByCellDivision();
122                     children.add(child);
123                 }
124             }
125         }
126         for(Cell dead:died){
127             cells.remove(dead);
128         }
129         cells.addAll(children);
130     }
131 
132     public List<Cell> getAllCells(){
133         return cells;
134     }
135 
136     public boolean hasFood(int x, int y) {
137         return simulatedEvolutionWorldLattice.hasFood(x,y);
138     }
139 }