Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/ProblemInstanceIdentifier/InstanceDescriptor.cs @ 16101

Last change on this file since 16101 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: 3.7 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using System.Text.RegularExpressions;
4using HeuristicLab.Common;
5using HeuristicLab.Problems.QuadraticAssignment;
6
7namespace ProblemInstanceIdentifier {
8  public class InstanceDescriptor {
9    public string Name { get; set; }
10    public string Cls { get; private set; }
11    public int Dimension { get; set; }
12
13    public string[] FeatureNames { get; set; }
14    public double[] FeatureValues { get; set; }
15
16    public double DescriptionEffort { get; set; }
17   
18    private InstanceDescriptor() { }
19
20    public InstanceDescriptor(string name, string cls, int dimension, string[] names, double[] values) {
21      Name = name;
22      Cls = cls;
23      Dimension = dimension;
24      FeatureNames = names;
25      FeatureValues = values;
26    }
27
28    public InstanceDescriptor(InstanceDescriptor other) {
29      Name = other.Name;
30      Cls = other.Cls;
31      Dimension = other.Dimension;
32      FeatureNames = (string[])other.FeatureNames.Clone();
33      FeatureValues = (double[]) other.FeatureValues.Clone();
34      DescriptionEffort = other.DescriptionEffort;
35    }
36
37    public static InstanceDescriptor FromProblemOnly(QuadraticAssignmentProblem qap) {
38      return new InstanceDescriptor() {
39        Name = qap.Name,
40        Cls = GetClass(qap.Name),
41        Dimension = qap.Weights.Rows,
42        FeatureNames = new string[0],
43        FeatureValues = new double[0],
44        DescriptionEffort = 0
45      };
46    }
47
48    public static string GetClass(string name) {
49      var cls = name.Substring(0, 3);
50      var subCls = name.Last();
51      switch (cls) {
52        case "lip":
53          cls = name.Substring(0, 4) + "-" + subCls;
54          break;
55        case "RAN":
56          cls = name.Substring(0, 4) + "-" + name[name.Length - 2] + name[name.Length - 1];
57          break;
58        case "tai":
59          if (Regex.IsMatch(name, "tai\\d+e\\d+")) cls += "-e";
60          else if (char.IsLetter(subCls)) cls += "-" + subCls;
61          break;
62      }
63      return cls;
64    }
65
66    public double CalculateSimilarity(InstanceDescriptor other) {
67      return FeatureValues.Select((v, i) => (v - other.FeatureValues[i]) * (v - other.FeatureValues[i])).Sum();
68    }
69
70    public override string ToString() {
71      return Name;
72    }
73  }
74
75  public class InstancesStandardizer {
76    private double[] featureMeans;
77    private double[] featureStdevs;
78
79    public IEnumerable<double> GetMeans() {
80      return featureMeans;
81    }
82    public IEnumerable<double> GetStdevs() {
83      return featureStdevs;
84    }
85
86    private InstancesStandardizer() { }
87
88    public static InstancesStandardizer Create(IList<InstanceDescriptor> instances) {
89      var standardizer = new InstancesStandardizer();
90      var featureLength = instances.First().FeatureValues.Length;
91      standardizer.featureMeans =
92        Enumerable.Range(0, featureLength)
93        .Select(x => instances.Select(y => y.FeatureValues[x]).Average()).ToArray();
94      standardizer.featureStdevs =
95        Enumerable.Range(0, featureLength)
96        .Select(x => instances.Select(y => y.FeatureValues[x]).StandardDeviation()).ToArray();
97
98      return standardizer;
99    }
100    public static InstancesStandardizer CreateAndApply(IList<InstanceDescriptor> instances) {
101      var standardizer = Create(instances);
102      standardizer.Apply(instances);
103      return standardizer;
104    }
105
106    public void Apply(IList<InstanceDescriptor> instances) {
107      for (var i = 0; i < instances.Count; i++) {
108        var inst = instances[i];
109        for (var x = 0; x < featureMeans.Length; x++)
110          inst.FeatureValues[x] = (inst.FeatureValues[x] - featureMeans[x]) / featureStdevs[x];
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.