コード例 #1
0
        internal void darwin(double [] spark)//Hopefully m0ar better than nature!!!!
        {
            DNA  rna       = new DNA();
            bool evolution = new bool();
            int  asteroid  = (int)(cross.Next(1000) * Math.Pow(unit, 2));

            this.populate();
            if (!(spark.Length == 0))
            {
                organism[0].Strand            = spark;
                organism[(int)pop / 2].Strand = spark;
                organism[pop - 1].Strand      = spark;
            }
            this.fitness();
            DNA[] PrevEpoc = organism;
            do
            {
                if (asteroid <= 0)
                {
                    PrevEpoc = organism;
                    pop      = 100 * Program.BYTE_SIZE;

                    motherNature.Afterthought(varArray, ref spark);
                    PrevEpoc = organism;
                    organism = new DNA[pop];
                    for (int c = 0; c < pop; c++)
                    {
                        organism[c] = new DNA();
                    }
                    organism[(int)pop / 2].Strand = spark;
                    organism[pop - 1].Strand      = spark;
                    organism[0] = PrevEpoc[0];
                    this.fitness();
                    asteroid = (int)(cross.Next(100 * unit) * Math.Pow(unit, 2));
                }

                bool entropy = true;
                if (pop <= this.unit * 10)
                {
                    pop      = 100 * Program.BYTE_SIZE;
                    PrevEpoc = organism;
                    organism = new DNA[pop];
                    for (int c = 0; c < pop; c++)
                    {
                        organism[c] = new DNA();
                    }
                    int i = 0;
                    foreach (DNA fossil in PrevEpoc)
                    {
                        organism[i] = fossil;
                        i++;
                    }
                    this.fitness();
                }
                do
                {
                    entropy = false;
                    for (int c = 0; c < pop; c++)
                    {
                        if (organism[c].solver)
                        {
                            answer    = organism[c].Strand;
                            evolution = true;
                            break;
                        }
                        if (evolution)
                        {
                            break;
                        }
                        if (cross.Next(101) > 97)//about 3% chance
                        {
                            this.mutate(ref organism[c]);
                        }
                        if (organism[c].fitlevel < 0)
                        {
                            organism[c] = organism[pop - 1];
                            pop--;
                        }
                        if (c > 0)
                        {
                            if (organism[c].fitlevel < organism[c - 1].fitlevel)
                            {
                                rna             = organism[c - 1];
                                organism[c - 1] = organism[c];
                                organism[c]     = rna;
                                entropy         = true;
                            }
                        }
                    }
                } while (entropy);

                if (evolution)
                {
                    break;
                }

                for (int c = 0; c < pop; c++)
                {
                    bool same = new bool();
                    int  i    = 0;
                    if (pop <= 10)
                    {
                        break;
                    }
                    if (c > 0)
                    {
                        foreach (double d in organism[c].Strand)
                        {
                            if (!(d.Equals(organism[c - 1].Strand[i])))
                            {
                                same = false;
                                break;
                            }
                        }
                    }
                    if (same)
                    {
                        organism[c] = organism[pop - 1];
                        pop--;
                    }
                    else if (organism[c].fitlevel == organism[pop - 1].fitlevel)
                    {
                        if (cross.Next(101) < 77)//about 77% chance
                        {
                            this.mutate(ref organism[c]);
                            this.mutate(ref organism[pop - 1]);
                        }
                        else
                        {
                            pop--;
                        }
                    }
                    else if (organism[c].fitlevel < organism[(int)(pop / 6)].fitlevel)
                    {
                        this.breed(ref organism[c]);
                    }
                    else
                    {
                        this.mutate(ref organism[c]);
                    }
                }

                asteroid--;
            } while (!evolution);
        }