Browse files

dev

  • Loading branch information...
1 parent 271ae03 commit e9cbbe0a844308254f9da8a5071e9efb043fcda1 @egonelbre committed Jun 20, 2012
Showing with 246 additions and 130 deletions.
  1. +10 −16 indexnew.html
  2. +35 −6 js/buttons.js
  3. +1 −1 js/def.js
  4. +4 −0 js/main.js
  5. +9 −9 js/net.js
  6. +37 −82 js/netgui.js
  7. +141 −12 js/neury.js
  8. +9 −4 js/neury.pegjs
View
26 indexnew.html
@@ -32,30 +32,23 @@
<canvas id="view"></canvas>
<textarea id="input">
# output nodes ~ input nodes
-h.1 ~ 1000x - 333
-h.2 ~ -1000x - 333
-h.3 ~ 1000y - 333
-h.4 ~ -1000y - 333
-c.1 ~ -1000h[1..4]
-c.2 ~ 1000h[1..4] - 2000
-out ~ -10c[1..2] + 1
+h.1 ~ 10x - 3
+h.2 ~ -10x - 3
+h.3 ~ 10y - 3
+h.4 ~ -10y - 3
+c.1 ~ -10h[1..4] + 10
+c.2 ~ 10h[1..4] - 20
+out ~ -25c.1 - 10c.2 + 1
# define layers
= x y
= h[1..4]
= c[1..2]
= out
-#!x:horz
-#!y:vert
+x : value = horz
+y : value = vert
-## output nodes ~ input nodes
-#y ~ -2x
-#z ~ -3.5y
-#w ~ -25z
-#y ~ -7.6w
-#
-#!x:horz
</textarea>
<br />
<div id="log">
@@ -67,6 +60,7 @@
<button id="stop">Stop</button>
<script src="js/def.js"></script>
+<script src="js/math.js"></script>
<script src="js/vector.js"></script>
<script src="js/matrix.js"></script>
<script src="js/canvas.js"></script>
View
41 js/buttons.js
@@ -1,7 +1,8 @@
var loadWires = function(state, net, def){
- for(var k = 0; k < def.input.length; k += 1){
- var nodeDef = def.input[k],
+ for(var k = 0; k < def.output.length; k += 1){
+ var nodeDef = def.output[k],
node = net.Node(nodeDef.name);
+ console.log(node.name, ":", def.offset);
node.bias = def.offset;
}
@@ -13,7 +14,7 @@ var loadWires = function(state, net, def){
to = net.Node(toDef.name);
var wire = net.Wire(from, to);
- wire.modifier = fromDef.multiplier / toDef.multiplier;
+ wire.setWeight(fromDef.multiplier / toDef.multiplier);
}
}
}
@@ -30,16 +31,44 @@ var loadLayouts = function(state, net, def){
state.layer += 1;
}
+var doAssignments = function(state, net, def){
+ if(def.property == "value"){
+ for(var i = 0; i < def.nodes.length; i += 1){
+ var node = net.Node(def.nodes[i].name),
+ val = node.value;
+ if(def.value == "vert"){
+ var i = 0;
+ for(var y = 0; y < val.size.y; y += 1)
+ for(var x = 0; x < val.size.x; x += 1){
+ val.value[i] = y*2 / val.size.y - 1.0;
+ i += 1;
+ }
+ }
+ if(def.value == "horz"){
+ var i = 0;
+ for(var y = 0; y < val.size.y; y += 1)
+ for(var x = 0; x < val.size.x; x += 1){
+ val.value[i] = x*2 / val.size.x - 1.0;
+ i += 1;
+ }
+ }
+ node.update();
+ }
+ }
+}
+
var loadFromDefiniton = function(def){
var net = new Net(),
state = {layer : 0};
for(var i = 0; i < def.length; i += 1){
var row = def[i];
if(row.typ == "wire")
- loadWires(state, net, row.def)
+ loadWires(state, net, row.def);
else if(row.typ == "layout")
loadLayouts(state, net, row.def);
+ else if(row.typ == "assign")
+ doAssignments(state, net, row.def);
}
return net;
};
@@ -62,9 +91,9 @@ step.onclick = function(){
};
run.onclick = function(){
- console.log();
+ main.net.cpu.paused = false;
};
stop.onclick = function(){
- console.log();
+ main.net.cpu.paused = true;
};
View
2 js/def.js
@@ -90,7 +90,7 @@ Function.methods({
var other = this.apply( that, arguments );
return ( typeof other === 'object' && other ) || that;
},
- inherits : function( Parent ){
+ inherit : function( Parent ){
this.prototype = new Parent();
return this;
},
View
4 js/main.js
@@ -43,6 +43,10 @@ main = {
net = new Net();
main.net = net;
+setInterval(function(){
+ main.net.cpu.process();
+}, 50);
+
main.huds.add(new NetUI());
main.huds.add(new NetMover());
main.huds.add(new WireAdjuster());
View
18 js/net.js
@@ -105,16 +105,16 @@ Net.methods({
return new Matrix({x:50, y:50});
},
aggregate : function(value, bias, inputs){
+ if(inputs.length <= 0)
+ return;
// aggregate inputs
value.clear(bias);
for(var i = 0; i < inputs.length; i += 1){
- var inp = inputs[i];
- value.add(inp.data, inp.modifier);
+ var input = inputs[i];
+ value.add(input.data, input.weight);
}
- value.applyFunc(function(x){
- return x > 0.0 ? 1.0 : x < 0.0 ? -1.0 : 0.0;
- });
+ value.applyFunc(Math.sigmoid);
}
});
@@ -203,7 +203,7 @@ Port.methods({
function Wire(from, to){
this.from = from;
this.to = to;
- this.modifier = 1.0;
+ this.weight = 1.0;
this.view = null;
this.from.wires.push(this);
@@ -218,10 +218,10 @@ Wire.methods({
// get the value from source port
get: function(){
var data = this.from.get();
- return {data: data, modifier: this.modifier};
+ return {data: data, weight: this.weight};
},
- setModifier: function(value){
- this.modifier = value;
+ setWeight: function(value){
+ this.weight = value;
this.signal(this);
}
});
View
119 js/netgui.js
@@ -62,8 +62,8 @@ NetUI.methods({
ctx.fillStyle = "#000";
- var text = wire.modifier >= 1000 ?
- wire.modifier.toFixed(0): wire.modifier.toFixed(1);
+ var text = wire.weight >= 1000 ?
+ wire.weight.toFixed(0): wire.weight.toFixed(1);
ctx.fillTextC(text, view.center.x, view.center.y);
},
@@ -118,20 +118,20 @@ NetAdjuster.methods({
findItem : function(e){
return null;
},
- adjustItem : function(e){
+ adjustItem : function(item, e){
},
touch : function(action, e){
if(!this.adjusting && (action != "start"))
return;
if(e.button != 0) return;
if(action == "start")
- this.selection = this.findItem(e);
+ this.selection = this.findItem(action, e);
if(this.selection == null)
return false;
- this.adjustItem(this.selection, e);
+ this.adjustItem(action, e, this.selection);
if((action == "cancel") || (action == "end"))
this.selection = null;
@@ -141,109 +141,64 @@ NetAdjuster.methods({
}
})
+
function NetMover(){
- //todo : inherit from NetAdjuster
- this.moving = false;
- this.selection = null;
+ NetAdjuster.call(this);
this.last = {x:0, y:0};
}
-
+NetMover.inherit(NetAdjuster);
NetMover.methods({
- getNet : function(){
- return main.net;
- },
- render : function(ctx){
- },
- touch : function(action, e){
- if(!this.moving && (action != "start"))
- return;
- if(e.button != 0) return;
-
+ findItem : function(action, e){
var net = this.getNet(),
keys = Object.keys(net.nodes);
-
- if(action == "start"){
- for(var i = 0; i < keys.length; i += 1){
- var node = net.nodes[keys[i]];
- if (node.view == null)
- continue;
-
- var distSq = V.distSq(node.view.pos, e.scroll);
- if(distSq < node.view.radius * node.view.radius){
- this.selection = node;
- break;
- }
- }
+ for(var i = keys.length - 1; i >= 0; i -= 1){
+ var node = net.nodes[keys[i]];
+ if (node.view == null)
+ continue;
+
+ var distSq = V.distSq(node.view.pos, e.scroll);
+ if(distSq < node.view.radius * node.view.radius)
+ return node;
}
-
- if(this.selection == null)
- return false;
-
+ },
+ adjustItem : function(action, e, item){
if(action == "start")
V.set(this.last, e.scroll);
this.selection.view.pos.x += e.scroll.x - this.last.x;
this.selection.view.pos.y += e.scroll.y - this.last.y;
V.set(this.last, e.scroll);
-
- if((action == "cancel") || (action == "end"))
- this.selection = null;
- this.moving = this.selection != null;
-
- return true;
}
});
function WireAdjuster(){
- //todo : inherit from NetAdjuster
- this.moving = false;
- this.selection = null;
- this.last = {x:0, y:0};
+ NetAdjuster.call(this);
+ this.last = {x:0,y:0};
}
-
+WireAdjuster.inherit(NetAdjuster);
WireAdjuster.methods({
- getNet : function(){
- return main.net;
- },
- render : function(ctx){
- },
- touch : function(action, e){
- if(!this.moving && (action != "start"))
- return;
- if(e.button != 0) return;
-
+ findItem : function(action, e){
var net = this.getNet();
- if(action == "start"){
- for(var i = 0; i < net.wires.length; i += 1){
- var wire = net.wires[i];
- if (wire.view == null)
- continue;
-
- var distSq = V.distSq(wire.view.center, e.scroll);
- if(distSq < wire.view.radius * wire.view.radius){
- this.selection = wire;
- break;
- }
- }
+ for(var i = net.wires.length-1; i >= 0; i -= 1){
+ var wire = net.wires[i];
+ if (wire.view == null)
+ continue;
+
+ var distSq = V.distSq(wire.view.center, e.scroll);
+ if(distSq < wire.view.radius * wire.view.radius)
+ return wire;
}
-
- if(this.selection == null)
- return false;
-
+ },
+ adjustItem : function(action, e, item){
if(action == "start")
V.set(this.last, e.pos);
- var value = this.selection.modifier - (e.pos.y - this.last.y)/2;
- this.selection.setModifier(value);
+ var value = this.selection.weight - (e.pos.y - this.last.y)/2;
+ this.selection.setWeight(value);
V.set(this.last, e.pos);
-
- if((action == "cancel") || (action == "end"))
- this.selection = null;
- this.moving = this.selection != null;
-
- return true;
}
-});
+});
+
View
153 js/neury.js
@@ -40,8 +40,9 @@ neury = (function(){
"start": parse_start,
"line": parse_line,
"comment": parse_comment,
- "assignment": parse_assignment,
- "layouting": parse_layouting,
+ "wires": parse_wires,
+ "layout": parse_layout,
+ "assign": parse_assign,
"nodes": parse_nodes,
"node": parse_node,
"range_node": parse_range_node,
@@ -261,7 +262,7 @@ neury = (function(){
pos0 = clone(pos);
pos1 = clone(pos);
- result0 = parse_assignment();
+ result0 = parse_wires();
if (result0 !== null) {
result1 = parse_comment();
result1 = result1 !== null ? result1 : "";
@@ -276,15 +277,15 @@ neury = (function(){
pos = clone(pos1);
}
if (result0 !== null) {
- result0 = (function(offset, line, column, assign) {return {typ:"wire", "def":assign};})(pos0.offset, pos0.line, pos0.column, result0[0]);
+ result0 = (function(offset, line, column, wires) {return {typ:"wire", "def":wires};})(pos0.offset, pos0.line, pos0.column, result0[0]);
}
if (result0 === null) {
pos = clone(pos0);
}
if (result0 === null) {
pos0 = clone(pos);
pos1 = clone(pos);
- result0 = parse_layouting();
+ result0 = parse_layout();
if (result0 !== null) {
result1 = parse_comment();
result1 = result1 !== null ? result1 : "";
@@ -306,14 +307,38 @@ neury = (function(){
}
if (result0 === null) {
pos0 = clone(pos);
- result0 = parse_comment();
- result0 = result0 !== null ? result0 : "";
+ pos1 = clone(pos);
+ result0 = parse_assign();
if (result0 !== null) {
- result0 = (function(offset, line, column) {return 0;})(pos0.offset, pos0.line, pos0.column);
+ result1 = parse_comment();
+ result1 = result1 !== null ? result1 : "";
+ if (result1 !== null) {
+ result0 = [result0, result1];
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ if (result0 !== null) {
+ result0 = (function(offset, line, column, assign) {return {typ:"assign", "def":assign};})(pos0.offset, pos0.line, pos0.column, result0[0]);
}
if (result0 === null) {
pos = clone(pos0);
}
+ if (result0 === null) {
+ pos0 = clone(pos);
+ result0 = parse_comment();
+ result0 = result0 !== null ? result0 : "";
+ if (result0 !== null) {
+ result0 = (function(offset, line, column) {return 0;})(pos0.offset, pos0.line, pos0.column);
+ }
+ if (result0 === null) {
+ pos = clone(pos0);
+ }
+ }
}
}
@@ -392,8 +417,8 @@ neury = (function(){
return result0;
}
- function parse_assignment() {
- var cacheKey = "assignment@" + pos.offset;
+ function parse_wires() {
+ var cacheKey = "wires@" + pos.offset;
var cachedResult = cache[cacheKey];
if (cachedResult) {
pos = clone(cachedResult.nextPos);
@@ -502,8 +527,8 @@ neury = (function(){
return result0;
}
- function parse_layouting() {
- var cacheKey = "layouting@" + pos.offset;
+ function parse_layout() {
+ var cacheKey = "layout@" + pos.offset;
var cachedResult = cache[cacheKey];
if (cachedResult) {
pos = clone(cachedResult.nextPos);
@@ -562,6 +587,110 @@ neury = (function(){
return result0;
}
+ function parse_assign() {
+ var cacheKey = "assign@" + pos.offset;
+ var cachedResult = cache[cacheKey];
+ if (cachedResult) {
+ pos = clone(cachedResult.nextPos);
+ return cachedResult.result;
+ }
+
+ var result0, result1, result2, result3, result4, result5, result6, result7, result8, result9;
+ var pos0, pos1;
+
+ pos0 = clone(pos);
+ pos1 = clone(pos);
+ result0 = parse_space();
+ if (result0 !== null) {
+ result1 = parse_nodes();
+ if (result1 !== null) {
+ result2 = parse_space();
+ if (result2 !== null) {
+ if (input.charCodeAt(pos.offset) === 58) {
+ result3 = ":";
+ advance(pos, 1);
+ } else {
+ result3 = null;
+ if (reportFailures === 0) {
+ matchFailed("\":\"");
+ }
+ }
+ if (result3 !== null) {
+ result4 = parse_space();
+ if (result4 !== null) {
+ result5 = parse_identifier();
+ if (result5 !== null) {
+ result6 = parse_space();
+ if (result6 !== null) {
+ if (input.charCodeAt(pos.offset) === 61) {
+ result7 = "=";
+ advance(pos, 1);
+ } else {
+ result7 = null;
+ if (reportFailures === 0) {
+ matchFailed("\"=\"");
+ }
+ }
+ if (result7 !== null) {
+ result8 = parse_space();
+ if (result8 !== null) {
+ result9 = parse_identifier();
+ if (result9 !== null) {
+ result0 = [result0, result1, result2, result3, result4, result5, result6, result7, result8, result9];
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ } else {
+ result0 = null;
+ pos = clone(pos1);
+ }
+ if (result0 !== null) {
+ result0 = (function(offset, line, column, nodes, property, value) { return { nodes: nodes, property : property, value : value }; })(pos0.offset, pos0.line, pos0.column, result0[1], result0[5], result0[9]);
+ }
+ if (result0 === null) {
+ pos = clone(pos0);
+ }
+
+ cache[cacheKey] = {
+ nextPos: clone(pos),
+ result: result0
+ };
+ return result0;
+ }
+
function parse_nodes() {
var cacheKey = "nodes@" + pos.offset;
var cachedResult = cache[cacheKey];
View
13 js/neury.pegjs
@@ -11,23 +11,28 @@ start
}
line
- = assign:assignment comment? {return {typ:"wire", "def":assign};}
- / layout:layouting comment? {return {typ:"layout", "def":layout};}
+ = wires:wires comment? {return {typ:"wire", "def":wires};}
+ / layout:layout comment? {return {typ:"layout", "def":layout};}
+ / assign:assign comment? {return {typ:"assign", "def":assign};}
/ comment? {return 0;}
comment
= space "#" [^\n]*
-assignment
+wires
= output:nodes space "~" space input:nodes off:(space "+"? space float)?
{
var off = off ? off[3] : 0.0;
return {output: output, input: input, offset:off};
}
-layouting
+layout
= space "=" space nodes:nodes { return {nodes:nodes}; }
+assign
+ = space nodes:nodes space ":" space property:identifier space "=" space value:identifier
+{ return { nodes: nodes, property : property, value : value }; }
+
nodes
= first:node rest:( space "+"? space node )*
{

0 comments on commit e9cbbe0

Please sign in to comment.