Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.Instances.QAPLIB/3.3/OneSizeInstanceProvider.cs @ 17717

Last change on this file since 17717 was 16096, checked in by abeham, 6 years ago

#2457:

  • Changed calculation of correlation length (using limit introduced Hordijk 1996)
  • Changed RuggednessCalculator (no more a HL item)
  • Added additional, information-analysis-based features for directed walks
  • Added generic DirectedWalk algorithm (as described in thesis)
  • Made OneSizeInstanceProvider parametrizable
  • Adapted program for analyzing problem instance reidentification
File size: 5.0 KB
RevLine 
[14691]1using System;
2using System.Collections.Generic;
3using System.Linq;
[15031]4using System.Text.RegularExpressions;
[16096]5using HeuristicLab.Common;
[14691]6
7namespace HeuristicLab.Problems.Instances.QAPLIB {
[16096]8  public class OneSize10InstanceProvider : OneSizeInstanceProvider {
9    public OneSize10InstanceProvider() : base(10) { }
10  }
11  public class OneSize25InstanceProvider : OneSizeInstanceProvider {
12    public OneSize25InstanceProvider() : base(25) { }
13  }
14  public class OneSize50InstanceProvider : OneSizeInstanceProvider {
15    public OneSize50InstanceProvider() : base(50) { }
16  }
17  public class OneSize100InstanceProvider : OneSizeInstanceProvider {
18    public OneSize100InstanceProvider() : base(100) { }
19  }
20
[14691]21  public class OneSizeInstanceProvider : ProblemInstanceProvider<QAPData> {
[16096]22    public override string Name { get { return "One Size (n = " + Dimension + ")"; } }
23    public override string Description { get { return "Instances from various libraries reduced to a dimension of " + Dimension + "."; } }
24    public override Uri WebLink { get { return null; } }
25    public override string ReferencePublication {
26      get {
27        return
28@"A. Beham, E. Pitzer, S. Wagner, M. Affenzeller. 2017.
29Integrating Exploratory Landscape Analysis into Metaheuristic Algorithms.
30Lecture Notes in Computer Science 10671, Las Palmas de Gran Canaria, Spanien, pp. 473-480";
31      }
32    }
33
34    public int Dimension { get; private set; }
35
36    public OneSizeInstanceProvider(int dimension) {
37      Dimension = dimension;
38    }
39
[14691]40    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
41      var drezner = new DreznerQAPInstanceProvider();
[15031]42      foreach (var desc in drezner.GetDataDescriptors()) {
43        var dim = int.Parse(Regex.Match(desc.Name, "dre(?<g>\\d+)").Groups["g"].Value);
[16096]44        if (dim < Dimension) continue;
45        yield return new OneSizeDataDescriptor(desc.Name + (dim == Dimension ? "" : "-" + Dimension), desc.Description, drezner, desc);
[15031]46      }
47      // Microarray instances are all greater than 25 dimensions
[14691]48      var microarray = new MicroarrayQAPInstanceProvider();
[15031]49      foreach (var desc in microarray.GetDataDescriptors()) {
[16096]50        var instance = microarray.LoadData(desc);
51        if (instance.Dimension < Dimension) continue;
52        yield return new OneSizeDataDescriptor(desc.Name + (instance.Dimension == Dimension ? "" : "-" + Dimension), desc.Description, microarray, desc);
[15031]53      }
[14691]54      var qaplib = new QAPLIBInstanceProvider();
[15031]55      foreach (var desc in qaplib.GetDataDescriptors()) {
56        var instance = qaplib.LoadData(desc);
[16096]57        if (instance.Dimension < Dimension) continue;
58        yield return new OneSizeDataDescriptor(desc.Name + (instance.Dimension == Dimension ? "" : "-" + Dimension), desc.Description, qaplib, desc);
[15031]59      }
60      // Taillard's instances are basically from the same distribution
61      // to avoid over-representation in the set only the tai27e are taken and reduced to 25 dimension
[14691]62      var taillard = new TaillardQAPInstanceProvider();
[16096]63      var count = 0;
64      foreach (var desc in taillard.GetDataDescriptors().OrderBy(x => x.Name, new NaturalStringComparer())) {
65        var dim = int.Parse(Regex.Match(desc.Name, "tai(?<g>\\d+)e").Groups["g"].Value);
66        if (dim < Dimension) continue;
67        yield return new OneSizeDataDescriptor(desc.Name + (dim == Dimension ? "" : "-" + Dimension), desc.Description, taillard, desc);
68        if (++count == 10) break;
[15031]69      }
[14691]70    }
71
72    public override QAPData LoadData(IDataDescriptor descriptor) {
73      var desc = (OneSizeDataDescriptor)descriptor;
74      var data = desc.ActualProvider.LoadData(desc.ActualDescriptor);
[16096]75
76      if (data.Dimension <= Dimension) {
[14691]77        return data;
78      }
79      var rand = new Random(data.Dimension);
80
81      var tmp = Enumerable.Range(0, data.Dimension).ToArray();
82      Shuffle(tmp, rand);
83      var throwAway = new bool[data.Dimension];
[16096]84      foreach (var t in tmp.Take(data.Dimension - Dimension))
[14691]85        throwAway[t] = true;
86
[16096]87      var weights = new double[Dimension, Dimension];
88      var distances = new double[Dimension, Dimension];
[14691]89      var k = 0;
90      for (var i = 0; i < data.Dimension; i++) {
91        if (throwAway[i]) continue;
92        var h = 0;
93        for (var j = 0; j < data.Dimension; j++) {
94          if (throwAway[j]) continue;
95          weights[k, h] = data.Weights[i, j];
96          distances[k, h] = data.Distances[i, j];
97          h++;
98        }
99        k++;
100      }
101
102      data.Weights = weights;
103      data.Distances = distances;
104      data.BestKnownAssignment = null;
105      data.BestKnownQuality = null;
[16096]106      data.Dimension = Dimension;
107      data.Name += "-" + Dimension;
108      data.Description += " (reduced to " + Dimension + " dimensions)";
[14691]109
110      return data;
111    }
[15031]112   
[14691]113    private static void Shuffle(int[] p, Random random) {
114      for (var i = p.Length - 1; i > 0; i--) {
115        var swapIndex = random.Next(i + 1);
116        var h = p[swapIndex];
117        p[swapIndex] = p[i];
118        p[i] = h;
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.