コード例 #1
0
        public void PartiallyMatchedCrossoverTest2()
        {
            Organism parent1 = new Organism();
            Organism parent2 = new Organism();

            parent1.Chromosomes.Add(new Chromosome(3, "001110"));
            parent2.Chromosomes.Add(new Chromosome(3, "110001"));

            IRandom rand = new Deterministic(1, 0);
            PartiallyMatchedCrossover linker = new PartiallyMatchedCrossover(rand, 1);
            var children = linker.CrossLink(parent1, parent2);

            Assert.AreEqual("110001", children.Item1.ToString());
            Assert.AreEqual("001110", children.Item2.ToString());
        }
コード例 #2
0
        public void PmxOperatorShouldReturnCorrectChildren()
        {
            IGene[] parent1 = new IGene[]
            {
                new UintGene(1), new UintGene(2), new UintGene(3),
                new UintGene(5), new UintGene(4), new UintGene(6),
                new UintGene(7), new UintGene(8), new UintGene(9)
            };
            IGene[] parent2 = new IGene[]
            {
                new UintGene(4), new UintGene(5), new UintGene(2),
                new UintGene(1), new UintGene(8), new UintGene(7),
                new UintGene(6), new UintGene(9), new UintGene(3)
            };
            IGene[] expectedChild1 = new IGene[]
            {
                new UintGene(8), new UintGene(1), new UintGene(2),
                new UintGene(5), new UintGene(4), new UintGene(6),
                new UintGene(7), new UintGene(9), new UintGene(3)
            };
            IGene[] expectedChild2 = new IGene[]
            {
                new UintGene(5), new UintGene(2), new UintGene(3),
                new UintGene(1), new UintGene(8), new UintGene(7),
                new UintGene(6), new UintGene(4), new UintGene(9)
            };
            int cutPoint1 = 2, cutPoint2 = 6;

            IGene[] child1, child2;
            var     pmx = new PartiallyMatchedCrossover();

            pmx.Run(parent1, parent2, out child1, out child2, cutPoint1, cutPoint2);
            bool areFirstChildrenEqual = true, areSecondChildrenEqual = true;

            for (int i = 0; i < expectedChild1.Length; ++i)
            {
                areFirstChildrenEqual &= expectedChild1[i].Equals(child1[i]);
            }
            for (int i = 0; i < expectedChild2.Length; ++i)
            {
                areSecondChildrenEqual &= expectedChild2[i].Equals(child2[i]);
            }
            Assert.AreEqual(true, areFirstChildrenEqual);
            Assert.AreEqual(true, areSecondChildrenEqual);
        }
コード例 #3
0
        public void PartiallyMatchedCrossoverApplyTest()
        {
            TestRandom  random = new TestRandom();
            Permutation parent1, parent2, expected, actual;

            // The following test is based on an example from Larranaga, 1999. Genetic Algorithms for the Traveling Salesman Problem.
            random.Reset();
            random.IntNumbers = new int[] { 3, 5 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 6, 4, 0, 5, 7, 1, 3 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 6, 7, 3, 4, 5, 1, 0 });
            Assert.IsTrue(expected.Validate());
            actual = PartiallyMatchedCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));
            // The following test is based on an example from Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press. p. 134.
            random.Reset();
            random.IntNumbers = new int[] { 5, 7 };
            parent1           = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            Assert.IsTrue(parent1.Validate());
            parent2 = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 5, 6, 0, 9, 1, 3, 8, 4, 7 });
            Assert.IsTrue(parent2.Validate());
            expected = new Permutation(PermutationTypes.RelativeUndirected, new int[] { 2, 1, 3, 0, 9, 5, 6, 7, 4, 8 });
            Assert.IsTrue(expected.Validate());
            actual = PartiallyMatchedCrossover.Apply(random, parent1, parent2);
            Assert.IsTrue(actual.Validate());
            Assert.IsTrue(Auxiliary.PermutationIsEqualByPosition(expected, actual));

            // perform a test when the two permutations are of unequal length
            random.Reset();
            bool exceptionFired = false;

            try {
                PartiallyMatchedCrossover.Apply(random, new Permutation(PermutationTypes.RelativeUndirected, 8), new Permutation(PermutationTypes.RelativeUndirected, 6));
            }
            catch (System.ArgumentException) {
                exceptionFired = true;
            }
            Assert.IsTrue(exceptionFired);
        }
コード例 #4
0
    public override void Main()
    {
        DateTime start = DateTime.UtcNow;

        QuadraticAssignmentProblem qap;

        if (vars.Contains("qap"))
        {
            qap = vars.qap;
        }
        else
        {
            var provider = new DreznerQAPInstanceProvider();
            var instance = provider.GetDataDescriptors().Single(x => x.Name == "dre56");
            var data     = provider.LoadData(instance);
            qap = new QuadraticAssignmentProblem();
            qap.Load(data);
            vars.qap = qap;
        }

        const uint   seed         = 0;
        const int    popSize      = 100;
        const int    generations  = 1000;
        const double mutationRate = 0.05;

        var random     = new MersenneTwister(seed);
        var population = new Permutation[popSize];
        var qualities  = new double[popSize];
        var nextGen    = new Permutation[popSize];
        var nextQual   = new double[popSize];

        var qualityChart = new DataTable("Quality Chart");
        var qualityRow   = new DataRow("Best Quality");

        qualityChart.Rows.Add(qualityRow);
        vars.qualityChart = qualityChart;

        for (int i = 0; i < popSize; i++)
        {
            population[i] = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random);
            qualities[i]  = QAPEvaluator.Apply(population[i], qap.Weights, qap.Distances);
        }
        var bestQuality           = qualities.Min();
        var bestQualityGeneration = 0;

        for (int g = 0; g < generations; g++)
        {
            var parents = population.SampleProportional(random, 2 * popSize, qualities, windowing: true, inverseProportional: true).ToArray();
            for (int i = 0; i < popSize; i++)
            {
                nextGen[i] = PartiallyMatchedCrossover.Apply(random, parents[i * 2], parents[i * 2 + 1]);
                if (random.NextDouble() < mutationRate)
                {
                    Swap2Manipulator.Apply(random, nextGen[i]);
                }
                nextQual[i] = QAPEvaluator.Apply(nextGen[i], qap.Weights, qap.Distances);
                if (nextQual[i] < bestQuality)
                {
                    bestQuality           = nextQual[i];
                    bestQualityGeneration = g;
                }
            }
            qualityRow.Values.Add(bestQuality);
            Array.Copy(nextGen, population, popSize);
            Array.Copy(nextQual, qualities, popSize);
        }

        vars.elapsed            = new TimeSpanValue(DateTime.UtcNow - start);
        vars.bestQuality        = bestQuality;
        vars.bestQualityFoundAt = bestQualityGeneration;
    }