Skip to content
Snippets Groups Projects
Commit 4b6ac86e authored by Jeffrey Phillips Freeman's avatar Jeffrey Phillips Freeman :boom:
Browse files

Java dANN 0.1 Alpha Release Canidate 2

git-svn-id: svn://svn.syncleus.com/dANN/tags/java_dann-0.1-alpha-RC2@26 6ae8b97b-f314-0410-8212-aecf10b92ded
parents
No related branches found
No related tags found
No related merge requests found
Showing with 1629 additions and 0 deletions
[submodule "lib"]
path = lib
url = git://git.syncleus.com/dANN-core-lib.git
Open Source Community License
Type C, Version 1.0
(c) Jeffrey Phillips Freeman, 2004
A. DEFINITIONS
For the purpose of this license we will set forth certain definitions
for the ease of communications:
"you" - Any person granted use of source code under this license by the
inclusion of this license in the source by the author and copyright holder.
"program" - The Source code this license is bundled as well as any executables
and derivative work under copyright law. Any program this license is assigned
to, and means either the Program or any derivative work under copyright law.
Meaning any work containing the program or a portion of it, modified or
verbatim, and/or translated into other languages.
"Authors' Recursive License" - A special license granted to certain
contributors of the program. See section F below.
"this license" - The Open Source Public License - Type C Version 1.0 or any
later version at your option, as published by Jeffrey Phillips Freeman. All
future versions will be in the same spirit but will correct any legal
oversights addressed by earlier versions.
"source code" - The preferred form of the program for making modifications to
it that make up the program in part or in whole. For executables this means the
source code, plus any modules that are needed to compile it, including scripts
that control its compiling. This need not include components not normally
needed for compiling such as operating system libraries, or the kernel.
"distribute" - The act of distributing, publishing, or otherwise making the
program available to one or more persons, in whole or in part.
"entity" - Any one of the of the following: person, group of persons, company
or entity under law.
B. SCOPE
This license addresses copying, distribution, execution and modification of the
source code. Everything else is outside the scope of this license and no rights
are granted either written or implied outside it's scope.
You may not copy, distribute, execute, or sub-license except under the terms
and conditions of this license. Any attempt to do so outside of this license is
void and will immediately and automatically terminate any rights you have under
this license. This license is guaranteed not to be terminated so long as you
follow the rules and conditions of this license.
You are not required to accept this license since you have not signed it.
However if you do not accept this license then all privileges and rights
granted herein is void. Unless specifically granted through another legal
license you shall not have the right to copy, distribute, execute or
sub-license this program in part or in whole.
If for any reason you are not able to follow the conditions of this license
including, but not limited to, court order, agreement, or law, then you are not
excused from the conditions of this license. If for any reason you cannot
follow the terms and conditions of this license then you are not given the
privileges associated with them. For example, if you cannot provide the source
code to accompany the executable you cannot distribute the program.
C. EXECUTION
This License does not restrict execution of the program, including any uses of
it's output so long as the terms and conditions of this license are upheld. The
right to execution is immediately revoked for any program under this license
found in violation of the terms.
D. DISTRIBUTION
You may copy and distribute the program according to the terms of this license
so long as you perform at least one of the following:
1. Offer the source code to be distributed through the same medium by which
the program is distributed at no additional cost except for the cost of
offering and distributing the source code.
2. Provide the program with a written offer for physical distribution good for
at least one year or until the distributing entity no longer exists, whichever
comes first.
3. Provide the program bundled with the source code. If the program is offered
without the bundled source code then the source code must still be made
available through one of the two options above.
E. LICENSE PROPOGATION
You may copy and distribute the program verbatim, according to the terms and
conditions of this license, so long as all copyright and warranty disclaimers
remain unchanged and all references to this license remain. All copies of the
source code and its individual pages must appropriately and in plan sight
contain a notice of this license. The program must be distributed with a copy
of this license and all notices should point to the included copy.
Propagation of this license, and the authors' recursive license below is not
effected by mere aggregation of the program with other work on the same storage
medium. The propagation of these licenses only applies to work which were done
due to knowledge of the source code or output or were intended at some point in
its design to interact primarily with the program. This includes, but is not
limited to, libraries designed for the program, source code that is compiled
using the source code or portions of it, or programs designed specifically to
interact with this program closely invoking its execution with arguments or
inputs.
Any time you distribute the program the recipient is automatically granted this
license from the original licensor. You may not impose any greater restrictions
to the recipient and it is not your responsibility to enforce this compliance
of this license by its recipients.
You may copy and distribute, according to the terms and conditions of this
license, any modified version of the program or portion of it, creating a
derivative work, as long as all of the following conditions are met:
1. You must record what files you have changed and when they were changed and
include that record in the source code. Any files part of the source code which
were changed must have clear and obvious notices of what was changed. You can
record your changes in a external file included with the source code so long as
the files which were changed contain a clear and obvious notice pointing to the
external file.
2. You must allow any code you distribute that is derived form the program or
contains the program, in whole or in part, to be governed under this license.
3. You must grant an Author's Recursive License, as described in the section
Authors' Recursive License to any copyright holder of any source code used in
the program that your modifications interact with. You also grant the same
Authors' Recursive License to anyone that copyright holder had to issue an
Authors' Recursive License to in order to publish modifications. Interactions
are included, but not limited to, calling of functions, calling or using parts
of a class, using variables provided by the interface, and any similar
interaction.
4. If any notices are displayed in the output of the program making a notice of
warranty or license they must remain in tact. If the nature of the program is
changed such that the output containing any such notices are no longer used an
equivalent notice must be placed elsewhere in the output in plain sight. The
only exception to this is if the program will no longer have any output at all,
or the output it produces is inappropriate to contain such a notice.
F. AUTHORS' RECURSIVE LICENSE
The Recursive Authors' License gives the following rights and privileges to the
individuals explained in section E subsection 3 above. The license is as
follows, ending at the end of this section:
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the any copyright notices, and a
copy of this license, and any references to this license in the source code.
2. Redistributions in binary form must reproduce any copyright notices, and a
copy of this license.
3. The names of its contributors and copyright holders may not be used to
endorse or promote products derived from this software without specific prior
written permission.
4. The disclaimer in section G of this license is accepted.
G. DISCLAIMER
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
import java.util.Random;
/**
* Largly under developed this class will contain or generate a basic list of
* properties used by the individual neurons, and layers. This will be the Class
* genetic algorithms should inherit from.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public class DNA
{
// <editor-fold defaultstate="collapsed" desc="Attributes">
/**
* The learning rate is used by neurons to determine how influential each
* training set will be on its current state.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.Synapse#learnWeight
*/
public double learningRate = 0.02;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
/**
* Creates a new instance of DNA with default values.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public DNA()
{
}
// </editor-fold>
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* This is a special type of neuron that receives input.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
* @see com.syncleus.dann.OutputNeuronProcessingUnit
*/
public class InputNeuronProcessingUnit extends NeuronProcessingUnit
{
/**
* Holds the current input value for this neuron<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected double input = 0;
/**
* Creates a new instance of InputNeuronProcessingUnit<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param OwnedDNAToSet This dna class will determine the various properties
* of the layer.
*/
public InputNeuronProcessingUnit(DNA OwnedDNAToSet)
{
super(OwnedDNAToSet);
}
/**
* This method sets the current input on the neuron.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inputToSet The value to set the current input to.
*/
public void setInput(double inputToSet)
{
this.input = inputToSet;
}
/**
* Refreshes the output of the neuron based on the current input<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public void propogate()
{
//calculate the current input activity
this.activity = 0;
Object[] SynapseArray = super.sourceSynapses.toArray();
for(int Lcv = 0; Lcv < SynapseArray.length; Lcv++)
{
Synapse CurrentSynapse = (Synapse) SynapseArray[Lcv];
this.activity += CurrentSynapse.getOutput();
}
//Add the bias to the activity
super.activity += super.biasWeight;
//add the input to the activity
super.activity += this.input;
//calculate the activity function and set the result as the output
super.setOutput( super.activationFunction() );
}
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
import java.util.ArrayList;
/**
* A special ProcessingUnit which can contain other ProcessingUnits as children.
* <BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public class LayerProcessingUnit extends ProcessingUnit
{
// <editor-fold defaultstate="collapsed" desc="Attributes">
/**
* This contains all the neurons considered to be a part of this layer. Any
* one neuron can only belong to one layer. But one layer owns many neurons.
* <BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public ArrayList<ProcessingUnit> children = new ArrayList<ProcessingUnit>();
/**
* This will determine most of the properties of the layer.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
DNA ownedDNA;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
/**
* Creates a new instance of LayerProcessingUnit<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param OwnedDNAToSet This dna class will determine the various properties
* of the layer.
*/
public LayerProcessingUnit(DNA OwnedDNAToSet)
{
this.ownedDNA = OwnedDNAToSet;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Topology Manipulation">
/**
* Adds another processing unit to this layer.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param toAdd the ProcessingUnit to add.
*/
public void add(ProcessingUnit toAdd)
{
this.children.add(toAdd);
}
/**
* Connects all the ProcessingUnits in this layer recursivly to all the
* ProcessingUnits in another layer.
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param toConnectTo This is the layer the neurons will be connecting
* to.
* @see com.syncleus.dann.LayerProcessingUnit#connectTo
*/
public void connectAllTo(ProcessingUnit toConnectTo)
{
for( ProcessingUnit currentChild : this.children )
{
if( currentChild instanceof LayerProcessingUnit )
{
((LayerProcessingUnit)currentChild).connectAllTo(toConnectTo);
}
else
{
if( toConnectTo instanceof LayerProcessingUnit )
{
ArrayList<ProcessingUnit> toConnectToChildren = ((LayerProcessingUnit)toConnectTo).getChildrenRecursivly();
for( ProcessingUnit currentOut : toConnectToChildren )
{
currentChild.connectTo(currentOut);
}
}
else
{
currentChild.connectTo(toConnectTo);
}
}
}
}
/**
* Obtains all the ProcessingUnits owned recursivly excluding
* LayerProcessingUnits.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private ArrayList<ProcessingUnit> getChildrenRecursivly()
{
ArrayList<ProcessingUnit> returnList = new ArrayList<ProcessingUnit>();
for( ProcessingUnit currentChild : this.children )
{
if( currentChild instanceof LayerProcessingUnit )
returnList.addAll(((LayerProcessingUnit)currentChild).getChildrenRecursivly());
else
returnList.add(currentChild);
}
return returnList;
}
/**
* Randomly returns one of the children.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return A randomly selected child.
*/
private ProcessingUnit getRandomChild()
{
return this.children.get(this.random.nextInt(this.children.size()));
}
/**
* This causes a random child ProcessingUnit to create a connection with
* the specified ProcessingUnti.
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outUnit The ProcessingUnit to connect to.
* @see com.syncleus.dann.ProcessingUnit#connectFrom
*/
public void connectTo(ProcessingUnit outUnit)
{
this.getRandomChild().connectTo(outUnit);
}
/**
* This will cause the incomming connection synapse to be passed randomly to
* one of its children.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The synapse to connect from.
* @see com.syncleus.dann.ProcessingUnit#connectTo
*/
protected void connectFrom(Synapse inSynapse)
{
this.getRandomChild().connectFrom(inSynapse);
}
/**
* Causes the ProcessingUnit to disconnect all outgoing connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#disconnectAllSources
* @see com.syncleus.dann.ProcessingUnit#disconnectAll
*/
public void disconnectAllDestinations()
{
for( ProcessingUnit currentChild : this.children )
{
currentChild.disconnectAllDestinations();
}
}
/**
* Causes the ProcessingUnit to disconnect all incomming connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#disconnectAllDestinations
* @see com.syncleus.dann.ProcessingUnit#disconnectAll
*/
public void disconnectAllSources()
{
for( ProcessingUnit currentChild : this.children )
{
currentChild.disconnectAllSources();
}
}
/**
* Disconnects from a perticular outgoing connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The outgoing synapse to disconnect from.<BR>
* @see com.syncleus.dann.ProcessingUnit#removeSource
* @throws SynapseNotConnectedException Thrown if the specified synapse is not
* currently connected.
*/
public void disconnectDestination(Synapse outSynapse) throws SynapseNotConnectedException
{
boolean found = true;
for( ProcessingUnit currentChild : this.children )
{
found = true;
try
{
currentChild.disconnectDestination(outSynapse);
}
catch(SynapseNotConnectedException caughtException)
{
found = false;
}
if( found == true )
return;
}
}
/**
* Disconnects from a perticular incomming connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to disconnect from.<BR>
* @see com.syncleus.dann.ProcessingUnit#removeDestination
* @throws SynapseNotConnectedException Thrown if the specified synapse is not
* currently connected.
*/
public void disconnectSource(Synapse inSynapse) throws SynapseNotConnectedException
{
boolean found = true;
for( ProcessingUnit currentChild : this.children )
{
found = true;
try
{
currentChild.disconnectSource(inSynapse);
}
catch(SynapseNotConnectedException caughtException)
{
found = false;
}
if( found == true )
return;
}
}
/**
* Called internally to facilitate the removal of a connection. It removes
* the specified synapse from memory assuming it has already been
* disconnected<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.ProcessingUnit#disconnectSource
* @throws SynapseDoesNotExistException Thrown if the specified synapse
* does not exist as a source synapse.
*/
protected void removeDestination(Synapse outSynapse) throws SynapseDoesNotExistException
{
boolean found = true;
for( ProcessingUnit currentChild : this.children )
{
found = true;
try
{
currentChild.removeDestination(outSynapse);
}
catch(SynapseDoesNotExistException caughtException)
{
found = false;
}
if( found == true )
return;
}
}
/**
* Called internally to facilitate the removal of a connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.ProcessingUnit#disconnectDestination
* @throws SynapseDoesNotExistException Thrown if the specified synapse
* does not exist as a source synapse.
*/
protected void removeSource(Synapse inSynapse) throws SynapseDoesNotExistException
{
boolean found = true;
for( ProcessingUnit currentChild : this.children )
{
found = true;
try
{
currentChild.removeSource(inSynapse);
}
catch(SynapseDoesNotExistException caughtException)
{
found = false;
}
if( found == true )
return;
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Propogation">
/**
* Propogates the output of the ProcessingUnits from the incoming synapse to
* the outgoign one.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#propogate
*/
public void propogate()
{
for( ProcessingUnit currentChild : this.children )
{
currentChild.propogate();
}
}
/**
* Back propogates the taining set of the ProcessingUnits from the outgoing
* synapse to the incomming one.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#backPropogate
*/
public void backPropogate()
{
for( ProcessingUnit currentChild : this.children )
{
currentChild.backPropogate();
}
}
// </editor-fold>
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
import java.util.ArrayList;
import java.lang.Exception;
import java.lang.Math;
/**
* The neuron is the most fundemental component of the network; it is also the
* thinktank of the system. One neuron will usually connect to many other
* ProcessingUnits through synapses and receive input from many other
* ProcessingUnits in the same way.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
* @see com.syncleus.dann.Synapse
*/
public class NeuronProcessingUnit extends ProcessingUnit
{
// <editor-fold defaultstate="collapsed" desc="Attributes">
/**
* Represents the current excitation of the neuron from input
* signals<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected double activity = 0;
/**
* Represents the current output of the neuron<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private double output = 0;
/**
* The current weight of the bias input. The bias is an input that is always
* set to an on position. The bias weight usually adapts in the same manner
* as the rest of the synapse's weights.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected double biasWeight = 0;
/**
* An array list of all the synapses that this neuron is currently
* connection out to.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected ArrayList<Synapse> destination = new ArrayList<Synapse>();
/**
* All the synapses currently connecting into this neuron<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected ArrayList<Synapse> sourceSynapses = new ArrayList<Synapse>();
/**
* The DNA determines this neurons basic properties.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public DNA ownedDNA;
/**
* This represents the net effect of all the training data from all the
* inputs. It is essentially the reverse of the activity value.
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#activity
*/
public double deltaTrain = 0;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
/**
* Creates a new instance of NeuronProcessingUnit<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param OwnedDNAToSet This dna class will determine the various properties
* of the layer.
*/
public NeuronProcessingUnit(DNA OwnedDNAToSet)
{
this.ownedDNA = OwnedDNAToSet;
this.biasWeight = (super.random.nextDouble()*2.0)-1.0;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Topology Manipulation">
/**
* This method is called externally to connect to another ProcessingUnit.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outUnit The ProcessingUnit to connect to.
* @see com.syncleus.dann.NeuronProcessingUnit#connectFrom
*/
public void connectTo(ProcessingUnit outUnit)
{
//make sure you arent already connected to the neuron
int test;
if( outUnit == null)
return; // todo throw an exception
//connect to the neuron
Synapse NewSynapse = new Synapse(this, outUnit, (super.random.nextDouble() *2.0) - 1.0);
this.destination.add(NewSynapse);
outUnit.connectFrom(NewSynapse);
}
/**
* This method is called internally, between ProcessingUnits, to
* facilitate the connection process.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The synapse to connect from.
* @see com.syncleus.dann.NeuronProcessingUnit#connectTo
*/
protected void connectFrom(Synapse inSynapse)
{
//make sure you arent already connected fromt his neuron
//add the synapse to the source list
this.sourceSynapses.add(inSynapse);
}
/**
* Causes the ProcessingUnit to disconnect all outgoing connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectAllSources
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectAll
*/
public void disconnectAllDestinations()
{
for( Synapse currentDestination : this.destination )
{
try
{
this.disconnectDestination(currentDestination);
}
catch(SynapseNotConnectedException caughtException)
{
caughtException.printStackTrace();
throw new InternalError("this shouldnt happen: " + caughtException);
}
}
}
/**
* Causes the ProcessingUnit to disconnect all incomming connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectAllDestinations
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectAll
*/
public void disconnectAllSources()
{
for( Synapse currentSource : this.sourceSynapses )
{
try
{
this.disconnectSource(currentSource);
}
catch(SynapseNotConnectedException caughtException)
{
caughtException.printStackTrace();
throw new InternalError("this shouldnt happen: " + caughtException);
}
}
}
/**
* Disconnects from a perticular outgoing connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The outgoing synapse to disconnect from.<BR>
* @see com.syncleus.dann.NeuronProcessingUnit#removeSource
*/
public void disconnectDestination(Synapse outSynapse) throws SynapseNotConnectedException
{
if(this instanceof OutputNeuronProcessingUnit)
return; // TODO throw an error
if( this.destination.remove(outSynapse) == false )
throw new SynapseNotConnectedException("can not disconnect destination, does not exist.");
try
{
if( outSynapse.getDestination() != null )
outSynapse.getDestination().removeSource(outSynapse);
}
catch(SynapseDoesNotExistException caughtException)
{
//do nothing, its a recoverable exception
}
}
/**
* Disconnects from a perticular incomming connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to disconnect from.<BR>
* @see com.syncleus.dann.NeuronProcessingUnit#removeDestination
*/
public void disconnectSource(Synapse inSynapse) throws SynapseNotConnectedException
{
if(this instanceof InputNeuronProcessingUnit)
return; // TODO throw an error
if( this.sourceSynapses.remove(inSynapse) == false )
throw new SynapseNotConnectedException("can not disconnect source, does not exist.");
try
{
if( inSynapse.getSource() != null )
inSynapse.getSource().removeDestination(inSynapse);
}
catch(SynapseDoesNotExistException caughtException)
{
//do nothing, its a recoverable exception
}
}
/**
* Called internally to facilitate the removal of a connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectSource
*/
protected void removeDestination(Synapse outSynapse) throws SynapseDoesNotExistException
{
if(this instanceof OutputNeuronProcessingUnit)
return; // TODO throw an exception'
if( this.destination.remove(outSynapse) == false )
throw new SynapseDoesNotExistException("Can not remove destination, does not exist.");
}
/**
* Called internally to facilitate the removal of a connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.NeuronProcessingUnit#disconnectDestination
*/
protected void removeSource(Synapse inSynapse) throws SynapseDoesNotExistException
{
if(this instanceof InputNeuronProcessingUnit)
return; // TODO throw an exception
if( this.sourceSynapses.remove(inSynapse) == false )
throw new SynapseDoesNotExistException("Can not remove destination, does not exist.");
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Propogation">
/**
* Backpropogates the training data to all the incomming synapses.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public void backPropogate()
{
this.calculateDeltaTrain();
//step thru source synapses and make them learn their new weight.
Object[] SynapseArray = this.sourceSynapses.toArray();
for(int Lcv = 0; Lcv < SynapseArray.length; Lcv++)
{
Synapse CurrentSynapse = (Synapse) SynapseArray[Lcv];
CurrentSynapse.learnWeight(this.deltaTrain, this.ownedDNA.learningRate);
}
//learn the biases new weight
this.biasWeight += this.ownedDNA.learningRate * this.deltaTrain;
}
/**
* Calculates the Delta Train based on all the destination synapses<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#backPropogate
*/
public void calculateDeltaTrain()
{
this.deltaTrain = 0;
Object[] SynapseArray = this.destination.toArray();
for(int Lcv = 0; Lcv < SynapseArray.length; Lcv++)
{
Synapse CurrentSynapse = (Synapse) SynapseArray[Lcv];
this.deltaTrain += CurrentSynapse.getDifferential();
}
this.deltaTrain *= this.activationFunctionDerivitive();
}
/**
* Propogates the current output to all outgoing synapses.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public void propogate()
{
//calculate the current input activity
this.activity = 0;
Object[] SynapseArray = this.sourceSynapses.toArray();
for(int Lcv = 0; Lcv < SynapseArray.length; Lcv++)
{
Synapse CurrentSynapse = (Synapse) SynapseArray[Lcv];
this.activity += CurrentSynapse.getOutput();
}
//Add the bias to the activity
this.activity += this.biasWeight;
//calculate the activity function and set the result as the output
this.setOutput( this.activationFunction() );
}
/**
* sets the current output on all outgoing synapses.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#propogate
* @param newOutput The output value.
*/
protected void setOutput(double newOutput)
{
this.output = newOutput;
for( Synapse current : this.destination )
{
current.setInput(newOutput);
}
}
/**
* Gets the current output.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return The current output.
*/
public double getOutput()
{
return this.output;
}
/**
* obtains the output as a function of the current activity. This is a bound
* function (usually between -1 and 1) based on the current activity of the
* neuron.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return a bound value (between -1 and 1 if this function is not
* overwritten). It is a function of the neuron's current activity.
* @see com.syncleus.dann.NeuronProcessingUnit#propogate
*/
protected double activationFunction()
{
return Math.tanh(this.activity);
}
/**
* This must be the derivity of the ActivityFunction. As such it's output is
* also based on the current activity of the neuron. If the
* activationFunction is overwritten then this method must also be
* overwritten with the proper derivative.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return the derivative output of the activationFunction
* @see com.syncleus.dann.NeuronProcessingUnit#activationFunction
*/
protected double activationFunctionDerivitive()
{
return 1.0 - Math.pow(this.activationFunction(), 2.0);
}
// </editor-fold>
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* This is a special type of neuron that provides the output.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
* @see com.syncleus.dann.InputNeuronProcessingUnit
*/
public class OutputNeuronProcessingUnit extends NeuronProcessingUnit
{
/**
* holds the value for the current training set.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected double desired = 0;
/**
* Creates a new instance of OutputNeuronProcessingUnit<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param ownedDNAToSet This dna class will determine the various properties
* of the layer.
*/
public OutputNeuronProcessingUnit(DNA ownedDNAToSet)
{
super(ownedDNAToSet);
}
/**
* This method sets the current training set on the neuron.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param trainingToSet sets the current training set.
*/
public void setDesired(double trainingToSet)
{
this.desired = trainingToSet;
}
/**
* Calculates the Delta Train based on all the destination synapses<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#backPropogate
*/
public void calculateDeltaTrain()
{
this.deltaTrain = 0;
Object[] SynapseArray = super.destination.toArray();
for(int Lcv = 0; Lcv < SynapseArray.length; Lcv++)
{
Synapse CurrentSynapse = (Synapse) SynapseArray[Lcv];
this.deltaTrain += CurrentSynapse.getDifferential();
}
super.deltaTrain += (this.desired - super.getOutput());
super.deltaTrain *= super.activationFunctionDerivitive();
}
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
import java.util.Random;
/**
* This represents a blackbox unit that takes any number of inputs and produces
* anynumber of outputs. The inputs and outputs all connect to other
* ProcessingUnits.
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public abstract class ProcessingUnit
{
/**
* A shared random number generator.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
protected static final Random random = new Random();
/**
* This method is called externally to connect to another ProcessingUnit.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outUnit The ProcessingUnit to connect to.
* @see com.syncleus.dann.ProcessingUnit#connectFrom
*/
public abstract void connectTo(ProcessingUnit outUnit);
/**
* This method is called internally, between ProcessingUnits, to
* facilitate the connection process.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The synapse to connect from.
* @see com.syncleus.dann.ProcessingUnit#connectTo
*/
protected abstract void connectFrom(Synapse inSynapse);
/**
* Causes the ProcessingUnit to disconnect all outgoing connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#disconnectAllSources
* @see com.syncleus.dann.ProcessingUnit#disconnectAll
*/
public abstract void disconnectAllDestinations();
/**
* Causes the ProcessingUnit to disconnect all incomming connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#disconnectAllDestinations
* @see com.syncleus.dann.ProcessingUnit#disconnectAll
*/
public abstract void disconnectAllSources();
/**
* Causes the ProcessingUnit to disconnect all connections.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit#disconnectAllSources
* @see com.syncleus.dann.ProcessingUnit#disconnectAllDestinations
*/
public void disconnectAll()
{
this.disconnectAllDestinations();
this.disconnectAllSources();
}
/**
* Disconnects from a perticular outgoing connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The outgoing synapse to disconnect from.<BR>
* @see com.syncleus.dann.ProcessingUnit#removeSource
* @throws SynapseNotConnectedException Thrown if the specified synapse is not
* currently connected.
*/
public abstract void disconnectDestination(Synapse outSynapse) throws SynapseNotConnectedException;
/**
* Disconnects from a perticular incomming connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to disconnect from.<BR>
* @see com.syncleus.dann.ProcessingUnit#removeDestination
* @throws SynapseNotConnectedException Thrown if the specified synapse is not
* currently connected.
*/
public abstract void disconnectSource(Synapse inSynapse) throws SynapseNotConnectedException;
/**
* Called internally to facilitate the removal of a connection. It removes
* the specified synapse from memory assuming it has already been
* disconnected<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param outSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.ProcessingUnit#disconnectSource
* @throws SynapseDoesNotExistException Thrown if the specified synapse
* does not exist as a source synapse.
*/
protected abstract void removeDestination(Synapse outSynapse) throws SynapseDoesNotExistException;
/**
* Called internally to facilitate the removal of a connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param inSynapse The incomming synapse to remove from memory.<BR>
* @see com.syncleus.dann.ProcessingUnit#disconnectDestination
* @throws SynapseDoesNotExistException Thrown if the specified synapse
* does not exist as a source synapse.
*/
protected abstract void removeSource(Synapse inSynapse) throws SynapseDoesNotExistException;
/**
* Backpropogates the training data to all the incomming synapses.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public abstract void backPropogate();
/**
* Propogates the current output to all outgoing synapses.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
public abstract void propogate();
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* The synapse acts as a bridge between connected neurons. It is also where the
* connection weights are stores and manipulated.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
* @see com.syncleus.dann.ProcessingUnit
*/
public class Synapse
{
// <editor-fold defaultstate="collapsed" desc="Attributes">
/**
* The outgoing neuron connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private ProcessingUnit destination;
/**
* The incomming neuron connection.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private ProcessingUnit source;
/**
* The current weight of the synapse<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private double weight = 0.0;
/**
* The current output of the synapse<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private double output = 0.0;
/**
* The current input from the synapse<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private double input = 0.0;
/**
* The current synapse's deltaTrain<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
*/
private double deltaTrain = 0.0;
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Constructors">
/**
* Creates a new instance of Synapse<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @param sourceToSet The incomming neuron connection.
* @param destinationToSet The outgoing neuron connection.
* @param initialWeight The initial weight of the synapse
*/
public Synapse(ProcessingUnit sourceToSet, ProcessingUnit destinationToSet, double initialWeight)
{
this.destination = destinationToSet;
this.source = sourceToSet;
this.weight = initialWeight;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Link Traversal">
/**
* Obtains the incomming neuron.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return The source neuron.
*/
public ProcessingUnit getSource()
{
return this.source;
}
/**
* Obtains the outgoing neuron.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @return The destination neuron.
*/
public ProcessingUnit getDestination()
{
return this.destination;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Propogation">
/**
* learns the new weight based on the current training set<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#calculateDeltaTrain
*/
public void learnWeight(double deltaTrainToSet, double learningRate)
{
this.deltaTrain = deltaTrainToSet;
this.weight += learningRate * this.input * this.deltaTrain;
}
/**
* Calculates the current output of the synapse based on the input and
* weight<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#propogate
* @return the current synapse output.
*/
public double getOutput()
{
this.output = this.input * this.weight;
return this.output;
}
/**
* Set the current input for the synapse<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#propogate
* @param newInput The new input value to set.
*/
public void setInput(double newInput)
{
this.input = newInput;
}
/**
* Calculates the synapse differential. This is used when back propogating
* training sets.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @since 0.1
* @see com.syncleus.dann.NeuronProcessingUnit#backPropogate
* @return the current synapse differential.
*/
public double getDifferential()
{
return this.deltaTrain * this.weight;
}
// </editor-fold>
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* This indicates that a required synapse does not exist.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public class SynapseDoesNotExistException extends dannException
{
public SynapseDoesNotExistException()
{
}
public SynapseDoesNotExistException(String msg)
{
super(msg);
}
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* This means a synapse is not a connected synpase.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public class SynapseNotConnectedException extends dannException
{
public SynapseNotConnectedException()
{
}
public SynapseNotConnectedException(String msg)
{
super(msg);
}
}
/******************************************************************************
* *
* Copyright: (c) Jeffrey Phillips Freeman *
* *
* You may redistribute and modify this source code under the terms and *
* conditions of the Open Source Community License - Type C version 1.0 *
* or any later version as published by syncleus at http://www.syncleus.com. *
* There should be a copy of the license included with this file. If a copy *
* of the license is not included you are granted no right to distribute or *
* otherwise use this file except through a legal and valid license. You *
* should also contact syncleus at the information below if you cannot find *
* a license: *
* *
* Syncleus *
* 1116 McClellan St. *
* Philadelphia, PA 19148 *
* *
******************************************************************************/
package com.syncleus.dann;
/**
* All dANN specific exceptions that are thrown will either be a dannException
* or inherit from it.<BR>
* <!-- Author: Jeffrey Phillips Freeman -->
* @author Jeffrey Phillips Freeman
* @since 0.1
*/
public class dannException extends java.lang.Exception
{
public dannException()
{
}
public dannException(String msg)
{
super(msg);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment