source: trunk/sources/HeuristicLab.Tests/Test Resources/Script Sources/GA_QAP_Script.cs @ 11514

Last change on this file since 11514 was 11514, checked in by jkarder, 6 years ago

#2211:

  • updated/added unit tests
    • added AssemblyInitialize method to load all plugins, create output directories for (script) samples and initialize the MainForm
    • script code is now stored in test resource files
    • refactored unit tests
  • updated (script) samples
  • added Test.cmd
File size: 2.6 KB
Line 
1using System;
2using System.Linq;
3
4using HeuristicLab.Analysis;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.PermutationEncoding;
7using HeuristicLab.MainForm;
8using HeuristicLab.Problems.Instances.QAPLIB;
9using HeuristicLab.Problems.QuadraticAssignment;
10using HeuristicLab.Random;
11
12public class GAQAPScript : HeuristicLab.Scripting.CSharpScriptBase {
13  public override void Main() {
14    DateTime start = DateTime.UtcNow;
15
16    QuadraticAssignmentProblem qap;
17    if (vars.Contains("qap")) qap = vars.qap;
18    else {
19      var provider = new DreznerQAPInstanceProvider();
20      var instance = provider.GetDataDescriptors().Single(x => x.Name == "dre56");
21      var data = provider.LoadData(instance);
22      qap = new QuadraticAssignmentProblem();
23      qap.Load(data);
24      vars.qap = qap;
25    }
26
27    const uint seed = 0;
28    const int popSize = 100;
29    const int generations = 1000;
30    const double mutationRate = 0.05;
31
32    var random = new MersenneTwister(seed);
33    var population = new Permutation[popSize];
34    var qualities = new double[popSize];
35    var nextGen = new Permutation[popSize];
36    var nextQual = new double[popSize];
37
38    var qualityChart = new DataTable("Quality Chart");
39    var qualityRow = new DataRow("Best Quality");
40    qualityChart.Rows.Add(qualityRow);
41    vars.qualityChart = qualityChart;
42    MainFormManager.MainForm.ShowContent(qualityChart);
43
44    for (int i = 0; i < popSize; i++) {
45      population[i] = new Permutation(PermutationTypes.Absolute, qap.Weights.Rows, random);
46      qualities[i] = QAPEvaluator.Apply(population[i], qap.Weights, qap.Distances);
47    }
48    var bestQuality = qualities.Min();
49    var bestQualityGeneration = 0;
50
51    for (int g = 0; g < generations; g++) {
52      var parents = population.SampleProportional(random, 2 * popSize, qualities, windowing: true, inverseProportional: true).ToArray();
53      for (int i = 0; i < popSize; i++) {
54        nextGen[i] = PartiallyMatchedCrossover.Apply(random, parents[i * 2], parents[i * 2 + 1]);
55        if (random.NextDouble() < mutationRate) Swap2Manipulator.Apply(random, nextGen[i]);
56        nextQual[i] = QAPEvaluator.Apply(nextGen[i], qap.Weights, qap.Distances);
57        if (nextQual[i] < bestQuality) {
58          bestQuality = nextQual[i];
59          bestQualityGeneration = g;
60        }
61      }
62      qualityRow.Values.Add(bestQuality);
63      Array.Copy(nextGen, population, popSize);
64      Array.Copy(nextQual, qualities, popSize);
65    }
66
67    vars.elapsed = new TimeSpanValue(DateTime.UtcNow - start);
68    vars.bestQuality = bestQuality;
69    vars.bestQualityFoundAt = bestQualityGeneration;
70  }
71}
Note: See TracBrowser for help on using the repository browser.