source: branches/PerformanceComparison/ProblemInstanceIdentifier/InstanceDescriptor.cs @ 14691

Last change on this file since 14691 was 14691, checked in by abeham, 5 years ago

#2457: working on identification of problem instances

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