Skip to content

Instantly share code, notes, and snippets.

@LiorB-D
Created July 15, 2022 10:59
Show Gist options
  • Save LiorB-D/fbc1336d00d16c691eea9f57d6c558bd to your computer and use it in GitHub Desktop.
Save LiorB-D/fbc1336d00d16c691eea9f57d6c558bd to your computer and use it in GitHub Desktop.
function startNewGen() {
totalFitness = endGen();
let newCarArr = []; // The next generation
//Get parent weights
let parentWeights = [];
cars.forEach((c) => {
parentWeights.push(c.copyWeights());
});
//Create next generation
for (let i = 0; i < 10; i++) {
// Initialize Child
const child = new Car(
outerDi,
innerDi,
screenHt,
screenWd,
carDi,
obsts
);
let currWeights = child.model.getWeights();
let newWeights = [];
for (let i = 0; i < currWeights.length; i++) {
// Iterate through each layer
//Get Current Weight information
let currVals = currWeights[i].dataSync(); // Array format
let shape = currWeights[i].shape;
for (let j = 0; j < currVals.length; j++) {
// Iterate through each weight
// Select trait from random parent
currVals[j] = parentWeights[selectRandParent(totalFitness)][i][j];
}
let newTens = tf.tensor(currVals, shape);
newWeights[i] = newTens;
}
child.model.setWeights(newWeights);
newCarArr.push(child);
}
cars = newCarArr;
// Increment Generation and Update Label
currGen += 1;
infoLbl.elt.innerText =
"Gen: " + currGen + ", Top Score: " + topFitnessScore;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment