Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15762 was 15031, checked in by abeham, 8 years ago

#2457: worked on code for eurocast paper

File size: 4.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text.RegularExpressions;
5using HeuristicLab.Analysis.FitnessLandscape;
6using HeuristicLab.Common;
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 (Regex.IsMatch(name, "tai\\d+e\\d+")) cls += "-e";
71          else if (char.IsLetter(subCls)) cls += "-" + subCls;
72          break;
73      }
74      return cls;
75    }
76
77    public double CalculateSimilarity(InstanceDescriptor other) {
78      return FeatureValues.Select((v, i) => (v - other.FeatureValues[i]) * (v - other.FeatureValues[i])).Sum();
79    }
80
81    public override string ToString() {
82      return Name;
83    }
84  }
85
86  public class InstancesStandardizer {
87    private double[] featureMeans;
88    private double[] featureStdevs;
89
90    public IEnumerable<double> GetMeans() {
91      return featureMeans;
92    }
93    public IEnumerable<double> GetStdevs() {
94      return featureStdevs;
95    }
96
97    private InstancesStandardizer() { }
98
99    public static InstancesStandardizer Create(IList<InstanceDescriptor> instances) {
100      var standardizer = new InstancesStandardizer();
101      var featureLength = instances.First().FeatureValues.Length;
102      standardizer.featureMeans =
103        Enumerable.Range(0, featureLength)
104        .Select(x => instances.Select(y => y.FeatureValues[x]).Average()).ToArray();
105      standardizer.featureStdevs =
106        Enumerable.Range(0, featureLength)
107        .Select(x => instances.Select(y => y.FeatureValues[x]).StandardDeviation()).ToArray();
108
109      return standardizer;
110    }
111    public static InstancesStandardizer CreateAndApply(IList<InstanceDescriptor> instances) {
112      var standardizer = Create(instances);
113      standardizer.Apply(instances);
114      return standardizer;
115    }
116
117    public void Apply(IList<InstanceDescriptor> instances) {
118      for (var i = 0; i < instances.Count; i++) {
119        var inst = instances[i];
120        for (var x = 0; x < featureMeans.Length; x++)
121          inst.FeatureValues[x] = (inst.FeatureValues[x] - featureMeans[x]) / featureStdevs[x];
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.