Skip to content

I made a few aesthetic and functional changes #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 83 additions & 83 deletions GENOME.JS
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,23 @@ class Genome {
//sets up the NN as a list of this.nodes in order to be engaged

generateNetwork() {
this.connectNodes();
this.network = [];
//for each layer add the node in that layer, since layers cannot connect to themselves there is no need to order the this.nodes within a layer

for (var l = 0; l < this.layers; l++) { //for each layer
for (var i = 0; i < this.nodes.length; i++) { //for each node
if (this.nodes[i].layer == l) { //if that node is in that layer
this.network.push(this.nodes[i]);
}
this.connectNodes();
this.network = [];
//for each layer add the node in that layer, since layers cannot connect to themselves there is no need to order the this.nodes within a layer

for (var l = 0; l < this.layers; l++) { //for each layer
for (var i = 0; i < this.nodes.length; i++) { //for each node
if (this.nodes[i].layer == l) { //if that node is in that layer
this.network.push(this.nodes[i]);
}
}
}
//-----------------------------------------------------------------------------------------------------------------------------------------
//mutate the NN by adding a new node
//it does this by picking a random connection and disabling it then 2 new connections are added
//1 between the input node of the disabled connection and the new node
//and the other between the new node and the output of the disabled connection
}
//-----------------------------------------------------------------------------------------------------------------------------------------
//mutate the NN by adding a new node
//it does this by picking a random connection and disabling it then 2 new connections are added
//1 between the input node of the disabled connection and the new node
//and the other between the new node and the output of the disabled connection
addNode(innovationHistory) {
//pick a random connection to create a node between
if (this.genes.length == 0) {
Expand Down Expand Up @@ -183,37 +183,37 @@ class Genome {
//------------------------------------------------------------------------------------------------------------------
//adds a connection between 2 this.nodes which aren't currently connected
addConnection(innovationHistory) {
//cannot add a connection to a fully connected network
if (this.fullyConnected()) {
console.log("connection failed");
return;
}
//cannot add a connection to a fully connected network
if (this.fullyConnected()) {
console.log("connection failed");
return;
}


//get random this.nodes
var randomNode1 = floor(random(this.nodes.length));
var randomNode2 = floor(random(this.nodes.length));
while (this.randomConnectionNodesAreShit(randomNode1, randomNode2)) { //while the random this.nodes are no good
//get new ones
randomNode1 = floor(random(this.nodes.length));
randomNode2 = floor(random(this.nodes.length));
}
var temp;
if (this.nodes[randomNode1].layer > this.nodes[randomNode2].layer) { //if the first random node is after the second then switch
temp = randomNode2;
randomNode2 = randomNode1;
randomNode1 = temp;
}
//get random this.nodes
var randomNode1 = floor(random(this.nodes.length));
var randomNode2 = floor(random(this.nodes.length));
while (this.randomConnectionNodesAreShit(randomNode1, randomNode2)) { //while the random this.nodes are no good
//get new ones
randomNode1 = floor(random(this.nodes.length));
randomNode2 = floor(random(this.nodes.length));
}
var temp;
if (this.nodes[randomNode1].layer > this.nodes[randomNode2].layer) { //if the first random node is after the second then switch
temp = randomNode2;
randomNode2 = randomNode1;
randomNode1 = temp;
}

//get the innovation number of the connection
//this will be a new number if no identical genome has mutated in the same way
var connectionInnovationNumber = this.getInnovationNumber(innovationHistory, this.nodes[randomNode1], this.nodes[randomNode2]);
//add the connection with a random array
//get the innovation number of the connection
//this will be a new number if no identical genome has mutated in the same way
var connectionInnovationNumber = this.getInnovationNumber(innovationHistory, this.nodes[randomNode1], this.nodes[randomNode2]);
//add the connection with a random array

this.genes.push(new connectionGene(this.nodes[randomNode1], this.nodes[randomNode2], random(-1, 1), connectionInnovationNumber)); //changed this so if error here
this.connectNodes();
}
//-------------------------------------------------------------------------------------------------------------------------------------------
this.genes.push(new connectionGene(this.nodes[randomNode1], this.nodes[randomNode2], random(-1, 1), connectionInnovationNumber)); //changed this so if error here
this.connectNodes();
}
//-------------------------------------------------------------------------------------------------------------------------------------------
randomConnectionNodesAreShit(r1, r2) {
if (this.nodes[r1].layer == this.nodes[r2].layer) return true; // if the this.nodes are in the same layer
if (this.nodes[r1].isConnectedTo(this.nodes[r2])) return true; //if the this.nodes are already connected
Expand All @@ -228,29 +228,29 @@ class Genome {
//if this mutation has never been seen before then it will be given a new unique innovation number
//if this mutation matches a previous mutation then it will be given the same innovation number as the previous one
getInnovationNumber(innovationHistory, from, to) {
var isNew = true;
var connectionInnovationNumber = nextConnectionNo;
for (var i = 0; i < innovationHistory.length; i++) { //for each previous mutation
if (innovationHistory[i].matches(this, from, to)) { //if match found
isNew = false; //its not a new mutation
connectionInnovationNumber = innovationHistory[i].innovationNumber; //set the innovation number as the innovation number of the match
break;
}
var isNew = true;
var connectionInnovationNumber = nextConnectionNo;
for (var i = 0; i < innovationHistory.length; i++) { //for each previous mutation
if (innovationHistory[i].matches(this, from, to)) { //if match found
isNew = false; //its not a new mutation
connectionInnovationNumber = innovationHistory[i].innovationNumber; //set the innovation number as the innovation number of the match
break;
}
}

if (isNew) { //if the mutation is new then create an arrayList of varegers representing the current state of the genome
var innoNumbers = [];
for (var i = 0; i < this.genes.length; i++) { //set the innovation numbers
innoNumbers.push(this.genes[i].innovationNo);
}

//then add this mutation to the innovationHistory
innovationHistory.push(new connectionHistory(from.number, to.number, connectionInnovationNumber, innoNumbers));
nextConnectionNo++;
if (isNew) { //if the mutation is new then create an arrayList of varegers representing the current state of the genome
var innoNumbers = [];
for (var i = 0; i < this.genes.length; i++) { //set the innovation numbers
innoNumbers.push(this.genes[i].innovationNo);
}
return connectionInnovationNumber;

//then add this mutation to the innovationHistory
innovationHistory.push(new connectionHistory(from.number, to.number, connectionInnovationNumber, innoNumbers));
nextConnectionNo++;
}
//----------------------------------------------------------------------------------------------------------------------------------------
return connectionInnovationNumber;
}
//----------------------------------------------------------------------------------------------------------------------------------------

//returns whether the network is fully connected or not
fullyConnected() {
Expand Down Expand Up @@ -374,15 +374,15 @@ class Genome {
//----------------------------------------------------------------------------------------------------------------------------------------
//returns whether or not there is a gene matching the input innovation number in the input genome
matchingGene(parent2, innovationNumber) {
for (var i = 0; i < parent2.genes.length; i++) {
if (parent2.genes[i].innovationNo == innovationNumber) {
return i;
}
for (var i = 0; i < parent2.genes.length; i++) {
if (parent2.genes[i].innovationNo == innovationNumber) {
return i;
}
return -1; //no matching gene found
}
//----------------------------------------------------------------------------------------------------------------------------------------
//prints out info about the genome to the console
return -1; //no matching gene found
}
//----------------------------------------------------------------------------------------------------------------------------------------
//prints out info about the genome to the console
printGenome() {
console.log("Prvar genome layers:" + this.layers);
console.log("bias node: " + this.biasNode);
Expand All @@ -403,27 +403,27 @@ class Genome {
//returns a copy of this genome
clone() {

var clone = new Genome(this.inputs, this.outputs, true);
var clone = new Genome(this.inputs, this.outputs, true);

for (var i = 0; i < this.nodes.length; i++) { //copy this.nodes
clone.nodes.push(this.nodes[i].clone());
}
for (var i = 0; i < this.nodes.length; i++) { //copy this.nodes
clone.nodes.push(this.nodes[i].clone());
}

//copy all the connections so that they connect the clone new this.nodes
//copy all the connections so that they connect the clone new this.nodes

for (var i = 0; i < this.genes.length; i++) { //copy genes
clone.genes.push(this.genes[i].clone(clone.getNode(this.genes[i].fromNode.number), clone.getNode(this.genes[i].toNode.number)));
}
for (var i = 0; i < this.genes.length; i++) { //copy genes
clone.genes.push(this.genes[i].clone(clone.getNode(this.genes[i].fromNode.number), clone.getNode(this.genes[i].toNode.number)));
}

clone.layers = this.layers;
clone.nextNode = this.nextNode;
clone.biasNode = this.biasNode;
clone.connectNodes();
clone.layers = this.layers;
clone.nextNode = this.nextNode;
clone.biasNode = this.biasNode;
clone.connectNodes();

return clone;
}
//----------------------------------------------------------------------------------------------------------------------------------------
//draw the genome on the screen
return clone;
}
//----------------------------------------------------------------------------------------------------------------------------------------
//draw the genome on the screen
drawGenome(startX, startY, w, h) {
//i know its ugly but it works (and is not that important) so I'm not going to mess with it
var allNodes = []; //new ArrayList<ArrayList<Node>>();
Expand Down
80 changes: 40 additions & 40 deletions Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,57 @@ class Node {
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//the node sends its output to the inputs of the nodes its connected to
engage() {
if(this.layer != 0) { //no sigmoid for the inputs and bias
this.outputValue = this.sigmoid(this.inputSum);
}
if (this.layer != 0) { //no sigmoid for the inputs and bias
this.outputValue = this.sigmoid(this.inputSum);
}

for(var i = 0; i < this.outputConnections.length; i++) { //for each connection
if(this.outputConnections[i].enabled) { //dont do shit if not enabled
this.outputConnections[i].toNode.inputSum += this.outputConnections[i].weight * this.outputValue; //add the weighted output to the sum of the inputs of whatever node this node is connected to
}
for (var i = 0; i < this.outputConnections.length; i++) { //for each connection
if (this.outputConnections[i].enabled) { //dont do shit if not enabled
this.outputConnections[i].toNode.inputSum += this.outputConnections[i].weight * this.outputValue; //add the weighted output to the sum of the inputs of whatever node this node is connected to
}
}
//----------------------------------------------------------------------------------------------------------------------------------------
//not used
stepFunction(x) {
if(x < 0) {
return 0;
} else {
return 1;
}
}
//----------------------------------------------------------------------------------------------------------------------------------------
//not used
stepFunction(x) {
if (x < 0) {
return 0;
} else {
return 1;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//sigmoid activation function
sigmoid(x) {
return 1.0 / (1.0 + pow(Math.E, -4.9 * x)); //todo check pow
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
//returns whether this node connected to the parameter node
//used when adding a new connection
return 1.0 / (1.0 + pow(Math.E, -4.9 * x)); //todo check pow
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
//returns whether this node connected to the parameter node
//used when adding a new connection
isConnectedTo(node) {
if(node.layer == this.layer) { //nodes in the same this.layer cannot be connected
return false;
}
if (node.layer == this.layer) { //nodes in the same this.layer cannot be connected
return false;
}

//you get it
if(node.layer < this.layer) {
for(var i = 0; i < node.outputConnections.length; i++) {
if(node.outputConnections[i].toNode == this) {
return true;
}
//you get it
if (node.layer < this.layer) {
for (var i = 0; i < node.outputConnections.length; i++) {
if (node.outputConnections[i].toNode == this) {
return true;
}
} else {
for(var i = 0; i < this.outputConnections.length; i++) {
if(this.outputConnections[i].toNode == node) {
return true;
}
}
} else {
for (var i = 0; i < this.outputConnections.length; i++) {
if (this.outputConnections[i].toNode == node) {
return true;
}
}

return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a copy of this node

return false;
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a copy of this node
clone() {
var clone = new Node(this.number);
clone.layer = this.layer;
Expand Down
46 changes: 23 additions & 23 deletions Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ class Player {

//---------------------------------------------------------------------------------------------------------------------------------------------------------
show() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
move() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
update() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------

look() {
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
Expand All @@ -41,22 +41,22 @@ class Player {
//gets the output of the this.brain then converts them to actions
think() {

var max = 0;
var maxIndex = 0;
//get the output of the neural network
this.decision = this.brain.feedForward(this.vision);
var max = 0;
var maxIndex = 0;
//get the output of the neural network
this.decision = this.brain.feedForward(this.vision);

for (var i = 0; i < this.decision.length; i++) {
if (this.decision[i] > max) {
max = this.decision[i];
maxIndex = i;
}
for (var i = 0; i < this.decision.length; i++) {
if (this.decision[i] > max) {
max = this.decision[i];
maxIndex = i;
}

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a clone of this player with the same brian

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------
//returns a clone of this player with the same brian
clone() {
var clone = new Player();
clone.brain = this.brain.clone();
Expand Down Expand Up @@ -84,7 +84,7 @@ class Player {
}

//---------------------------------------------------------------------------------------------------------------------------------------------------------
//fot Genetic algorithm
//for Genetic algorithm
calculateFitness() {
this.fitness = random(10);
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<replace
Expand Down
Loading