Example #1
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);
        }
Example #2
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;
    }