Exemple #1
0
        public int bitCount(int INDIVIDUALS, int STRING_LENGTH, int MUTATE)
        {
            Genetic gen = new Genetic(FitnessFunctionCountOnes);
            gen.InitalizeGeneration(INDIVIDUALS, STRING_LENGTH, MUTATE);
            gen.SetBreedType(breed);
            gen.SetCrossType(cross);

            int? bestFitness = 0;

            int mileStoneCount = 0;

            List<Genome> generationData;

            do
            {
                generationData = gen.Breed();

                if (gen.currentGeneration[0].fitness > bestFitness )
                {
                    mileStoneCount += 1;
                    bestFitness = gen.currentGeneration[0].fitness;
                    //Console.WriteLine("Best Fitness " + gen.currentGeneration[0].fitness + " Current Generation: " + gen.generationCount);
                    //Console.WriteLine("Current String: " + gen.currentGeneration[0].genome.MakeString());
                    //Console.WriteLine();
                    //WriteResult("Tournament", "Single-Point", ITERATION, INDIVIDUALS, STRING_LENGTH, MUTATE, gen.generationCount, mileStoneCount);
                }

            } while (gen.currentGeneration[0].fitness < STRING_LENGTH);
            return gen.generationCount;
        }
Exemple #2
0
        public int defaultInitString(int INDIVIDUALS, int STRING_LENGTH, int MUTATE)
        {
            Genetic gen = new Genetic(FitnessFunctionTargetString);
            gen.InitalizeGeneration(INDIVIDUALS, STRING_LENGTH, MUTATE);
            gen.SetBreedType(breed);
            gen.SetCrossType(cross);

            int? bestFitness = 0;

            var generationData = gen.Breed();
            string removeN = BitArrayToStr(gen.currentGeneration[0].genome);

            while (removeN.CompareTo(System.Text.Encoding.UTF8.GetString(targetString)) != 0)
            {
                generationData = gen.Breed();

                if (gen.currentGeneration[0].fitness > bestFitness)
                {
                    removeN = BitArrayToStr(gen.currentGeneration[0].genome);
                    char[] invalid = System.IO.Path.GetInvalidFileNameChars();

                    bestFitness = gen.currentGeneration[0].fitness;
                    Console.WriteLine("Best Fitness " + gen.currentGeneration[0].fitness + " Current Generation: " + gen.generationCount);
                    Console.WriteLine("Current String: " + new String(removeN.Where(c => !invalid.Contains(c)).ToArray()));
                    Console.WriteLine();
                }

            }
            return gen.generationCount;
        }