Beispiel #1
0
        private bool AreOutputs(NeatNeuron a, NeatNeuron b)
        {
            var aOut = false;
            var bOut = false;

            var outputNeurons = GetOutputNeurons();

            for (var i = 0; i < outputNeurons.Count; i++)
            {
                var n = outputNeurons[i];
                if (n == a)
                {
                    aOut = true;
                }
                else if (n == b)
                {
                    bOut = true;
                }

                if (aOut && bOut)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #2
0
 private bool AreConnected(NeatNeuron a, NeatNeuron b)
 {
     for (var i = 0; i < b.Connections.Count; i++)
     {
         var c = b.Connections[i];
         if (!c.OutGoing && a == c.Neuron)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #3
0
        private bool CanConnect(NeatNeuron a, NeatNeuron b)
        {
            var same       = a == b;
            var canConnect = !same && !AreOutputs(a, b) && !AreConnected(a, b);

            if (!_neat.Structure.AllowRecurrentConnections)
            {
                var isRecurrent = a.Layer > b.Layer;
                canConnect = canConnect && !isRecurrent;
            }

            return(canConnect);
        }
Beispiel #4
0
        private void CategorizeNeuronBranchIntoLayers(NeatNeuron current, int depth = 0)
        {
            current.Layer = depth;
            var nextLayer = current.Layer + 1;

            for (var i = 0; i < current.Connections.Count; i++)
            {
                var c = current.Connections[i];

                if (nextLayer > c.Neuron.Layer && c.OutGoing != c.Recursive)
                {
                    CategorizeNeuronBranchIntoLayers(c.Neuron, nextLayer);
                }
            }
        }
Beispiel #5
0
        private void BuildFromGenes()
        {
            _neurons = new List <NeatNeuron>(Chromosome.NeuronCount);
            for (var i = 0; i < Chromosome.NeuronCount; i++)
            {
                var neuron = new NeatNeuron {
                    Activation = _neat.Neural.ActivationFunction
                };
                _neurons.Add(neuron);
            }

            for (var i = 0; i < Chromosome.GeneCount; i++)
            {
                var gene = Chromosome.GetGeneAt(i);

                if (gene.Enabled)
                {
                    var inConnection = new NeatNeuron.Connection();
                    inConnection.Weight    = gene.Weight;
                    inConnection.Recursive = gene.Recursive;
                    inConnection.Neuron    = _neurons[gene.From];
                    _neurons[gene.To].Connections.Add(inConnection);

                    var outConnection = new NeatNeuron.Connection();
                    outConnection.Weight    = gene.Weight;
                    outConnection.Recursive = gene.Recursive;
                    outConnection.Neuron    = _neurons[gene.To];
                    outConnection.OutGoing  = true;
                    _neurons[gene.From].Connections.Add(outConnection);
                }
            }

            for (var i = 0; i < _neat.Structure.BiasNeuronCount; i++)
            {
                _neurons[i].Value = 1.0;
            }

            CategorizeNeuronsIntoLayers();
        }