/* Copyright (c) 2005-2007 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn.v15.neuralnet; import java.text.DecimalFormat; import java.util.LinkedList; import java.util.Map; import java.util.Random; import java.util.TreeMap; /** * Test suite. Acts as some sample code of the thing in action as well. * * Currently a mess, but is better than nothing. * * Uses a single neural network to simultaniously find a random floating point number, 'x' * and solve a boolean logic expression. Tests the networks ability to: * - learn things at all * - learn two unrelated outputs * - ignore things that are irrelevent * - rock * * @author Joseph Gleason * */ public class NetworkTest { /** * @param args */ public static void main(String[] args) throws InterruptedException { new NetworkTest(); } public NetworkTest() throws InterruptedException { DecimalFormat df=new DecimalFormat("0.0000"); LinkedList Layers=new LinkedList(); Layers.add(16); Layers.add(16); Brain B=new Brain(new SigmoidFunction(),Layers); B.addInput("in1"); B.addInput("in2"); B.addInput("in3"); B.addInput("in4"); B.addInput("in5"); B.addInput("in6"); B.addInput("a"); B.addInput("b"); B.addInput("c"); B.addInput("d"); B.addOutput("bool"); B.addOutput("X"); LinkedList Answers_x=new LinkedList(); LinkedList Answers_bool=new LinkedList(); Random R=new Random(); int n=0; int right_x=0; int right_bool=0; Map LearningTargets=new TreeMap(); while(true) { /* X is a random number in the range 0.25-0.75 */ double X=0.25+R.nextDouble()/2.0; /* Some bits */ boolean a=R.nextBoolean(); boolean b=R.nextBoolean(); boolean c=R.nextBoolean(); boolean d=R.nextBoolean(); B.setInput("a",0.0); B.setInput("b",0.0); B.setInput("c",0.0); B.setInput("d",0.0); if (a) B.setInput("a", 1.0); if (b) B.setInput("b", 1.0); if (c) B.setInput("c", 1.0); if (d) B.setInput("d", 1.0); /* Giving the network some things that are similar to X. * The test here can it get X based on these inputs. * * Of course, some of them have nothing to do with X * like this random 'in1' and the bits a,b,c,d above */ B.setInput("in1", R.nextDouble()); //B.setInput("in2", -X/5.0); //also too easy B.setInput("in3", X+(R.nextDouble()-0.5)/2.0); B.setInput("in4",Math.sqrt(X)); B.setInput("in5", X*X); //B.setInput("in6", X); //too easy, leave this out /* See if the network can solve this boolean logic */ boolean x1=(a && b); boolean x2=(c || d); boolean o=(x1 && !x2) || (!x1 && x2); double expected_bool=0.25; if (o) expected_bool=0.75; //We want to see if it can get X back double O_X=B.getOutput("X"); //We want to solve boolean logic double O_bool=B.getOutput("bool"); //Check O_X //We are allowing it to be off by as much as 0.02 in either direction if ((X - 0.02 <= O_X) && (O_X <= X + 0.02)) { right_x++; Answers_x.add(1); } else { Answers_x.add(0); } //Check O_bool //Allowing a fairly wide range since we just want boolean output //for this one if ( ((o) && (O_bool > 0.55)) || ((!o) && (O_bool < 0.45)) ) { right_bool++; Answers_bool.add(1); } else { Answers_bool.add(0); } n++; //print every 10000 if (n%10000==0) { double r=right_x; r=r/Answers_x.size(); System.out.println("random x: " + n + " err: " + df.format(Math.abs(X-O_X)) + " expected: " + df.format(X) + " output: " + df.format(O_X) + " right: " + df.format(r)); r=right_bool; r=r/Answers_bool.size(); System.out.println("bool logic: " + n + " err: " + df.format(Math.abs(expected_bool-O_bool)) + " expected: " + df.format(expected_bool) + " output: " + df.format(O_bool) + " right: " + df.format(r)); } //Learning both our unrelated things on the same network LearningTargets.put("X",X); LearningTargets.put("bool",expected_bool); //learn B.backPropogate(0.010, LearningTargets); //System.out.println(B); //Thread.sleep(250); //Keeping a queue of the last 10000 right/wrongs //so that our right average is only for the last 10000 while (Answers_x.size() > 10000) { int I=Answers_x.removeFirst(); if (I==1) right_x--; } while (Answers_bool.size() > 10000) { int I=Answers_bool.removeFirst(); if (I==1) right_bool--; } } } }