Hi,
I’m trying to do my own implementation of a NEAT using processing, to be able to show my neural network, I have to order the different nodes in layers. The problem is that when I add a new node in the network, the layer number gets wrong when I update it. Thats because i’d need to increase the layer of the whole network.
To prevent that, I had the idea to recalculate all the layers using the current configuration of the network, starting with the input nodes, which have a layer number of 0.
How can I calculate the layer of all the other nodes ? I tried to implement a breadth first search, but it doesnt work because I have multiple input nodes. Does somebody have a different version that is able to calculate the distance from all the input nodes ?
To store the network, I use two different objects, Node and Connexion, the network is composed of an array of each object.
Here is the prototype of the two objects :
class Node {
int ID;
int type; //0=input, 1=hidden, 2=output
int layer; //The depth at which is the node
float x,y;
float bias, sum, output;
Node(int ID, int type) {
this.ID = ID;
this.type = type;
this.bias = random(-1,1);
}
class Connexion {
int input, output, innovation;
float weight;
boolean enabled;
Connexion(int input, int output, int innovation, float weight, boolean enabled) {
this.input = input; //ID of the input node
this.output = output; //ID of the output node
this.innovation = innovation; //Epoque of the connexion
this.weight = weight; //Weight to multiply
this.enabled = enabled; //Determines if the connexion is active
}
class Network {
Node[] nodes;
Connexion[] connexions;
int nbInputs, nbOutputs, totalNodes;
int nbConnexions;
float poidsMax = 5;
Network(int nbInputs, int nbOutputs) {
this.nbInputs = nbInputs;
this.nbOutputs = nbOutputs;
nbConnexions = 0;
totalNodes = nbInputs + nbOutputs;
nodes = new Node[totalNodes];
connexions = new Connexion[1];
for (int i=0; i<nbInputs; i++) { //Input nodes
nodes[i] = new Node(i, 0);
nodes[i].layer = 0;
}
for (int i=nbInputs; i<totalNodes; i++) { //Output nodes
nodes[i] = new Node(i, 2);
nodes[i].layer = 1;
}
}