private double calDeathRate(World x) { try { World y = listCells.getLast(); double pX = x.getPopulation(), pY = y.getPopulation(); return (pX - pY) / pX; } catch (NoSuchElementException e) { return -100000; } }
public void addState(World x) { double growthRate = calGrowthRate(x), deathRate = calDeathRate(x); int population = x.getPopulation(); listPopulationChange.add(generation == 0 ? population : population - listPopulation.getLast()); listPopulation.add(population); listGrowthRate.add(growthRate); listDeathRate.add(deathRate); if (generation == 1) { minPopulation = maxPopulation = population; maxGrowthRate = minGrowthRate = growthRate; maxDeathRate = deathRate; maxPopulationChange = minPopulationChange = listPopulationChange.getLast(); } if (generation > 1) { minPopulation = minPopulation > population ? population : minPopulation; maxPopulation = maxPopulation < population ? population : maxPopulation; maxGrowthRate = maxGrowthRate < growthRate ? growthRate : maxGrowthRate; maxDeathRate = maxDeathRate < deathRate ? deathRate : maxDeathRate; maxPopulationChange = Math.max(maxPopulationChange, listPopulationChange.getLast()); minPopulationChange = Math.min(minPopulationChange, listPopulationChange.getLast()); maxGrowthRate = Math.max(maxGrowthRate, listGrowthRate.getLast()); minGrowthRate = Math.min(minGrowthRate, listGrowthRate.getLast()); } generation++; listCells.add(x); }
public boolean checkLoop(World x) { int width = x.getWidth(), height = x.getHeight(); boolean[][] xCells = ((ArrayWorld) x).getCells(); for (World y : listCells) { boolean[][] yCells = ((ArrayWorld) y).getCells(); int i, j; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { if (x.getCell(j, i) != y.getCell(j, i)) break; } if (j < width) break; } if (i == height) { loopStart = y.getGeneration(); loopEnd = x.getGeneration() - 1; loopCycle = loopEnd - loopStart; return true; } } return false; }