1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
8 | using HeuristicLab.Problems.QuadraticAssignment;
|
---|
9 |
|
---|
10 | namespace 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 features = QAPDirectedWalk.Calculate(trajectories).ToDictionary(x => x.Name, x => x.Value);
|
---|
49 |
|
---|
50 | return new InstanceDescriptor() {
|
---|
51 | Name = qap.Name,
|
---|
52 | Cls = GetClass(qap.Name),
|
---|
53 | Dimension = qap.Weights.Rows,
|
---|
54 | FeatureNames = features.Keys.ToArray(),
|
---|
55 | FeatureValues = features.Values.Select(x => ((DoubleValue)x).Value).ToArray()
|
---|
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 | private InstancesStandardizer() { }
|
---|
90 |
|
---|
91 | public static InstancesStandardizer Create(IList<InstanceDescriptor> instances) {
|
---|
92 | var standardizer = new InstancesStandardizer();
|
---|
93 | var featureLength = instances.First().FeatureValues.Length;
|
---|
94 | standardizer.featureMeans =
|
---|
95 | Enumerable.Range(0, featureLength)
|
---|
96 | .Select(x => instances.Select(y => y.FeatureValues[x]).Average()).ToArray();
|
---|
97 | standardizer.featureStdevs =
|
---|
98 | Enumerable.Range(0, featureLength)
|
---|
99 | .Select(x => instances.Select(y => y.FeatureValues[x]).StandardDeviation()).ToArray();
|
---|
100 |
|
---|
101 | return standardizer;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public void Apply(IList<InstanceDescriptor> instances) {
|
---|
105 | for (var i = 0; i < instances.Count; i++) {
|
---|
106 | var inst = instances[i];
|
---|
107 | for (var x = 0; x < featureMeans.Length; x++)
|
---|
108 | inst.FeatureValues[x] = (inst.FeatureValues[x] - featureMeans[x]) / featureStdevs[x];
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|