Esempio n. 1
0
        /// <summary>
        /// Copy with mutations
        /// </summary>
        /// <param name="other"></param>
        public static OutputNeuron WithMutations(OutputNeuron other)
        {
            OutputNeuron ret = new OutputNeuron(other.bias);

            Mutate(ref ret.bias, 2);
            return(ret);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="parent"></param>
        public static NeuralNetwork NoMutations(NeuralNetwork parent)
        {
            NeuralNetwork ret = new NeuralNetwork();

            ret.m_layers       = parent.m_layers;
            ret.m_inputNeurons = new InputNeuron[parent.m_inputNeurons.Length];
            for (int i = 0; i < ret.m_inputNeurons.Length; i++)
            {
                ret.m_inputNeurons[i] = InputNeuron.NoMutations(parent.m_inputNeurons[i]);
            }
            ret.m_hiddenNeurons = new HiddenNeuron[parent.m_hiddenNeurons.Length][];
            for (int i = 0; i < ret.m_hiddenNeurons.Length; i++)
            {
                ret.m_hiddenNeurons[i] = new HiddenNeuron[parent.m_hiddenNeurons[i].Length];
                for (int j = 0; j < ret.m_hiddenNeurons[i].Length; j++)
                {
                    ret.m_hiddenNeurons[i][j] = HiddenNeuron.NoMutations(parent.m_hiddenNeurons[i][j]);
                }
            }
            ret.m_outputNeurons = new OutputNeuron[parent.m_outputNeurons.Length];
            for (int i = 0; i < ret.m_outputNeurons.Length; i++)
            {
                ret.m_outputNeurons[i] = OutputNeuron.NoMutations(parent.m_outputNeurons[i]);
            }
            return(ret);
        }
        /// <summary>
        /// The constructor for initializing a neural network with pre-set values
        /// </summary>
        /// <param name="layers"></param>
        /// <param name="connections"></param>
        /// <param name="biases"></param>
        public NeuralNetwork(int[] layers, float[] connections, float[] biases)
        {
            m_layers        = layers;
            m_inputNeurons  = new InputNeuron[layers[0]];
            m_hiddenNeurons = new HiddenNeuron[m_layers.Length - 2][];
            m_outputNeurons = new OutputNeuron[m_layers[m_layers.Length - 1]];
            int biasesIdx      = 0;
            int connectionsIdx = 0;

            //Input neurons loop
            for (int i = 0; i < m_layers[0]; i++)
            {
                int nextLayer = m_layers[1];
                m_inputNeurons[i] = new InputNeuron(nextLayer);

                for (int j = 0; j < nextLayer; j++)
                {
                    m_inputNeurons[i].connections[j] = connections[connectionsIdx++];
                }
            }

            //Hidden neurons loop
            for (int i = 0; i < m_hiddenNeurons.Length; i++)
            {
                int layer = m_layers[i + 1];
                m_hiddenNeurons[i] = new HiddenNeuron[layer];
                for (int j = 0; j < layer; j++)
                {
                    int nextLayer = m_layers[i + 2];
                    m_hiddenNeurons[i][j] = new HiddenNeuron(nextLayer, biases[biasesIdx++]);
                    for (int k = 0; k < nextLayer; k++)
                    {
                        m_hiddenNeurons[i][j].connections[k] = connections[connectionsIdx++];
                    }
                }
            }

            //Output neurons loop
            for (int i = 0; i < m_layers[m_layers.Length - 1]; i++)
            {
                m_outputNeurons[i] = new OutputNeuron(biases[biasesIdx++]);
            }
        }
        /// <summary>
        /// The constructor for initializing a neural network with random values
        /// </summary>
        /// <param name="layers"></param>
        public NeuralNetwork(int[] layers)
        {
            m_layers        = layers;
            m_inputNeurons  = new InputNeuron[layers[0]];
            m_hiddenNeurons = new HiddenNeuron[m_layers.Length - 2][];
            m_outputNeurons = new OutputNeuron[m_layers[m_layers.Length - 1]];

            //Input neurons loop
            for (int i = 0; i < m_layers[0]; i++)
            {
                int nextLayer = m_layers[1];
                m_inputNeurons[i] = new InputNeuron(nextLayer);

                for (int j = 0; j < nextLayer; j++)
                {
                    m_inputNeurons[i].connections[j] = GetRandomNeuronValue(2);
                }
            }

            //Hidden neurons loop
            for (int i = 0; i < m_hiddenNeurons.Length; i++)
            {
                int layer = m_layers[i + 1];
                m_hiddenNeurons[i] = new HiddenNeuron[layer];
                for (int j = 0; j < layer; j++)
                {
                    int nextLayer = m_layers[i + 2];
                    m_hiddenNeurons[i][j] = new HiddenNeuron(nextLayer, GetRandomNeuronValue(10));
                    for (int k = 0; k < nextLayer; k++)
                    {
                        m_hiddenNeurons[i][j].connections[k] = GetRandomNeuronValue(2);
                    }
                }
            }

            //Output neurons loop
            for (int i = 0; i < m_layers[m_layers.Length - 1]; i++)
            {
                m_outputNeurons[i] = new OutputNeuron(GetRandomNeuronValue(10));
            }
        }
 /// <summary>
 /// Takes another neural network and changes random values in it
 /// </summary>
 /// <param name="parent"></param>
 public NeuralNetwork(NeuralNetwork parent)
 {
     m_layers       = parent.m_layers;
     m_inputNeurons = new InputNeuron[parent.m_inputNeurons.Length];
     for (int i = 0; i < m_inputNeurons.Length; i++)
     {
         m_inputNeurons[i] = InputNeuron.WithMutations(parent.m_inputNeurons[i]);
     }
     m_hiddenNeurons = new HiddenNeuron[parent.m_hiddenNeurons.Length][];
     for (int i = 0; i < m_hiddenNeurons.Length; i++)
     {
         m_hiddenNeurons[i] = new HiddenNeuron[parent.m_hiddenNeurons[i].Length];
         for (int j = 0; j < m_hiddenNeurons[i].Length; j++)
         {
             m_hiddenNeurons[i][j] = HiddenNeuron.WithMutations(parent.m_hiddenNeurons[i][j]);
         }
     }
     m_outputNeurons = new OutputNeuron[parent.m_outputNeurons.Length];
     for (int i = 0; i < m_outputNeurons.Length; i++)
     {
         m_outputNeurons[i] = OutputNeuron.WithMutations(parent.m_outputNeurons[i]);
     }
 }
Esempio n. 6
0
 public static OutputNeuron NoMutations(OutputNeuron other)
 {
     return(new OutputNeuron(other.bias));
 }