8 Replies - 1587 Views - Last Post: 11 November 2011 - 09:14 AM Rate Topic: -----

#1 fftx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 11

Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 03:42 PM

sorry about the previous post, i accidently pressed the post button .
this is the code:
	static void output () {
		StringBuffer sb = new StringBuffer();
		String recall, accuracy;
		sb.append( "\n\rcycle: " + bpn.getLearningCycle() + "\n\n\r  input:       target:  output:    error:\n\r" );
		System.out.print(bpn.getNumberOfPatterns());
                for ( int x=0; x<bpn.getNumberOfPatterns(); x++ )
			sb.append(	"  " + bpn.getInputPattern(x) + "   " + bpn.getTargetPattern(x) + "  " + bpn.getOutputPattern(x) + "    " + bpn.getPatternError(x) + "\n\r" );
		System.out.println( sb );
	}
    public static void main ( String[] args ) {
               
		System.out.println( "\n\n\rDEMO OF A BACKPROPAGATION NET\n\r" );
		if ( args.length == 0 ) {
			System.out.println( "Missing argument --- Usage:  java BPN <display step>" );
			System.exit( 0 );
		}
		else {
                    
                        
            BackpropagationNet bpn = (new BPN()).new BackpropagationNet();
                      
			System.out.print( "Reading conversion file ..." );
			bpn.readConversionFile( "ascii2bin.cnv" );	// 1 ascii value -> 6 binary values
			System.out.println( "OK" );

			// some optional method calls
			bpn.setLearningRate( 0.25 );
			bpn.setMinimumError( 0.0005 );
			bpn.setAccuracy( 0.2 );
			bpn.setMaxLearningCycles( -1 );	// -1 = no maximum cycle
			bpn.setDisplayStep( Integer.parseInt( args[0] ) );	// get the argument

			System.out.print( "\n\rallright, let's learn...\n\r" );
			bpn.resetTime();
			while ( !bpn.finishedLearning() ) {		// while the net learns
				bpn.learn();						// perform one learning step
				if ( bpn.displayNow() ) output();	// display the current results
			}
			 output();
			System.out.println( "\n\rFINISHED." );
		}
	}


i can compile this code with no errors, but when i execute, i get this message:
Exception in thread "main" java.lang.NullPointerException in the output() function where sb.append appears.
I dont understand why is the NullPointer when StringBuffer can be modified

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 03:43 PM

Has bpn been initialized? I have a feeling it is null.
Was This Post Helpful? 0
  • +
  • -

#3 fftx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 03:45 PM

Yes it is. bpn i previously initialized
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 03:46 PM

Post all your code and your stack trace.
Was This Post Helpful? 0
  • +
  • -

#5 fftx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 03:59 PM

import java.io.*;
import java.lang.NullPointerException;


public class BPN {

     static BackpropagationNet bpn; 
     
