示例#1
0
        public IntDNA MutateBestOnly(IntDNA father)
        {
            var child = father.Fitness > Fitness ? father : this;

            //child.Mutate();
            child.GoodMutation();
            return(child);
        }
示例#2
0
        public IntDNA CrossOverTilde(IntDNA father)
        {
            //return (new DNA(father.Gene ^ Gene));
            var child = new IntDNA(father.Gene ^ Gene);

            child.Mutate();
            return(child);
        }
示例#3
0
        public static IntDNA[] DnaInitialize(int size = 20)
        {
            //var rng = new Random();
            var dnalist = new IntDNA[size];

            for (var i = 0; i < size; i++)
            {
                dnalist[i] = new IntDNA(Rng.Next(0, 65536));
            }
            return(dnalist);
        }
示例#4
0
        internal IntDNA CrossOverAnd(IntDNA father)
        {
            //var sumfitness = father.Fitness + Fitness;
            //var fatherfitness = father.Fitness / sumfitness;
            //var motherfitness =
            //return(new DNA(father.Gene & Gene));
            var child = new IntDNA(father.Gene & Gene);

            child.Mutate();
            return(child);
        }
示例#5
0
        public static void IntDna()
        {
            var bestgene       = 0;
            var bestfitness    = 0;
            var bestgeneration = 0;

            const int size        = 100;
            const int generations = 2000000;

            var dnaarray = IntDNA.DnaInitialize(size);

            for (var generation = 0; generation < generations; generation++)
            {
                //foreach (var dna in dnaarray)
                var generation1 = generation;
                Parallel.ForEach(dnaarray, dna =>
                {
                    dna.CalculateFitness();
                    if (bestfitness >= dna.Fitness)
                    {
                        return;
                    }
                    bestfitness    = dna.Fitness;
                    bestgene       = dna.Gene;
                    bestgeneration = generation1;
                    Console.WriteLine("Fitness: " + bestfitness + " gene: " + bestgene + " generation: " +
                                      bestgeneration);
                });
                var sorteddnalist = dnaarray.OrderByDescending(dna => dna.Fitness).ToList();

                for (var i = 0; i < sorteddnalist.Count - 1; i += 2)
                {
                    dnaarray[i] = sorteddnalist[i].MutateBestOnly(sorteddnalist[i + 1]);
                }
                //Console.WriteLine(generation);
            }

            Console.WriteLine("Fitness: " + bestfitness + " gene: " + bestgene + " generation: " + bestgeneration);
            Console.WriteLine("Fitness: " + Convert.ToString(bestfitness, 2) +
                              " gene: " + Convert.ToString(bestgene, 2) +
                              " generation: " + Convert.ToString(bestgeneration, 2));
        }