Пример #1
0
        /// <summary>
        ///		Constructs a new Brain object and creates the input and output layer.
        /// </summary>
        /// <param name="OwnedDNAToSet">Set the DNA properties of the brain</param>
        /// <param name="InputCount">The number of inputs into the network</param>
        /// <param name="OutputCount">The number of outputs out of the the network</param>
        public Brain(DNA OwnedDNAToSet, int InputCount, int OutputCount)
        {
            this.OwnedDNA            = OwnedDNAToSet;
            InLayer                  = new InputLayer(this, OwnedDNAToSet, null, false);
            OutLayer                 = new OutputLayer(this, OwnedDNAToSet, null, false);
            InLayer.DestinationLayer = OutLayer;
            OutLayer.SourceLayer     = InLayer;

            InLayer.AddNeurons(InputCount);
            OutLayer.AddNeurons(OutputCount);
        }
Пример #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);
                    }
                }
            }
        }