1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 |
|
---|
4 | using HeuristicLab.Analysis;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
7 | using HeuristicLab.Problems.Instances.QAPLIB;
|
---|
8 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
9 | using HeuristicLab.Random;
|
---|
10 |
|
---|
11 | public 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 | } |
---|