Source for de.webdings.jannis.neuralnet.Neuron

   1: /* Neuron.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: 
  19: /**
  20:  * Neurons are the basic units that compose neural nets. 
  21:  * The abstract class Neuron provides functionalities that 
  22:  * all types of
  23:  * neurons have in common. It is an abstract class
  24:  * for it does not provide an implementation of an
  25:  * activation function or a treshhold value.
  26:  * 
  27:  * @author Stefan Thesing<br>
  28:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  29:  * @version 0.1 10.08.2005
  30:  */
  31: public abstract class Neuron {
  32:     //attributes
  33:     /** activation a of the neuron. Can be used in 
  34:      * calculations for the activation funtion.
  35:      */
  36:      protected float a;
  37: 
  38:     /** net represents the summed up input reaching 
  39:      * the neuron.
  40:      * */
  41:      protected float net;
  42: 
  43:     /** connections is an array containing all outgoing
  44:      * {@link Synapse synaptical} connections of this 
  45:      * neuron.
  46:      */
  47:      protected Synapse[] connections;
  48:      
  49:     /**
  50:      * <code>numberOfConnections</code> represents
  51:      * the amount of outgoing connections of this 
  52:      * neuron. It is identical to the size of {@link
  53:      * #connections}
  54:      */
  55:      protected int numberOfConnections;
  56: 
  57:     /** fired returns true if the neuron has fired
  58:      * during the last time the net was presented with
  59:      * input.
  60:      */
  61:      protected boolean fired;
  62: 
  63:     /** shouldHaveFired can be used by learning methods.
  64:      * {@link Teacher) uses it, for example.
  65:      */
  66:      protected boolean shouldHaveFired;
  67: 
  68:     //constructor
  69:    /**
  70:     * Constructs a Neuron with default values:<br>
  71:     * a=0<br>
  72:     * net=0<br>
  73:     * fired=false<br>
  74:     * shouldHaveFired=<code>false</code><br>
  75:     * numberOfConnections=0
  76:     */
  77:     public Neuron() {
  78:         a = 0.0f;
  79:         net = 0.0f;
  80:         numberOfConnections = 0;
  81:         connections = new Synapse[numberOfConnections];
  82:         fired = false;
  83:         shouldHaveFired = false;
  84:     }
  85: 
  86:    //operations
  87:     /**
  88:      * connects this neuron with another one (target) by constructing
  89:      * a new {@link Synapse} with a randomly generated weight ranging
  90:      * from -0.2 and +0.2
  91:      * @param target the target neuron
  92:      * @see WeightRandomizer#generateRandomWeight()
  93:      */
  94:     public void addConnection(Neuron target) {
  95:         WeightRandomizer wr = new WeightRandomizer();
  96:         this.addConnection(target, wr.generateRandomWeight());
  97:     }
  98:     
  99:    /**
 100:     * connects this neuron with another one (target)
 101:     * by constructing a new {@link Synapse} with
 102:     * the specified weight.
 103:     * @param target the target neuron
 104:     * @param weight weight of the synaptical connection
 105:     */
 106:     public void addConnection(Neuron target, float weight){
 107:         this.addConnection(new Synapse(this, target, weight));
 108:     }
 109:    /**
 110:     * Adds a existing {@link Synapse} to the
 111:     * connections of this neuron.
 112:     * @param synapse the {@link Synapse} to add
 113:     */
 114:     public void addConnection(Synapse synapse) {
 115:         int i;
 116:         Synapse[] newConnections = new Synapse[numberOfConnections+1];
 117:         if(numberOfConnections>0) {
 118:             for(i=0;i<numberOfConnections;++i) {
 119:                 newConnections[i] = connections[i];
 120:             }
 121:         }
 122:         newConnections[numberOfConnections] = synapse;
 123:         ++numberOfConnections;
 124:         connections = newConnections;
 125:     }
 126:    /**
 127:     * represents the activations function of the 
 128:     * neuron. This is an abstract method that must
 129:     * be replaced by any concrete subclass of Neuron.<br>
 130:     * The simplest concrete implementation of this
 131:     * method is a "binary treshold function" (a 
 132:     * treshold value) found in {@link BiNeuron}.
 133:     * @return true if the treshold is reached.
 134:     */
 135:     public abstract boolean tresholdReached();
 136:    
 137:     /**
 138:      * @return a String representation of the activation function
 139:      * implemented in {@link #tresholdReached()}
 140:      */
 141:     public abstract String getActivationFunction();
 142:     
 143:    /**
 144:     * directly causes the neuron to fire.
 145:     */
 146:     public void fire() {
 147:         int i;
 148:         for(i=0;i<numberOfConnections;++i) {
 149:             connections[i].target.gatherActivation(connections[i].weight);
 150:         }
 151:         net=0;
 152:         fired = true;
 153:     }
 154: 
 155:    /**
 156:     * Gathers the net input.
 157:     * @param weight 
 158:     */
 159:     public void gatherActivation(float weight) {
 160:         net = net + weight;
 161:     }
 162: 
 163:    /**
 164:     * Sets all attributes (exclusive connections and
 165:     * numberOfConnections) back to default:
 166:     * a=0<br>
 167:     * net=0<br>
 168:     * fired=false<br>
 169:     * shouldHaveFired=false<br>
 170:     */
 171:    public void clear() {
 172:     a=0;
 173:     net=0;
 174:     fired=false;
 175:     shouldHaveFired=false;
 176:    }
 177:    /**
 178:     * @return Returns the connections.
 179:     */
 180:    public Synapse[] getConnections() {
 181:        return connections;
 182:    }
 183:     /**
 184:      * @param connections The connections to set.
 185:      */
 186:     public void setConnections(Synapse[] connections) {
 187:         this.connections = connections;
 188:     }
 189:     /**
 190:      * @return Returns the fired.
 191:      */
 192:     public boolean hasFired() {
 193:         return fired;
 194:     }
 195:     /**
 196:      * @param fired The fired to set.
 197:      */
 198:     public void setFired(boolean fired) {
 199:         this.fired = fired;
 200:     }
 201:     /**
 202:      * @return Returns the shouldHaveFired.
 203:      */
 204:     public boolean getShouldHaveFired() {
 205:         return shouldHaveFired;
 206:     }
 207:     /**
 208:      * @param shouldHaveFired The shouldHaveFired to set.
 209:      */
 210:     public void setShouldHaveFired(boolean shouldHaveFired) {
 211:         this.shouldHaveFired = shouldHaveFired;
 212:     }
 213:     /**
 214:      * @return Returns the a.
 215:      */
 216:     public float getA() {
 217:         return a;
 218:     }
 219:     /**
 220:      * @return Returns the net.
 221:      */
 222:     public float getNet() {
 223:         return net;
 224:     }
 225:     /**
 226:      * @return Returns the numberOfConnections.
 227:      */
 228:     public int getNumberOfConnections() {
 229:         return numberOfConnections;
 230:     }
 231: }

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