fixed species tracking bug

This commit is contained in:
MaxRobinsonTheGreat
2022-03-19 14:25:24 -05:00
parent 4f28f5ac9c
commit 0da27abd37
4 changed files with 33 additions and 14 deletions

View File

@@ -61,7 +61,7 @@ class Anatomy {
break; break;
} }
} }
this.checkTypeChange(cell.state); this.checkTypeChange();
return true; return true;
} }
@@ -93,9 +93,7 @@ class Anatomy {
} }
getNeighborsOfCell(col, row) { getNeighborsOfCell(col, row) {
var neighbors = []; var neighbors = [];
for (var x = -1; x <= 1; x++) { for (var x = -1; x <= 1; x++) {
for (var y = -1; y <= 1; y++) { for (var y = -1; y <= 1; y++) {
@@ -107,6 +105,19 @@ class Anatomy {
return neighbors; 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; module.exports = Anatomy;

View File

@@ -118,19 +118,21 @@ class Organism {
} }
} }
Math.max(this.food_collected -= this.foodNeeded(), 0); Math.max(this.food_collected -= this.foodNeeded(), 0);
} }
mutate() { mutate() {
let mutated = false; let added = false;
let changed = false;
let removed = false;
if (this.calcRandomChance(Hyperparams.addProb)) { if (this.calcRandomChance(Hyperparams.addProb)) {
// console.log('add')
let branch = this.anatomy.getRandomCell(); let branch = this.anatomy.getRandomCell();
let state = CellStates.getRandomLivingType();//branch.state; let state = CellStates.getRandomLivingType();//branch.state;
let growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)] let growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)]
let c = branch.loc_col+growth_direction[0]; let c = branch.loc_col+growth_direction[0];
let r = branch.loc_row+growth_direction[1]; let r = branch.loc_row+growth_direction[1];
if (this.anatomy.canAddCellAt(c, r)){ if (this.anatomy.canAddCellAt(c, r)){
mutated = true; added = true;
this.anatomy.addRandomizedCell(state, c, r); this.anatomy.addRandomizedCell(state, c, r);
} }
} }
@@ -138,15 +140,15 @@ class Organism {
let cell = this.anatomy.getRandomCell(); let cell = this.anatomy.getRandomCell();
let state = CellStates.getRandomLivingType(); let state = CellStates.getRandomLivingType();
this.anatomy.replaceCell(state, cell.loc_col, cell.loc_row); this.anatomy.replaceCell(state, cell.loc_col, cell.loc_row);
mutated = true; changed = true;
} }
if (this.calcRandomChance(Hyperparams.removeProb)){ if (this.calcRandomChance(Hyperparams.removeProb)){
if(this.anatomy.cells.length > 1) { if(this.anatomy.cells.length > 1) {
let cell = this.anatomy.getRandomCell(); 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) { calcRandomChance(prob) {

View File

@@ -18,7 +18,6 @@ const FossilRecord = {
}, },
addSpecies: function(org, ancestor) { addSpecies: function(org, ancestor) {
// console.log("Adding Species")
var new_species = new Species(org.anatomy, ancestor, this.env.total_ticks); var new_species = new Species(org.anatomy, ancestor, this.env.total_ticks);
this.extant_species.push(new_species); this.extant_species.push(new_species);
org.species = new_species; org.species = new_species;
@@ -26,7 +25,6 @@ const FossilRecord = {
}, },
addSpeciesObj: function(species) { addSpeciesObj: function(species) {
// console.log("Adding Species")
this.extant_species.push(species); this.extant_species.push(species);
return species; return species;
}, },
@@ -38,6 +36,9 @@ const FossilRecord = {
var s = this.extant_species[i]; var s = this.extant_species[i];
if (s == species) { if (s == species) {
this.extant_species.splice(i, 1); 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) { if (species.cumulative_pop < this.min_pop) {
return false; return false;
} }

View File

@@ -1,9 +1,15 @@
const CellStates = require("../Organism/Cell/CellStates"); 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 { class Species {
constructor(anatomy, ancestor, start_tick) { constructor(anatomy, ancestor, start_tick) {
this.anatomy = anatomy; 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.population = 1;
this.cumulative_pop = 1; this.cumulative_pop = 1;
this.start_tick = start_tick; this.start_tick = start_tick;
@@ -33,8 +39,7 @@ class Species {
this.population--; this.population--;
if (this.population <= 0) { if (this.population <= 0) {
this.extinct = true; this.extinct = true;
const FossilRecord = require("./FossilRecord"); getFossilRecord().fossilize(this);
FossilRecord.fossilize(this);
} }
} }