more control panel stuff
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
const CellTypes = require("./CellTypes");
|
||||
var Hyperparams = require("./Hyperparameters");
|
||||
const Hyperparams = require("./Hyperparameters");
|
||||
|
||||
// A cell exists in a grid system.
|
||||
class Cell{
|
||||
@@ -61,12 +61,12 @@ function growFood(self, env){
|
||||
if (self.owner.is_mover)
|
||||
return;
|
||||
var prob = Hyperparams.foodProdProb;
|
||||
// console.log(prob)
|
||||
for (var loc of Hyperparams.growableNeighbors){
|
||||
if (Math.random() * 100 <= prob){
|
||||
var loc = Hyperparams.growableNeighbors[Math.floor(Math.random() * Hyperparams.growableNeighbors.length)]
|
||||
var c=loc[0];
|
||||
var r=loc[1];
|
||||
var cell = env.grid_map.cellAt(self.col+c, self.row+r);
|
||||
if (cell != null && cell.type == CellTypes.empty && Math.random() * 100 <= prob){
|
||||
if (cell != null && cell.type == CellTypes.empty){
|
||||
env.changeCell(self.col+c, self.row+r, CellTypes.food, null);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ const CellTypes = {
|
||||
killer: 6,
|
||||
armor: 7,
|
||||
colors: ['#121D29', 'green', 'gray', 'orange', 'white', 'blue', 'red', 'purple'],
|
||||
getRandomLivingType: function(){
|
||||
getRandomLivingType: function() {
|
||||
return Math.floor(Math.random() * 5) + 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
var Hyperparams = require("./Hyperparameters");
|
||||
var Modes = require("./ControlModes");
|
||||
const Hyperparams = require("./Hyperparameters");
|
||||
const Modes = require("./ControlModes");
|
||||
const CellTypes = require("./CellTypes");
|
||||
|
||||
class ControlPanel {
|
||||
@@ -59,6 +59,25 @@ class ControlPanel {
|
||||
Hyperparams.lifespanMultiplier = lifespan;
|
||||
}
|
||||
}.bind(this));
|
||||
$('.mut-prob').change( function() {
|
||||
switch(this.id){
|
||||
case "add-prob":
|
||||
Hyperparams.addProb = this.value;
|
||||
Hyperparams.balanceMutationProbs(1);
|
||||
break;
|
||||
case "change-prob":
|
||||
Hyperparams.changeProb = this.value;
|
||||
Hyperparams.balanceMutationProbs(2);
|
||||
break;
|
||||
case "remove-prob":
|
||||
Hyperparams.removeProb = this.value;
|
||||
Hyperparams.balanceMutationProbs(3);
|
||||
break;
|
||||
}
|
||||
$('#add-prob').val(Math.floor(Hyperparams.addProb));
|
||||
$('#change-prob').val(Math.floor(Hyperparams.changeProb));
|
||||
$('#remove-prob').val(Math.floor(Hyperparams.removeProb));
|
||||
});
|
||||
}
|
||||
|
||||
defineModeControls() {
|
||||
@@ -81,6 +100,17 @@ class ControlPanel {
|
||||
$(".control-mode-button" ).css( "background-color", "lightgray" );
|
||||
$("#"+this.id).css("background-color", "darkgray");
|
||||
});
|
||||
|
||||
|
||||
$('#reset-env').click( function() {
|
||||
this.engine.env.reset();
|
||||
}.bind(this));
|
||||
$('#kill-all').click( function() {
|
||||
this.engine.env.clearOrganisms();
|
||||
}.bind(this));
|
||||
$('#clear-walls').click( function() {
|
||||
this.engine.env.clearWalls();
|
||||
}.bind(this));
|
||||
}
|
||||
|
||||
changeEngineSpeed(change_val) {
|
||||
@@ -96,7 +126,7 @@ class ControlPanel {
|
||||
if (org_count > this.organism_record)
|
||||
this.organism_record = org_count;
|
||||
$('#org-record').text("Highest count: " + this.organism_record);
|
||||
// this.env_controller.performModeAction();
|
||||
$('#avg-mut').text("Average Mutation Rate: " + Math.round(this.engine.env.averageMutability() * 100) / 100);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ class Environment{
|
||||
this.grid_map = new GridMap(this.grid_cols, this.grid_rows, cell_size);
|
||||
this.renderer.renderFullGrid();
|
||||
this.organisms = [];
|
||||
this.walls = [];
|
||||
this.total_mutability = 0;
|
||||
}
|
||||
|
||||
update(delta_time) {
|
||||
@@ -35,6 +37,7 @@ class Environment{
|
||||
|
||||
removeOrganisms(org_indeces) {
|
||||
for (var i of org_indeces.reverse()){
|
||||
this.total_mutability -= this.organisms[i].mutability;
|
||||
this.organisms.splice(i, 1);
|
||||
}
|
||||
}
|
||||
@@ -44,19 +47,47 @@ class Environment{
|
||||
var org = new Organism(center[0], center[1], this);
|
||||
org.addCell(CellTypes.mouth, 1, 1);
|
||||
org.addCell(CellTypes.producer, 0, 0);
|
||||
// org.addCell(CellTypes.mouth, 1, -1);
|
||||
org.addCell(CellTypes.mouth, -1, -1);
|
||||
this.addOrganism(org);
|
||||
}
|
||||
|
||||
addOrganism(organism) {
|
||||
organism.updateGrid();
|
||||
this.total_mutability += organism.mutability;
|
||||
this.organisms.push(organism);
|
||||
}
|
||||
|
||||
averageMutability() {
|
||||
if (this.organisms.length < 1)
|
||||
return 0;
|
||||
return this.total_mutability / this.organisms.length;
|
||||
}
|
||||
|
||||
changeCell(c, r, type, owner) {
|
||||
this.grid_map.setCellType(c, r, type);
|
||||
this.grid_map.setCellOwner(c, r, owner);
|
||||
this.renderer.addToRender(this.grid_map.cellAt(c, r));
|
||||
if(type == CellTypes.wall)
|
||||
this.walls.push(this.grid_map.cellAt(c, r));
|
||||
}
|
||||
|
||||
clearWalls() {
|
||||
for(var wall of this.walls)
|
||||
this.changeCell(wall.col, wall.row, CellTypes.empty, null);
|
||||
}
|
||||
|
||||
clearOrganisms() {
|
||||
for (var org of this.organisms)
|
||||
org.die();
|
||||
this.organisms = [];
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.organisms = [];
|
||||
this.grid_map.fillGrid(CellTypes.empty);
|
||||
this.renderer.renderFullGrid();
|
||||
this.total_mutability = 0;
|
||||
this.OriginOfLife();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
var Modes = require("./ControlModes");
|
||||
const Modes = require("./ControlModes");
|
||||
const CellTypes = require("./CellTypes");
|
||||
|
||||
class EnvironmentController{
|
||||
|
||||
@@ -19,9 +19,10 @@ class GridMap {
|
||||
}
|
||||
|
||||
fillGrid(type) {
|
||||
for (var col of grid) {
|
||||
for (var col of this.grid) {
|
||||
for (var cell of col){
|
||||
cell.setType(type);
|
||||
cell.owner = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,47 @@
|
||||
const Neighbors = require("./Neighbors");
|
||||
|
||||
var Hyperparams = {
|
||||
const Hyperparams = {
|
||||
lifespanMultiplier: 100,
|
||||
foodProdProb: 1,
|
||||
foodProdProb: 4,
|
||||
foodProdProbScalar: 4,
|
||||
killableNeighbors: Neighbors.adjacent,
|
||||
edibleNeighbors: Neighbors.adjacent,
|
||||
growableNeighbors: Neighbors.adjacent,
|
||||
|
||||
useGlobalMutability: false,
|
||||
globalMutability: 0,
|
||||
|
||||
addProb: 33,
|
||||
changeProb: 33,
|
||||
removeProb: 33,
|
||||
|
||||
// calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan.
|
||||
// calculates the optimal ratio where a producer cell is most likely to produce 1 food in its lifespan * a scalar of my choice :)
|
||||
calcProducerFoodRatio : function(lifespan_fixed=true) {
|
||||
if (lifespan_fixed) {
|
||||
// change the foodProdProb
|
||||
this.foodProdProb = 100 / this.lifespanMultiplier;
|
||||
this.foodProdProb = (100 / this.lifespanMultiplier) * this.foodProdProbScalar;
|
||||
}
|
||||
else {
|
||||
// change the lifespanMultiplier
|
||||
this.lifespanMultiplier = Math.floor(100 / this.foodProdProb);
|
||||
this.lifespanMultiplier = Math.floor(100 / (this.foodProdProb/this.foodProdProbScalar));
|
||||
}
|
||||
},
|
||||
|
||||
balanceMutationProbs : function(choice) {
|
||||
if (choice == 1) {
|
||||
var remaining = 100 - this.addProb;
|
||||
this.changeProb = remaining/2;
|
||||
this.removeProb = remaining/2;
|
||||
}
|
||||
else if (choice == 2) {
|
||||
var remaining = 100 - this.changeProb;
|
||||
this.addProb = remaining/2;
|
||||
this.removeProb = remaining/2;
|
||||
}
|
||||
else {
|
||||
var remaining = 100 - this.removeProb;
|
||||
this.changeProb = remaining/2;
|
||||
this.addProb = remaining/2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,8 @@ const CellTypes = require("./CellTypes");
|
||||
const Cell = require("./Cell");
|
||||
const GridMap = require("./GridMap");
|
||||
const LocalCell = require("./LocalCell");
|
||||
const { producer } = require("./CellTypes");
|
||||
const Neighbors = require("./Neighbors");
|
||||
var Hyperparams = require("./Hyperparameters");
|
||||
const Hyperparams = require("./Hyperparameters");
|
||||
|
||||
const directions = [[0,1],[0,-1],[1,0],[-1,0]]
|
||||
|
||||
@@ -89,7 +88,10 @@ class Organism {
|
||||
//produce mutated child
|
||||
//check nearby locations (is there room and a direct path)
|
||||
var org = new Organism(0, 0, this.env, this);
|
||||
if (Math.random() * 100 <= 3) {
|
||||
var prob = this.mutability;
|
||||
if (Hyperparams.useGlobalMutability)
|
||||
prob = Hyperparams.globalMutability;
|
||||
if (Math.random() * 100 <= this.mutability) {
|
||||
org.mutate();
|
||||
}
|
||||
if (Math.random() <= 0.5)
|
||||
@@ -120,27 +122,25 @@ class Organism {
|
||||
}
|
||||
|
||||
mutate() {
|
||||
var choice = Math.floor(Math.random() * 3);
|
||||
if (choice == 0) {
|
||||
var choice = Math.floor(Math.random() * 100);
|
||||
if (choice <= Hyperparams.addProb) {
|
||||
// add cell
|
||||
var type = CellTypes.getRandomLivingType();
|
||||
var num_to_add = Math.floor(Math.random() * 3) + 1;
|
||||
// for (var i=0; i<num_to_add; i++){
|
||||
var branch = this.cells[Math.floor(Math.random() * this.cells.length)];
|
||||
var growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)]
|
||||
var c = branch.loc_col+growth_direction[0];
|
||||
var r = branch.loc_row+growth_direction[1];
|
||||
return this.addCell(type, c, r);
|
||||
// }
|
||||
var branch = this.cells[Math.floor(Math.random() * this.cells.length)];
|
||||
var growth_direction = Neighbors.all[Math.floor(Math.random() * Neighbors.all.length)]
|
||||
var c = branch.loc_col+growth_direction[0];
|
||||
var r = branch.loc_row+growth_direction[1];
|
||||
return this.addCell(type, c, r);
|
||||
}
|
||||
else if (choice == 1){
|
||||
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb){
|
||||
// change cell
|
||||
var cell = this.cells[Math.floor(Math.random() * this.cells.length)];
|
||||
cell.type = CellTypes.getRandomLivingType();
|
||||
this.checkProducerMover(cell.type);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
else if (choice <= Hyperparams.addProb + Hyperparams.changeProb + Hyperparams.removeProb){
|
||||
// remove cell
|
||||
if(this.cells.length > 1) {
|
||||
this.cells.splice(Math.floor(Math.random() * this.cells.length), 1);
|
||||
|
||||
Reference in New Issue
Block a user