xxxxxxxxxx
185
let vehicles = [];
let food = [];
let poison = [];
let initPop = 20;
let population = [];
let averagePopulationList = [];
let averagePopulation = 0;
let popRecord = 0;
let averageCounter = 1;
var debug;
var graphs;
var family;
let dnaData;
function preload(){
dnaData = loadStrings("data.txt");
}
function setup() {
createCanvas(windowWidth, windowHeight);
create();
debug = createCheckbox("debug");
graphs = createCheckbox("graphs");
family = createCheckbox("family");
}
function findBest(){
let record = 0;
let best = null;
for(let v of vehicles){
if(v.lifeSpan > record){
record = v.lifeSpan;
best = v;
}
}
return best;
}
function keyPressed(){
if(keyCode === 83){
let best = findBest();
console.log("Saved Data: " + best.dna)
}
if(keyCode == 76){
let stringData = join(dnaData,"");
let listData = float(split(stringData,","));
vehicles = [];
food = [];
poison = [];
initPop = 20;
population = [];
averagePopulationList = [];
averagePopulation = 0;
popRecord = 0;
averageCounter = 1;
create(listData);
}
if(keyCode == 78){
vehicles = [];
food = [];
poison = [];
initPop = 20;
population = [];
averagePopulationList = [];
averagePopulation = 0;
popRecord = 0;
averageCounter = 1;
create();
}
}
function create(dna){
for(let i = 0; i < initPop; i++){
if(dna){
console.log("Trained Model");
vehicles.push(new Vehicle(random(width),random(height),dna));
}else{
console.log("New Model");
vehicles.push(new Vehicle(random(width),random(height)));
}
}
for(let i = 0; i < 40; i++){
food.push(createVector(random(width),random(height)));
}
for(let i = 0; i < 20; i++){
poison.push(createVector(random(width),random(height)));
}
}
function draw() {
background(51);
if(random(1) < 0.15){
let x = random(width);
let y = random(height);
food.push(createVector(x,y));
}
if(random(1) < 0.01 && poison.length < 50){
poison.push(createVector(random(width),random(height)));
}
for(let f of food){
fill(0,255,0);
noStroke();
ellipse(f.x,f.y,5,5);
}
for(let p of poison){
fill(255,0,0);
noStroke();
ellipse(p.x,p.y,5,5);
}
let maters = [];
for(let i = vehicles.length-1; i >=0; i--){
vehicles[i].boundaries();
vehicles[i].behaviors(vehicles,food, poison);
vehicles[i].update();
vehicles[i].display();
var newVehicle = vehicles[i].clone();
if(newVehicle != null){
vehicles.push(newVehicle);
}
if(vehicles[i].dead()){
food.push(createVector(vehicles[i].position.x,vehicles[i].position.y));
vehicles.splice(i,1);
}
// if(vehicles[i].mate()){
// maters.push(vehicles[i]);
// }
}
// for(let m of maters){
// if(m.partner == null){
// let p = random(maters);
// if(p == m){
// m.partner = p;
// }else{
// m.partner = p;
// p.partner = m;
// }
// }
// }
//graph
if(population.length >= 400){
population.splice(1,1);
}
if(averagePopulationList.length >= 400){
averagePopulationList.splice(1,1);
}
population.push(vehicles.length);
for(let p of population){
if(p > popRecord){
popRecord = p;
}
}
averagePopulation = addToAverage(averagePopulation,averageCounter,vehicles.length);
averageCounter++;
averagePopulationList.push(averagePopulation);
if(graphs.checked()){
drawLines(0,height-200,200,population,popRecord,averagePopulationList);
}
}
function addToAverage(average,size,value){
return (size*average + value) / (size+1);
}