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

   1: /* BiNetToNNML.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.NeuronNotFoundException;
  19: import de.webdings.jannis.exceptions.NeuronTypeMismatchException;
  20: import de.webdings.jannis.neuralnet.NeuralNet;
  21: import de.webdings.jannis.neuralnet.Neuron;
  22: import de.webdings.jannis.neuralnet.NeuronIDFinder;
  23: import de.webdings.jannis.neuralnet.Synapse;
  24: 
  25: /**
  26:  * BiNetToNNML is used to convert a neural net of
  27:  * BiNeurons to a String containing a NNML representation
  28:  * of that net. The purpose of this String is to be written
  29:  * to a file. The output XML is written according to NNML 0.3
  30:  * 
  31:  * @author Stefan Thesing<br>
  32:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  33:  * @version 0.1 11.08.2005
  34:  */
  35: public class BiNetToNNML extends NetToNNML {
  36: 
  37:     /* (non-Javadoc)
  38:      * @see de.webdings.jannis.neuralnet.nnml.NetToNNML#generateString(de.webdings.jannis.neuralnet.NeuralNet)
  39:      */
  40:     public String generateString(NeuralNet net) throws NeuronTypeMismatchException, NeuronNotFoundException {
  41:         if(net.getType().equals("de.webdings.jannis.neuralnet.BiNeuron")) {
  42:             return this.generateString(net.getLayers());
  43:         } else {
  44:             throw new NeuronTypeMismatchException();
  45:         }
  46:     }
  47:     /* (non-Javadoc)
  48:      * @see de.webdings.jannis.neuralnet.nnml.NetToNNML#generateString(de.webdings.jannis.neuralnet.Neuron[][])
  49:      */
  50:     public String generateString(Neuron[][] layers) throws NeuronTypeMismatchException, NeuronNotFoundException {
  51:         NeuralNet net = new NeuralNet(layers);
  52:         String type = null;
  53:         if(net.getType().equals("de.webdings.jannis.neuralnet.BiNeuron")) {
  54:             type = "bineuron";
  55:         } else {
  56:             throw new NeuronTypeMismatchException("Unknown neuron type");
  57:         }
  58:         this.finder = new NeuronIDFinder(layers);
  59:         s = new String("<?xml version=\"1.0\"?>\n<!DOCTYPE neural_net SYSTEM \"nnml.dtd\">\n");
  60:         s = s+ "<neural_net type=\"" + type + "\">\n";
  61:         int i;
  62:         for (i = 0; i < layers.length; ++i) {
  63:           nextLayer(layers[i]);
  64:         }
  65:         for (i = 0; i < layers.length; ++i) {
  66:           for(int j=0;j<layers[i].length;++j) {
  67:             for(int k=0;k<layers[i][j].getConnections().length;++k) {
  68:               this.nextSynapse(layers[i][j].getConnections()[k]);
  69:             }
  70:           }
  71:         }
  72:         s = s+ "</neural_net>";
  73:         return s;
  74:       
  75:     }
  76:     
  77:     private void nextSynapse(Synapse synapse) throws NeuronNotFoundException {
  78:         s = s+ "<synapse weight=\"" + synapse.getWeight() + "\">\n";
  79:         s = s+ "<source layerID=\"" + finder.getLayerID(synapse.getSource())
  80:             + "\" neuronID=\"" + finder.getNeuronID(synapse.getSource()) + "\"/>\n";
  81:         s = s+ "<target layerID=\"" + finder.getLayerID(synapse.getTarget())
  82:             + "\" neuronID=\"" + finder.getNeuronID(synapse.getTarget()) + "\"/>\n";
  83:         s = s+ "</synapse>\n";
  84:       }
  85: 
  86:       private void nextNeuron(Neuron neuron)  {
  87:         s = s+ "<neuron sigma=\"" + neuron.getActivationFunction() + "\"/>\n";
  88:       }
  89: 
  90:       private void nextLayer(Neuron[] layer)  {
  91:         int j;
  92:         s = s+ "<layer>\n";
  93:         for(j=0;j<layer.length;++j) {
  94:           nextNeuron(layer[j]);
  95:         }
  96:         s = s+ "</layer>\n";
  97:       }
  98:      
  99:       
 100: }

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