Exemple #1
0
        /// <summary>
        /// Constructs a new instance of the <see cref="NeuralNetwork"/> class.
        /// </summary>
        /// <param name="nodeGenes">The list of node genes.</param>
        /// <param name="edgeGenes">The list of edge genes.</param>
        internal NeuralNetwork(IList <NeuralChromosome.NodeGene> nodeGenes, IList <NeuralChromosome.EdgeGene> edgeGenes, ActivationFunction activationFunction)
        {
            _activation         = activationFunction;
            _activationFunction = ActivationFunctions.Get(activationFunction);

            var neuronMap = new Dictionary <uint, Neuron>();

            foreach (var gene in nodeGenes)
            {
                var neuron = new Neuron(gene.ID);
                _neurons.Add(neuron);
                neuronMap[gene.ID] = neuron;

                if (gene.Type == NeuralChromosome.NodeGene.NodeType.Input)
                {
                    _inputNeurons.Add(neuron);
                }
                else if (gene.Type == NeuralChromosome.NodeGene.NodeType.Output)
                {
                    _outputNeurons.Add(neuron);
                }
            }

            foreach (var gene in edgeGenes)
            {
                if (gene.Enabled)
                {
                    var edge = new Connection(neuronMap[gene.Input.ID], neuronMap[gene.Output.ID], gene.Weight);
                    _connections.Add(edge);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Constructs a new instance of the <see cref="NeuralNetwork"/> class.
        /// </summary>
        /// <param name="adjacencyMatrix">The adjacency matrix representation of the network.</param>
        /// <param name="inputNeurons">The input neuron ids.</param>
        /// <param name="outputNeurons">The output neuron ids.</param>
        /// <param name="activationFunction">The activation function.</param>
        public NeuralNetwork(double[,] adjacencyMatrix, IList <uint> inputNeurons, IList <uint> outputNeurons,
                             ActivationFunction activationFunction = ActivationFunction.Sigmoid) //Temp! need to fix chromosome metadata.
        {
            _adjacencyMatrix = adjacencyMatrix;

            _activation         = activationFunction;
            _activationFunction = ActivationFunctions.Get(activationFunction);

            var n = adjacencyMatrix.GetLength(0);
            var m = adjacencyMatrix.GetLength(1);

            if (n == 0 || m == 0)
            {
                throw new ArgumentException("Error! Adjacency matrix cannot contain a 0 dimension.");
            }

            if (n != m)
            {
                throw new ArgumentException("Error! Adjacency matrix must be a square matrix.");
            }

            if (inputNeurons.Any(index => index >= n))
            {
                throw new ArgumentException("Error! Input neuron index out of bounds.");
            }

            if (outputNeurons.Any(index => index >= n))
            {
                throw new ArgumentException("Error! Ouput neuron index out of bounds.");
            }

            if (inputNeurons.Intersect(outputNeurons).Count() != 0)
            {
                throw new ArgumentException("Error! Input neurons cannot also be output neuron.");
            }

            for (var i = 0; i < n; i++)
            {
                _neurons.Add(new Neuron((uint)i));
            }

            for (var i = 0; i < n; i++)
            {
                for (var j = 0; j < n; j++)
                {
                    if (adjacencyMatrix[i, j] != 0)
                    {
                        _connections.Add(new Connection(_neurons[i], _neurons[j], adjacencyMatrix[i, j]));
                    }
                }
            }

            foreach (var index in inputNeurons)
            {
                _inputNeurons.Add(_neurons[(int)index]);
            }

            foreach (var index in outputNeurons)
            {
                _outputNeurons.Add(_neurons[(int)index]);
            }
        }