Source for de.webdings.jannis.neuralnet.Coordinator

   1: /* Coordinator.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 java.io.IOException;
  19: 
  20: import de.webdings.jannis.exceptions.PatternCreateException;
  21: import de.webdings.jannis.exceptions.PatternGiverReaderCommunicationException;
  22: import de.webdings.tools.files.TextFiles;
  23: 
  24: /**
  25:  * <code>Coordinator</code> is used to manage the activites of a 
  26:  * feed-forward neural net. In particular it coordinates 
  27:  * presenting the neural net with input patterns 
  28:  * (utilizing a {@link PatternGiver}) and registering the 
  29:  * output produced by the net (utilizing a 
  30:  * {@link PatternReader}.</p>
  31:  * <p>It also manages the segmentation of time that is 
  32:  * needed when simulating a parallel process on a serial
  33:  * machine. This means that a neuron that receives sufficient
  34:  * activation to cause it to fire doesn't fire immediately.
  35:  * It waits for the coordinator to tell it that all expected
  36:  * parts of the net activation has been received. This is 
  37:  * needed in almost every net architecture, but especially 
  38:  * in feed-forward nets, that feature layers that become
  39:  * active one after the other.<br>
  40:  * In its current state, <code>Coordinator</code> might be 
  41:  * able to do this for other net architectures, I haven't 
  42:  * given it much thought, yet. But it is designed for 
  43:  * feed-forward nets. 
  44:  * 
  45:  * @author Stefan Thesing<br>
  46:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  47:  * @version 0.1 10.08.2005
  48:  * 
  49:  */
  50: public class Coordinator {
  51:     /**
  52:      * The layers of the neural net to be coordinated.
  53:      */
  54:     //attributes
  55:     public Neuron[][] layers;
  56:     /**
  57:      * See {@link PatternGiver}
  58:      */
  59:     public PatternGiver giver;
  60:     /**
  61:      * See {@link PatternReader}
  62:      */
  63:     public PatternReader reader;
  64:     //constructors
  65:     /**
  66:      * @param net
  67:      * @param inputPattern
  68:      */
  69:     public Coordinator(NeuralNet net, Pattern[] inputPattern) {
  70:         this(net.getLayers(), inputPattern);
  71:     }
  72:     
  73:     
  74:     /**
  75:      * @param layers
  76:      * @param inputPattern
  77:      */
  78:     public Coordinator(Neuron[][] layers, Pattern[] inputPattern) {
  79:         this.layers = layers;
  80:         this.giver = new PatternGiver(layers[0], inputPattern);
  81:         this.reader = new PatternReader(layers[layers.length-1], giver.getPattern().length);
  82:     }
  83:     /**
  84:      * @param net
  85:      * @param fileNameInputPattern
  86:      * @throws IOException
  87:      * @throws PatternCreateException 
  88:      */
  89:     public Coordinator(NeuralNet net, String fileNameInputPattern) throws IOException, PatternCreateException {
  90:         this(net.getLayers(), fileNameInputPattern);
  91:     }
  92:     
  93:     /**
  94:      * @param layers
  95:      * @param fileNameInputPattern
  96:      * @throws IOException
  97:      * @throws PatternCreateException 
  98:      */
  99:     public Coordinator(Neuron[][] layers, String fileNameInputPattern) throws IOException, PatternCreateException  {
 100:       this.layers = layers;
 101:       this.giver = new PatternGiver(layers[0], fileNameInputPattern);
 102:       this.reader = new PatternReader(layers[layers.length-1], giver.getPattern().length);
 103:     }
 104: 
 105:     /**
 106:      * @param net
 107:      * @param giver
 108:      * @param reader
 109:      */
 110:     public Coordinator(NeuralNet net, PatternGiver giver, PatternReader reader) {
 111:         this(net.getLayers(), giver, reader);
 112:     }
 113:     
 114:     /**
 115:      * @param layers
 116:      * @param giver
 117:      * @param reader
 118:      */
 119:     public Coordinator(Neuron[][] layers, PatternGiver giver, PatternReader reader) {
 120:      this.layers = layers;
 121:      this.giver = giver;
 122:      this.reader = reader;
 123:     }
 124:     //methods
 125:     /**
 126:      * Starts presenting the net with the input pattern
 127:      * 
 128:      * @throws PatternGiverReaderCommunicationException
 129:      */
 130:     public void start() throws PatternGiverReaderCommunicationException {
 131:       int i,j;
 132:       giver.nextPattern();
 133:       for(i=1;i<layers.length;++i) {
 134:         for (j = 0; j < layers[i].length; ++j) {
 135:           if (layers[i][j].tresholdReached()) {
 136:             layers[i][j].fire();
 137:           }
 138:         }
 139:       }
 140:       reader.readPattern();
 141:       if(giver.numberSent() == reader.numberOfPatternsRead()) {
 142:         clearAll();
 143:         if(reader.numberOfPatternsRead() < reader.numberOfPatternsToRead){
 144:           start();
 145:         }
 146:       } else {
 147:         throw new PatternGiverReaderCommunicationException("There was an error in the communication between " +
 148:                 "PatternGiver and PatternReader!");
 149:       }
 150:     }
 151: 
 152:     /**
 153:      * Clears all residual activation and memory functions
 154:      * of the neurons in the coordinated net by calling the
 155:      * {@link de.webdings.neuralnet.Neuron#clear()}-method
 156:      * of the {@link de.webdings.neuralnet.Neuron}s 
 157:      * contained in the net.
 158:      */
 159:     public void clearAll() {
 160:       int i,j;
 161:       for(i=0;i<layers.length;++i) {
 162:        for(j=0;j<layers[i].length; ++j) {
 163:         layers[i][j].clear();
 164:        }
 165:       }
 166: 
 167:     }
 168: 
 169:     /**
 170:      * Saves the produced output pattern to a file of
 171:      * the specified filename.
 172:      * @param filename
 173:      * @throws IOException
 174:      */
 175:     public void savePattern(String filename) throws IOException {
 176:       TextFiles.writeToFile(filename, PatternConverter.patternToStr(reader.getPattern(), layers[layers.length-1].length));
 177:     }
 178: }

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