
console.log("\nTest the learning capabilities of an Artificial Neural Net by learning the rules of Rock Paper Sissors. There are 3 boolean inputs (rock, paper, sissors) for each player, and 1 boolean output (win/no win) for each.")

var ROCK = 0 , PAPER = 1 , SISSORS = 2

var config = {
	inputs 				: 6 ,
	hiddenNodesPerLayer	: 4 ,	/* these are simple rules */
	outputs				: 2
}

console.log("The config is " + JSON.stringify(config))

var neuralnet = require("../neuralnet.js")(config)

function random() { 
	return Math.min(2,Math.max(0,(Math.round(Math.random()*3-0.5))))
}

function createInputs(player1,player2) {
	var result = [ 0 , 0 , 0 , 0 , 0 , 0 ]
	result[player1] = 1
	result[3+player2] = 1
	return result
}

function createOutputs(player1,player2) {
	if ( player1 == ROCK && player2 == PAPER ) {
		return [ 0 , 1 ]
	} else if ( player1 == ROCK && player2 == SISSORS ) {
		return [ 1 , 0 ]
	} else if ( player1 == PAPER && player2 == ROCK ) {
		return [ 1 , 0 ]
	} else if ( player1 == PAPER && player2 == SISSORS ) {
		return [ 0 , 1 ]
	} else if ( player1 == SISSORS && player2 == PAPER ) {
		return [ 1 , 0 ]
	} else if ( player1 == SISSORS && player2 == ROCK ) {
		return [ 0 , 1 ]
	} else {
		return [ 0 , 0 ]
	}
}

function doOneTraining() {
	for( var a = 0 ; a < 3 ; a ++)
		for( var b = 0 ; b < 3 ; b ++)
			neuralnet.train( createInputs(a,b) , createOutputs(a,b) )
}


function example() {

	var choices = [" ROCK  " , " PAPER " , "SISSORS"]

	function print(num) {
		return "'"+Math.round(num * 100)+"'"
	}

	for( var a = 0 ; a < 3 ; a ++)
	for( var b = 0 ; b < 3 ; b ++) {

	var inputs = createInputs(a,b)
	var predicted = neuralnet.predictBoolean( inputs )
	predicted = JSON.stringify(predicted)
	var expected = JSON.stringify(createOutputs(a,b))

	console.log(
		choices[a]+" vs "+ choices[b] +
		" expect "+expected+
		" and was "+predicted+
		" which is " + ( expected == predicted ? "correct" : "INCORRECT" )
		)
	}
}

console.log("\nPrior to training, here is how it preforms:\n")
example()


var TRAINING = 500

console.log("\n=== Training starts ===");
for( var count = 0 ; count < TRAINING ; count ++) {
	doOneTraining()
	if ( count % 20 == 0 ) { process.stdout.write(".") }
}
console.log("\n=== Training ends ===");

console.log("\nHere is how it preforms after training "+TRAINING+" iterations:\n")
example()

console.log()
@@ -304,64 +304,8 @@
 6 ,%0A
-%09hiddenNodesPerLayer%09: 4 ,%09/* these are simple rules */%0A
 %09out
@@ -1199,16 +1199,37 @@
 ing() %7B%0A
+%09var allequal = true%0A
 %09for( va
@@ -1288,69 +1288,405 @@
  ++)
+ %7B
 %0A%09%09%09
-neuralnet.train( createInputs(a,b) , createOutputs(a,b) )
+var actual = neuralnet.predict( createInputs(a,b) )%0A%09%09%09var expected = createOutputs(a,b)%0A%09%09%09var equal = true%0A%09%09%09for( var index = 0 ; index %3C expected.length && equal ; index++) %7B%0A%09%09%09%09equal = Math.round(actual%5Bindex%5D) == Math.round(expected%5Bindex%5D)%0A%09%09%09%7D%0A%09%09%09if ( !equal ) %7B%0A%09%09%09%09neuralnet.train( createInputs(a,b) , createOutputs(a,b) )%0A%09%09%09%7D%0A%09%09%09allequal = allequal && equal%0A%09%09%7D%0A%09return allequal
 %0A%7D%0A%0A
@@ -2333,17 +2333,18 @@
 INING = 
-5
+10
 00%0A%0Acons
@@ -2423,23 +2423,12 @@
 ING 
-; count ++) %7B%0A%09
+&& !
 doOn
@@ -2438,16 +2438,30 @@
 aining()
+ ; count ++) %7B
 %0A%09if ( c
@@ -2606,24 +2606,21 @@
 ining %22+
-TRAINING
+count
 +%22 itera
@@ -2654,8 +2654,9 @@
 le.log()
+%0A

update example to stop when 100% correct