/* Copyright (c) 2005-2007 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn.v15.neuralnet; import java.util.Random; /** * Behold the noble neuron. * * It takes the summation of the product of the input values times their weights * and runs that through the activation function (g). * * Put another way: * output = g( sum(W_i * o_i) ) * * sum() sum over all inputs * W_i - weight of input i * o_i - value from input i * * There is no reason for a user of this package to muck directly with neurons. * They should be used via the Brain class. However, I've left the Neuron class * unprotected in case someone wants to use them directly. * * @author Joseph Gleason * */ public class Neuron implements NetworkSource { /** * */ private static final long serialVersionUID = 1535769647774627461L; private NetFunction FunctG; private long StateSerial_Value; private long StateSerial_Sum; private double Value; private double BackAccumulator; private double Sum; //private TreeMap M; private Random R; private double InputWeights[]; private NetworkSource Inputs[]; private int NumInputs; public Neuron(NetFunction g, Random r) { StateSerial_Value=-1; StateSerial_Sum=-1; FunctG=g; BackAccumulator=0; Sum=0; R=r; NumInputs=0; } /** * Get the output value * @param state_serial * @return the output for this neuron */ public double getValue(long state_serial) { if (state_serial != StateSerial_Value) { double s=getSum(state_serial); Value=FunctG.functionG(s); StateSerial_Value=state_serial; } return Value; } /** * Get the output of the sumation of all the inputs*weights before the * translation function is run. * * @param state_serial * @return the sum */ public double getSum(long state_serial) { if (state_serial != StateSerial_Sum) { Sum=0.0; for(int i=0; i 25.0) w=25.0; if (w < -25.0) w=-25.0; InputWeights[i]=w; } } /* push changes to next layer */ for(int i=0; i