Example #1
0
 private void AdjustNeuronCount(NeatGene gene)
 {
     if (gene.To + 1 > NeuronCount)
     {
         NeuronCount = gene.To + 1;
     }
 }
Example #2
0
        public bool Equals(NeatGene other)
        {
            if (other == null)
            {
                return(false);
            }

            return(History == other.History || From == other.From && To == other.To);
        }
Example #3
0
        private NeatGene FindGene(NeatGene gene)
        {
            for (var i = 0; i < _cache.Count; i++)
            {
                if (_cache[i] == gene)
                {
                    return(_cache[i]);
                }
            }

            return(null);
        }
Example #4
0
        public void AssignHistory(NeatGene gene)
        {
            var innov = FindGene(gene);

            if (innov == null)
            {
                gene.History = _nextHistory++;
                _cache.Add(gene);
            }
            else
            {
                gene.History = innov.History;
            }
        }
Example #5
0
        public NeatChromosome(Neat neat, int inputCount, int outputCount)
        {
            _neat       = neat;
            InputCount  = inputCount;
            OutputCount = outputCount;
            _genes      = new List <NeatGene>();

            for (var i = 0; i < inputCount + neat.Structure.BiasNeuronCount; i++)
            {
                for (var j = 0; j < outputCount; j++)
                {
                    var gene = new NeatGene(neat, i, j + inputCount + neat.Structure.BiasNeuronCount);
                    AppendGene(gene);
                }
            }
        }
Example #6
0
        private void AddRandomNeuron(InnovationCacher currentGeneration)
        {
            var randGene = GetRandomEnabledGene();
            var index    = Chromosome.NeuronCount;

            var g1 = new NeatGene(_neat, randGene.From, index);

            g1.Weight    = 1.0;
            g1.Recursive = randGene.Recursive;
            currentGeneration.AssignHistory(g1);

            var g2 = new NeatGene(_neat, index, randGene.To);

            g2.Weight    = randGene.Weight;
            g2.Recursive = randGene.Recursive;
            currentGeneration.AssignHistory(g2);

            randGene.Enabled = false;

            Chromosome.AppendGene(g1);
            Chromosome.AppendGene(g2);
        }
Example #7
0
        private void AddRandomConnection(InnovationCacher currentGeneration)
        {
            var neuronPair = GetTwoUnconnectedNeurons();
            var fromNeuron = neuronPair[0];
            var toNeuron   = neuronPair[1];

            var newConnectionGene = new NeatGene(_neat, _neurons.IndexOf(fromNeuron), _neurons.IndexOf(toNeuron));

            if (fromNeuron.Layer > toNeuron.Layer)
            {
                if (!_neat.Structure.AllowRecurrentConnections)
                {
                    throw new Exception("Illegal recurrent connection");
                }

                newConnectionGene.Recursive = true;
            }

            currentGeneration.AssignHistory(newConnectionGene);

            var inConnection = new NeatNeuron.Connection();

            inConnection.Recursive = newConnectionGene.Recursive;
            inConnection.Neuron    = fromNeuron;
            inConnection.Weight    = newConnectionGene.Weight;
            toNeuron.Connections.Add(inConnection);

            var outConnection = new NeatNeuron.Connection();

            outConnection.Recursive = newConnectionGene.Recursive;
            outConnection.Neuron    = toNeuron;
            outConnection.Weight    = newConnectionGene.Weight;
            outConnection.OutGoing  = true;
            fromNeuron.Connections.Add(outConnection);

            Chromosome.AppendGene(newConnectionGene);
            CategorizeNeuronsIntoLayers();
        }
Example #8
0
 public bool ContainsGene(NeatGene gene)
 {
     return(_genes.Contains(gene));
 }
Example #9
0
 public void InsertGeneAt(NeatGene gene, int index)
 {
     AdjustNeuronCount(gene);
     _genes.Insert(index, gene);
 }
Example #10
0
 public void AppendGene(NeatGene gene)
 {
     AdjustNeuronCount(gene);
     _genes.Add(gene);
 }