Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Tests/HeuristicLab.Scripting-3.3/Script Sources/GAQAPScriptSource.cs @ 13689

Last change on this file since 13689 was 11789, checked in by jkarder, 10 years ago

#2262: refactored scripting unit tests

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