Skip to content

Commit

Permalink
ADD_NODE improvement
Browse files Browse the repository at this point in the history
* ADD_NODE no longer removes gates
* Now, disconnect() automatically ungates the connection before
disconnecting it
  • Loading branch information
wagenaartje committed Apr 17, 2017
1 parent 6f08f82 commit 4593a04
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 48 deletions.
35 changes: 20 additions & 15 deletions dist/neataptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Node.prototype = {
this.connections.out.splice(i, 1);
var j = conn.to.connections.in.indexOf(conn);
conn.to.connections.in.splice(j, 1);
if(conn.gater != null) this.ungate(conn);
break;
}
}
Expand Down Expand Up @@ -961,18 +962,14 @@ Network.prototype = {
*/
disconnect: function(from, to){
// Delete the connection in the network's connection array
if(from != to){
for(conn in this.connections){
if(this.connections[conn].from == from && this.connections[conn].to == to){
this.connections.splice(conn, 1);
break;
}
}
} else {
for(conn in this.selfconns){
if(this.selfconns[conn].from == from){
this.selfconns.splice(conn, 1);
}
var connections = from == to ? this.selfconns : this.connections;

for(conn in connections){
var connection = connections[conn];
if(connection.from == from && connection.to == to){
if(connection.gater != null) this.ungate(connection);
connections.splice(conn, 1);
break;
}
}

Expand Down Expand Up @@ -1027,8 +1024,15 @@ Network.prototype = {
this.nodes.splice(minBound, 0, node);

// Now create two new connections
this.connect(connection.from, node);
this.connect(node, connection.to);
var newConn1 = this.connect(connection.from, node)[0];
var newConn2 = this.connect(node, connection.to)[0];

// Check if the original connection was gated
if(connection.gater != null){
var gater = connection.gater;
gater.ungate(connection);
gater.gate(Math.random() >= 0.5 ? newConn1 : newConn2);
}
break;
case Mutation.SUB_NODE:
// Check if there are nodes left to remove
Expand Down Expand Up @@ -1485,7 +1489,8 @@ Network.prototype = {
json.links.push({
source: this.nodes.indexOf(connection.gater),
target: index,
weight: connection.gater.activation
weight: connection.gater.activation,
gate: true
});
}

Expand Down
2 changes: 1 addition & 1 deletion dist/neataptic.min.js

Large diffs are not rendered by default.

35 changes: 20 additions & 15 deletions docs/cdn/neataptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ Node.prototype = {
this.connections.out.splice(i, 1);
var j = conn.to.connections.in.indexOf(conn);
conn.to.connections.in.splice(j, 1);
if(conn.gater != null) this.ungate(conn);
break;
}
}
Expand Down Expand Up @@ -961,18 +962,14 @@ Network.prototype = {
*/
disconnect: function(from, to){
// Delete the connection in the network's connection array
if(from != to){
for(conn in this.connections){
if(this.connections[conn].from == from && this.connections[conn].to == to){
this.connections.splice(conn, 1);
break;
}
}
} else {
for(conn in this.selfconns){
if(this.selfconns[conn].from == from){
this.selfconns.splice(conn, 1);
}
var connections = from == to ? this.selfconns : this.connections;

for(conn in connections){
var connection = connections[conn];
if(connection.from == from && connection.to == to){
if(connection.gater != null) this.ungate(connection);
connections.splice(conn, 1);
break;
}
}

Expand Down Expand Up @@ -1027,8 +1024,15 @@ Network.prototype = {
this.nodes.splice(minBound, 0, node);

// Now create two new connections
this.connect(connection.from, node);
this.connect(node, connection.to);
var newConn1 = this.connect(connection.from, node)[0];
var newConn2 = this.connect(node, connection.to)[0];

// Check if the original connection was gated
if(connection.gater != null){
var gater = connection.gater;
gater.ungate(connection);
gater.gate(Math.random() >= 0.5 ? newConn1 : newConn2);
}
break;
case Mutation.SUB_NODE:
// Check if there are nodes left to remove
Expand Down Expand Up @@ -1485,7 +1489,8 @@ Network.prototype = {
json.links.push({
source: this.nodes.indexOf(connection.gater),
target: index,
weight: connection.gater.activation
weight: connection.gater.activation,
gate: true
});
}

Expand Down
2 changes: 1 addition & 1 deletion docs/cdn/neataptic.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions graph/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ var drawGraph = function(graph, panel, activation) {
.style("stroke", function (d) {
if(activation){
return activationColor(d.source.activation * d.weight, graph.main.maxActivation * graph.main.maxWeight);
} else if(d.gate){
if(d.source.activation){
return activationColor(d.source.activation, graph.main.maxActivation);
} else{
return 'rgb(255,0,0)';
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "neataptic",
"version": "1.0.9",
"version": "1.0.12",
"description": "Architecture-free neural network library with genetic algorithm implementations",
"main": "./src/neataptic",
"scripts": {
Expand Down
34 changes: 19 additions & 15 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,14 @@ Network.prototype = {
*/
disconnect: function(from, to){
// Delete the connection in the network's connection array
if(from != to){
for(conn in this.connections){
if(this.connections[conn].from == from && this.connections[conn].to == to){
this.connections.splice(conn, 1);
break;
}
}
} else {
for(conn in this.selfconns){
if(this.selfconns[conn].from == from){
this.selfconns.splice(conn, 1);
}
var connections = from == to ? this.selfconns : this.connections;

for(conn in connections){
var connection = connections[conn];
if(connection.from == from && connection.to == to){
if(connection.gater != null) this.ungate(connection);
connections.splice(conn, 1);
break;
}
}

Expand Down Expand Up @@ -181,8 +177,15 @@ Network.prototype = {
this.nodes.splice(minBound, 0, node);

// Now create two new connections
this.connect(connection.from, node);
this.connect(node, connection.to);
var newConn1 = this.connect(connection.from, node)[0];
var newConn2 = this.connect(node, connection.to)[0];

// Check if the original connection was gated
if(connection.gater != null){
var gater = connection.gater;
gater.ungate(connection);
gater.gate(Math.random() >= 0.5 ? newConn1 : newConn2);
}
break;
case Mutation.SUB_NODE:
// Check if there are nodes left to remove
Expand Down Expand Up @@ -639,7 +642,8 @@ Network.prototype = {
json.links.push({
source: this.nodes.indexOf(connection.gater),
target: index,
weight: connection.gater.activation
weight: connection.gater.activation,
gate: true
});
}

Expand Down
1 change: 1 addition & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ Node.prototype = {
this.connections.out.splice(i, 1);
var j = conn.to.connections.in.indexOf(conn);
conn.to.connections.in.splice(j, 1);
if(conn.gater != null) this.ungate(conn);
break;
}
}
Expand Down

0 comments on commit 4593a04

Please sign in to comment.