Пример #1
0
        /// <summary>
        /// Constructor (Crossover two genome)
        /// </summary>
        /// <param name="genome1"></param>
        /// <param name="genome2"></param>
        public Genome(Genome genome1, Genome genome2)
        {
            this._ea   = genome1._ea;
            this._pool = genome1._pool;

            if (genome2._fitness > genome1._fitness)
            {
                Genome temp = genome1;
                genome1 = genome2;
                genome2 = temp;
            }

            Gene[] innovations = new Gene[this._ea.Pool.Innovation];
            foreach (Gene g2 in genome2._genes)
            {
                innovations[g2.Innovation] = g2;
            }

            foreach (Gene g1 in genome1._genes)
            {
                Gene g2 = innovations[g1.Innovation];
                if (g2 != null && g2.Enable && this._ea.Random.Next(0, 2) == 1)
                {
                    this._genes.Add(new Gene(g2));
                }
                else
                {
                    this._genes.Add(new Gene(g1));
                }
            }

            this._maxNeuron = Math.Max(genome1._maxNeuron, genome2._maxNeuron);
            this._connectionsMutationChance = genome1._connectionsMutationChance;
            this._linkMutationChance        = genome1._linkMutationChance;
            this._biasMutationChance        = genome1._biasMutationChance;
            this._nodeMutationChance        = genome1._nodeMutationChance;
            this._enableMutationChance      = genome1._enableMutationChance;
            this._disableMutationChance     = genome1._disableMutationChance;
        }
Пример #2
0
        /// <summary>
        /// Get weights of two genome
        /// </summary>
        /// <param name="genome1"></param>
        /// <param name="genome2"></param>
        /// <returns></returns>
        private double GetWeights(Genome genome1, Genome genome2)
        {
            double sum        = 0;
            int    coincident = 0;

            Gene[] genes = new Gene[this._ea.Pool.Innovation];

            foreach (Gene gene in genome2.Genes)
            {
                genes[gene.Innovation] = gene;
            }

            foreach (Gene gene in genome1.Genes)
            {
                if (genes[gene.Innovation] != null)
                {
                    sum += Math.Abs(gene.Weight - genes[gene.Innovation].Weight);
                    coincident++;
                }
            }

            return(coincident == 0 ? 0 : sum / coincident);
        }
Пример #3
0
        /// <summary>
        /// Node mutate
        /// </summary>
        private void NodeMutate()
        {
            if (this._genes.Count == 0)
            {
                return;
            }

            //int maxNode = this._network.Neurons.Count - this._ea.Outputs;
            this._maxNeuron++;
            //Neuron neuron = new Neuron(this._ea, ++maxNode);
            //this._network.Neurons.Add(maxNode, neuron);

            Gene cGene = null;
            Gene nGene = null;

            cGene = this._genes[this._ea.Random.Next(0, this._genes.Count - 1)];
            if (!cGene.Enable)
            {
                return;
            }
            cGene.Enable = false;

            nGene            = new Gene(cGene);
            nGene.Out        = this._maxNeuron;
            nGene.Weight     = 1.0d;
            nGene.Innovation = this._pool.GetNextInnovation();
            nGene.Enable     = true;
            this._genes.Add(nGene);

            nGene            = new Gene(cGene);
            nGene.Into       = this._maxNeuron;
            nGene.Weight     = 1.0d;
            nGene.Innovation = this._pool.GetNextInnovation();
            nGene.Enable     = true;
            this._genes.Add(nGene);
        }