diff --git a/src/Organism/Anatomy.js b/src/Organism/Anatomy.js index b1c3cb6..1e8cebb 100644 --- a/src/Organism/Anatomy.js +++ b/src/Organism/Anatomy.js @@ -61,7 +61,7 @@ class Anatomy { break; } } - this.checkTypeChange(cell.state); + this.checkTypeChange(); return true; } @@ -93,9 +93,7 @@ class Anatomy { } getNeighborsOfCell(col, row) { - var neighbors = []; - for (var x = -1; x <= 1; x++) { for (var y = -1; y <= 1; y++) { @@ -107,6 +105,19 @@ class Anatomy { return neighbors; } + + isEqual(anatomy) { // currently unused helper func. inefficient, avoid usage in prod. + if (this.cells.length !== anatomy.cells.length) return false; + for (let i in this.cells) { + let my_cell = this.cells[i]; + let their_cell = anatomy.cells[i]; + if (my_cell.loc_col !== their_cell.loc_col || + my_cell.loc_row !== their_cell.loc_row || + my_cell.state !== their_cell.state) + return false; + } + return true; + } } module.exports = Anatomy; \ No newline at end of file diff --git a/src/Organism/Organism.js b/src/Organism/Organism.js index 2d00ee6..dcaabaa 100644 --- a/src/Organism/Organism.js +++ b/src/Organism/Organism.js @@ -118,19 +118,21 @@ class Organism { } } Math.max(this.food_collected -= this.foodNeeded(), 0); - } mutate() { - let mutated = false; + let added = false; + let changed = false; + let removed = false; if (this.calcRandomChance(Hyperparams.addProb)) { + // console.log('add') let branch = this.anatomy.getRandomCell(); let state = CellStates.getRandomLivingType();//branch.state; let growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)] let c = branch.loc_col+growth_direction[0]; let r = branch.loc_row+growth_direction[1]; if (this.anatomy.canAddCellAt(c, r)){ - mutated = true; + added = true; this.anatomy.addRandomizedCell(state, c, r); } } @@ -138,15 +140,15 @@ class Organism { let cell = this.anatomy.getRandomCell(); let state = CellStates.getRandomLivingType(); this.anatomy.replaceCell(state, cell.loc_col, cell.loc_row); - mutated = true; + changed = true; } if (this.calcRandomChance(Hyperparams.removeProb)){ if(this.anatomy.cells.length > 1) { let cell = this.anatomy.getRandomCell(); - mutated = this.anatomy.removeCell(cell.loc_col, cell.loc_row); + removed = this.anatomy.removeCell(cell.loc_col, cell.loc_row); } } - return mutated; + return added || changed || removed; } calcRandomChance(prob) { diff --git a/src/Stats/FossilRecord.js b/src/Stats/FossilRecord.js index 3189f7a..e9007fa 100644 --- a/src/Stats/FossilRecord.js +++ b/src/Stats/FossilRecord.js @@ -18,7 +18,6 @@ const FossilRecord = { }, addSpecies: function(org, ancestor) { - // console.log("Adding Species") var new_species = new Species(org.anatomy, ancestor, this.env.total_ticks); this.extant_species.push(new_species); org.species = new_species; @@ -26,7 +25,6 @@ const FossilRecord = { }, addSpeciesObj: function(species) { - // console.log("Adding Species") this.extant_species.push(species); return species; }, @@ -38,6 +36,9 @@ const FossilRecord = { var s = this.extant_species[i]; if (s == species) { this.extant_species.splice(i, 1); + species.ancestor = undefined; // garbage collect dead species + // if (species.ancestor) + // species.ancestor.ancestor = undefined; if (species.cumulative_pop < this.min_pop) { return false; } diff --git a/src/Stats/Species.js b/src/Stats/Species.js index f251844..050e70b 100644 --- a/src/Stats/Species.js +++ b/src/Stats/Species.js @@ -1,9 +1,15 @@ const CellStates = require("../Organism/Cell/CellStates"); +let FossilRecord = undefined; // workaround to a circular dependency problem +const getFossilRecord = () => { + if (!FossilRecord) + FossilRecord = require("./FossilRecord"); + return FossilRecord; +} class Species { constructor(anatomy, ancestor, start_tick) { this.anatomy = anatomy; - // this.ancestor = ancestor; // garbage collect ancestors to avoid memory problems + this.ancestor = ancestor; // eventually need to garbage collect ancestors to avoid memory problems this.population = 1; this.cumulative_pop = 1; this.start_tick = start_tick; @@ -33,8 +39,7 @@ class Species { this.population--; if (this.population <= 0) { this.extinct = true; - const FossilRecord = require("./FossilRecord"); - FossilRecord.fossilize(this); + getFossilRecord().fossilize(this); } }