コード例 #1
0
 public override IEnumerator <DoubleGene> GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }
コード例 #2
0
 public override IEnumerator <IntegerGene> GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }
コード例 #3
0
 public override IEnumerator <CharacterGene> GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }
コード例 #4
0
        /// <summary>
        /// Fitness of this is greater than or equal to fitness of other
        /// </summary>
        /// <returns>child of this and other</returns>
        internal Genome Crossover(Genome other, Random rnd, bool sameFitness, int disableChance = 75)
        {
            var enumThis  = Genes.GetEnumerator();
            var enumOther = other.Genes.GetEnumerator();
            var dict      = new Dictionary <int, Node>();
            SortedList <int, Gene> genes = new SortedList <int, Gene>(Genes.Count);

            Gene curr1 = enumThis.MoveNext() ? enumThis.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
            Gene curr2 = enumOther.MoveNext() ? enumOther.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);

            while (true)
            {
                if (curr1.Innovation == curr2.Innovation)
                {
                    if (curr1.Innovation == -1)
                    {
                        break;
                    }
                    if (rnd.Next(0, 100) > 50)
                    {
                        AddGeneFromThis(curr1, curr2, dict, rnd, genes);
                    }
                    else
                    {
                        AddGeneFromOther(curr1, curr2, dict, rnd, genes);
                    }
                    curr1 = enumThis.MoveNext() ? enumThis.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                    curr2 = enumOther.MoveNext() ? enumOther.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                }
                else if (curr1.Innovation < curr2.Innovation)
                {
                    if (curr1.Innovation == -1)
                    {
                        if (sameFitness)
                        {
                            AddGeneFromThis(curr2, curr1, dict, rnd, genes);
                        }
                        curr2 = enumOther.MoveNext() ? enumOther.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                    }
                    else
                    {
                        AddGeneFromThis(curr1, curr2, dict, rnd, genes);
                        curr1 = enumThis.MoveNext() ? enumThis.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                    }
                }
                else
                {
                    if (curr2.Innovation == -1)
                    {
                        AddGeneFromThis(curr1, curr2, dict, rnd, genes);
                        curr1 = enumThis.MoveNext() ? enumThis.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                    }
                    else
                    {
                        if (sameFitness)
                        {
                            AddGeneFromThis(curr2, curr1, dict, rnd, genes);
                        }
                        // THE OTHER GENE HAS LOWER FITNESS SO WHY ADD IT'S GENE ??? - case onlu with same fitness of genomes
                        curr2 = enumOther.MoveNext() ? enumOther.Current.Value : new Gene(null, null, 0f, GeneType.Enabled, -1);
                    }
                }
            }

            var nodes = new List <Node>(Nodes.Count);

            // Copy Nodes from Genes from Other Genome
            foreach (var item in dict)
            {
                nodes.Add(item.Value);
            }
            Node node;

            for (int i = 0; i < Nodes.Count; i++)
            {
                if (dict.TryGetValue(Nodes[i].Number, out node))
                {
                    ;
                }
                else
                {
                    nodes.Add(Nodes[i].ShallowCopy());
                }
            }

            nodes.Sort(new NodeNumberComparer());


            var child = new Genome(nodes, genes, null);

            // CORRECT LAYER MESS
            if (sameFitness)
            {
                bool notdone = false;
                do
                {
                    notdone = false;
                    foreach (var item in genes)
                    {
                        var g = item.Value;
                        while (g.In.Layer >= g.Out.Layer)
                        {
                            g.Out.Layer++;
                            notdone = true;
                        }
                    }
                }while (notdone);
            }
            return(child);
        }
コード例 #5
0
 public override IEnumerator <EnumGene <T> > GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }
コード例 #6
0
 public override IEnumerator <AnyGene <TAllele> > GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }
コード例 #7
0
 public override IEnumerator <LongGene> GetEnumerator()
 {
     return(Genes.GetEnumerator());
 }