#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[StorableClass]
public class SymbolicDataAnalysisExpressionTreeSimilarityCalculator : SingleSuccessorOperator {
private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
private const string CurrentSymbolicExpressionTreeParameterName = "CurrentSymbolicExpressionTree";
private const string SimilarityValuesParmeterName = "Similarity";
// comparer parameters
private const string MatchVariablesParameterName = "MatchVariableNames";
private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
private const string MatchConstantValuesParameterName = "MatchConstantValues";
public IScopeTreeLookupParameter SymbolicExpressionTreeParameter {
get { return (IScopeTreeLookupParameter)Parameters[SymbolicExpressionTreeParameterName]; }
}
public IValueParameter CurrentSymbolicExpressionTreeParameter {
get { return (IValueParameter)Parameters[CurrentSymbolicExpressionTreeParameterName]; }
}
public ILookupParameter MatchVariableNamesParameter {
get { return (ILookupParameter)Parameters[MatchVariablesParameterName]; }
}
public ILookupParameter MatchVariableWeightsParameter {
get { return (ILookupParameter)Parameters[MatchVariableWeightsParameterName]; }
}
public ILookupParameter MatchConstantValuesParameter {
get { return (ILookupParameter)Parameters[MatchConstantValuesParameterName]; }
}
public ILookupParameter SimilarityParameter {
get { return (ILookupParameter)Parameters[SimilarityValuesParmeterName]; }
}
public ISymbolicExpressionTree CurrentSymbolicExpressionTree {
get { return CurrentSymbolicExpressionTreeParameter.Value; }
set { CurrentSymbolicExpressionTreeParameter.Value = value; }
}
public SymbolicExpressionTreeNodeSimilarityComparer SimilarityComparer { get; set; }
public int MaximumTreeDepth { get; set; }
protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(
SymbolicDataAnalysisExpressionTreeSimilarityCalculator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(this, cloner);
}
[StorableConstructor]
protected SymbolicDataAnalysisExpressionTreeSimilarityCalculator(bool deserializing)
: base(deserializing) {
}
public SymbolicDataAnalysisExpressionTreeSimilarityCalculator()
: base() {
Parameters.Add(new ScopeTreeLookupParameter(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
Parameters.Add(new ValueParameter(CurrentSymbolicExpressionTreeParameterName, ""));
Parameters.Add(new LookupParameter(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names."));
Parameters.Add(new LookupParameter(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weghts."));
Parameters.Add(new LookupParameter(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values."));
Parameters.Add(new LookupParameter(SimilarityValuesParmeterName, ""));
}
public override IOperation Apply() {
var trees = SymbolicExpressionTreeParameter.ActualValue;
double similarity = 0.0;
var current = CurrentSymbolicExpressionTree;
bool found = false;
foreach (var tree in trees) {
if (tree == current) {
found = true;
continue;
}
if (found) {
similarity += MaxCommonSubtreeSimilarity(current, tree, SimilarityComparer);
}
}
lock (SimilarityParameter.ActualValue) {
SimilarityParameter.ActualValue.Value += similarity;
}
return base.Apply();
}
public static double CalculateSimilarity(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
return 2.0 * SymbolicExpressionTreeMatching.Match(a, b, comparer) / (a.GetLength() + b.GetLength());
}
///
/// Try to match each pair of nodes from trees a and b and return a similarity value based on the maximum number of matched node pairs.
///
///
///
///
/// A similarity value computed as 2.0 * MaxNumberOfMatchedPairs / (Sum of both tree sizes)
public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
int max = 0;
var rootA = a.Root.GetSubtree(0).GetSubtree(0);
var rootB = b.Root.GetSubtree(0).GetSubtree(0);
foreach (var aa in rootA.IterateNodesBreadth()) {
int lenA = aa.GetLength();
if (lenA <= max) continue;
foreach (var bb in rootB.IterateNodesBreadth()) {
int lenB = bb.GetLength();
if (lenB <= max) continue;
int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);
if (max < matches) max = matches;
}
}
return 2.0 * max / (rootA.GetLength() + rootB.GetLength());
}
// returns true if both nodes are variables, or both are constants, or both are functions
private static bool SameType(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
if (a is VariableTreeNode) {
return b is VariableTreeNode;
}
if (a is ConstantTreeNode) {
return b is ConstantTreeNode;
}
return true;
}
}
}