[14690] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[14678] | 4 | using HeuristicLab.Analysis.FitnessLandscape;
|
---|
[14690] | 5 | using HeuristicLab.Common;
|
---|
[14678] | 6 | using HeuristicLab.Data;
|
---|
[14690] | 7 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
[14678] | 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; }
|
---|
[14690] | 15 |
|
---|
| 16 | public string[] FeatureNames { get; set; }
|
---|
| 17 | public double[] FeatureValues { get; set; }
|
---|
[14678] | 18 |
|
---|
| 19 | private InstanceDescriptor() { }
|
---|
| 20 |
|
---|
[14690] | 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]
|
---|
[14678] | 44 | };
|
---|
[14690] | 45 | }
|
---|
| 46 |
|
---|
| 47 | public static InstanceDescriptor FromPaths(QuadraticAssignmentProblem qap, List<List<Tuple<Permutation, double>>> trajectories) {
|
---|
[14691] | 48 | var result = PermutationPathAnalysis.GetCharacteristics(trajectories);
|
---|
| 49 |
|
---|
[14678] | 50 | return new InstanceDescriptor() {
|
---|
| 51 | Name = qap.Name,
|
---|
| 52 | Cls = GetClass(qap.Name),
|
---|
| 53 | Dimension = qap.Weights.Rows,
|
---|
[14691] | 54 | FeatureNames = result.GetNames(),
|
---|
| 55 | FeatureValues = result.GetValues()
|
---|
[14678] | 56 | };
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[14690] | 59 | public static string GetClass(string name) {
|
---|
[14678] | 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) {
|
---|
[14690] | 77 | return FeatureValues.Select((v, i) => (v - other.FeatureValues[i]) * (v - other.FeatureValues[i])).Sum();
|
---|
[14678] | 78 | }
|
---|
| 79 |
|
---|
| 80 | public override string ToString() {
|
---|
| 81 | return Name;
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[14690] | 84 |
|
---|
| 85 | public class InstancesStandardizer {
|
---|
| 86 | private double[] featureMeans;
|
---|
| 87 | private double[] featureStdevs;
|
---|
| 88 |
|
---|
[14691] | 89 | public IEnumerable<double> GetMeans() {
|
---|
| 90 | return featureMeans;
|
---|
| 91 | }
|
---|
| 92 | public IEnumerable<double> GetStdevs() {
|
---|
| 93 | return featureStdevs;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[14690] | 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 | }
|
---|
[14691] | 110 | public static InstancesStandardizer CreateAndApply(IList<InstanceDescriptor> instances) {
|
---|
| 111 | var standardizer = Create(instances);
|
---|
| 112 | standardizer.Apply(instances);
|
---|
| 113 | return standardizer;
|
---|
| 114 | }
|
---|
[14690] | 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 | }
|
---|
[14678] | 124 | }
|
---|