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
23
24
25
26
27
28
29
30
31
32
33
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
44
45 private List<Cell> cells;
46
47
48
49
50 private final int INITIAL_POPULATION = 20;
51
52
53
54
55 private Random random;
56
57
58
59
60 private WorldPoint worldDimensions;
61
62
63
64
65 private SimulatedEvolutionWorldLattice simulatedEvolutionWorldLattice;
66
67
68
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
81 }
82
83
84
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
105
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 }