Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemInstanceAnalysis/ProblemInstanceAnalyzer.cs @ 14678

Last change on this file since 14678 was 14678, checked in by abeham, 7 years ago

#2457: worked on problem instance detection

File size: 4.2 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 order = Enumerable.Range(0, kbCharacteristics.Rows)
81        .Select(row => new { Row = row, MSE = kbCharacteristics.GetRow(row).Zip(currentCharacteristics, (a, b) => (a - b) * (a - b)).Average() })
82        .OrderBy(x => x.MSE);
83
84      var instances = kbCharacteristics.RowNames.ToList();
85      while (instances.Count < kbCharacteristics.Rows)
86        instances.Add(instances.Count.ToString(CultureInfo.CurrentCulture.NumberFormat));
87
88      var result = new StringMatrix(instances.Count, 2);
89      var idx = 0;
90      foreach (var o in order) {
91        result[idx, 0] = instances[o.Row];
92        result[idx, 1] = o.MSE.ToString(CultureInfo.CurrentCulture.NumberFormat);
93        idx++;
94      }
95
96      similarInstParam.ActualValue = result;
97
98      return base.Apply();
99    }
100
101    protected abstract DoubleArray GetCharacteristics();
102  }
103}
Note: See TracBrowser for help on using the repository browser.