     public class BackpropagationNet extends NeuralNet
/*     */ {
/*     */   Vector neuronLayerVector;
/*     */   NeuronLayer[] neuronLayerArray;
/*     */   WeightMatrix[] weightMatrixArray;
/*     */   Pattern[] inputPatternArray;
/*     */   Pattern[] targetPatternArray;
/*     */   String[] outputPatternArray;
/*     */   double minimumError;
/*     */   double error;
/*     */   double accuracy;
/*     */   float[][] layerOutputError;
/*     */   String[][] conversionTable;
/*     */   int lastLayer;
/*     */   int lastMatrix;
/*     */   int lastPattern;
/*     */   int multiplier;
/*     */ 
/*     */  public BackpropagationNet()
            {
/*     */   
/*  64 */     this.learningCycle = 0;
/*  65 */     this.maxLearningCycles = -1;
/*  66 */     this.minimumError = 0.0005D;
/*  67 */     this.learningRate = 0.25D;
/*  68 */     this.error = 1000.0D;
/*  69 */     this.multiplier = 0;
/*  70 */     this.accuracy = 0.2D;
/*  71 */     this.neuronLayerVector = new Vector();
/*  72 */     this.stopLearning = false;
/*  73 */     resetTime();
/*     */   }
    
/*     */ 
/*     */    void addNeuronLayer(int paramInt)
/*     */   {
/*  79 */     this.neuronLayerVector.addElement(new NeuronLayer(paramInt * this.multiplier));
/*     */   }
/*     */ 
/*     */   void connectLayers()
/*     */   {
/*  85 */     this.weightMatrixArray = new WeightMatrix[this.neuronLayerVector.size() - 1];
/*  86 */     this.neuronLayerArray = new NeuronLayer[this.neuronLayerVector.size()];
/*  87 */     int i = 0;
/*  88 */     for (Enumeration localEnumeration = this.neuronLayerVector.elements(); localEnumeration.hasMoreElements(); )
/*  89 */       this.neuronLayerArray[(i++)] = ((NeuronLayer)localEnumeration.nextElement());
/*  90 */     this.neuronLayerVector = null;
/*  91 */     for (i = 0; i < this.weightMatrixArray.length; i++) {
/*  92 */       this.weightMatrixArray[i] = new WeightMatrix(this.neuronLayerArray[i].size(), this.neuronLayerArray[(i + 1)].size(), true);
/*  93 */       this.weightMatrixArray[i].init();
/*     */     }
/*  95 */     this.lastLayer = (this.neuronLayerArray.length - 1);
/*  96 */     this.lastMatrix = (this.weightMatrixArray.length - 1);
/*     */   }
/*     */ 
/*     */   void setMinimumError(double paramDouble)
/*     */   {
/* 101 */     this.minimumError = paramDouble;
/*     */   }
/*     */ 
/*     */   double getMinimumError()
/*     */   {
/* 106 */     return this.minimumError;
/*     */   }
/*     */ 
/*     */   void setAccuracy(double paramDouble)
/*     */   {
/* 111 */     this.accuracy = paramDouble;
/*     */   }
/*     */ 
/*     */   double getAccuracy()
/*     */   {
/* 116 */     return this.accuracy;
/*     */   }
/*     */ 
/*     */   float[][] getWeightValues(int paramInt)
/*     */   {
/* 121 */     return this.weightMatrixArray[paramInt].getWeights();
/*     */   }
/*     */ 
/*     */   float[] getNeuronOutputs(int paramInt)
/*     */   {
/* 126 */     return this.neuronLayerArray[paramInt].getOutput();
/*     */   }
/*     */ 
/*     */   int getNumberOfLayers()
/*     */   {
/* 131 */     return this.neuronLayerArray.length;
/*     */   }
/*     */ 
/*     */   int getNumberOfNeurons(int paramInt)
/*     */   {
/* 136 */     return this.neuronLayerArray[paramInt].size();
/*     */   }
/*     */ 
/*     */   int getNumberOfWeights()
/*     */   {
/* 141 */     int i = 0;
/* 142 */     for (int j = 0; j <= this.lastMatrix; j++)
/* 143 */       i += this.weightMatrixArray[j].size();
/* 144 */     return i;
/*     */   }
/*     */ 
/*     */   int getNumberOfWeights(int paramInt)
/*     */   {
/* 149 */     return this.weightMatrixArray[paramInt].size();
/*     */   }
/*     */ 
/*     */   int getNumberOfPatterns()
/*     */   {
/* 154 */     return this.inputPatternArray.length;
/*     */   }
/*     */ 
/*     */   String getInputPattern(int paramInt)
/*     */   {
/* 159 */     return this.inputPatternArray[paramInt].getPatternString();
/*     */   }
/*     */ 
/*     */   String getTargetPattern(int paramInt)
/*     */   {
/* 164 */     return this.targetPatternArray[paramInt].getPatternString();
/*     */   }
/*     */ 
/*     */   String getOutputPattern(int paramInt)
/*     */   {
/* 170 */     String str1 = new String();
/* 171 */     String str2 = new String();
/* 172 */     float f = 0.0F;
/* 173 */     for (int i = 0; i < this.layerOutputError[0].length; i++) {
/* 174 */       f = this.targetPatternArray[paramInt].getValue(i) - this.layerOutputError[paramInt][i];
/* 175 */       str2 = str2 + (f < this.accuracy / 10.0D ? "0" : "1");
/*     */     }
/* 177 */     str1 = "";
/* 178 */     for (int j = 0; j < str2.length(); j += this.multiplier) {
/* 179 */       str1 = str1 + getAsciiValue(str2.substring(j, j + this.multiplier));
/*     */     }
/* 181 */     return str1;
/*     */   }
/*     */ 
/*     */   float getPatternError(int paramInt)
/*     */   {
/* 187 */     float f = 0.0F;
/* 188 */     for (int i = 0; i < this.layerOutputError[0].length; i++) {
/* 189 */       f += Math.abs(this.layerOutputError[paramInt][i]);
/*     */     }
/* 191 */     return f;
/*     */   }
/*     */ 
/*     */   double getError()
/*     */   {
/* 197 */     return this.error;
/*     */   }
/*     */ 
/*     */   void learn()
/*     */   {
/* 204 */     if ((this.error > this.minimumError) && ((this.learningCycle < this.maxLearningCycles) || (this.maxLearningCycles == -1))) {
/* 205 */       this.learningCycle += 1;
/* 206 */       for (int i = 0; i <= this.lastPattern; i++)
/*     */       {
/* 210 */         this.neuronLayerArray[0].setInput(this.inputPatternArray[i]);
/* 211 */         for (int j = 1; j <= this.lastLayer; j++) {
/* 212 */           this.neuronLayerArray[j].computeInput(this.neuronLayerArray[(j - 1)], this.weightMatrixArray[(j - 1)]);
/* 213 */           this.neuronLayerArray[j].computeOutput();
/*     */         }
/*     */ 
/* 217 */         this.neuronLayerArray[this.lastLayer].computeLayerError(this.targetPatternArray[i]);
/*     */ 
/* 219 */         this.layerOutputError[i] = this.neuronLayerArray[this.lastLayer].getLayerError();
/*     */ 
/* 222 */         for (int k = this.lastMatrix; k >= 0; k--) {
/* 223 */           this.weightMatrixArray[k].changeWeights(this.neuronLayerArray[k].getOutput(), this.neuronLayerArray[(k + 1)].getLayerError(), this.learningRate);
/* 224 */           if (k > 0) {
/* 225 */             this.neuronLayerArray[k].computeLayerError(this.neuronLayerArray[(k + 1)], this.weightMatrixArray[k]);
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 230 */       double d = 0.0D;
/* 231 */       for (int m = 0; m < this.layerOutputError.length; m++) {
/* 232 */         for (int n = 0; n < this.layerOutputError[0].length; n++) {
/* 233 */           d += square(this.layerOutputError[m][n]);
/*     */         }
/*     */       }
/* 236 */       this.error = Math.abs(d * 0.5D);
/*     */ 
/* 204 */       return;
/*     */     }
/*     */ 
/* 239 */     this.stopLearning = true;
/*     */   }
/*     */ 
/*     */   String recall(String paramString)
/*     */   {
/* 245 */     Pattern localPattern = new Pattern(paramString, this.conversionTable);
/* 246 */     float[] arrayOfFloat = new float[this.targetPatternArray[0].size()];
/*     */ 
/* 248 */     this.neuronLayerArray[0].setInput(localPattern);
/* 249 */     for (int i = 1; i <= this.lastLayer; i++) {
/* 250 */       this.neuronLayerArray[i].computeInput(this.neuronLayerArray[(i - 1)], this.weightMatrixArray[(i - 1)]);
/* 251 */       this.neuronLayerArray[i].computeOutput();
/*     */     }
/* 253 */     arrayOfFloat = this.neuronLayerArray[this.lastLayer].getOutput();
/*     */     String str2;
/* 254 */     String str1 = str2 = "";
/* 255 */     for (int j = 0; j < arrayOfFloat.length; j++) str1 = str1 + (arrayOfFloat[j] < this.accuracy ? "0" : "1");
/* 256 */     for (int k = 0; k < str1.length(); k += this.multiplier) str2 = str2 + getAsciiValue(str1.substring(k, k + this.multiplier));
/* 257 */     return str2;
/*     */   }
/*     */ 
/*     */   synchronized void readConversionFile(String paramString)
/*     */   {
/* 263 */     String str = null;
/*     */     try {
/* 265 */       DataInputStream localDataInputStream = new DataInputStream(new FileInputStream(paramString));
/* 266 */       
                BufferedReader br = new BufferedReader(new InputStreamReader(localDataInputStream));
                int i = Integer.parseInt(br.readLine());
/* 267 */       this.conversionTable = new String[i][2];
/* 268 */       for (int j = 0; j < i; j++) {
/* 269 */         str = br.readLine();
/* 270 */         this.conversionTable[j][0] = String.valueOf(str.charAt(0));
/* 271 */         this.conversionTable[j][1] = str.substring(1);
/*     */       }
/* 273 */       this.multiplier = this.conversionTable[0][1].length();
/*     */ 
/* 264 */       return;
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 276 */       error(105);
/*     */ 
/* 264 */       return;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/* 279 */       error(104);
/*     */     }
/*     */   }
/*     */ 
/*     */   String getAsciiValue(String paramString)
/*     */   {
/* 287 */     int i = 0; int j = this.conversionTable.length; int k = 0; int m = 0;
/* 288 */     while (i < j) {
/* 289 */       k = i + j >> 1;
/* 290 */       m = paramString.compareTo(this.conversionTable[k][1]);
/* 291 */       if (m == 0) {
/* 292 */         return this.conversionTable[k][0];
/*     */       }
/* 294 */       if (m > 0) i = k; else {
/* 295 */         j = k;
/*     */       }
/*     */     }
/* 298 */     return "*";
/*     */   }
/*     */ 
/*     */   synchronized void readPatternFile(String paramString)
/*     */   {
/* 313 */     String str = null;
/*     */     try {
/* 315 */       DataInputStream localDataInputStream = new DataInputStream(new FileInputStream(paramString));
                BufferedReader pr = new BufferedReader(new InputStreamReader(localDataInputStream));
/*     */       try {
/* 317 */         int i = Integer.parseInt(pr.readLine());
/* 318 */         int j = Integer.parseInt(pr.readLine());
/* 319 */         if (j * this.multiplier != this.neuronLayerArray[0].size()) {
/* 320 */           error(106);
/*     */         }
/*     */ 
/* 321 */         int k = Integer.parseInt(pr.readLine());
/* 322 */         if (k * this.multiplier != this.neuronLayerArray[this.lastLayer].size()) {
/* 323 */           error(107);
/*     */         }
/*     */ 
/* 324 */         this.inputPatternArray = new Pattern[i];
/* 325 */         this.targetPatternArray = new Pattern[i];
/* 326 */         this.outputPatternArray = new String[i];
/* 327 */         this.lastPattern = (this.inputPatternArray.length - 1);
/* 328 */         this.layerOutputError = new float[this.lastPattern + 1][this.neuronLayerArray[this.lastLayer].size()];
/* 329 */         for (int m = 0; m < i; m++) {
/* 330 */           str = pr.readLine();
/* 331 */           if (str == null) {
/* 332 */             error(102);
/*     */           }
/* 334 */           else if (str.length() != j + k + 1) {
/* 335 */             error(100);
/*     */           }
/*     */           else
/*     */           {
/* 337 */             this.inputPatternArray[m] = new Pattern(str.substring(0, j), this.conversionTable);
/* 338 */             this.targetPatternArray[m] = new Pattern(str.substring(j + 1), this.conversionTable);
/* 339 */             this.outputPatternArray[m] = new String();
/*     */           }
/*     */         }
/* 316 */         return;
/*     */       }
/*     */       catch (EOFException localEOFException)
/*     */       {
/* 345 */         error(102);
/*     */ 
/* 316 */         return;
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 349 */       error(105);
/*     */ 
/* 314 */       return;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/* 352 */       error(104);
/*     */     }
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     BackpropagationNet
 * JD-Core Version:    0.6.0
 */

	
	/*
	 * compares two patterns and returns an accuracy value
	 */
	static String compare ( String recallPattern, String targetPattern ) {
		int acc = 0, len = recallPattern.length();
		char[] rp, tp = new char[len];
		rp = recallPattern.toCharArray();
		tp = targetPattern.toCharArray();
		for ( int i=0; i<len; i++ ) if ( rp[i] == tp[i] ) acc++;
		if ( acc == len ) return ( "  GOT IT!" );
		return ( "  (" + acc + "/" + len + ": " +  String.valueOf( (int)(acc*100)/len ) + "%)" );
	}


	/*
	 * shows the results
	 */
	static void output () {
		StringBuffer sb = new StringBuffer();
		String recall, accuracy;
		sb.append( "\n\rcycle: " + bpn.getLearningCycle() + "\n\n\r  input:       target:  output:    error:\n\r" );
		System.out.print(bpn.getNumberOfPatterns());
                for ( int x=0; x<bpn.getNumberOfPatterns(); x++ )
			sb.append(	"  " + bpn.getInputPattern(x) + "   " + bpn.getTargetPattern(x) + "  " + bpn.getOutputPattern(x) + "    " + bpn.getPatternError(x) + "\n\r" );

		sb.append( "\n\r" );
		for ( int r=0; r<bpn.getNumberOfPatterns(); r++ ) {
			recall = bpn.recall( bpn.getInputPattern(r) );
			accuracy = compare( recall, bpn.getTargetPattern(r) );
			sb.append( "  recalling '" + bpn.getInputPattern(r) + "': " + recall + accuracy + "\n\r" );
		}

		double neterror = bpn.getError();
		String acc = (neterror>1.0) ? "2 bad" : String.valueOf((1.0-neterror)*100)+"%";
		sb.append( "\n\rminerror: " + bpn.getMinimumError() + "\n\rneterror: " + neterror + "\n\raccuracy: " + acc + "\n\rtime    : " + bpn.getElapsedTime() + " sec" );
		sb.append( "\n\r-----------------------------------------------------" );
		System.out.println( sb );
	}
        //
	//BPN.BackpropagationNet bpn = bp.new BackpropagationNet();

	/*
	 * here's the action...
	 */
	public static void main ( String[] args ) {
                //BackpropagationNet bpn;
		System.out.println( "\n\n\rDEMO OF A BACKPROPAGATION NET\n\r" );
		if ( args.length == 0 ) {
			System.out.println( "Missing argument --- Usage:  java BPN <display step>" );
			System.exit( 0 );
		}
		else {
                        
                        //bpn = new BackpropagationNet();
                        BackpropagationNet bpn = (new BPN()).new BackpropagationNet();
                        
			System.out.print( "Reading conversion file ..." );
			bpn.readConversionFile( "ascii2bin.cnv" );	// 1 ascii value -> 6 binary values
			System.out.println( "OK" );

			System.out.print( "Creating neuron layers ..." );
			bpn.addNeuronLayer( 10 );	// input layer for towns (must be 10, see pattern file)
			bpn.addNeuronLayer( 4 );	// hidden layer (number of neurons may vary)
										// (more hidden layers are possible)
			bpn.addNeuronLayer( 7 );	// output layer for countrys (must be 7, see pattern file)
			System.out.println( "OK" );

			System.out.print( "Connecting neuron layers ..." );
			bpn.connectLayers();
			System.out.println( "OK" );

			System.out.println( "\n\rNet structure:" );
			for ( int i=0; i<bpn.getNumberOfLayers(); i++ )
				System.out.println( "layer " + i + ": " + bpn.getNumberOfNeurons(i) + " neurons" );
			System.out.println( "weights: " + bpn.getNumberOfWeights() + "\n\r" );

			System.out.print( "Reading pattern file ..." );
			bpn.readPatternFile( "towns.pat" );
			System.out.println( "OK - patterns: " + bpn.getNumberOfPatterns() );

			// some optional method calls
			bpn.setLearningRate( 0.25 );
			bpn.setMinimumError( 0.0005 );
			bpn.setAccuracy( 0.2 );
			bpn.setMaxLearningCycles( -1 );	// -1 = no maximum cycle
			//bpn.setDisplayStep( Integer.parseInt( args[0] ) );	// get the argument

			System.out.print( "\n\rallright, let's learn...\n\r" );
			bpn.resetTime();
			while ( !bpn.finishedLearning() ) {		// while the net learns
				bpn.learn();						// perform one learning step
				//if ( bpn.displayNow() ) output();	// display the current results
			}
			 output();
			System.out.println( "\n\rFINISHED." );
		}
	}
        /*    */ public class InputValue
/*    */ {
/*    */   int x;
/*    */   int y;
/*    */   int z;
/*    */ 
/*    */   public InputValue()
/*    */   {
/* 29 */     this.x = 0;
/* 30 */     this.y = 0;
/* 31 */     this.z = 0;
/*    */   }
/*    */ 
/*    */   public InputValue(int paramInt1, int paramInt2, int paramInt3)
/*    */   {
/* 38 */     this.x = paramInt1;
/* 39 */     this.y = paramInt2;
/* 40 */     this.z = paramInt3;
/*    */   }
/*    */ 
/*    */   void setX(int paramInt)
/*    */   {
/* 47 */     this.x = paramInt;
/*    */   }
/*    */ 
/*    */   void setY(int paramInt)
/*    */   {
/* 54 */     this.y = paramInt;
/*    */   }
/*    */ 
/*    */   void setZ(int paramInt)
/*    */   {
/* 61 */     this.z = paramInt;
/*    */   }
/*    */ 
/*    */   int getX()
/*    */   {
/* 68 */     return this.x;
/*    */   }
/*    */ 
/*    */   int getY()
/*    */   {
/* 75 */     return this.y;
/*    */   }
/*    */ 
/*    */   int getZ()
/*    */   {
/* 82 */     return this.z;
/*    */   }
/*    */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     InputValue
 * JD-Core Version:    0.6.0
 */
        
/*     */ 
/*     */ abstract class NeuralNet
/*     */ {
/*  24 */   final int PATTERN_LENGTH = 100;
/*  25 */   final int PATTERN_VALUE = 101;
/*  26 */   final int PATTERNFILE_LENGTH = 102;
/*     */ 
/*  28 */   final int GENERAL_IO = 104;
/*  29 */   final int FILE_NOT_FOUND = 105;
/*  30 */   final int NUMBER_OF_IVALUES = 106;
/*  31 */   final int NUMBER_OF_TVALUES = 107;
/*  32 */   final int TOO_FEW_LEARNINGCYCLES = 108;
/*     */   double learningRate;
/*     */   int learningCycle;
/*  42 */   int maxLearningCycles = -1;
/*     */   String maxLearningCyclesString;
/*     */   int displayStep;
/*     */   long startTime;
/*  52 */   boolean learnInfinitely = false;
/*     */ 
/*  55 */   boolean stopLearning = false;
/*     */ 
/*     */   boolean finishedLearning()
/*     */   {
/*  59 */     return this.stopLearning;
/*     */   }
/*     */ 
/*     */   void error(int paramInt)
/*     */   {
/*  66 */     String str1 = null;
/*     */ 
/*  68 */     String str2 = null;
/*     */ 
/*  70 */     switch (paramInt) {
/*     */     case 105:
/*  72 */       str1 = "The specified pattern file couldn't be found.";
/*  73 */       str2 = "Check path and filename of your pattern file.";
/*  74 */       break;
/*     */     case 104:
/*  76 */       str1 = "A general IO error occurred while reading a file.";
/*  77 */       str2 = "Check the accessed file, maybe it's corrupted.";
/*  78 */       break;
/*     */     case 100:
/*  80 */       str1 = "A pattern in the specified pattern file has the wrong number of values.";
/*  81 */       str2 = "The amount of values in one row must be: value in second line + value in third line + 1.";
/*  82 */       break;
/*     */     case 106:
/*  84 */       str1 = "The number of input values in the specified pattern file is not the same as the number of neurons in the input neuron layer.";
/*  85 */       str2 = "Set the value in the second line of your pattern file to the number of input neurons or change the number of neurons in the input layer.";
/*  86 */       break;
/*     */     case 107:
/*  88 */       str1 = "The number of target values in the specified pattern file is not the same as the number of neurons in the output neuron layer.";
/*  89 */       str2 = "Set the value in the third line of your pattern file to the number of output neurons.";
/*  90 */       break;
/*     */     case 101:
/*  92 */       str1 = "A pattern value in the specified pattern file was wrong.";
/*  93 */       str2 = "Use pattern values that are 0 or 1.";
/*  94 */       break;
/*     */     case 102:
/*  96 */       str1 = "The number of patterns in the specified pattern file does not match the given number.";
/*  97 */       str2 = "Correct the number in the first line of your pattern file.";
/*  98 */       break;
/*     */     case 108:
/* 100 */       str1 = "There are too few maximum learning cycles defined.";
/* 101 */       str2 = "Increase the value of the maximum learning cycles or set it to -1 if the net should learn infinitely.";
/* 102 */       break;
/*     */     case 103:
/*     */     }
/*     */ 
/* 110 */     System.out.println("Error [" + paramInt + "]:\n\r" + str1);
/* 111 */     System.out.println("Try this: " + str2 + "\n\r");
/* 112 */     System.exit(0);
/*     */   }
/*     */ 
/*     */   public double square(double paramDouble)
/*     */   {
/* 117 */     return paramDouble * paramDouble;
/*     */   }
/*     */ 
/*     */   void setLearningRate(double paramDouble)
/*     */   {
/* 122 */     this.learningRate = paramDouble;
/*     */   }
/*     */ 
/*     */   double getLearningRate()
/*     */   {
/* 127 */     return this.learningRate;
/*     */   }
/*     */ 
/*     */   void setDisplayStep(int paramInt)
/*     */   {
/* 132 */     this.displayStep = paramInt;
/*     */   }
/*     */ 
/*     */   boolean displayNow()
/*     */   {
/* 137 */     return this.learningCycle % this.displayStep == 0;
/*     */   }
/*     */ 
/*     */   void resetTime() {
/* 141 */     this.startTime = System.currentTimeMillis();
/*     */   }
/*     */ 
/*     */   String getElapsedTime() {
/* 145 */     return String.valueOf((float)((System.currentTimeMillis() - this.startTime) * 0.001D));
/*     */   }
/*     */ 
/*     */   void setMaxLearningCycles(int paramInt)
/*     */   {
/* 151 */     if (paramInt == -1) {
/* 152 */       this.learnInfinitely = true;
/* 153 */       this.maxLearningCycles = -1;
/* 154 */       this.maxLearningCyclesString = "infinite";
/*     */ 
/* 151 */       return;
/*     */     }
/*     */ 
/* 157 */     if (paramInt > 0) {
/* 158 */       this.maxLearningCycles = paramInt;
/* 159 */       this.learnInfinitely = false;
/* 160 */       this.maxLearningCyclesString = String.valueOf(paramInt);
/*     */ 
/* 157 */       return;
/*     */     }
/*     */ 
/* 163 */     error(108);
/*     */   }
/*     */ 
/*     */   int getMaxLearningCycles()
/*     */   {
/* 169 */     return this.maxLearningCycles;
/*     */   }
/*     */ 
/*     */   void incLearningCycle()
/*     */   {
/* 174 */     this.learningCycle += 1;
/*     */   }
/*     */ 
/*     */   int getLearningCycle()
/*     */   {
/* 179 */     return this.learningCycle;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     NeuralNet
 * JD-Core Version:    0.6.0
 *//*     */ public static class Neuron
/*     */ {
/*     */   float input;
/*     */   float output;
/*     */   float outputError;
/*     */ 
/*     */   public Neuron()
/*     */   {
/*  33 */     init(0.0F);
/*     */   }
/*     */ 
/*     */   void init(float paramFloat)
/*     */   {
/*  40 */     this.input = paramFloat;
/*  41 */     this.output = paramFloat;
/*  42 */     this.outputError = 0.0F;
/*     */   }
/*     */ 
/*     */   void computeInput(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2)
/*     */   {
/*  49 */     this.input = 0.0F;
/*  50 */     for (int i = 0; i < paramArrayOfFloat1.length; i++) {
/*  51 */       if ((paramArrayOfFloat1[i] != 0.0F) && (paramArrayOfFloat2[i] != 0.0F))
/*  52 */         this.input += paramArrayOfFloat1[i] * paramArrayOfFloat2[i];
/*     */     }
/*  54 */     this.input += paramArrayOfFloat2[(paramArrayOfFloat2.length - 1)];
/*     */   }
/*     */ 
/*     */   void activateSigmoid()
/*     */   {
/*  61 */     this.output = (1.0F / (1.0F + (float)Math.exp(-this.input)));
/*     */   }
/*     */ 
/*     */   void computeOutputError(float paramFloat)
/*     */   {
/*  68 */     if ((this.output != 0.0F) && (this.output != 1.0F))
/*  69 */       this.outputError = ((paramFloat - this.output) * this.output * (1.0F - this.output));
/*     */   }
/*     */ 
/*     */   void computeOutputError(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2)
/*     */   {
/*  76 */     this.outputError = 0.0F;
/*  77 */     for (int i = 0; i < paramArrayOfFloat1.length; i++)
/*  78 */       if ((paramArrayOfFloat1[i] != 0.0F) && (this.output != 0.0F) && (this.output != 1.0F))
/*  79 */         this.outputError += paramArrayOfFloat1[i] * paramArrayOfFloat2[i] * this.output * (1.0F - this.output);
/*     */   }
/*     */ 
/*     */   float getInput()
/*     */   {
/*  87 */     return this.input;
/*     */   }
/*     */ 
/*     */   float getOutput()
/*     */   {
/*  94 */     return this.output;
/*     */   }
/*     */ 
/*     */   float getOutputError()
/*     */   {
/* 101 */     return this.outputError;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     Neuron
 * JD-Core Version:    0.6.0
 *//*     */
            public static class NeuronLayer
/*     */ {
/*     */   Neuron[] neuron;
/*     */ 
/*     */   NeuronLayer(int paramInt)
/*     */   {
/*  27 */     this.neuron = new Neuron[paramInt];
/*  28 */     for (int i = 0; i < paramInt; i++)
/*  29 */       this.neuron[i] = new Neuron();
/*     */   }
/*     */ 
/*     */   void setInput(Pattern paramPattern)
/*     */   {
/*  38 */     for (int i = 0; i < this.neuron.length; i++)
/*  39 */       this.neuron[i].init(paramPattern.getValue(i));
/*     */   }
/*     */ 
/*     */   void setInput(InputValue paramInputValue)
/*     */   {
/*  48 */     float[] arrayOfFloat = new float[3];
/*  49 */     arrayOfFloat[0] = paramInputValue.getX();
/*  50 */     arrayOfFloat[1] = paramInputValue.getY();
/*  51 */     arrayOfFloat[2] = paramInputValue.getZ();
/*  52 */     for (int i = 0; i < this.neuron.length; i++)
/*  53 */       this.neuron[i].init(arrayOfFloat[i]);
/*     */   }
/*     */ 
/*     */   void computeInput(NeuronLayer paramNeuronLayer, WeightMatrix paramWeightMatrix)
/*     */   {
/*  61 */     for (int i = 0; i < this.neuron.length; i++)
/*  62 */       this.neuron[i].computeInput(paramNeuronLayer.getOutput(), paramWeightMatrix.getInputWeights(i));
/*     */   }
/*     */ 
/*     */   void computeOutput()
/*     */   {
/*  71 */     for (int i = 0; i < this.neuron.length; i++)
/*  72 */       this.neuron[i].activateSigmoid();
/*     */   }
/*     */ 
/*     */   void computeLayerError(Pattern paramPattern)
/*     */   {
/*  81 */     for (int i = 0; i < this.neuron.length; i++)
/*  82 */       this.neuron[i].computeOutputError(paramPattern.getValue(i));
/*     */   }
/*     */ 
/*     */   void computeLayerError(NeuronLayer paramNeuronLayer, WeightMatrix paramWeightMatrix)
/*     */   {
/*  90 */     for (int i = 0; i < this.neuron.length; i++)
/*  91 */       this.neuron[i].computeOutputError(paramNeuronLayer.getLayerError(), paramWeightMatrix.getOutputWeights(i));
/*     */   }
/*     */ 
/*     */   float[] getOutput()
/*     */   {
/*  99 */     float[] arrayOfFloat = new float[this.neuron.length];
/* 100 */     for (int i = 0; i < this.neuron.length; i++) {
/* 101 */       arrayOfFloat[i] = this.neuron[i].getOutput();
/*     */     }
/* 103 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   float[] getLayerError()
/*     */   {
/* 110 */     float[] arrayOfFloat = new float[this.neuron.length];
/* 111 */     for (int i = 0; i < this.neuron.length; i++) {
/* 112 */       arrayOfFloat[i] = this.neuron[i].getOutputError();
/*     */     }
/* 114 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 121 */     return this.neuron.length;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     NeuronLayer
 * JD-Core Version:    0.6.0
 */
        /*     */ public static class Pattern
/*     */ {
/*     */   int[] pat;
/*     */   int nr;
/*     */   String type;
/*     */   String patString;
/*     */ 
/*     */   public Pattern(String paramString, String[][] paramArrayOfString)
/*     */   {
/*  73 */     this.patString = paramString;
/*  74 */     this.pat = new int[paramString.length() * paramArrayOfString[0][1].length()];
/*     */ 
/*  76 */     String str1 = new String();
/*  77 */     String str2 = new String();
/*  78 */     for (int i = 0; i < paramString.length(); i++) {
/*  79 */       for (int j = 0; j < paramArrayOfString.length; j++) {
/*  80 */         str2 = String.valueOf(paramString.charAt(i));
/*  81 */         if (str2.compareTo(" ") == 0) {
/*  82 */           str1 = str1 + paramArrayOfString[0][1];
/*  83 */           break;
/*     */         }
/*     */ 
/*  86 */         if (str2.compareTo(paramArrayOfString[j][0]) == 0) {
/*  87 */           str1 = str1 + paramArrayOfString[j][1];
/*  88 */           break;
/*     */         }
/*     */       }
/*     */     }
/*  92 */     char[] arrayOfChar = new char[str1.length()];
/*  93 */     arrayOfChar = str1.toCharArray();
/*  94 */     for (int k = 0; k < this.pat.length; k++)
/*  95 */       this.pat[k] = Character.digit(arrayOfChar[k], 10);
/*     */   }
/*     */ 
/*     */   String getPatternString()
/*     */   {
/* 102 */     return this.patString;
/*     */   }
/*     */ 
/*     */   float getValue(int paramInt)
/*     */   {
/* 107 */     return this.pat[paramInt];
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 112 */     return this.pat.length;
/*     */   }
/*     */ 
/*     */   boolean checkValues()
/*     */   {
/* 117 */     for (int i = 0; i < this.pat.length; i++) {
/* 118 */       if ((this.pat[i] < 0) || (this.pat[i] > 1))
/* 119 */         return false;
/*     */     }
/* 121 */     return true;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     Pattern
 * JD-Core Version:    0.6.0
 *//*     */ public static class WeightMatrix
/*     */ {
/*     */   float[][] weight;
/*     */   float[] bias;
/*     */   int size;
/*     */ 
/*     */   public WeightMatrix(int paramInt1, int paramInt2, boolean paramBoolean)
/*     */   {
/*  37 */     this.weight = new float[paramInt1][paramInt2];
/*  38 */     this.bias = new float[paramInt2];
/*  39 */     for (int i = 0; i < paramInt2; i++) {
/*  40 */       this.bias[i] = 1.0F;
/*     */     }
/*  42 */     this.size = (paramInt1 * paramInt2);
/*  43 */     if (paramBoolean == true)
/*  44 */       this.size += paramInt2;
/*     */   }
/*     */ 
/*     */   void init()
/*     */   {
/*  52 */     for (int i = 0; i < this.weight.length; i++) {
/*  53 */       for (int j = 0; j < this.weight[0].length; j++) {
/*  54 */         this.weight[i][j] = ((float)Math.random() * 2.0F - 1.0F);
/*     */       }
/*     */     }
/*  57 */     for (int j = 0; j < this.weight[0].length; j++)
/*  58 */       this.bias[j] = ((float)Math.random() * 2.0F - 1.0F);
/*     */   }
/*     */ 
/*     */   void init(float[][] paramArrayOfFloat)
/*     */   {
/*  67 */     for (int i = 0; i < this.weight.length; i++)
/*  68 */       for (int j = 0; j < this.weight[0].length; j++)
/*  69 */         this.weight[i][j] = paramArrayOfFloat[i][j];
/*     */   }
/*     */ 
/*     */   void init(InputValue[] paramArrayOfInputValue, int paramInt)
/*     */   {
/*     */     int i;
/*  78 */     switch (paramInt) {
/*     */     case 1:
/*  80 */       for (i = 0; i < this.weight[0].length; i++) {
/*  81 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*     */       }
/*  83 */       return;
/*     */     case 2:
/*  85 */       for (i = 0; i < this.weight[0].length; i++) {
/*  86 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*  87 */         this.weight[1][i] = paramArrayOfInputValue[i].getY();
/*     */       }
/*  89 */       return;
/*     */     case 3:
/*  91 */       for (i = 0; i < this.weight[0].length; i++) {
/*  92 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*  93 */         this.weight[1][i] = paramArrayOfInputValue[i].getY();
/*  94 */         this.weight[2][i] = paramArrayOfInputValue[i].getZ();
/*     */       }
/*  96 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   void changeWeights(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2, double paramDouble)
/*     */   {
/*     */     float f;
/* 104 */     for (int i = 0; i < this.weight.length; i++) {
/* 105 */       if (paramArrayOfFloat1[i] != 0.0F) {
/* 106 */         for (int j = 0; j < this.weight[0].length; j++) {
/* 107 */           if ((paramArrayOfFloat2[j] != 1.0F) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 108 */             f = this.weight[i][j];
/* 109 */             this.weight[i][j] = 0.0F;
/* 110 */             this.weight[i][j] = (f + paramArrayOfFloat1[i] * paramArrayOfFloat2[j] * (1.0F - paramArrayOfFloat2[j]) * (float)paramDouble);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 115 */     for (int j = 0; j < this.bias.length; j++)
/* 116 */       if ((paramArrayOfFloat2[j] != 1.0F) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 117 */         f = this.bias[j];
/* 118 */         this.bias[j] = 0.0F;
/* 119 */         this.bias[j] = (f + paramArrayOfFloat2[j] * (1.0F - paramArrayOfFloat2[j]) * (float)paramDouble);
/*     */       }
/*     */   }
/*     */ 
/*     */   void changeWeightsKFM(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2, double paramDouble)
/*     */   {
/* 128 */     for (int i = 0; i < this.weight.length; i++)
/* 129 */       for (int j = 0; j < this.weight[0].length; j++)
/* 130 */         if ((paramArrayOfFloat1[i] != this.weight[i][j]) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 131 */           float f = this.weight[i][j];
/* 132 */           this.weight[i][j] = 0.0F;
/* 133 */           this.weight[i][j] = (f + paramArrayOfFloat2[j] * (paramArrayOfFloat1[i] - f) * (float)paramDouble);
/*     */         }
/*     */   }
/*     */ 
/*     */   float[] getInputWeights(int paramInt)
/*     */   {
/* 143 */     float[] arrayOfFloat = new float[this.weight.length + 1];
/* 144 */     for (int i = 0; i < this.weight.length; i++) {
/* 145 */       arrayOfFloat[i] = this.weight[i][paramInt];
/*     */     }
/* 147 */     arrayOfFloat[this.weight.length] = this.bias[paramInt];
/* 148 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   float[] getOutputWeights(int paramInt)
/*     */   {
/* 155 */     float[] arrayOfFloat = new float[this.weight[0].length];
/* 156 */     for (int i = 0; i < this.weight[0].length; i++) {
/* 157 */       arrayOfFloat[i] = this.weight[paramInt][i];
/*     */     }
/* 159 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 166 */     return this.size;
/*     */   }
/*     */ 
/*     */   float[][] getWeights()
/*     */   {
/* 173 */     return this.weight;
/*     */   }
/*     */ 
/*     */   float[] getBiases()
/*     */   {
/* 180 */     return this.bias;
/*     */   }
/*     */ }

}



import java.io.*;
import java.lang.NullPointerException;


public class BPN {

     static BackpropagationNet bpn; 
     
     public class BackpropagationNet extends NeuralNet
/*     */ {
/*     */   Vector neuronLayerVector;
/*     */   NeuronLayer[] neuronLayerArray;
/*     */   WeightMatrix[] weightMatrixArray;
/*     */   Pattern[] inputPatternArray;
/*     */   Pattern[] targetPatternArray;
/*     */   String[] outputPatternArray;
/*     */   double minimumError;
/*     */   double error;
/*     */   double accuracy;
/*     */   float[][] layerOutputError;
/*     */   String[][] conversionTable;
/*     */   int lastLayer;
/*     */   int lastMatrix;
/*     */   int lastPattern;
/*     */   int multiplier;
/*     */ 
/*     */  public BackpropagationNet()
            {
/*     */   
/*  64 */     this.learningCycle = 0;
/*  65 */     this.maxLearningCycles = -1;
/*  66 */     this.minimumError = 0.0005D;
/*  67 */     this.learningRate = 0.25D;
/*  68 */     this.error = 1000.0D;
/*  69 */     this.multiplier = 0;
/*  70 */     this.accuracy = 0.2D;
/*  71 */     this.neuronLayerVector = new Vector();
/*  72 */     this.stopLearning = false;
/*  73 */     resetTime();
/*     */   }
    
/*     */ 
/*     */    void addNeuronLayer(int paramInt)
/*     */   {
/*  79 */     this.neuronLayerVector.addElement(new NeuronLayer(paramInt * this.multiplier));
/*     */   }
/*     */ 
/*     */   void connectLayers()
/*     */   {
/*  85 */     this.weightMatrixArray = new WeightMatrix[this.neuronLayerVector.size() - 1];
/*  86 */     this.neuronLayerArray = new NeuronLayer[this.neuronLayerVector.size()];
/*  87 */     int i = 0;
/*  88 */     for (Enumeration localEnumeration = this.neuronLayerVector.elements(); localEnumeration.hasMoreElements(); )
/*  89 */       this.neuronLayerArray[(i++)] = ((NeuronLayer)localEnumeration.nextElement());
/*  90 */     this.neuronLayerVector = null;
/*  91 */     for (i = 0; i < this.weightMatrixArray.length; i++) {
/*  92 */       this.weightMatrixArray[i] = new WeightMatrix(this.neuronLayerArray[i].size(), this.neuronLayerArray[(i + 1)].size(), true);
/*  93 */       this.weightMatrixArray[i].init();
/*     */     }
/*  95 */     this.lastLayer = (this.neuronLayerArray.length - 1);
/*  96 */     this.lastMatrix = (this.weightMatrixArray.length - 1);
/*     */   }
/*     */ 
/*     */   void setMinimumError(double paramDouble)
/*     */   {
/* 101 */     this.minimumError = paramDouble;
/*     */   }
/*     */ 
/*     */   double getMinimumError()
/*     */   {
/* 106 */     return this.minimumError;
/*     */   }
/*     */ 
/*     */   void setAccuracy(double paramDouble)
/*     */   {
/* 111 */     this.accuracy = paramDouble;
/*     */   }
/*     */ 
/*     */   double getAccuracy()
/*     */   {
/* 116 */     return this.accuracy;
/*     */   }
/*     */ 
/*     */   float[][] getWeightValues(int paramInt)
/*     */   {
/* 121 */     return this.weightMatrixArray[paramInt].getWeights();
/*     */   }
/*     */ 
/*     */   float[] getNeuronOutputs(int paramInt)
/*     */   {
/* 126 */     return this.neuronLayerArray[paramInt].getOutput();
/*     */   }
/*     */ 
/*     */   int getNumberOfLayers()
/*     */   {
/* 131 */     return this.neuronLayerArray.length;
/*     */   }
/*     */ 
/*     */   int getNumberOfNeurons(int paramInt)
/*     */   {
/* 136 */     return this.neuronLayerArray[paramInt].size();
/*     */   }
/*     */ 
/*     */   int getNumberOfWeights()
/*     */   {
/* 141 */     int i = 0;
/* 142 */     for (int j = 0; j <= this.lastMatrix; j++)
/* 143 */       i += this.weightMatrixArray[j].size();
/* 144 */     return i;
/*     */   }
/*     */ 
/*     */   int getNumberOfWeights(int paramInt)
/*     */   {
/* 149 */     return this.weightMatrixArray[paramInt].size();
/*     */   }
/*     */ 
/*     */   int getNumberOfPatterns()
/*     */   {
/* 154 */     return this.inputPatternArray.length;
/*     */   }
/*     */ 
/*     */   String getInputPattern(int paramInt)
/*     */   {
/* 159 */     return this.inputPatternArray[paramInt].getPatternString();
/*     */   }
/*     */ 
/*     */   String getTargetPattern(int paramInt)
/*     */   {
/* 164 */     return this.targetPatternArray[paramInt].getPatternString();
/*     */   }
/*     */ 
/*     */   String getOutputPattern(int paramInt)
/*     */   {
/* 170 */     String str1 = new String();
/* 171 */     String str2 = new String();
/* 172 */     float f = 0.0F;
/* 173 */     for (int i = 0; i < this.layerOutputError[0].length; i++) {
/* 174 */       f = this.targetPatternArray[paramInt].getValue(i) - this.layerOutputError[paramInt][i];
/* 175 */       str2 = str2 + (f < this.accuracy / 10.0D ? "0" : "1");
/*     */     }
/* 177 */     str1 = "";
/* 178 */     for (int j = 0; j < str2.length(); j += this.multiplier) {
/* 179 */       str1 = str1 + getAsciiValue(str2.substring(j, j + this.multiplier));
/*     */     }
/* 181 */     return str1;
/*     */   }
/*     */ 
/*     */   float getPatternError(int paramInt)
/*     */   {
/* 187 */     float f = 0.0F;
/* 188 */     for (int i = 0; i < this.layerOutputError[0].length; i++) {
/* 189 */       f += Math.abs(this.layerOutputError[paramInt][i]);
/*     */     }
/* 191 */     return f;
/*     */   }
/*     */ 
/*     */   double getError()
/*     */   {
/* 197 */     return this.error;
/*     */   }
/*     */ 
/*     */   void learn()
/*     */   {
/* 204 */     if ((this.error > this.minimumError) && ((this.learningCycle < this.maxLearningCycles) || (this.maxLearningCycles == -1))) {
/* 205 */       this.learningCycle += 1;
/* 206 */       for (int i = 0; i <= this.lastPattern; i++)
/*     */       {
/* 210 */         this.neuronLayerArray[0].setInput(this.inputPatternArray[i]);
/* 211 */         for (int j = 1; j <= this.lastLayer; j++) {
/* 212 */           this.neuronLayerArray[j].computeInput(this.neuronLayerArray[(j - 1)], this.weightMatrixArray[(j - 1)]);
/* 213 */           this.neuronLayerArray[j].computeOutput();
/*     */         }
/*     */ 
/* 217 */         this.neuronLayerArray[this.lastLayer].computeLayerError(this.targetPatternArray[i]);
/*     */ 
/* 219 */         this.layerOutputError[i] = this.neuronLayerArray[this.lastLayer].getLayerError();
/*     */ 
/* 222 */         for (int k = this.lastMatrix; k >= 0; k--) {
/* 223 */           this.weightMatrixArray[k].changeWeights(this.neuronLayerArray[k].getOutput(), this.neuronLayerArray[(k + 1)].getLayerError(), this.learningRate);
/* 224 */           if (k > 0) {
/* 225 */             this.neuronLayerArray[k].computeLayerError(this.neuronLayerArray[(k + 1)], this.weightMatrixArray[k]);
/*     */           }
/*     */         }
/*     */       }
/*     */ 
/* 230 */       double d = 0.0D;
/* 231 */       for (int m = 0; m < this.layerOutputError.length; m++) {
/* 232 */         for (int n = 0; n < this.layerOutputError[0].length; n++) {
/* 233 */           d += square(this.layerOutputError[m][n]);
/*     */         }
/*     */       }
/* 236 */       this.error = Math.abs(d * 0.5D);
/*     */ 
/* 204 */       return;
/*     */     }
/*     */ 
/* 239 */     this.stopLearning = true;
/*     */   }
/*     */ 
/*     */   String recall(String paramString)
/*     */   {
/* 245 */     Pattern localPattern = new Pattern(paramString, this.conversionTable);
/* 246 */     float[] arrayOfFloat = new float[this.targetPatternArray[0].size()];
/*     */ 
/* 248 */     this.neuronLayerArray[0].setInput(localPattern);
/* 249 */     for (int i = 1; i <= this.lastLayer; i++) {
/* 250 */       this.neuronLayerArray[i].computeInput(this.neuronLayerArray[(i - 1)], this.weightMatrixArray[(i - 1)]);
/* 251 */       this.neuronLayerArray[i].computeOutput();
/*     */     }
/* 253 */     arrayOfFloat = this.neuronLayerArray[this.lastLayer].getOutput();
/*     */     String str2;
/* 254 */     String str1 = str2 = "";
/* 255 */     for (int j = 0; j < arrayOfFloat.length; j++) str1 = str1 + (arrayOfFloat[j] < this.accuracy ? "0" : "1");
/* 256 */     for (int k = 0; k < str1.length(); k += this.multiplier) str2 = str2 + getAsciiValue(str1.substring(k, k + this.multiplier));
/* 257 */     return str2;
/*     */   }
/*     */ 
/*     */   synchronized void readConversionFile(String paramString)
/*     */   {
/* 263 */     String str = null;
/*     */     try {
/* 265 */       DataInputStream localDataInputStream = new DataInputStream(new FileInputStream(paramString));
/* 266 */       
                BufferedReader br = new BufferedReader(new InputStreamReader(localDataInputStream));
                int i = Integer.parseInt(br.readLine());
/* 267 */       this.conversionTable = new String[i][2];
/* 268 */       for (int j = 0; j < i; j++) {
/* 269 */         str = br.readLine();
/* 270 */         this.conversionTable[j][0] = String.valueOf(str.charAt(0));
/* 271 */         this.conversionTable[j][1] = str.substring(1);
/*     */       }
/* 273 */       this.multiplier = this.conversionTable[0][1].length();
/*     */ 
/* 264 */       return;
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 276 */       error(105);
/*     */ 
/* 264 */       return;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/* 279 */       error(104);
/*     */     }
/*     */   }
/*     */ 
/*     */   String getAsciiValue(String paramString)
/*     */   {
/* 287 */     int i = 0; int j = this.conversionTable.length; int k = 0; int m = 0;
/* 288 */     while (i < j) {
/* 289 */       k = i + j >> 1;
/* 290 */       m = paramString.compareTo(this.conversionTable[k][1]);
/* 291 */       if (m == 0) {
/* 292 */         return this.conversionTable[k][0];
/*     */       }
/* 294 */       if (m > 0) i = k; else {
/* 295 */         j = k;
/*     */       }
/*     */     }
/* 298 */     return "*";
/*     */   }
/*     */ 
/*     */   synchronized void readPatternFile(String paramString)
/*     */   {
/* 313 */     String str = null;
/*     */     try {
/* 315 */       DataInputStream localDataInputStream = new DataInputStream(new FileInputStream(paramString));
                BufferedReader pr = new BufferedReader(new InputStreamReader(localDataInputStream));
/*     */       try {
/* 317 */         int i = Integer.parseInt(pr.readLine());
/* 318 */         int j = Integer.parseInt(pr.readLine());
/* 319 */         if (j * this.multiplier != this.neuronLayerArray[0].size()) {
/* 320 */           error(106);
/*     */         }
/*     */ 
/* 321 */         int k = Integer.parseInt(pr.readLine());
/* 322 */         if (k * this.multiplier != this.neuronLayerArray[this.lastLayer].size()) {
/* 323 */           error(107);
/*     */         }
/*     */ 
/* 324 */         this.inputPatternArray = new Pattern[i];
/* 325 */         this.targetPatternArray = new Pattern[i];
/* 326 */         this.outputPatternArray = new String[i];
/* 327 */         this.lastPattern = (this.inputPatternArray.length - 1);
/* 328 */         this.layerOutputError = new float[this.lastPattern + 1][this.neuronLayerArray[this.lastLayer].size()];
/* 329 */         for (int m = 0; m < i; m++) {
/* 330 */           str = pr.readLine();
/* 331 */           if (str == null) {
/* 332 */             error(102);
/*     */           }
/* 334 */           else if (str.length() != j + k + 1) {
/* 335 */             error(100);
/*     */           }
/*     */           else
/*     */           {
/* 337 */             this.inputPatternArray[m] = new Pattern(str.substring(0, j), this.conversionTable);
/* 338 */             this.targetPatternArray[m] = new Pattern(str.substring(j + 1), this.conversionTable);
/* 339 */             this.outputPatternArray[m] = new String();
/*     */           }
/*     */         }
/* 316 */         return;
/*     */       }
/*     */       catch (EOFException localEOFException)
/*     */       {
/* 345 */         error(102);
/*     */ 
/* 316 */         return;
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileNotFoundException localFileNotFoundException)
/*     */     {
/* 349 */       error(105);
/*     */ 
/* 314 */       return;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/* 352 */       error(104);
/*     */     }
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     BackpropagationNet
 * JD-Core Version:    0.6.0
 */

	
	/*
	 * compares two patterns and returns an accuracy value
	 */
	static String compare ( String recallPattern, String targetPattern ) {
		int acc = 0, len = recallPattern.length();
		char[] rp, tp = new char[len];
		rp = recallPattern.toCharArray();
		tp = targetPattern.toCharArray();
		for ( int i=0; i<len; i++ ) if ( rp[i] == tp[i] ) acc++;
		if ( acc == len ) return ( "  GOT IT!" );
		return ( "  (" + acc + "/" + len + ": " +  String.valueOf( (int)(acc*100)/len ) + "%)" );
	}


	/*
	 * shows the results
	 */
	static void output () {
		StringBuffer sb = new StringBuffer();
		String recall, accuracy;
		sb.append( "\n\rcycle: " + bpn.getLearningCycle() + "\n\n\r  input:       target:  output:    error:\n\r" );
		System.out.print(bpn.getNumberOfPatterns());
                for ( int x=0; x<bpn.getNumberOfPatterns(); x++ )
			sb.append(	"  " + bpn.getInputPattern(x) + "   " + bpn.getTargetPattern(x) + "  " + bpn.getOutputPattern(x) + "    " + bpn.getPatternError(x) + "\n\r" );

		sb.append( "\n\r" );
		for ( int r=0; r<bpn.getNumberOfPatterns(); r++ ) {
			recall = bpn.recall( bpn.getInputPattern(r) );
			accuracy = compare( recall, bpn.getTargetPattern(r) );
			sb.append( "  recalling '" + bpn.getInputPattern(r) + "': " + recall + accuracy + "\n\r" );
		}

		double neterror = bpn.getError();
		String acc = (neterror>1.0) ? "2 bad" : String.valueOf((1.0-neterror)*100)+"%";
		sb.append( "\n\rminerror: " + bpn.getMinimumError() + "\n\rneterror: " + neterror + "\n\raccuracy: " + acc + "\n\rtime    : " + bpn.getElapsedTime() + " sec" );
		sb.append( "\n\r-----------------------------------------------------" );
		System.out.println( sb );
	}
        //
	//BPN.BackpropagationNet bpn = bp.new BackpropagationNet();

	/*
	 * here's the action...
	 */
	public static void main ( String[] args ) {
                //BackpropagationNet bpn;
		System.out.println( "\n\n\rDEMO OF A BACKPROPAGATION NET\n\r" );
		if ( args.length == 0 ) {
			System.out.println( "Missing argument --- Usage:  java BPN <display step>" );
			System.exit( 0 );
		}
		else {
                        
                        //bpn = new BackpropagationNet();
                        BackpropagationNet bpn = (new BPN()).new BackpropagationNet();
                        
			System.out.print( "Reading conversion file ..." );
			bpn.readConversionFile( "ascii2bin.cnv" );	// 1 ascii value -> 6 binary values
			System.out.println( "OK" );

			System.out.print( "Creating neuron layers ..." );
			bpn.addNeuronLayer( 10 );	// input layer for towns (must be 10, see pattern file)
			bpn.addNeuronLayer( 4 );	// hidden layer (number of neurons may vary)
										// (more hidden layers are possible)
			bpn.addNeuronLayer( 7 );	// output layer for countrys (must be 7, see pattern file)
			System.out.println( "OK" );

			System.out.print( "Connecting neuron layers ..." );
			bpn.connectLayers();
			System.out.println( "OK" );

			System.out.println( "\n\rNet structure:" );
			for ( int i=0; i<bpn.getNumberOfLayers(); i++ )
				System.out.println( "layer " + i + ": " + bpn.getNumberOfNeurons(i) + " neurons" );
			System.out.println( "weights: " + bpn.getNumberOfWeights() + "\n\r" );

			System.out.print( "Reading pattern file ..." );
			bpn.readPatternFile( "towns.pat" );
			System.out.println( "OK - patterns: " + bpn.getNumberOfPatterns() );

			// some optional method calls
			bpn.setLearningRate( 0.25 );
			bpn.setMinimumError( 0.0005 );
			bpn.setAccuracy( 0.2 );
			bpn.setMaxLearningCycles( -1 );	// -1 = no maximum cycle
			//bpn.setDisplayStep( Integer.parseInt( args[0] ) );	// get the argument

			System.out.print( "\n\rallright, let's learn...\n\r" );
			bpn.resetTime();
			while ( !bpn.finishedLearning() ) {		// while the net learns
				bpn.learn();						// perform one learning step
				//if ( bpn.displayNow() ) output();	// display the current results
			}
			 output();
			System.out.println( "\n\rFINISHED." );
		}
	}
        /*    */ public class InputValue
/*    */ {
/*    */   int x;
/*    */   int y;
/*    */   int z;
/*    */ 
/*    */   public InputValue()
/*    */   {
/* 29 */     this.x = 0;
/* 30 */     this.y = 0;
/* 31 */     this.z = 0;
/*    */   }
/*    */ 
/*    */   public InputValue(int paramInt1, int paramInt2, int paramInt3)
/*    */   {
/* 38 */     this.x = paramInt1;
/* 39 */     this.y = paramInt2;
/* 40 */     this.z = paramInt3;
/*    */   }
/*    */ 
/*    */   void setX(int paramInt)
/*    */   {
/* 47 */     this.x = paramInt;
/*    */   }
/*    */ 
/*    */   void setY(int paramInt)
/*    */   {
/* 54 */     this.y = paramInt;
/*    */   }
/*    */ 
/*    */   void setZ(int paramInt)
/*    */   {
/* 61 */     this.z = paramInt;
/*    */   }
/*    */ 
/*    */   int getX()
/*    */   {
/* 68 */     return this.x;
/*    */   }
/*    */ 
/*    */   int getY()
/*    */   {
/* 75 */     return this.y;
/*    */   }
/*    */ 
/*    */   int getZ()
/*    */   {
/* 82 */     return this.z;
/*    */   }
/*    */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     InputValue
 * JD-Core Version:    0.6.0
 */
        
/*     */ 
/*     */ abstract class NeuralNet
/*     */ {
/*  24 */   final int PATTERN_LENGTH = 100;
/*  25 */   final int PATTERN_VALUE = 101;
/*  26 */   final int PATTERNFILE_LENGTH = 102;
/*     */ 
/*  28 */   final int GENERAL_IO = 104;
/*  29 */   final int FILE_NOT_FOUND = 105;
/*  30 */   final int NUMBER_OF_IVALUES = 106;
/*  31 */   final int NUMBER_OF_TVALUES = 107;
/*  32 */   final int TOO_FEW_LEARNINGCYCLES = 108;
/*     */   double learningRate;
/*     */   int learningCycle;
/*  42 */   int maxLearningCycles = -1;
/*     */   String maxLearningCyclesString;
/*     */   int displayStep;
/*     */   long startTime;
/*  52 */   boolean learnInfinitely = false;
/*     */ 
/*  55 */   boolean stopLearning = false;
/*     */ 
/*     */   boolean finishedLearning()
/*     */   {
/*  59 */     return this.stopLearning;
/*     */   }
/*     */ 
/*     */   void error(int paramInt)
/*     */   {
/*  66 */     String str1 = null;
/*     */ 
/*  68 */     String str2 = null;
/*     */ 
/*  70 */     switch (paramInt) {
/*     */     case 105:
/*  72 */       str1 = "The specified pattern file couldn't be found.";
/*  73 */       str2 = "Check path and filename of your pattern file.";
/*  74 */       break;
/*     */     case 104:
/*  76 */       str1 = "A general IO error occurred while reading a file.";
/*  77 */       str2 = "Check the accessed file, maybe it's corrupted.";
/*  78 */       break;
/*     */     case 100:
/*  80 */       str1 = "A pattern in the specified pattern file has the wrong number of values.";
/*  81 */       str2 = "The amount of values in one row must be: value in second line + value in third line + 1.";
/*  82 */       break;
/*     */     case 106:
/*  84 */       str1 = "The number of input values in the specified pattern file is not the same as the number of neurons in the input neuron layer.";
/*  85 */       str2 = "Set the value in the second line of your pattern file to the number of input neurons or change the number of neurons in the input layer.";
/*  86 */       break;
/*     */     case 107:
/*  88 */       str1 = "The number of target values in the specified pattern file is not the same as the number of neurons in the output neuron layer.";
/*  89 */       str2 = "Set the value in the third line of your pattern file to the number of output neurons.";
/*  90 */       break;
/*     */     case 101:
/*  92 */       str1 = "A pattern value in the specified pattern file was wrong.";
/*  93 */       str2 = "Use pattern values that are 0 or 1.";
/*  94 */       break;
/*     */     case 102:
/*  96 */       str1 = "The number of patterns in the specified pattern file does not match the given number.";
/*  97 */       str2 = "Correct the number in the first line of your pattern file.";
/*  98 */       break;
/*     */     case 108:
/* 100 */       str1 = "There are too few maximum learning cycles defined.";
/* 101 */       str2 = "Increase the value of the maximum learning cycles or set it to -1 if the net should learn infinitely.";
/* 102 */       break;
/*     */     case 103:
/*     */     }
/*     */ 
/* 110 */     System.out.println("Error [" + paramInt + "]:\n\r" + str1);
/* 111 */     System.out.println("Try this: " + str2 + "\n\r");
/* 112 */     System.exit(0);
/*     */   }
/*     */ 
/*     */   public double square(double paramDouble)
/*     */   {
/* 117 */     return paramDouble * paramDouble;
/*     */   }
/*     */ 
/*     */   void setLearningRate(double paramDouble)
/*     */   {
/* 122 */     this.learningRate = paramDouble;
/*     */   }
/*     */ 
/*     */   double getLearningRate()
/*     */   {
/* 127 */     return this.learningRate;
/*     */   }
/*     */ 
/*     */   void setDisplayStep(int paramInt)
/*     */   {
/* 132 */     this.displayStep = paramInt;
/*     */   }
/*     */ 
/*     */   boolean displayNow()
/*     */   {
/* 137 */     return this.learningCycle % this.displayStep == 0;
/*     */   }
/*     */ 
/*     */   void resetTime() {
/* 141 */     this.startTime = System.currentTimeMillis();
/*     */   }
/*     */ 
/*     */   String getElapsedTime() {
/* 145 */     return String.valueOf((float)((System.currentTimeMillis() - this.startTime) * 0.001D));
/*     */   }
/*     */ 
/*     */   void setMaxLearningCycles(int paramInt)
/*     */   {
/* 151 */     if (paramInt == -1) {
/* 152 */       this.learnInfinitely = true;
/* 153 */       this.maxLearningCycles = -1;
/* 154 */       this.maxLearningCyclesString = "infinite";
/*     */ 
/* 151 */       return;
/*     */     }
/*     */ 
/* 157 */     if (paramInt > 0) {
/* 158 */       this.maxLearningCycles = paramInt;
/* 159 */       this.learnInfinitely = false;
/* 160 */       this.maxLearningCyclesString = String.valueOf(paramInt);
/*     */ 
/* 157 */       return;
/*     */     }
/*     */ 
/* 163 */     error(108);
/*     */   }
/*     */ 
/*     */   int getMaxLearningCycles()
/*     */   {
/* 169 */     return this.maxLearningCycles;
/*     */   }
/*     */ 
/*     */   void incLearningCycle()
/*     */   {
/* 174 */     this.learningCycle += 1;
/*     */   }
/*     */ 
/*     */   int getLearningCycle()
/*     */   {
/* 179 */     return this.learningCycle;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     NeuralNet
 * JD-Core Version:    0.6.0
 *//*     */ public static class Neuron
/*     */ {
/*     */   float input;
/*     */   float output;
/*     */   float outputError;
/*     */ 
/*     */   public Neuron()
/*     */   {
/*  33 */     init(0.0F);
/*     */   }
/*     */ 
/*     */   void init(float paramFloat)
/*     */   {
/*  40 */     this.input = paramFloat;
/*  41 */     this.output = paramFloat;
/*  42 */     this.outputError = 0.0F;
/*     */   }
/*     */ 
/*     */   void computeInput(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2)
/*     */   {
/*  49 */     this.input = 0.0F;
/*  50 */     for (int i = 0; i < paramArrayOfFloat1.length; i++) {
/*  51 */       if ((paramArrayOfFloat1[i] != 0.0F) && (paramArrayOfFloat2[i] != 0.0F))
/*  52 */         this.input += paramArrayOfFloat1[i] * paramArrayOfFloat2[i];
/*     */     }
/*  54 */     this.input += paramArrayOfFloat2[(paramArrayOfFloat2.length - 1)];
/*     */   }
/*     */ 
/*     */   void activateSigmoid()
/*     */   {
/*  61 */     this.output = (1.0F / (1.0F + (float)Math.exp(-this.input)));
/*     */   }
/*     */ 
/*     */   void computeOutputError(float paramFloat)
/*     */   {
/*  68 */     if ((this.output != 0.0F) && (this.output != 1.0F))
/*  69 */       this.outputError = ((paramFloat - this.output) * this.output * (1.0F - this.output));
/*     */   }
/*     */ 
/*     */   void computeOutputError(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2)
/*     */   {
/*  76 */     this.outputError = 0.0F;
/*  77 */     for (int i = 0; i < paramArrayOfFloat1.length; i++)
/*  78 */       if ((paramArrayOfFloat1[i] != 0.0F) && (this.output != 0.0F) && (this.output != 1.0F))
/*  79 */         this.outputError += paramArrayOfFloat1[i] * paramArrayOfFloat2[i] * this.output * (1.0F - this.output);
/*     */   }
/*     */ 
/*     */   float getInput()
/*     */   {
/*  87 */     return this.input;
/*     */   }
/*     */ 
/*     */   float getOutput()
/*     */   {
/*  94 */     return this.output;
/*     */   }
/*     */ 
/*     */   float getOutputError()
/*     */   {
/* 101 */     return this.outputError;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     Neuron
 * JD-Core Version:    0.6.0
 *//*     */
            public static class NeuronLayer
/*     */ {
/*     */   Neuron[] neuron;
/*     */ 
/*     */   NeuronLayer(int paramInt)
/*     */   {
/*  27 */     this.neuron = new Neuron[paramInt];
/*  28 */     for (int i = 0; i < paramInt; i++)
/*  29 */       this.neuron[i] = new Neuron();
/*     */   }
/*     */ 
/*     */   void setInput(Pattern paramPattern)
/*     */   {
/*  38 */     for (int i = 0; i < this.neuron.length; i++)
/*  39 */       this.neuron[i].init(paramPattern.getValue(i));
/*     */   }
/*     */ 
/*     */   void setInput(InputValue paramInputValue)
/*     */   {
/*  48 */     float[] arrayOfFloat = new float[3];
/*  49 */     arrayOfFloat[0] = paramInputValue.getX();
/*  50 */     arrayOfFloat[1] = paramInputValue.getY();
/*  51 */     arrayOfFloat[2] = paramInputValue.getZ();
/*  52 */     for (int i = 0; i < this.neuron.length; i++)
/*  53 */       this.neuron[i].init(arrayOfFloat[i]);
/*     */   }
/*     */ 
/*     */   void computeInput(NeuronLayer paramNeuronLayer, WeightMatrix paramWeightMatrix)
/*     */   {
/*  61 */     for (int i = 0; i < this.neuron.length; i++)
/*  62 */       this.neuron[i].computeInput(paramNeuronLayer.getOutput(), paramWeightMatrix.getInputWeights(i));
/*     */   }
/*     */ 
/*     */   void computeOutput()
/*     */   {
/*  71 */     for (int i = 0; i < this.neuron.length; i++)
/*  72 */       this.neuron[i].activateSigmoid();
/*     */   }
/*     */ 
/*     */   void computeLayerError(Pattern paramPattern)
/*     */   {
/*  81 */     for (int i = 0; i < this.neuron.length; i++)
/*  82 */       this.neuron[i].computeOutputError(paramPattern.getValue(i));
/*     */   }
/*     */ 
/*     */   void computeLayerError(NeuronLayer paramNeuronLayer, WeightMatrix paramWeightMatrix)
/*     */   {
/*  90 */     for (int i = 0; i < this.neuron.length; i++)
/*  91 */       this.neuron[i].computeOutputError(paramNeuronLayer.getLayerError(), paramWeightMatrix.getOutputWeights(i));
/*     */   }
/*     */ 
/*     */   float[] getOutput()
/*     */   {
/*  99 */     float[] arrayOfFloat = new float[this.neuron.length];
/* 100 */     for (int i = 0; i < this.neuron.length; i++) {
/* 101 */       arrayOfFloat[i] = this.neuron[i].getOutput();
/*     */     }
/* 103 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   float[] getLayerError()
/*     */   {
/* 110 */     float[] arrayOfFloat = new float[this.neuron.length];
/* 111 */     for (int i = 0; i < this.neuron.length; i++) {
/* 112 */       arrayOfFloat[i] = this.neuron[i].getOutputError();
/*     */     }
/* 114 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 121 */     return this.neuron.length;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     NeuronLayer
 * JD-Core Version:    0.6.0
 */
        /*     */ public static class Pattern
/*     */ {
/*     */   int[] pat;
/*     */   int nr;
/*     */   String type;
/*     */   String patString;
/*     */ 
/*     */   public Pattern(String paramString, String[][] paramArrayOfString)
/*     */   {
/*  73 */     this.patString = paramString;
/*  74 */     this.pat = new int[paramString.length() * paramArrayOfString[0][1].length()];
/*     */ 
/*  76 */     String str1 = new String();
/*  77 */     String str2 = new String();
/*  78 */     for (int i = 0; i < paramString.length(); i++) {
/*  79 */       for (int j = 0; j < paramArrayOfString.length; j++) {
/*  80 */         str2 = String.valueOf(paramString.charAt(i));
/*  81 */         if (str2.compareTo(" ") == 0) {
/*  82 */           str1 = str1 + paramArrayOfString[0][1];
/*  83 */           break;
/*     */         }
/*     */ 
/*  86 */         if (str2.compareTo(paramArrayOfString[j][0]) == 0) {
/*  87 */           str1 = str1 + paramArrayOfString[j][1];
/*  88 */           break;
/*     */         }
/*     */       }
/*     */     }
/*  92 */     char[] arrayOfChar = new char[str1.length()];
/*  93 */     arrayOfChar = str1.toCharArray();
/*  94 */     for (int k = 0; k < this.pat.length; k++)
/*  95 */       this.pat[k] = Character.digit(arrayOfChar[k], 10);
/*     */   }
/*     */ 
/*     */   String getPatternString()
/*     */   {
/* 102 */     return this.patString;
/*     */   }
/*     */ 
/*     */   float getValue(int paramInt)
/*     */   {
/* 107 */     return this.pat[paramInt];
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 112 */     return this.pat.length;
/*     */   }
/*     */ 
/*     */   boolean checkValues()
/*     */   {
/* 117 */     for (int i = 0; i < this.pat.length; i++) {
/* 118 */       if ((this.pat[i] < 0) || (this.pat[i] > 1))
/* 119 */         return false;
/*     */     }
/* 121 */     return true;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     Pattern
 * JD-Core Version:    0.6.0
 *//*     */ public static class WeightMatrix
/*     */ {
/*     */   float[][] weight;
/*     */   float[] bias;
/*     */   int size;
/*     */ 
/*     */   public WeightMatrix(int paramInt1, int paramInt2, boolean paramBoolean)
/*     */   {
/*  37 */     this.weight = new float[paramInt1][paramInt2];
/*  38 */     this.bias = new float[paramInt2];
/*  39 */     for (int i = 0; i < paramInt2; i++) {
/*  40 */       this.bias[i] = 1.0F;
/*     */     }
/*  42 */     this.size = (paramInt1 * paramInt2);
/*  43 */     if (paramBoolean == true)
/*  44 */       this.size += paramInt2;
/*     */   }
/*     */ 
/*     */   void init()
/*     */   {
/*  52 */     for (int i = 0; i < this.weight.length; i++) {
/*  53 */       for (int j = 0; j < this.weight[0].length; j++) {
/*  54 */         this.weight[i][j] = ((float)Math.random() * 2.0F - 1.0F);
/*     */       }
/*     */     }
/*  57 */     for (int j = 0; j < this.weight[0].length; j++)
/*  58 */       this.bias[j] = ((float)Math.random() * 2.0F - 1.0F);
/*     */   }
/*     */ 
/*     */   void init(float[][] paramArrayOfFloat)
/*     */   {
/*  67 */     for (int i = 0; i < this.weight.length; i++)
/*  68 */       for (int j = 0; j < this.weight[0].length; j++)
/*  69 */         this.weight[i][j] = paramArrayOfFloat[i][j];
/*     */   }
/*     */ 
/*     */   void init(InputValue[] paramArrayOfInputValue, int paramInt)
/*     */   {
/*     */     int i;
/*  78 */     switch (paramInt) {
/*     */     case 1:
/*  80 */       for (i = 0; i < this.weight[0].length; i++) {
/*  81 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*     */       }
/*  83 */       return;
/*     */     case 2:
/*  85 */       for (i = 0; i < this.weight[0].length; i++) {
/*  86 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*  87 */         this.weight[1][i] = paramArrayOfInputValue[i].getY();
/*     */       }
/*  89 */       return;
/*     */     case 3:
/*  91 */       for (i = 0; i < this.weight[0].length; i++) {
/*  92 */         this.weight[0][i] = paramArrayOfInputValue[i].getX();
/*  93 */         this.weight[1][i] = paramArrayOfInputValue[i].getY();
/*  94 */         this.weight[2][i] = paramArrayOfInputValue[i].getZ();
/*     */       }
/*  96 */       return;
/*     */     }
/*     */   }
/*     */ 
/*     */   void changeWeights(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2, double paramDouble)
/*     */   {
/*     */     float f;
/* 104 */     for (int i = 0; i < this.weight.length; i++) {
/* 105 */       if (paramArrayOfFloat1[i] != 0.0F) {
/* 106 */         for (int j = 0; j < this.weight[0].length; j++) {
/* 107 */           if ((paramArrayOfFloat2[j] != 1.0F) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 108 */             f = this.weight[i][j];
/* 109 */             this.weight[i][j] = 0.0F;
/* 110 */             this.weight[i][j] = (f + paramArrayOfFloat1[i] * paramArrayOfFloat2[j] * (1.0F - paramArrayOfFloat2[j]) * (float)paramDouble);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 115 */     for (int j = 0; j < this.bias.length; j++)
/* 116 */       if ((paramArrayOfFloat2[j] != 1.0F) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 117 */         f = this.bias[j];
/* 118 */         this.bias[j] = 0.0F;
/* 119 */         this.bias[j] = (f + paramArrayOfFloat2[j] * (1.0F - paramArrayOfFloat2[j]) * (float)paramDouble);
/*     */       }
/*     */   }
/*     */ 
/*     */   void changeWeightsKFM(float[] paramArrayOfFloat1, float[] paramArrayOfFloat2, double paramDouble)
/*     */   {
/* 128 */     for (int i = 0; i < this.weight.length; i++)
/* 129 */       for (int j = 0; j < this.weight[0].length; j++)
/* 130 */         if ((paramArrayOfFloat1[i] != this.weight[i][j]) && (paramArrayOfFloat2[j] != 0.0F)) {
/* 131 */           float f = this.weight[i][j];
/* 132 */           this.weight[i][j] = 0.0F;
/* 133 */           this.weight[i][j] = (f + paramArrayOfFloat2[j] * (paramArrayOfFloat1[i] - f) * (float)paramDouble);
/*     */         }
/*     */   }
/*     */ 
/*     */   float[] getInputWeights(int paramInt)
/*     */   {
/* 143 */     float[] arrayOfFloat = new float[this.weight.length + 1];
/* 144 */     for (int i = 0; i < this.weight.length; i++) {
/* 145 */       arrayOfFloat[i] = this.weight[i][paramInt];
/*     */     }
/* 147 */     arrayOfFloat[this.weight.length] = this.bias[paramInt];
/* 148 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   float[] getOutputWeights(int paramInt)
/*     */   {
/* 155 */     float[] arrayOfFloat = new float[this.weight[0].length];
/* 156 */     for (int i = 0; i < this.weight[0].length; i++) {
/* 157 */       arrayOfFloat[i] = this.weight[paramInt][i];
/*     */     }
/* 159 */     return arrayOfFloat;
/*     */   }
/*     */ 
/*     */   int size()
/*     */   {
/* 166 */     return this.size;
/*     */   }
/*     */ 
/*     */   float[][] getWeights()
/*     */   {
/* 173 */     return this.weight;
/*     */   }
/*     */ 
/*     */   float[] getBiases()
/*     */   {
/* 180 */     return this.bias;
/*     */   }
/*     */ }

/* Location:           D:\Projects\Java_projects\nnbpn\
 * Qualified Name:     WeightMatrix
 * JD-Core Version:    0.6.0
 */
        
        
        
        
        
        
        
        
        
        
}
// EOC BPN


This post has been edited by macosxnerd101: 10 November 2011 - 04:00 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 10 November 2011 - 04:02 PM

You don't need to add line numbers in yourself. The syntax highlighter handles that. Also, where is your stack trace? And post just your relevant code. It seems like a lot of code for what you're asking.
Was This Post Helpful? 0
  • +
  • -

#7 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 11 November 2011 - 02:45 AM

You do this in main:
BackpropagationNet bpn = (new BPN()).new BackpropagationNet();

But bpn is a local variable here. It is not the same bpn that is used in output().
output() uses the static bpn that is declared here:
public class BPN {

     static BackpropagationNet bpn; 

You never initialize that.

This post has been edited by Veitch: 11 November 2011 - 02:46 AM

Was This Post Helpful? 1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10649
  • View blog
  • Posts: 39,548
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.NullPointerException

Posted 11 November 2011 - 07:27 AM

Also, Java Collections like Vector are generic. Using Collections without generics is a deprecated practice and will produce a deprecation warning.
Was This Post Helpful? 1
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,359
  • Joined: 19-March 11

Re: Exception in thread "main" java.lang.NullPointerException

Posted 11 November 2011 - 09:14 AM

View PostVeitch, on 11 November 2011 - 04:45 AM, said:

You do this in main:
BackpropagationNet bpn = (new BPN()).new BackpropagationNet();

But bpn is a local variable here. It is not the same bpn that is used in output().
output() uses the static bpn that is declared here:
public class BPN {

     static BackpropagationNet bpn; 

You never initialize that.



Veitch has nailed it. One of the common "gotchas" in Java is the ability to "shadow" a variable, that is, to have multiple variables with the same name within a single class declaration. The way to understand this is this: there are two sorts of variables in a Java class, which we can call "fields" and "locals" for clarity. "Fields" pertain to the class level: they are properties of the class, they persist throughout the life of the class, and an object of the class can see all of its fields at all times. You can have only one field of a given name within a given class. "Locals" pertain to methods. They are created when the method is called, and they disappear when the method returns. They are not visible to other methods of the class: even in a multi-threaded environment, a given method is a closed world, and no other method can refer to its local variables. Local variables can have the same names as fields, and this is where the confusion arises. It is possible to declare two variables, a local and a field, which look like the same thing, as you do, and if you're not careful, you can lose track of which one is which. It is also possible to create confusion by creating multiple local variables, in multiple methods, with the same name.

There are some ordinary rules of namespace hygiene which will prevent this sort of confusion, if you understand why they are rules.

1) Names should always be descriptive and short, in that order. It is more important to communicate the purpose of each variable than to save keystrokes, and at the same it is important to do this as concisely as possible. Both of these rules are aimed at allowing the reader of your code (you or some other programmer) to keep the variables straight in their head. It is hard to remember what "String foo" represents, it's easier to remember what "String name" is going to stand for. Having said that, it is easier to keep "String oldName" in your head than "String theFormerNameOfThisObject". Thumb rule: when you choose a variable name, ask yourself if an intelligent person would guess correctly what it represents, given the context of the code and nothing else. Then, ask yourself if there is any shorter name which would pass the first test.

2) Variables should be declared as locally as possible. There is some debate about whether local variables should be declared at first use or at the head of their containing method, and there are good reasons supporting each position, but there is no reason to declare a variable at a higher scope than needed.

3) Minimize globals. This is a corollary to number 2: only variables which actually need to persist over the life of the object should be global, and methods should work only on their passed arguments whenever possible. For example, if you have a method which eliminates duplicate items from an array, you should pass the array as a parameter to the method, build a new array in the method, and return that new array, rather than writing a method which modifies some array which is a field of the class.

4) Locals should only shadow fields when they represent those fields in some way. The classic example of this is the constructor:

public Person(String name, Date birthday, String SSN, CakeType favoriteCake)
{
  this.name = name;
  this.birthday = birthday;
  this.SSN = SSN;
  this.favoriteCake = favoriteCake;
}


Here, we give the local variables (declared in the parameter list) the same names as the fields which they will be set to. We "unshadow" the fields by using the this keyword, which simply stands for "the current object". That is, this.name just means "the field name of the current object."


Put these rules into your head and try to understand why they're useful, and you'll avoid a lot of namespace issues in the future.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1