#region License Information /* HeuristicLab * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Globalization; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.FitnessLandscape { [Item("Problem Instance Similarity Analyzer", "Base class for analyzing whether certain obtained characteristics match the characteristics of already known problem instances.")] [StorableClass] public abstract class ProblemInstanceAnalyzer : SingleSuccessorOperator, IAnalyzer { public bool EnabledByDefault { get { return false; } } [Storable] private IResultParameter similarInstParam; public IResultParameter SimilarInstancesParameter { get { return similarInstParam; } } [Storable] private IValueParameter characteristicsParam; public IValueParameter CharacteristicsParameter { get { return characteristicsParam; } } public DoubleMatrix Characteristics { get { return CharacteristicsParameter.Value; } set { CharacteristicsParameter.Value = value; } } [StorableConstructor] protected ProblemInstanceAnalyzer(bool deserializing) : base(deserializing) { } protected ProblemInstanceAnalyzer(ProblemInstanceAnalyzer original, Cloner cloner) : base(original, cloner) { similarInstParam = cloner.Clone(original.similarInstParam); characteristicsParam = cloner.Clone(original.characteristicsParam); } public ProblemInstanceAnalyzer() { Parameters.Add(similarInstParam = new ResultParameter("Similar Instances", "Ordered enumeration of similar problem instances to the one currently observed.", "Results", new StringMatrix(new [,] { { "undefined", double.NaN.ToString(CultureInfo.CurrentCulture.NumberFormat) } }))); Parameters.Add(characteristicsParam = new ValueParameter("Characteristics", "The matrix that contains the characteristics data and corresponding problem instance as row name.")); } public override IOperation Apply() { var kbCharacteristics = Characteristics; if (kbCharacteristics == null) throw new InvalidOperationException("No characteristics are given."); var currentCharacteristics = GetCharacteristics(); if (currentCharacteristics == null) return base.Apply(); var order = Enumerable.Range(0, kbCharacteristics.Rows) .Select(row => new { Row = row, MSE = kbCharacteristics.GetRow(row).Zip(currentCharacteristics, (a, b) => (a - b) * (a - b)).Average() }) .OrderBy(x => x.MSE); var instances = kbCharacteristics.RowNames.ToList(); while (instances.Count < kbCharacteristics.Rows) instances.Add(instances.Count.ToString(CultureInfo.CurrentCulture.NumberFormat)); var result = new StringMatrix(instances.Count, 2); var idx = 0; foreach (var o in order) { result[idx, 0] = instances[o.Row]; result[idx, 1] = o.MSE.ToString(CultureInfo.CurrentCulture.NumberFormat); idx++; } similarInstParam.ActualValue = result; return base.Apply(); } protected abstract DoubleArray GetCharacteristics(); } }