Source for de.webdings.jannis.neuralnet.nnml.NNMLToBiNet

   1: /* NNMLToBiNet.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.nnml;
  17: 
  18: import de.webdings.jannis.exceptions.NNMLException;
  19: import de.webdings.jannis.exceptions.NeuronTypeMismatchException;
  20: import de.webdings.jannis.neuralnet.BiNeuron;
  21: import de.webdings.jannis.neuralnet.Neuron;
  22: import de.webdings.tools.CharToFloat;
  23: import de.webdings.tools.CharToInt;
  24: import de.webdings.tools.StringSearch;
  25: 
  26: /**
  27:  * NNMLToBiNet is used to construct a neural net with
  28:  * BiNeurons from a NNML representation. 
  29:  * 
  30:  * @author Stefan Thesing<br>
  31:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  32:  * @version 0.3 11.08.2005
  33:  */
  34: public class NNMLToBiNet extends NNMLToNet {
  35: 
  36:     /* (non-Javadoc)
  37:      * @see de.webdings.jannis.neuralnet.nnml.NNMLToNet#convert(java.lang.String)
  38:      */
  39:     /**
  40:      * This overrides the method inherited from {@link NNMLToNet} .
  41:      * It calls {@link NNMLToBiNet#convertToBiNet(String s)}, so it
  42:      * actually returns an array containing layers of 
  43:      * BiNeurons. 
  44:      * @throws NNMLException
  45:      * @throws NeuronTypeMismatchException 
  46:      */
  47:     public Neuron[][] convertToNet(String s) throws NNMLException, NeuronTypeMismatchException  {
  48:         return this.convertToBiNet(s);
  49:     }
  50: 
  51:     private BiNeuron parseNeuron(String neuron) {
  52:         return new BiNeuron(CharToFloat.convert(
  53:                 neuron.substring(neuron.indexOf("=")+2, 
  54:                 neuron.indexOf(".")+2)));
  55:     }
  56: 
  57:     private BiNeuron[] parseLayer(String layerStr) {
  58:         String[] s = new String[0];
  59:         String[] buffer;
  60:         while(layerStr.indexOf("<neuron") != -1) {
  61:           buffer = new String[s.length+1];
  62:           for(int i=0;i<s.length;++i) {
  63:             buffer[i] = s[i];
  64:           }
  65:           buffer[s.length] = layerStr.substring(layerStr.indexOf("<neuron"), layerStr.indexOf("/>"));
  66:           layerStr = layerStr.substring(layerStr.indexOf("/>")+1);
  67:           s = buffer;
  68:         }
  69:         //Construct an array for the layer
  70:         BiNeuron[] layer = new BiNeuron[s.length];
  71:         //fill the array
  72:         for(int i=0;i<layer.length;++i) {
  73:           layer[i] = parseNeuron(s[i]);
  74:         }
  75:         return layer;
  76:       }
  77: 
  78:       private BiNeuron[][] parseLayers(String layersStr) {
  79:         String[] s = new String[0];
  80:         String[] buffer;
  81:         //fill the array with the strings representing
  82:         //each layer
  83:         while(layersStr.indexOf("<layer>") != -1) {
  84:           buffer = new String[s.length+1];
  85:           for(int i=0;i<s.length;++i) {
  86:             buffer[i] = s[i];
  87:           }
  88:           buffer[s.length] = layersStr.substring(layersStr.indexOf("<layer>"), layersStr.indexOf("</layer>"));
  89:           layersStr = layersStr.substring(layersStr.indexOf("</layer>")+1);
  90:           s = buffer;
  91:         }
  92:         //construct array for the layers
  93:         BiNeuron[][] layers = new BiNeuron[s.length][];
  94:         //fill the array
  95:         for(int i=0;i<layers.length;++i) {
  96:             layers[i] = parseLayer(s[i]);
  97:         }
  98:         return layers;
  99:       }
 100: 
 101:       private void parseSynapses(String synapses, BiNeuron[][] schichten) throws NNMLException  {
 102:         try {
 103:             while(synapses.indexOf("<synapse") != -1) {
 104:               synapses = synapses.substring(synapses.indexOf("<synapse"));
 105:               String weightStr = synapses.substring(synapses.indexOf("=")+2, synapses.indexOf(".")+2);
 106:               float weight = CharToFloat.convert(weightStr);
 107:               synapses = synapses.substring(synapses.indexOf("<source"));
 108:               int sourceLayerID = CharToInt.convert(synapses.charAt(synapses.indexOf("=")+2));
 109:               synapses = synapses.substring(synapses.indexOf("neuronID"));
 110:               int sourceNeuronID = CharToInt.convert(synapses.charAt(synapses.indexOf("=")+2));
 111:               synapses = synapses.substring(synapses.indexOf("<target"));
 112:               int targetLayerID = CharToInt.convert(synapses.charAt(synapses.indexOf("=")+2));
 113:               synapses = synapses.substring(synapses.indexOf("neuronID"));
 114:               int targetNeuronID = CharToInt.convert(synapses.charAt(synapses.indexOf("=")+2));
 115: 
 116:               schichten[sourceLayerID][sourceNeuronID].addConnection(schichten[targetLayerID][targetNeuronID], weight);
 117:             }
 118:         } catch (NumberFormatException e) {
 119:             throw new NNMLException("Error in parsing a neuron ID or layer ID", e.getCause());
 120:         }
 121:       }
 122: 
 123:     /**
 124:      * See {@link #convertToNet(String)}
 125:      * @param s string containing NNML representation of the net
 126:      * @return array containing layers of BiNeurons
 127:      * @throws NNMLException
 128:      * @throws NeuronTypeMismatchException 
 129:      */
 130:     public BiNeuron[][] convertToBiNet(String s) throws NNMLException, NeuronTypeMismatchException  {
 131:         String layersStr;
 132:         String synapses;
 133:         String typeStr;
 134:         int beginType = s.indexOf("<neural_net type=");
 135:         int beginLayers = s.indexOf("<layer>");
 136:         typeStr = s.substring(beginType, beginLayers-1);
 137:         if(!StringSearch.stringContains(typeStr, "type=\"bineuron\"")){
 138:             throw new NeuronTypeMismatchException("Unsupported neuron type!");
 139:         }
 140:         int beginSynapsen = s.indexOf("<synapse");
 141:         if(beginLayers < 0 || beginSynapsen <0) {
 142:           throw new NNMLException("This is not valid NNML!");
 143:         } else {
 144:           layersStr = s.substring(beginLayers, beginSynapsen-1);
 145:           synapses = s.substring(beginSynapsen);
 146:           BiNeuron[][] schichten = parseLayers(layersStr);
 147:           parseSynapses(synapses, schichten);
 148:           return schichten;
 149:         }
 150:       }
 151:     
 152: }

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