Changeset 11197 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
- Timestamp:
- 07/16/14 16:32:48 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPopulationDiversityAnalyzer.cs
r10464 r11197 49 49 private const string StoreHistoryParameterName = "StoreHistory"; 50 50 // comparer parameters 51 private const string MatchVariablesParameterName = "MatchVariableNames";52 private const string MatchVariableWeightsParameterName = "MatchVariableWeights";53 private const string MatchConstantValuesParameterName = "MatchConstantValues";54 51 private const string SimilarityValuesParmeterName = "Similarity"; 52 53 private const string DistanceCalculatorParameterName = "DistanceCalculator"; 55 54 56 55 #endregion … … 78 77 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } 79 78 } 80 public ValueParameter<BoolValue> MatchVariableNamesParameter {81 get { return (ValueParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }82 }83 public ValueParameter<BoolValue> MatchVariableWeightsParameter {84 get { return (ValueParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }85 }86 public ValueParameter<BoolValue> MatchConstantValuesParameter {87 get { return (ValueParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }88 }89 79 public ILookupParameter<DoubleValue> SimilarityParameter { 90 80 get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; } 81 } 82 public IValueParameter<IDistanceCalculator> DistanceCalculatorParameter { 83 get { return (IValueParameter<IDistanceCalculator>)Parameters[DistanceCalculatorParameterName]; } 91 84 } 92 85 #endregion … … 122 115 Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored.")); 123 116 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far.")); 124 Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names.", new BoolValue(true)));125 Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights.", new BoolValue(true)));126 Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values.", new BoolValue(true)));127 117 Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false))); 128 118 Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, "")); 129 119 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 120 Parameters.Add(new ValueParameter<IDistanceCalculator>(DistanceCalculatorParameterName, "The distance calculator between trees.", new BottomUpDistanceCalculator())); 130 121 UpdateCounterParameter.Hidden = true; 131 122 UpdateIntervalParameter.Hidden = true; … … 159 150 SimilarityParameter.ActualValue = new DoubleValue(); 160 151 161 var comparer = new SymbolicExpressionTreeNodeSimilarityComparer {162 MatchConstantValues = MatchConstantValuesParameter.Value.Value,163 MatchVariableNames = MatchVariableNamesParameter.Value.Value,164 MatchVariableWeights = MatchVariableWeightsParameter.Value.Value165 };166 167 152 var operations = new OperationCollection { Parallel = true }; 168 153 foreach (var tree in trees) { 169 var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator {154 var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(DistanceCalculatorParameter.Value) { 170 155 CurrentSymbolicExpressionTree = tree, 171 SimilarityComparer = comparer,172 156 MaximumTreeDepth = MaximumSymbolicExpressionTreeDepth.Value 173 157 }; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r11015 r11197 189 189 <SubType>Code</SubType> 190 190 </Compile> 191 <Compile Include="Interfaces\IDistanceCalculator.cs" /> 191 192 <Compile Include="Matching\SymbolicExpressionTreeCanonicalSorter.cs" /> 192 193 <Compile Include="Matching\SymbolicExpressionTreeEqualityComparer.cs" /> … … 307 308 <Compile Include="Tracking\SymbolicDataAnalysisExpressionTracing.cs" /> 308 309 <Compile Include="TreeDistance\BottomUpDistanceCalculator.cs" /> 310 <Compile Include="TreeDistance\IsomorphicTreeDistance.cs" /> 309 311 <None Include="HeuristicLab.snk" /> 310 312 <None Include="Plugin.cs.frame" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs
r11015 r11197 39 39 private const string MatchConstantValuesParameterName = "MatchConstantValues"; 40 40 41 private IDistanceCalculator distanceCalculator; 42 41 43 public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter { 42 44 get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; } … … 45 47 get { return (IValueParameter<ISymbolicExpressionTree>)Parameters[CurrentSymbolicExpressionTreeParameterName]; } 46 48 } 47 public ILookupParameter<BoolValue> MatchVariableNamesParameter { 48 get { return (ILookupParameter<BoolValue>)Parameters[MatchVariablesParameterName]; } 49 } 50 public ILookupParameter<BoolValue> MatchVariableWeightsParameter { 51 get { return (ILookupParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; } 52 } 53 public ILookupParameter<BoolValue> MatchConstantValuesParameter { 54 get { return (ILookupParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; } 55 } 49 56 50 public ILookupParameter<DoubleValue> SimilarityParameter { 57 51 get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; } … … 61 55 set { CurrentSymbolicExpressionTreeParameter.Value = value; } 62 56 } 63 public SymbolicExpressionTreeNodeSimilarityComparer SimilarityComparer { get; set; }64 57 65 58 public int MaximumTreeDepth { get; set; } … … 79 72 } 80 73 81 public SymbolicDataAnalysisExpressionTreeSimilarityCalculator() 74 private SymbolicDataAnalysisExpressionTreeSimilarityCalculator() { 75 } 76 77 public SymbolicDataAnalysisExpressionTreeSimilarityCalculator(IDistanceCalculator distanceCalculator) 82 78 : base() { 79 this.distanceCalculator = distanceCalculator; 80 83 81 Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 84 82 Parameters.Add(new ValueParameter<ISymbolicExpressionTree>(CurrentSymbolicExpressionTreeParameterName, "")); … … 103 101 104 102 if (found) { 105 // similarity += MaxCommonSubtreeSimilarity(current, tree, SimilarityComparer); 106 var distance = BottomUpDistanceCalculator.CalculateDistance(current, tree) / (current.Length + tree.Length); 103 var distance = distanceCalculator.CalculateDistance(current, tree); 107 104 similarity += 1 - distance; 108 105 } … … 114 111 return base.Apply(); 115 112 } 116 117 public static double CalculateSimilarity(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {118 return 2.0 * SymbolicExpressionTreeMatching.Match(a, b, comparer) / (a.GetLength() + b.GetLength());119 }120 121 /// <summary>122 /// 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.123 /// </summary>124 /// <param name="a"></param>125 /// <param name="b"></param>126 /// <param name="comparer"></param>127 /// <returns>A similarity value computed as 2.0 * MaxNumberOfMatchedPairs / (Sum of both tree sizes)</returns>128 public static double MaxCommonSubtreeSimilarity(ISymbolicExpressionTree a, ISymbolicExpressionTree b, SymbolicExpressionTreeNodeSimilarityComparer comparer) {129 int max = 0;130 var rootA = a.Root.GetSubtree(0).GetSubtree(0);131 var rootB = b.Root.GetSubtree(0).GetSubtree(0);132 foreach (var aa in rootA.IterateNodesBreadth()) {133 int lenA = aa.GetLength();134 if (lenA <= max) continue;135 foreach (var bb in rootB.IterateNodesBreadth()) {136 int lenB = bb.GetLength();137 if (lenB <= max) continue;138 int matches = SymbolicExpressionTreeMatching.Match(aa, bb, comparer);139 if (max < matches) max = matches;140 }141 }142 return 2.0 * max / (rootA.GetLength() + rootB.GetLength());143 }144 145 // returns true if both nodes are variables, or both are constants, or both are functions146 private static bool SameType(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {147 if (a is VariableTreeNode) {148 return b is VariableTreeNode;149 }150 if (a is ConstantTreeNode) {151 return b is ConstantTreeNode;152 }153 return true;154 }155 113 } 156 114 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeDistance/BottomUpDistanceCalculator.cs
r11165 r11197 23 23 using System.Collections.Generic; 24 24 using System.Globalization; 25 using System.IO;26 25 using System.Linq; 27 26 using System.Text; 28 27 using System.Text.RegularExpressions; 28 using HeuristicLab.Common; 29 using HeuristicLab.Core; 29 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views; 31 32 using HeuristicLab.EvolutionTracking; 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 34 33 35 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 36 38 /// G. Valiente, "An Efficient Bottom-up Distance Between Trees", http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.102.7958 37 39 /// </summary> 38 public static class BottomUpDistanceCalculator { 40 [StorableClass] 41 [Item("Bottom-up distance calculator", "An operator that calculates the bottom-up distance between two symbolic expression trees.")] 42 public class BottomUpDistanceCalculator : Item, IDistanceCalculator { 43 public BottomUpDistanceCalculator() { } 44 45 protected BottomUpDistanceCalculator(BottomUpDistanceCalculator original, Cloner cloner) 46 : base(original, cloner) { 47 } 48 49 public override IDeepCloneable Clone(Cloner cloner) { 50 return new BottomUpDistanceCalculator(this, cloner); 51 } 52 39 53 /// <summary> 40 54 /// Creates a compact representation of the two trees as a directed acyclic graph … … 77 91 bool found = false; 78 92 // for all nodes w in G in reverse order 79 foreach (var w in G.Vertices.Reverse()) { 93 var vertices = G.Vertices.Reverse(); 94 foreach (var w in vertices) { 80 95 if (Height(v) != Height(w) || v.SubtreeCount != w.OutDegree || Label(v) != w.Label) 81 96 break; … … 86 101 if (V.SequenceEqual(W)) { 87 102 K[v] = w; 88 // // merge vertices89 // foreach (var a in K[v].InArcs) {90 // a.Target = w;91 // }92 // G.RemoveVertex(K[v]);93 94 103 found = true; 95 104 break; … … 98 107 99 108 if (!found) { 100 var w = new Vertex { Content = v, Label = Label(v) , Weight = Height(v)};109 var w = new Vertex { Content = v, Label = Label(v) }; 101 110 G.AddVertex(w); 102 111 K[v] = w; … … 115 124 } 116 125 }; 117 118 using (var file = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "graph.dot"))) {119 var str = G.ExportDot();120 file.WriteLine(str);121 }122 126 123 127 return K; … … 158 162 } 159 163 160 public static double CalculateDistance(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, double p = 1, double q = 1, double r = 1) { 164 // return a normalized distance measure in the interval (0,1) 165 public double CalculateDistance(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { 166 return BottomUpDistance(t1, t2) / (t1.Length + t2.Length); 167 } 168 169 public static double BottomUpDistance(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, double p = 1, double q = 1, double r = 1) { 161 170 var K = Compact(t1, t2); 162 171 var M = Mapping(t1, t2, K); … … 177 186 178 187 double distance = s * p + i * q + d * r; 179 180 if (distance / (t1.Length + t2.Length) > 0.5 && M.Count > 0) {181 using (var file = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "map.tex"))) {182 var str = FormatMapping(t1, t2, M);183 file.WriteLine(str);184 }185 }186 187 188 return distance; 188 189 } … … 193 194 194 195 private static int Height(IVertex v) { 195 return (int)Math.Round(v.Weight); // use the vertex weight as height in this particular context196 return Height((ISymbolicExpressionTreeNode)v.Content); 196 197 } 197 198 … … 213 214 var r2 = Item2.Root; 214 215 215 var virtualRootSymbol = new StartSymbol(); 216 var virtualRootNode = virtualRootSymbol.CreateTreeNode(); 217 virtualRootNode.AddSubtree(r1); 218 virtualRootNode.AddSubtree(r2); 219 220 var nodes = virtualRootNode.IterateNodesPostfix().ToList(); 221 222 virtualRootNode.RemoveSubtree(0); 223 virtualRootNode.RemoveSubtree(0); 224 225 for (int i = 0; i < nodes.Count - 1; ++i) { yield return nodes[i]; } 216 var nodes = r1.IterateNodesPostfix().Select(x => new { Node = x, Height = r1.GetBranchLevel(x) }) 217 .Concat(r2.IterateNodesPostfix().Select(x => new { Node = x, Height = r2.GetBranchLevel(x) })); 218 219 return nodes.OrderByDescending(x => x.Height).Select(x => x.Node); 220 221 // 222 // var p1 = r1.Parent; 223 // var p2 = r2.Parent; 224 // 225 // var virtualRootSymbol = new StartSymbol(); 226 // var virtualRootNode = virtualRootSymbol.CreateTreeNode(); 227 // 228 // virtualRootNode.AddSubtree(r1); 229 // virtualRootNode.AddSubtree(r2); 230 // 231 // var nodes = virtualRootNode.IterateNodesPostfix().ToList(); 232 // 233 // virtualRootNode.RemoveSubtree(0); 234 // virtualRootNode.RemoveSubtree(0); 235 // 236 // r1.Parent = p1; 237 // r2.Parent = p2; 238 // 239 // for (int i = 0; i < nodes.Count - 1; ++i) { yield return nodes[i]; } 226 240 } 227 241 } … … 257 271 return sb.ToString(); 258 272 } 273 259 274 } 260 275 }
Note: See TracChangeset
for help on using the changeset viewer.