Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemInstanceAnalysis/ProblemInstanceAnalyzer.cs @ 16036

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

#2457: working on identification of problem instances

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Globalization;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Analysis.FitnessLandscape {
34  [Item("Problem Instance Similarity Analyzer", "Base class for analyzing whether certain obtained characteristics match the characteristics of already known problem instances.")]
35  [StorableClass]
36  public abstract class ProblemInstanceAnalyzer : SingleSuccessorOperator, IAnalyzer {
37
38    public bool EnabledByDefault {
39      get { return false; }
40    }
41
42    [Storable]
43    private IResultParameter<StringMatrix> similarInstParam;
44    public IResultParameter<StringMatrix> SimilarInstancesParameter {
45      get { return similarInstParam; }
46    }
47
48    [Storable]
49    private IValueParameter<DoubleMatrix> characteristicsParam;
50    public IValueParameter<DoubleMatrix> CharacteristicsParameter {
51      get { return characteristicsParam; }
52    }
53
54    public DoubleMatrix Characteristics {
55      get { return CharacteristicsParameter.Value; }
56      set { CharacteristicsParameter.Value = value; }
57    }
58
59    [StorableConstructor]
60    protected ProblemInstanceAnalyzer(bool deserializing) : base(deserializing) { }
61
62    protected ProblemInstanceAnalyzer(ProblemInstanceAnalyzer original, Cloner cloner)
63      : base(original, cloner) {
64      similarInstParam = cloner.Clone(original.similarInstParam);
65      characteristicsParam = cloner.Clone(original.characteristicsParam);
66    }
67
68    public ProblemInstanceAnalyzer() {
69      Parameters.Add(similarInstParam = new ResultParameter<StringMatrix>("Similar Instances", "Ordered enumeration of similar problem instances to the one currently observed.", "Results", new StringMatrix(new [,] { { "undefined", double.NaN.ToString(CultureInfo.CurrentCulture.NumberFormat) } })));
70      Parameters.Add(characteristicsParam = new ValueParameter<DoubleMatrix>("Characteristics", "The matrix that contains the characteristics data and corresponding problem instance as row name."));
71    }
72
73    public override IOperation Apply() {
74      var kbCharacteristics = Characteristics;
75      if (kbCharacteristics == null) throw new InvalidOperationException("No characteristics are given.");
76
77      var currentCharacteristics = GetCharacteristics();
78      if (currentCharacteristics == null) return base.Apply();
79
80      var means = kbCharacteristics.GetRow(kbCharacteristics.Rows - 2).ToArray();
81      var stdevs = kbCharacteristics.GetRow(kbCharacteristics.Rows - 1).ToArray();
82
83      for (var i = 0; i < means.Length; i++) {
84        currentCharacteristics[i] = (currentCharacteristics[i] - means[i]) / stdevs[i];
85      }
86
87      var order = Enumerable.Range(0, kbCharacteristics.Rows - 2)
88        .Select(row => new { Row = row, MSE = kbCharacteristics.GetRow(row).Zip(currentCharacteristics, (a, b) => (a - b) * (a - b)).Average() })
89        .OrderBy(x => x.MSE);
90
91      var instances = kbCharacteristics.RowNames.ToList();
92      while (instances.Count < kbCharacteristics.Rows - 2)
93        instances.Add(instances.Count.ToString(CultureInfo.CurrentCulture.NumberFormat));
94
95      var result = new StringMatrix(instances.Count, 2);
96      var idx = 0;
97      foreach (var o in order) {
98        result[idx, 0] = instances[o.Row];
99        result[idx, 1] = o.MSE.ToString(CultureInfo.CurrentCulture.NumberFormat);
100        idx++;
101      }
102
103      similarInstParam.ActualValue = result;
104
105      return base.Apply();
106    }
107
108    protected abstract DoubleArray GetCharacteristics();
109  }
110}
Note: See TracBrowser for help on using the repository browser.