コード例 #1
0
 public NumberVectorTests()
 {
     mutationManager     = new IntUniformMutationManager(0, 100);
     evaluator           = new BasicEvaluator();
     populationGenerator = new IntVectorChromosomePopulationGenerator(VECTOR_SIZE, 0, 1, mutationManager, evaluator);
     crossoverManager    = new SinglePointCrossoverManager <int>(mutationManager, evaluator);
 }
コード例 #2
0
        public static void TestChromosomeChanged(this IMutationManager <string> mutationManager)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            // Since there's a certain chance that this test will fail, I want to run it twice
            var passed = false;

            for (int i = 0; i < 4; i++)
            {
                try
                {
                    var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                    var after  = mutationManager.Mutate(before.ToArray());

                    before.AssertAreNotTheSame(after);
                    passed = true;
                }
                catch
                {
                    // Do nothing
                }
            }
            Assert.IsTrue(passed);
        }
        /// <summary>
        /// Creates a population of chromosomes of type VectorChromosome&lt;T&gt; in which each chromosome contains every element exactly once.
        /// </summary>
        public AllElementsVectorChromosomePopulationGenerator(ICollection<T> elements, IMutationManager<T> mutationManager, IEvaluator evaluator)
        {
            if (!elements.Any())
                throw new GeneticAlgorithmException($"{nameof(elements)} is empty");

            this.elements = elements;
            this.mutationManager = mutationManager;
            this.evaluator = evaluator;
        }
コード例 #4
0
 public static void AssertAllValuesAreWithinRange <T>(this IMutationManager <T> mutationManager, int maxValue, int minValue)
 {
     for (int i = 0; i < attempts; i++)
     {
         var value = mutationManager.Mutate(new[] { default(T) }).First().ToInt();
         Assert.IsTrue(value <= maxValue, $"{nameof(value)} ({value}) > {nameof(maxValue)} ({maxValue})");
         Assert.IsTrue(value >= minValue, $"{nameof(value)} ({value}) < {nameof(minValue)} ({minValue})");
     }
 }
コード例 #5
0
        public static void AssertValuesAreScattered <T>(this IMutationManager <T> mutationManager)
        {
            var values = new HashSet <int>();

            for (int i = 0; i < 50; i++)
            {
                var value = mutationManager.Mutate(new[] { default(T) }).First().ToInt();
                values.Add(value.ToInt());
            }

            Assert.IsTrue(values.Count > 8, $"We only got {values.Count} different values");
        }
コード例 #6
0
        public static void CheckMutationsHappenWithRightProbability <T>(this IMutationManager <T> mutationManager, Func <T, bool> isMutated)
        {
            var mutatedGenomes = 0;

            for (int i = 0; i < attempts; i++)
            {
                var newChromosome = mutationManager.Mutate(new[]
                                                           { default(T), default(T), default(T), default(T), default(T) });
                mutatedGenomes += newChromosome.Count(isMutated);
            }

            mutatedGenomes.AssertIsWithinRange(attempts, attempts * 0.1);
        }
コード例 #7
0
        public static void TestAllElementsInEachVector(this IMutationManager <string> mutationManager, int testRuns)
        {
            var elements = new List <string> {
                "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"
            };
            var generator = new AllElementsVectorChromosomePopulationGenerator <string>(elements, null, null);

            for (int i = 0; i < testRuns; i++)
            {
                var before = ((VectorChromosome <string>)generator.GeneratePopulation(1).First()).GetVector();
                var after  = mutationManager.Mutate(before.ToArray());
                before.AssertContainSameElements(after);
            }
        }
コード例 #8
0
        public static void AssertAllValuesAreGenerated <T>(this IMutationManager <T> mutationManager, int maxValue, int minValue)
        {
            var gottenValues = new HashSet <int>();
            var runs         = maxValue - minValue;

            for (int i = 0; i < runs * 5; i++)
            {
                gottenValues.Add(mutationManager.Mutate(new[] { default(T) }).First().ToInt() + 5);
            }

            for (int i = 1; i < runs; i++)
            {
                Assert.IsTrue(gottenValues.Contains(i), $"We didn't get {i}");
            }
        }
コード例 #9
0
        /// <summary>
        /// This class will create chromosomes of type VectorChromosome&lt;int&gt;
        /// </summary>
        /// <param name="vectorSize">The size of the generated chromosomes</param>
        /// <param name="minGenome">The genomes will be equal to or greater than minGenom</param>
        /// <param name="maxGenome">The genomes will be equal to or smaller than minGenom</param>
        /// <param name="mutationManager">A mutation manager to use</param>
        /// <param name="evaluator">An evaluator to use</param>
        public IntVectorChromosomePopulationGenerator(int vectorSize, int minGenome, int maxGenome, IMutationManager <int> mutationManager, IEvaluator evaluator)
        {
            if (vectorSize <= 0)
            {
                throw new GeneticAlgorithmException($"{nameof(vectorSize)} must be bigger than 0. It was {vectorSize}");
            }
            if (maxGenome < minGenome)
            {
                throw new GeneticAlgorithmException($"{nameof(maxGenome)} ({maxGenome}) must be bigger than {nameof(minGenome)} ({minGenome})");
            }

            this.vectorSize      = vectorSize;
            this.minGenome       = minGenome;
            this.maxGenome       = maxGenome;
            this.mutationManager = mutationManager;
            this.evaluator       = evaluator;
        }
