Esempio n. 1
0
        public void Compare_NullSecondParameter()
        {
            var chromosome = new Mock <Chromosome <string> >(string.Empty);

            var comparer = new ChromosomeComparer <string>();

            Action action = () => comparer.Compare(chromosome.Object, null);

            action.ShouldThrow <ArgumentNullException>("Second parameter is null.");
        }
        public void Compare_NullSecondParameter()
        {
            var chromosome = new Mock<Chromosome<string>>(string.Empty);

            var comparer = new ChromosomeComparer<string>();

            Action action = () => comparer.Compare(chromosome.Object, null);

            action.ShouldThrow<ArgumentNullException>("Second parameter is null.");
        }
Esempio n. 3
0
        public void Solve()
        {
            if (Settings.MutationType == MutationType.AfterSelection)
            {
                Settings.MutationChance = 0;
            }
            if (Settings.MutationType == MutationType.BeforeSelection)
            {
                Settings.MutationChanceAfterCrossing = 0;
            }

            FirstMutation = new Dictionary <Chromosome, Chromosome>();

            foreach (var item in InitialChromosomes)
            {
                Chromosome chr = item.Mutate(Settings.MutationChance);
                chr.GId = Id;
                if (!item.EqualByGenes(chr))
                {
                    if (chr.IsValid())
                    {
                        FirstMutation.Add(item, chr);
                    }
                }
            }

            AfterFirstMutation = new List <Chromosome>();
            AfterFirstMutation.AddRange(InitialChromosomes);


            AfterFirstMutation.AddRange(FirstMutation.Values.AsEnumerable());
            foreach (var item in FirstMutation.Keys)
            {
                if (FirstMutation[item].IsValid())
                {
                    AfterFirstMutation.RemoveAll(ch => ch.EqualByGenes(item));
                }
            }


            Pairs1 = new List <ChromosomePair>();
            for (int i = 0; i < AfterFirstMutation.Count / 2; i++)
            {
                Pairs1.Add(SelectionService.GetPair(AfterFirstMutation, Settings.SelectionType));
            }

            Winners1 = Pairs1.Select(p => (p.Chr1.F > p.Chr2.F) ? p.Chr1 : p.Chr2).ToList();

            Pairs2 = new List <ChromosomePair>();
            for (int i = 0; i < AfterFirstMutation.Count / 2; i++)
            {
                Pairs2.Add(SelectionService.GetPair(AfterFirstMutation, Settings.SelectionType));
            }

            Winners2 = Pairs2.Select(p => (p.Chr1.F > p.Chr2.F) ? p.Chr1 : p.Chr2).ToList();

            PairsForGeneticOperation = new List <ChromosomePair>();
            for (int i = 0; i < Winners1.Count; i++)
            {
                PairsForGeneticOperation.Add(new ChromosomePair()
                {
                    Chr1 = Winners1[i], Chr2 = Winners2[i]
                });
            }

            GeneticOperationRezults = new List <Chromosome>();
            foreach (var item in PairsForGeneticOperation)
            {
                List <Chromosome> pairRezult = CrossingService.DoCrossing(item, Settings.CrossingGenNumber, Settings.MutationChanceAfterCrossing);
                for (int i = 0; i < pairRezult.Count; i++)
                {
                    pairRezult[i].GId = Id;
                }
                GeneticOperationRezults.AddRange(pairRezult.Where(p => p.IsValid()));
            }

            AllFinishChromosome = new List <Chromosome>();
            AllFinishChromosome.AddRange(AfterFirstMutation);
            AllFinishChromosome.AddRange(GeneticOperationRezults);

            ChromosomeComparer comparer = new ChromosomeComparer();

            Output         = AllFinishChromosome.Distinct(comparer).OrderByDescending(ch => ch.F).Take(Settings.SurvivedCount).ToList();
            BestChromosome = Output[0];
            BestF          = BestChromosome.F;

            IsSolved = true;
        }
Esempio n. 4
0
 public void SetUp()
 {
     unit = new ChromosomeComparer <double>();
 }
Esempio n. 5
0
        public void Solve()
        {
            if (Settings.MutationType == MutationType.AfterSelection)
                Settings.MutationChance = 0;
            if (Settings.MutationType == MutationType.BeforeSelection)
                Settings.MutationChanceAfterCrossing = 0;

            FirstMutation = new Dictionary<Chromosome, Chromosome>();

            foreach (var item in InitialChromosomes)
            {
                Chromosome chr = item.Mutate(Settings.MutationChance);
                chr.GId = Id;
                if (!item.EqualByGenes(chr))
                    if (chr.IsValid())
                        FirstMutation.Add(item, chr);
            }

            AfterFirstMutation = new List<Chromosome>();
            AfterFirstMutation.AddRange(InitialChromosomes);

            AfterFirstMutation.AddRange(FirstMutation.Values.AsEnumerable());
            foreach (var item in FirstMutation.Keys)
            {
                if (FirstMutation[item].IsValid())
                    AfterFirstMutation.RemoveAll(ch => ch.EqualByGenes(item));
            }

            Pairs1 = new List<ChromosomePair>();
            for (int i = 0; i < AfterFirstMutation.Count / 2; i++)
            {
                Pairs1.Add(SelectionService.GetPair(AfterFirstMutation, Settings.SelectionType));
            }

            Winners1 = Pairs1.Select(p => (p.Chr1.F > p.Chr2.F) ? p.Chr1 : p.Chr2).ToList();

            Pairs2 = new List<ChromosomePair>();
            for (int i = 0; i < AfterFirstMutation.Count / 2; i++)
            {
                Pairs2.Add(SelectionService.GetPair(AfterFirstMutation, Settings.SelectionType));
            }

            Winners2 = Pairs2.Select(p => (p.Chr1.F > p.Chr2.F) ? p.Chr1 : p.Chr2).ToList();

            PairsForGeneticOperation = new List<ChromosomePair>();
            for (int i = 0; i < Winners1.Count; i++)
            {
                PairsForGeneticOperation.Add(new ChromosomePair() { Chr1 = Winners1[i], Chr2 = Winners2[i] });
            }

            GeneticOperationRezults = new List<Chromosome>();
            foreach (var item in PairsForGeneticOperation)
            {
                List<Chromosome> pairRezult = CrossingService.DoCrossing(item, Settings.CrossingGenNumber, Settings.MutationChanceAfterCrossing);
                for (int i = 0; i < pairRezult.Count; i++)
                {
                    pairRezult[i].GId = Id;
                }
                GeneticOperationRezults.AddRange(pairRezult.Where(p => p.IsValid()));
            }

            AllFinishChromosome = new List<Chromosome>();
            AllFinishChromosome.AddRange(AfterFirstMutation);
            AllFinishChromosome.AddRange(GeneticOperationRezults);

            ChromosomeComparer comparer = new ChromosomeComparer();
            Output = AllFinishChromosome.Distinct(comparer).OrderByDescending(ch => ch.F).Take(Settings.SurvivedCount).ToList();
            BestChromosome = Output[0];
            BestF = BestChromosome.F;

            IsSolved = true;
        }