Source for de.webdings.jannis.neuralnet.PatternReader

   1: /* PatternReader.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Jannis.</p>
   3:  <p>Jannis is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Jannis is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Jannis; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.jannis.neuralnet;
  17: 
  18: import de.webdings.jannis.exceptions.PatternGiverReaderCommunicationException;
  19: 
  20: /**
  21:  * PatternReader is used to read the output 
  22:  * produced by a {@link NeuralNet}. The output is stored in
  23:  * an array of {@link Pattern}s. It can also export the 
  24:  * output to a {@link java.util.String} of '0's and '1's.
  25:  * 
  26:  * @author Stefan Thesing<br>
  27:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  28:  * @version 0.1 10.08.2005
  29:  */
  30: public class PatternReader {
  31:     //attribute
  32:     /**
  33:      * The output layer the output is read from
  34:      */
  35:     private Neuron[] outputLayer;
  36:     /**
  37:      * Well, the number of patterns to read...
  38:      */
  39:     protected int numberOfPatternsToRead;
  40:     /**
  41:      * The array of {@link Pattern}s the output is stored in
  42:      */
  43:     private Pattern[] pattern;
  44:     /**
  45:      * <code>counter</code> is used to keep track of the
  46:      * number of patterns that have already been read.
  47:      */
  48:     private int counter;
  49:     
  50:     //constructor
  51:     /**
  52:      * @param outputLayer
  53:      * @param numberOfPatternsToRead
  54:      */
  55:     public PatternReader(Neuron[] outputLayer, int numberOfPatternsToRead) {
  56:       this.outputLayer = outputLayer;
  57:       this.numberOfPatternsToRead = numberOfPatternsToRead;
  58:       this.pattern = new Pattern[numberOfPatternsToRead];
  59:       this.counter=0;
  60:     }
  61:     //methods
  62:     /**
  63:      * @return the number of patterns that have already been
  64:      * read
  65:      */
  66:     public int numberOfPatternsRead(){
  67:       return counter;
  68:     }
  69: 
  70:     /**
  71:      * reads the current output of the neural net
  72:      * @throws PatternGiverReaderCommunicationException
  73:      */
  74:     public void readPattern() throws PatternGiverReaderCommunicationException {
  75:      if(counter >= numberOfPatternsToRead) {
  76:        throw new PatternGiverReaderCommunicationException("An error occured when reading output from the" +
  77:                "output layer!");
  78:      } else {
  79:        int i;
  80:        pattern[counter] = new Pattern(new boolean[outputLayer.length]);
  81:        for (i = 0; i < outputLayer.length; ++i) {
  82:          pattern[counter].entries[i] = outputLayer[i].hasFired();
  83:        }
  84:        ++counter;
  85:      }
  86:     }
  87: 
  88:     /**
  89:      * @return a {@link java.lang.String} containing '0's
  90:      * and '1's that represents the read output patterns.
  91:      */
  92:     public String exportPattern(){
  93:       return PatternConverter.patternToStr(pattern, outputLayer.length);
  94:     }
  95:     
  96:     /**
  97:      * @return Returns the numberOfPatternsToRead.
  98:      */
  99:     public int getNumberOfPatternsToRead() {
 100:         return numberOfPatternsToRead;
 101:     }
 102:     /**
 103:      * @param numberOfPatternsToRead The numberOfPatternsToRead to set.
 104:      */
 105:     public void setNumberOfPatternsToRead(int numberOfPatternsToRead) {
 106:         this.numberOfPatternsToRead = numberOfPatternsToRead;
 107:     }
 108:     /**
 109:      * @return Returns the outputLayer.
 110:      */
 111:     public Neuron[] getOutputLayer() {
 112:         return outputLayer;
 113:     }
 114:     /**
 115:      * @param outputLayer The outputLayer to set.
 116:      */
 117:     public void setOutputLayer(Neuron[] outputLayer) {
 118:         this.outputLayer = outputLayer;
 119:     }
 120:     /**
 121:      * @return Returns the patterns read from the net.
 122:      */
 123:     public Pattern[] getPattern() {
 124:         return pattern;
 125:     }
 126:     
 127: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.