public void GetDreznerQAPInstanceTest()
        {
            var           target             = new DreznerQAPInstanceProvider();
            StringBuilder erroneousInstances = new StringBuilder();
            int           count = 0;

            foreach (var id in target.GetDataDescriptors())
            {
                try {
                    target.LoadData(id);
                } catch (Exception ex) {
                    erroneousInstances.AppendLine(id.Name + ": " + ex.Message);
                }
                count++;
            }
            Assert.IsTrue(count > 0, "No problem instances were found.");
            Assert.IsTrue(erroneousInstances.Length == 0, "Some instances could not be parsed: " + Environment.NewLine + erroneousInstances.ToString());
        }
        private CSharpScript CreateGAQAPScript()
        {
            var script = new CSharpScript {
                Name        = ScriptItemName,
                Description = ScriptItemDescription
            };

            #region Variables
            var provider = new DreznerQAPInstanceProvider();
            var instance = provider.GetDataDescriptors().Single(x => x.Name == ProblemInstanceName);
            var data     = provider.LoadData(instance);
            var problem  = new QuadraticAssignmentProblem();
            problem.Load(data);
            script.VariableStore.Add(ProblemInstanceName, problem);
            #endregion
            #region Code
            script.Code = ScriptSources.GAQAPScriptSource;
            #endregion
            return(script);
        }
Example #3
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;
    }