コード例 #1
0
        public void MixChromosomesTest_NormalChromosome_NormalPower_TestOutputLength_ShouldPass(int pow)
        {
            var rd          = DefaultResearchParameters.GetDefaultResearchDefinitions(pow);
            var chromosomeA = Chromosome.NewRandomChromosome(rd);
            var chromosomeB = Chromosome.NewRandomChromosome(rd);

            _testOutputHelper.WriteLine(chromosomeA.GeneInBinary());
            _testOutputHelper.WriteLine(chromosomeB.GeneInBinary());

            var output = PostSelection.MixChromosomes(chromosomeA, chromosomeB, 1);

            foreach (var chromosome in output)
            {
                Assert.True(chromosome.GeneInBinary().Length == pow);
                _testOutputHelper.WriteLine(chromosome.GeneInBinary());
            }
        }
コード例 #2
0
        public void MixChromosomesTest_EdgeChromosomes_NormalPower_ShouldPass(int pow)
        {
            var rd = DefaultResearchParameters.GetDefaultResearchDefinitions(pow);
            var highestChromosome = DefaultResearchParameters.GetHigherChromosome(rd);
            var lowestChromosome  = DefaultResearchParameters.GetLowestChromosome(rd);

            _testOutputHelper.WriteLine(highestChromosome.GeneInBinary());
            _testOutputHelper.WriteLine(lowestChromosome.GeneInBinary());

            var output = PostSelection.MixChromosomes(highestChromosome, lowestChromosome, 1);

            foreach (var chromosome in output)
            {
                Assert.Contains("1", chromosome.GeneInBinary());
                Assert.Contains("0", chromosome.GeneInBinary());
                _testOutputHelper.WriteLine(chromosome.GeneInBinary());
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: MateuszSiwiecki/GA
        /*
         *  var functionUnderStudy = new Func<double, double>(x => 0.2 * Math.Pow(x, 3) + 0.1 * Math.Pow(x, 2) - 8 * x);
         *  var fitFunction = new Func<double, double>(x => -(0.2 * Math.Pow(x, 3) + 0.1 * Math.Pow(x, 2) - 8 * x));
         *
         *  var functionUnderStudy = new Func<double, double>(x => x - Math.Pow(x, 2) + Math.Pow(x, 3));
         *  var fitFunction = functionUnderStudy;
         *
         *  var functionUnderStudy = new Func<double, double>(Math.Sin);
         *  var fitFunction = functionUnderStudy;
         */
        public static void Main(string[] args)
        {
            var iterations         = 1000;
            var functionUnderStudy = new Func <double[], double>(x => 0.2 * Math.Pow(x[0], 3) + 0.1 * Math.Pow(x[0], 2) - 8 * x[0]);
            var fitFunction        = new Func <double[], double>(x => Math.Sin(x[0]));
            var cd = new ChromosomeDefinition(10);
            var fc = new Function(functionUnderStudy, fitFunction, -7, 7);
            var rd = new ResearchDefinitions(cd, fc, 100, 0.1, 0.5);
            var listOfMediumAbsFitness = new List <double>();

            var startPop = Fitness.FitPop(
                Chromosome.NewRandomPopulation(
                    rd,
                    rd.Population));

            listOfMediumAbsFitness.Add(startPop.Sum(x => x.Fitness) / startPop.Count);

            var preselectedPop = new RouletteWheel().DrawChromosomes(startPop);

            var nextGenPop     = Fitness.FitPop(PostSelection.CreateNewPopulation(preselectedPop, rd.CrossChance, rd.MutationChance));
            var bestChromosome = new BestChromosome()
            {
                bestChromosome = nextGenPop.Max(x => x),
                generation     = 1
            };

            listOfMediumAbsFitness.Add(nextGenPop.Sum(x => x.Fitness) / nextGenPop.Count);

            for (int i = 1; i < iterations; i++)
            {
                nextGenPop = Fitness.FitPop(PostSelection.CreateNewPopulation(nextGenPop, rd.CrossChance, rd.MutationChance));
                if (bestChromosome.bestChromosome.AbsFitness < nextGenPop.Max(x => x.AbsFitness))
                {
                    bestChromosome.bestChromosome = nextGenPop.FirstOrDefault(x => x.AbsFitness == nextGenPop.Max(y => y.AbsFitness));
                    bestChromosome.generation     = i + 1;
                }
                listOfMediumAbsFitness.Add(nextGenPop.Sum(x => x.Fitness) / nextGenPop.Count);
            }

            listOfMediumAbsFitness.ForEach(Console.WriteLine);
        }
コード例 #4
0
        public void MixChromosomesTest_TryFindCrossingPointOnEndAllel_ShouldPass()
        {
            var rd = DefaultResearchParameters.GetDefaultResearchDefinitions(4);
            var highestChromosome = DefaultResearchParameters.GetHigherChromosome(rd);
            var lowestChromosome  = DefaultResearchParameters.GetLowestChromosome(rd);
            var output            = new List <Chromosome>();
            var found             = false;

            for (int i = 0; i < 1000; i++)
            {
                output = PostSelection.MixChromosomes(highestChromosome, lowestChromosome, 1);
                if (output.All(x => x.GeneInBinary() != "1110"))
                {
                    continue;
                }
                found = true;
                break;
            }
            _testOutputHelper.WriteLine(output[0].GeneInBinary());
            _testOutputHelper.WriteLine(output[1].GeneInBinary());
            Assert.True(found);
        }