コード例 #10
0
        public static void AssertCommonValuesAreMoreLikely <T>(this IMutationManager <T> mutationManager, double bourderValue)
        {
            var smallCount = 0;
            var bigCount   = 0;

            for (int i = 0; i < 10; i++)
            {
                var value = Math.Abs(mutationManager.Mutate(new[] { default(T) }).First().ToInt());
                Console.WriteLine(value);
                if (value >= bourderValue)
                {
                    bigCount++;
                }
                else
                {
                    smallCount++;
                }
            }

            Assert.IsTrue(smallCount > bigCount, "Got to many big genomes");
        }
        /// <summary>
        /// Creates a population of chromosomes of type VectorChromosome&lt;T&gt; by using the elements in element.
        /// </summary>
        public FromElementsVectorChromosomePopulationGenerator(T[] elements, int vectorLength, bool repeatElements, IMutationManager <T> mutationManager, IEvaluator evaluator)
        {
            if (elements.Length == 0)
            {
                throw new GeneticAlgorithmException($"{nameof(elements)} is empty");
            }

            if (vectorLength <= 0)
            {
                throw new GeneticAlgorithmException($"{nameof(vectorLength)} must be greater than 0 (it was {vectorLength})");
            }

            if (!repeatElements && elements.Length < vectorLength)
            {
                throw new GeneticAlgorithmException($"{nameof(elements)}.Count is only {elements.Length}, while {nameof(vectorLength)} is {vectorLength}.");
            }

            this.repeatElements = repeatElements;
            length               = vectorLength;
            this.elements        = elements;
            this.mutationManager = mutationManager;
            this.evaluator       = evaluator;
        }
 public GeneralEdgeRecombinationCrossover(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #13
0
 public K_PointCrossoverManager(int k, IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.k = k;
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #14
0
 /// <summary>
 /// PositionBasedCrossover Works on chromosomes of type VectorChromosome&lt;T&gt;.
 /// It assumes that both parents are of the same length, that every genome appears only once in each parent,
 /// and that both parents contain the same genomes (but probably in different orders).
 /// If one of these conditions isn't met, OrderBasedCrossover may throw an exception.
 ///
 /// Also, the Equals method must be implemented for type T.
 ///  </summary>
 public PositionBasedCrossoverManager(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #15
0
 /// <summary>
 /// Ordered crossover Works on chromosomes of type VectorChromosome&lt;T&gt;.
 /// It assumes that both parents are of the same length, that every genome appears only once in each parent,
 /// and that both parents contain the same genomes (but probably in different orders).
 /// If one of these conditions isn't met, OrderCrossover may throw an exception.
 ///
 /// Also, the Equals method must be implemented for type T.
 ///  </summary>
 public OrderCrossover(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #16
0
 /// <summary>
 /// HeuristicCrossover Works on chromosomes of type VectorChromosome&lt;T&gt;.
 /// It assumes that both parents are of the same length, that every genome appears only once in each parent,
 /// and that both parents contain the same genomes (but probably in different orders).
 /// If one of these conditions isn't met, HeuristicCrossover may throw an exception.
 ///
 /// Also, the Equals method must be implemented for type T.
 /// </summary>
 public HeuristicCrossover(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #17
0
 public SinglePointCrossoverManager(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     kPointCrossover = new K_PointCrossoverManager <T>(1, mutationManager, evaluator);
 }
コード例 #18
0
 public UniformCrossoverManager(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
 /// <summary>
 /// AlternatingPositionCrossover Works on chromosomes of type VectorChromosome&lt;T&gt;.
 /// It assumes that both parents are of the same length, that every genome appears only once in each parent,
 /// and that both parents contain the same genomes (but probably in different orders).
 /// If one of these conditions isn't met, AlternatingPositionCrossover may throw an exception.
 ///
 /// Also, the Equals method must be implemented for type T.
 ///  </summary>
 public AlternatingPositionCrossover(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #20
0
 /// <summary>
 /// PartiallyMatchedCrossover Works on chromosomes of type VectorChromosome&lt;T&gt;.
 /// It assumes that both parents are of the same length, that every genome appears only once in each parent,
 /// and that both parents contain the same genomes (but probably in different orders).
 /// If one of these conditions isn't met, PartiallyMatchedCrossover may throw an exception.
 ///
 /// Also, the Equals method must be implemented for type T.
 /// </summary>
 public PartiallyMappedCrossover(IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #21
0
 public VectorChromosome(T[] vector, IMutationManager <T> mutationManager, IEvaluator evaluator)
 {
     this.vector          = vector;
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }
コード例 #22
0
 public BinaryVectorChromosomePopulationGenerator(int vectorSize, IMutationManager <bool> mutationManager, IEvaluator evaluator)
 {
     this.vectorSize      = vectorSize;
     this.mutationManager = mutationManager;
     this.evaluator       = evaluator;
 }