Example #1
0
        /// <summary>
        ///		Sets the current input on the neurons in the layer.
        /// </summary>
        /// <param name="InputToSet">Input data to set.</param>
        public void SetInput(double[] InputToSet)
        {
            IEnumerator NeuronEnum = this.NeuronsOwned.GetEnumerator();
            int         Lcv        = 0;

            while ((NeuronEnum.MoveNext()) && (InputToSet.Length > Lcv))
            {
                if (NeuronEnum.Current is InputNeuron)
                {
                    InputNeuron CurrentNeuron = NeuronEnum.Current as InputNeuron;
                    CurrentNeuron.SetInputNeuronInput(InputToSet[Lcv]);
                }
                else
                {
                    throw new Exception("NeuronsOwned should only contain InputNeurons");
                }

                Lcv++;
            }
        }
Example #2
0
        protected Brain(SerializationInfo info, StreamingContext context)
        {
            //Get brains attributes
            this.LayerCount = info.GetInt32("LayerCount");
            // Input layer and output layer must be set later

            //Get all the layers
            Layer LastLayer = null;

            for (int LayerLcv = this.LayerCount - 1; LayerLcv >= 0; LayerLcv--)
            {
                string LayerIdName = "Layer" + LayerLcv;

                uint LayerUid    = info.GetUInt32(LayerIdName + "Uid");
                bool UseByteRes  = info.GetBoolean(LayerIdName + "UseByteRes");
                bool IsInput     = info.GetBoolean(LayerIdName + "IsInput");
                bool IsOutput    = info.GetBoolean(LayerIdName + "IsOutput");
                int  NeuronCount = info.GetInt32(LayerIdName + "NeuronCount");
                if (IsInput)
                {
                    Layer NewLayer = new InputLayer(this, UseByteRes, LayerUid);
                    this.InLayer = (NewLayer as InputLayer);
                    NewLayer.DestinationLayer = LastLayer;
                    LastLayer.SourceLayer     = NewLayer;
                    LastLayer = NewLayer;
                }
                else if (IsOutput)
                {
                    LastLayer     = new OutputLayer(this, UseByteRes, LayerUid);
                    this.OutLayer = (LastLayer as OutputLayer);
                }
                else
                {
                    Layer NewLayer = new Layer(this, UseByteRes, LayerUid);
                    NewLayer.DestinationLayer = LastLayer;
                    LastLayer.SourceLayer     = NewLayer;
                    LastLayer = NewLayer;
                }

                //Get all of this layers neurons
                for (int NeuronLcv = 0; NeuronLcv < NeuronCount; NeuronLcv++)
                {
                    string NeuronIdName = LayerIdName + "Neuron" + NeuronLcv;

                    double BiasWeight  = info.GetDouble(NeuronIdName + "BiasWeight");
                    bool   UseByte     = info.GetBoolean(NeuronIdName + "UseByteResolution");
                    int    OutConCount = info.GetInt32(NeuronIdName + "OutConCount");
                    uint   NeuronUid   = info.GetUInt32(NeuronIdName + "Uid");

                    Neuron NewNeuron = null;
                    if (IsInput)
                    {
                        NewNeuron = new InputNeuron(LastLayer, UseByte, BiasWeight, NeuronUid);
                    }
                    else if (IsOutput)
                    {
                        NewNeuron = new OutputNeuron(LastLayer, UseByte, BiasWeight, NeuronUid);
                    }
                    else
                    {
                        NewNeuron = new Neuron(LastLayer, UseByte, BiasWeight, NeuronUid);
                    }

                    LastLayer.AddNeuron(NewNeuron);

                    //Connect the new neuron to all its destinations
                    for (int SynapseLcv = 0; SynapseLcv < OutConCount; SynapseLcv++)
                    {
                        string SynapseIdName = NeuronIdName + "Synapse" + SynapseLcv;

                        uint   ToUid  = info.GetUInt32(SynapseIdName + "ToUid");
                        double Weight = info.GetDouble(SynapseIdName + "Weight");

                        Neuron ToConnectTo = this.FindNeuron(LastLayer, ToUid);

                        NewNeuron.ConnectToNeuron(ToConnectTo, Weight);
                    }
                }
            }
        }