Skip to content

Commit

Permalink
survival mode v 0.1 (#34)
Browse files Browse the repository at this point in the history
* survival mode v 0.1

* fintess is now binary (1 = survived, 0 = died)
  • Loading branch information
leshchenko1979 committed Feb 11, 2020
1 parent 8ded784 commit 8ebd613
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 42 deletions.
7 changes: 3 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Launch index.html",
"type": "chrome",
"type": "pwa-chrome",
"request": "launch",
"file": "${workspaceFolder}/index.html"
"name": "Open index.html",
"file": "c:\\Users\\leshc\\OneDrive\\Документы\\GitHub\\fishbowl\\index.html"
}
]
}
6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<script src="https://d3js.org/d3.v5.min.js"></script>
<table>
<tr>
<td width=20%>
<td >
<div id="stats" style="vertical-align: top; font: 1em sans-serif;">

<p>Generation: <span id="generation">1</span></p>
Expand All @@ -25,8 +25,8 @@
<canvas id="viz" width="200px" height="200px"></canvas>
<canvas id="vizfit" width="200px" height="200px"></canvas>
</div>
</td>
<td width=80%>
</td >
<td>
<div id="canvas"></div>
</td>
</tr>
Expand Down
31 changes: 21 additions & 10 deletions js/critter.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
class Critter extends Obj {

constructor(x, y, brainIndex) {
constructor(x, y, brain) {
super(x, y);
this.size = 20;
this.movement = new p5.Vector;
this.eatenTimer = 0;
this.thought = 0;

this.brainIndex = brainIndex;
this.brain = neat.population[brainIndex];

this.brain = brain;
this.color;
this.foodVision;
}

draw() {
fill(color(this.brainIndex * 10, 50, 70));
fill(color(this.color, 50, 70));
super.draw();
if (VERBOSE) {
fill("black");
Expand Down Expand Up @@ -56,9 +57,6 @@ class Critter extends Obj {
//expend energy on ambient bodily functions
this.size *= 0.999;

// add my size to the corresponding brain statistics
maxTotalFishSize[this.brainIndex][0] += this.size;

//die if too small
if (this.size < 5) {
this.delete();
Expand Down Expand Up @@ -158,16 +156,24 @@ class Critter extends Obj {
// split

if ((this.size > 20) && (this.thought > 2) && (this.thought <= 3)) {

this.size /= 2;
var critter = new Critter(0, 0, this.brainIndex);

var b = this.brain.clone();
b.mutateRandom();
this.brain.mutateRandom();
var critter = new Critter(this.position.x, this.position.y, b);
neat.resize([b]);

var v = p5.Vector.random2D();
v.setMag(this.movement.mag());
// v.mult(2);
critter.setPosition(this.position);
critter.setSize(this.size);
critter.setMovement(v);
critter.color = this.color;
objs.push(critter);

this.movement = p5.Vector.mult(v, -1);

return;
}

Expand All @@ -181,6 +187,11 @@ class Critter extends Obj {

}

delete() {
super.delete();
this.brain.score = 0;
}

click(x, y) {
if (p5.Vector.dist(this.position, createVector(x, y)) < this.size) {
while (this.size > 5) {
Expand Down
52 changes: 27 additions & 25 deletions js/fishbowl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var objs = [];
var foodDensity = [];
var stats = [];
var neat;
var maxTotalFishSize = [];
var NNcomplexity;

const SENSES = 4;
Expand All @@ -18,6 +17,8 @@ const FOOD_DENSITY_GRID_STEP = 20;
const FOOD_DENSITY_THRESHOLD = 20;
const VISION_RANGE = 200;

const INITIAL_FOOD_AMOUNT = 100;

const VERBOSE = true;

var FDG_WIDTH, FDG_HEIGHT;
Expand All @@ -30,7 +31,7 @@ const ITERATIONS = 1000;
const MUTATION_RATE = 0.3;
const ELITISM_PERCENT = 0.1;

const MAX_FRAMES_PER_GENERATION = 100;
const MAX_FRAMES_PER_GENERATION = 2000;
var framesLeft = MAX_FRAMES_PER_GENERATION;


Expand All @@ -44,7 +45,7 @@ function mouseClicked() {
*/

function setup() {
var canvas = createCanvas(windowWidth * 0.9 - 20, windowHeight - 30);
var canvas = createCanvas(windowWidth - 220, windowHeight - 25);
canvas.parent('canvas');
background("gray");
colorMode(HSB, 100);
Expand All @@ -59,12 +60,13 @@ function setup() {

//add objects

for (var i = 0; i < 500; i++)
for (var i = 0; i < INITIAL_FOOD_AMOUNT; i++)
objs.push(new Food(random() * width, random() * height));

for (var i = 0; i < INITIAL_PLAYER_AMOUNT; i++) {
objs.push(new Critter(random() * width, random() * height, i));
maxTotalFishSize.push([0, 0]); // [max size of the corresponding brainIndex on this frame, max size in this iteration]
for (i = 0; i < INITIAL_PLAYER_AMOUNT; i++) {
let o = new Critter(random() * width, random() * height, neat.population[i]);
objs.push(o);
o.color = i * 10;
}

// add charts
Expand Down Expand Up @@ -129,10 +131,6 @@ function draw() {

//main cycle

// init food stats

maxTotalFishSize.forEach(el => (el[0] = 0));

// let the objects live

var i = 0;
Expand All @@ -142,10 +140,6 @@ function draw() {
}
while (objs.length - 1 > i++);

// finalize fish stats on this frame

maxTotalFishSize.forEach(el => (el[1] = Math.max(el[0], el[1])));

// delete deleted

i = 0;
Expand All @@ -170,7 +164,7 @@ function draw() {

// new generation

if ((--framesLeft == 0) || (objs.filter(obj => obj instanceof Critter).length == 0))
if ((--framesLeft == 0) || (objs.filter(obj => obj instanceof Critter).length == 1))
newGeneration();

}
Expand All @@ -179,13 +173,21 @@ function newGeneration()

{


//evaluation

for (i = 0; i < INITIAL_PLAYER_AMOUNT; i++)
neat.population[i].score = maxTotalFishSize[i][0] + maxTotalFishSize[i][1];
objs.filter(obj => obj instanceof Critter).forEach(fish => fish.brain.score = 1);
objs = [];

neat.sort();

var maxfitness = neat.getFittest().score;

newPopulation = Array(INITIAL_PLAYER_AMOUNT).fill(0);

neat.population = newPopulation.map (() => neat.getOffspring());

neat.evolve();
neat.mutate();
neat.generation++;

// save neat to localstorage

Expand All @@ -197,18 +199,19 @@ function newGeneration()

//create new food

for (var i = 0; i < 500; i++)
for (var i = 0; i < INITIAL_FOOD_AMOUNT; i++)
objs.push(new Food(random() * width, random() * height));

// create new fish

for (i = 0; i < INITIAL_PLAYER_AMOUNT; i++) {
objs.push(new Critter(random() * width, random() * height, i));
let o = new Critter(random() * width, random() * height, neat.population[i]);
objs.push(o);
o.color = i * 10;
}

// udpate fitness viz

var maxfitness = maxTotalFishSize.reduce((acc, el) => Math.max(el[0] + el[1], acc), 0);
vizfit.data.labels.push(neat.generation);
vizfit.data.datasets[0].data.push(maxfitness);
vizfit.data.datasets[1].data.push(NNcomplexity);
Expand All @@ -219,12 +222,11 @@ function newGeneration()
//reset fish size stats

NNcomplexity = neat.population.reduce((acc, el) => Math.max(acc, el.nodes.length + el.connections.length), 0);
maxTotalFishSize = maxTotalFishSize.map(() => { return ([0, 0]) });
td.fill(0);

//reset frame countdown

framesLeft = Math.floor(maxfitness * 10);
framesLeft = MAX_FRAMES_PER_GENERATION;

}

Expand Down

0 comments on commit 8ebd613

Please sign in to comment.