Class Network

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<Neuron>

    public class Network
    extends java.lang.Object
    implements java.lang.Iterable<Neuron>, java.io.Serializable
    Neural network, composed of Neuron instances and the links between them. Although updating a neuron's state is thread-safe, modifying the network's topology (adding or removing links) is not.
    Since:
    3.3
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int featureSize
      Neuron's features set size.
      private java.util.concurrent.ConcurrentHashMap<java.lang.Long,​java.util.Set<java.lang.Long>> linkMap
      Links.
      private java.util.concurrent.ConcurrentHashMap<java.lang.Long,​Neuron> neuronMap
      Neurons.
      private java.util.concurrent.atomic.AtomicLong nextId
      Next available neuron identifier.
      private static long serialVersionUID
      Serializable.
    • Constructor Summary

      Constructors 
      Constructor Description
      Network​(long initialIdentifier, int featureSize)  
      Network​(long nextId, int featureSize, Neuron[] neuronList, long[][] neighbourIdList)
      Constructor with restricted access, solely used for deserialization.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addLink​(Neuron a, Neuron b)
      Adds a link from neuron a to neuron b.
      private void addLinkToLinkSet​(java.util.Set<java.lang.Long> linkSet, long id)
      Adds a link to neuron id in given linkSet.
      Network copy()
      Performs a deep copy of this instance.
      long createNeuron​(double[] features)
      Creates a neuron and assigns it a unique identifier.
      private java.lang.Long createNextId()
      Creates a neuron identifier.
      void deleteLink​(Neuron a, Neuron b)
      Deletes the link between neurons a and b.
      private void deleteLinkFromLinkSet​(java.util.Set<java.lang.Long> linkSet, long id)
      Deletes a link to neuron id in given linkSet.
      void deleteNeuron​(Neuron neuron)
      Deletes a neuron.
      int getFeaturesSize()
      Gets the size of the neurons' features set.
      java.util.Collection<Neuron> getNeighbours​(java.lang.Iterable<Neuron> neurons)
      Retrieves the neurons in the neighbourhood of any neuron in the neurons list.
      java.util.Collection<Neuron> getNeighbours​(java.lang.Iterable<Neuron> neurons, java.lang.Iterable<Neuron> exclude)
      Retrieves the neurons in the neighbourhood of any neuron in the neurons list.
      java.util.Collection<Neuron> getNeighbours​(Neuron neuron)
      Retrieves the neighbours of the given neuron.
      java.util.Collection<Neuron> getNeighbours​(Neuron neuron, java.lang.Iterable<Neuron> exclude)
      Retrieves the neighbours of the given neuron.
      Neuron getNeuron​(long id)
      Retrieves the neuron with the given (unique) id.
      java.util.Collection<Neuron> getNeurons​(java.util.Comparator<Neuron> comparator)
      Creates a list of the neurons, sorted in a custom order.
      java.util.Iterator<Neuron> iterator()
      private void readObject​(java.io.ObjectInputStream in)
      Prevents proxy bypass.
      private java.lang.Object writeReplace()
      Custom serialization.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • serialVersionUID

        private static final long serialVersionUID
        Serializable.
        See Also:
        Constant Field Values
      • neuronMap

        private final java.util.concurrent.ConcurrentHashMap<java.lang.Long,​Neuron> neuronMap
        Neurons.
      • nextId

        private final java.util.concurrent.atomic.AtomicLong nextId
        Next available neuron identifier.
      • featureSize

        private final int featureSize
        Neuron's features set size.
      • linkMap

        private final java.util.concurrent.ConcurrentHashMap<java.lang.Long,​java.util.Set<java.lang.Long>> linkMap
        Links.
    • Constructor Detail

      • Network

        Network​(long nextId,
                int featureSize,
                Neuron[] neuronList,
                long[][] neighbourIdList)
        Constructor with restricted access, solely used for deserialization.
        Parameters:
        nextId - Next available identifier.
        featureSize - Number of features.
        neuronList - Neurons.
        neighbourIdList - Links associated to each of the neurons in neuronList.
        Throws:
        MathIllegalStateException - if an inconsistency is detected (which probably means that the serialized form has been corrupted).
      • Network

        public Network​(long initialIdentifier,
                       int featureSize)
        Parameters:
        initialIdentifier - Identifier for the first neuron that will be added to this network.
        featureSize - Size of the neuron's features.
    • Method Detail

      • copy

        public Network copy()
        Performs a deep copy of this instance. Upon return, the copied and original instances will be independent: Updating one will not affect the other.
        Returns:
        a new instance with the same state as this instance.
        Since:
        3.6
      • iterator

        public java.util.Iterator<Neuron> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<Neuron>
      • getNeurons

        public java.util.Collection<Neuron> getNeurons​(java.util.Comparator<Neuron> comparator)
        Creates a list of the neurons, sorted in a custom order.
        Parameters:
        comparator - Comparator used for sorting the neurons.
        Returns:
        a list of neurons, sorted in the order prescribed by the given comparator.
        See Also:
        Network.NeuronIdentifierComparator
      • createNeuron

        public long createNeuron​(double[] features)
        Creates a neuron and assigns it a unique identifier.
        Parameters:
        features - Initial values for the neuron's features.
        Returns:
        the neuron's identifier.
        Throws:
        DimensionMismatchException - if the length of features is different from the expected size (as set by the constructor).
      • deleteNeuron

        public void deleteNeuron​(Neuron neuron)
        Deletes a neuron. Links from all neighbours to the removed neuron will also be deleted.
        Parameters:
        neuron - Neuron to be removed from this network.
        Throws:
        java.util.NoSuchElementException - if n does not belong to this network.
      • getFeaturesSize

        public int getFeaturesSize()
        Gets the size of the neurons' features set.
        Returns:
        the size of the features set.
      • addLink

        public void addLink​(Neuron a,
                            Neuron b)
        Adds a link from neuron a to neuron b. Note: the link is not bi-directional; if a bi-directional link is required, an additional call must be made with a and b exchanged in the argument list.
        Parameters:
        a - Neuron.
        b - Neuron.
        Throws:
        java.util.NoSuchElementException - if the neurons do not exist in the network.
      • addLinkToLinkSet

        private void addLinkToLinkSet​(java.util.Set<java.lang.Long> linkSet,
                                      long id)
        Adds a link to neuron id in given linkSet. Note: no check verifies that the identifier indeed belongs to this network.
        Parameters:
        linkSet - Neuron identifier.
        id - Neuron identifier.
      • deleteLink

        public void deleteLink​(Neuron a,
                               Neuron b)
        Deletes the link between neurons a and b.
        Parameters:
        a - Neuron.
        b - Neuron.
        Throws:
        java.util.NoSuchElementException - if the neurons do not exist in the network.
      • deleteLinkFromLinkSet

        private void deleteLinkFromLinkSet​(java.util.Set<java.lang.Long> linkSet,
                                           long id)
        Deletes a link to neuron id in given linkSet. Note: no check verifies that the identifier indeed belongs to this network.
        Parameters:
        linkSet - Neuron identifier.
        id - Neuron identifier.
      • getNeuron

        public Neuron getNeuron​(long id)
        Retrieves the neuron with the given (unique) id.
        Parameters:
        id - Identifier.
        Returns:
        the neuron associated with the given id.
        Throws:
        java.util.NoSuchElementException - if the neuron does not exist in the network.
      • getNeighbours

        public java.util.Collection<Neuron> getNeighbours​(java.lang.Iterable<Neuron> neurons)
        Retrieves the neurons in the neighbourhood of any neuron in the neurons list.
        Parameters:
        neurons - Neurons for which to retrieve the neighbours.
        Returns:
        the list of neighbours.
        See Also:
        getNeighbours(Iterable,Iterable)
      • getNeighbours

        public java.util.Collection<Neuron> getNeighbours​(java.lang.Iterable<Neuron> neurons,
                                                          java.lang.Iterable<Neuron> exclude)
        Retrieves the neurons in the neighbourhood of any neuron in the neurons list. The exclude list allows to retrieve the "concentric" neighbourhoods by removing the neurons that belong to the inner "circles".
        Parameters:
        neurons - Neurons for which to retrieve the neighbours.
        exclude - Neurons to exclude from the returned list. Can be null.
        Returns:
        the list of neighbours.
      • getNeighbours

        public java.util.Collection<Neuron> getNeighbours​(Neuron neuron)
        Retrieves the neighbours of the given neuron.
        Parameters:
        neuron - Neuron for which to retrieve the neighbours.
        Returns:
        the list of neighbours.
        See Also:
        getNeighbours(Neuron,Iterable)
      • getNeighbours

        public java.util.Collection<Neuron> getNeighbours​(Neuron neuron,
                                                          java.lang.Iterable<Neuron> exclude)
        Retrieves the neighbours of the given neuron.
        Parameters:
        neuron - Neuron for which to retrieve the neighbours.
        exclude - Neurons to exclude from the returned list. Can be null.
        Returns:
        the list of neighbours.
      • createNextId

        private java.lang.Long createNextId()
        Creates a neuron identifier.
        Returns:
        a value that will serve as a unique identifier.
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
        Prevents proxy bypass.
        Parameters:
        in - Input stream.
      • writeReplace

        private java.lang.Object writeReplace()
        Custom serialization.
        Returns:
        the proxy instance that will be actually serialized.