Changeset 11458
- Timestamp:
- 10/13/14 13:03:24 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 5 added
- 1 deleted
- 11 edited
- 1 copied
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Property svn:ignore
-
old new 5 5 TreeDistance 6 6 SymbolicDataAnalysisExpressionTreeMatching.cs 7 Importer
-
- Property svn:ignore
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers
-
Property
svn:ignore
set to
SymbolicDataAnalysisExpressionSizefairCrossover.cs
-
Property
svn:ignore
set to
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs
r11208 r11458 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; 29 28 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 29 31 [StorableClass] … … 86 88 var psi = new Psi(); 87 89 var sineIntegral = new SineIntegral(); 90 var passthrough = new Passthrough(); 88 91 89 92 var @if = new IfThenElse(); … … 113 116 var exponentialAndLogarithmicSymbols = new GroupSymbol(ExponentialFunctionsName, new List<ISymbol> { exp, log }); 114 117 var specialFunctions = new GroupSymbol(SpecialFunctionsName, new List<ISymbol> { airyA, airyB, bessel, cosineIntegral, dawson, erf, expIntegralEi, 115 fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral });118 fresnelCosineIntegral,fresnelSineIntegral,gamma,hypCosineIntegral,hypSineIntegral,norm, psi, sineIntegral, passthrough}); 116 119 var terminalSymbols = new GroupSymbol(TerminalsName, new List<ISymbol> { constant, variableSymbol }); 117 120 var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, specialFunctions, terminalSymbols }); … … 156 159 SetSubtreeCount(laggedVariable, 0, 0); 157 160 SetSubtreeCount(autoregressiveVariable, 0, 0); 161 SetSubtreeCount(passthrough, 1, 1); 158 162 #endregion 159 163 … … 224 228 AddAllowedChildSymbol(derivative, powerSymbols); 225 229 AddAllowedChildSymbol(derivative, conditionSymbols); 230 231 AddAllowedChildSymbol(passthrough, realValuedSymbols); 232 AddAllowedChildSymbol(passthrough, powerSymbols); 233 AddAllowedChildSymbol(passthrough, conditionSymbols); 234 AddAllowedChildSymbol(passthrough, timeSeriesSymbols); 235 AddAllowedChildSymbol(passthrough, specialFunctions); 226 236 #endregion 227 237 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r11318 r11458 105 105 <Private>False</Private> 106 106 </Reference> 107 <Reference Include="HeuristicLab.Common-3.3"> 107 <Reference Include="HeuristicLab.Common-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 108 <SpecificVersion>False</SpecificVersion> 108 109 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Common-3.3.dll</HintPath> 109 110 <Private>False</Private> … … 190 191 <SubType>Code</SubType> 191 192 </Compile> 193 <Compile Include="Crossovers\SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs" /> 194 <Compile Include="Importer\SymbolicExpressionImporter.cs" /> 195 <Compile Include="Importer\Token.cs" /> 192 196 <Compile Include="Interfaces\IModelBacktransformator.cs" /> 193 <Compile Include="Interfaces\I DistanceCalculator.cs" />197 <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionSimilarityCalculator.cs" /> 194 198 <Compile Include="Matching\SymbolicExpressionTreeCanonicalSorter.cs" /> 195 199 <Compile Include="Matching\SymbolicExpressionTreeEqualityComparer.cs" /> … … 263 267 <Compile Include="Symbols\AiryB.cs" /> 264 268 <Compile Include="Symbols\Bessel.cs" /> 269 <Compile Include="Symbols\Passthrough.cs" /> 265 270 <Compile Include="Symbols\Xor.cs" /> 266 271 <Compile Include="Symbols\Erf.cs" /> … … 305 310 <Compile Include="Symbols\VariableConditionTreeNode.cs" /> 306 311 <Compile Include="Symbols\VariableTreeNode.cs" /> 312 <Compile Include="Tracking\TraceCalculator.cs" /> 307 313 <Compile Include="TransformationToSymbolicTreeMapper.cs" /> 308 314 <Compile Include="Tracking\FragmentGraph\FragmentGraph.cs" /> … … 349 355 <Project>{423bd94f-963a-438e-ba45-3bb3d61cd03b}</Project> 350 356 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views-3.4</Name> 357 <Private>False</Private> 351 358 </ProjectReference> 352 359 <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicDataAnalysisExpressionSimilarityCalculator.cs
r11350 r11458 1 using HeuristicLab.Core; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2014 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 22 using HeuristicLab.Core; 2 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 3 24 4 25 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 5 public interface I DistanceCalculator : IItem {6 double Calculate Distance(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2);26 public interface ISymbolicDataAnalysisExpressionSimilarityCalculator : IItem { 27 double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2); 7 28 } 8 29 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
r11208 r11458 23 23 using System.Collections.Generic; 24 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; 25 26 26 27 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 83 84 public const byte Erf = 43; 84 85 public const byte Bessel = 44; 86 public const byte Passthrough = 45; 85 87 86 88 private static Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>() { … … 130 132 { typeof(Norm), OpCodes.Norm}, 131 133 { typeof(Erf), OpCodes.Erf}, 132 { typeof(Bessel), OpCodes.Bessel} 134 { typeof(Bessel), OpCodes.Bessel}, 135 { typeof(Passthrough), OpCodes.Passthrough} 133 136 }; 134 137 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r11232 r11458 111 111 112 112 var code = SymbolicExpressionTreeLinearCompiler.Compile(tree, OpCodes.MapSymbolToOpCode); 113 PrepareInstructions(code, dataset); 114 return rows.Select(row => Evaluate(dataset, row, code)); 115 } 116 117 // NOTE: do not use this method when evaluating trees. this method is provided as a shortcut for evaluating subtrees ad-hoc 118 public IEnumerable<double> GetValues(ISymbolicExpressionTreeNode node, Dataset dataset, IEnumerable<int> rows) { 119 var code = SymbolicExpressionTreeLinearCompiler.Compile(node, OpCodes.MapSymbolToOpCode); 113 120 PrepareInstructions(code, dataset); 114 121 return rows.Select(row => Evaluate(dataset, row, code)); … … 329 336 state.Reset(); 330 337 instr.value = interpreter.Evaluate(dataset, ref row, state); 338 } else if (instr.opCode == OpCodes.Passthrough) { 339 instr.value = code[instr.childIndex].value; 331 340 } else { 332 341 var errorText = string.Format("The {0} symbol is not supported by the linear interpreter. To support this symbol, please use the SymbolicDataAnalysisExpressionTreeInterpreter.", instr.dynamicNode.Symbol.Name); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Matching/SymbolicExpressionTreeMatching.cs merged: 10562,11392
r11385 r11458 77 77 78 78 public static ISymbolicExpressionTreeNode Difference(this ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNode other) { 79 var these = node.IterateNodesPrefix().ToList(); 80 var others = other.IterateNodesPrefix().ToList(); 81 82 var minCount = Math.Min(these.Count, others.Count); 79 var a = node.IterateNodesPrefix().ToList(); 80 var b = other.IterateNodesPrefix().ToList(); 83 81 var list = new List<ISymbolicExpressionTreeNode>(); 84 85 for (int i = 0; i < minCount; ++i) { 86 if (these[i].ToString() != others[i].ToString()) 87 list.Add(these[i]); 82 for (int i = 0, j = 0; i < a.Count && j < b.Count; ++i, ++j) { 83 var s1 = a[i].ToString(); 84 var s2 = b[j].ToString(); 85 if (s1 == s2) continue; 86 list.Add(a[i]); 87 // skip subtrees since the parents are already different 88 i += a[i].SubtreeCount; 89 j += b[j].SubtreeCount; 88 90 } 89 90 return list.Count > 0 ? LowestCommonAncestor(node, list) : null;91 ISymbolicExpressionTreeNode result = list.Count > 0 ? LowestCommonAncestor(node, list) : null; 92 return result; 91 93 } 92 94 … … 98 100 return nodes[0]; 99 101 100 int lowestLevel = nodes.Min(x => root.GetBranchLevel(x));102 int minLevel = nodes.Min(x => root.GetBranchLevel(x)); 101 103 102 104 // bring the nodes in the nodes to the same level (relative to the root) … … 104 106 var node = nodes[i]; 105 107 var level = root.GetBranchLevel(node); 106 for (int j = lowestLevel; j < level; ++j)108 for (int j = minLevel; j < level; ++j) 107 109 node = node.Parent; 108 110 nodes[i] = node; - Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/BottomUpSimilarityCalculator.cs
r11229 r11458 32 32 [StorableClass] 33 33 [Item("BottomUpSimilarityCalculator", "A similarity calculator which uses the tree bottom-up distance as a similarity metric.")] 34 public class BottomUpSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {34 public class BottomUpSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator, ISymbolicDataAnalysisExpressionSimilarityCalculator { 35 35 private readonly HashSet<string> commutativeSymbols = new HashSet<string> { "Addition", "Multiplication", "Average", "And", "Or", "Xor" }; 36 36 … … 39 39 public override IDeepCloneable Clone(Cloner cloner) { 40 40 return new BottomUpSimilarityCalculator(this, cloner); 41 } 42 43 public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { 44 if (t1 == t2) 45 return 1; 46 47 var map = ComputeBottomUpMapping(t1.Root, t2.Root); 48 return 2.0 * map.Count / (t1.Length + t2.Length); 41 49 } 42 50 … … 52 60 throw new ArgumentException("Cannot calculate similarity when one of the arguments is null."); 53 61 54 var similarity = CalculateS olutionSimilarity(t1, t2);62 var similarity = CalculateSimilarity(t1, t2); 55 63 if (similarity > 1.0) 56 64 throw new Exception("Similarity value cannot be greater than 1"); 57 65 58 66 return similarity; 59 }60 61 public double CalculateSolutionSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {62 if (t1 == t2)63 return 1;64 65 var map = ComputeBottomUpMapping(t1.Root, t2.Root);66 return 2.0 * map.Count / (t1.Length + t2.Length);67 67 } 68 68 … … 174 174 } 175 175 176 if (n == n1 || n == n2) 177 continue; 178 176 179 var p = n.Parent; 177 180 if (p == null) -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/MaxCommonSubtreeSimilarityCalculator.cs
r11230 r11458 30 30 [StorableClass] 31 31 [Item("MaxCommonSubtreeSimilarityCalculator", "A similarity calculator based on the size of the maximum common subtree between two trees")] 32 public class MaxCommonSubtreeSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator { 33 public MaxCommonSubtreeSimilarityCalculator() { } 32 public class MaxCommonSubtreeSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator, ISymbolicDataAnalysisExpressionSimilarityCalculator { 33 private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer; 34 public bool MatchVariableNames { 35 get { return comparer.MatchVariableNames; } 36 set { comparer.MatchVariableNames = value; } 37 } 38 39 public bool MatchVariableWeights { 40 get { return comparer.MatchVariableWeights; } 41 set { comparer.MatchVariableWeights = value; } 42 } 43 44 public bool MatchConstantValues { 45 get { return comparer.MatchConstantValues; } 46 set { comparer.MatchConstantValues = value; } 47 } 48 49 [StorableConstructor] 50 protected MaxCommonSubtreeSimilarityCalculator(bool deserializing) : base(deserializing) { } 51 52 public override IDeepCloneable Clone(Cloner cloner) { 53 return new MaxCommonSubtreeSimilarityCalculator(this, cloner); 54 } 34 55 35 56 protected MaxCommonSubtreeSimilarityCalculator(MaxCommonSubtreeSimilarityCalculator original, Cloner cloner) … … 37 58 } 38 59 39 public override IDeepCloneable Clone(Cloner cloner) { 40 return new MaxCommonSubtreeSimilarityCalculator(this, cloner); 60 public MaxCommonSubtreeSimilarityCalculator() { 61 comparer = new SymbolicExpressionTreeNodeSimilarityComparer { 62 MatchConstantValues = true, 63 MatchVariableNames = true, 64 MatchVariableWeights = true 65 }; 66 } 67 68 public MaxCommonSubtreeSimilarityCalculator(bool matchVariableNames, bool matchVariableWeights, bool matchConstantValues) { 69 comparer = new SymbolicExpressionTreeNodeSimilarityComparer { 70 MatchConstantValues = matchConstantValues, 71 MatchVariableNames = matchVariableNames, 72 MatchVariableWeights = matchVariableWeights 73 }; 74 } 75 76 public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { 77 return MaxCommonSubtreeSimilarity(t1, t2, comparer); 41 78 } 42 79 … … 47 84 if (t1 == null || t2 == null) 48 85 throw new ArgumentException("Cannot calculate similarity when one of the arguments is null."); 49 var comparer = new SymbolicExpressionTreeNodeSimilarityComparer {50 MatchConstantValues = true,51 MatchVariableNames = true,52 MatchVariableWeights = true53 };54 86 55 87 return MaxCommonSubtreeSimilarity(t1, t2, comparer); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SimilarityCalculators/PhenotypicSimilarityCalculator.cs
r11291 r11458 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 25 26 using HeuristicLab.Optimization.Operators; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 29 30 [Item("PhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")] 30 31 [StorableClass] 31 public class PhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {32 public class PhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator, ISymbolicDataAnalysisExpressionSimilarityCalculator { 32 33 public PhenotypicSimilarityCalculator(PhenotypicSimilarityCalculator original, Cloner cloner) 33 34 : base(original, cloner) { … … 41 42 public override IDeepCloneable Clone(Cloner cloner) { 42 43 return new PhenotypicSimilarityCalculator(this, cloner); 44 } 45 46 public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) { 47 throw new System.NotImplementedException(); 43 48 } 44 49 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionTracing.cs
r11388 r11458 48 48 var parent = parentNode; // current parent 49 49 50 IGenealogyGraphNode<ISymbolicExpressionTree> n1 = null; 51 int i1 = -1; 52 50 53 while (true) { 54 if (node == n1 && index == i1) 55 throw new InvalidOperationException("Infinite loop detected"); 56 57 n1 = node; 58 i1 = index; 59 51 60 if (!node.Parents.Any()) { 52 61 // no tracing if there are no parents, return a fragment node representing the current trace 53 var fragmentNode = new FragmentNode(node ) { SubtreeIndex = index };62 var fragmentNode = new FragmentNode(node.Data) { SubtreeIndex = index }; 54 63 if (parent != null) { 55 var arc = new Arc(parent, fragmentNode);64 var arc = new GenealogyGraphArc(parent, fragmentNode); 56 65 parent.AddArc(arc); 57 66 fragmentNode.AddArc(arc); … … 82 91 if (parents.Count == 1) { 83 92 // we are tracing a mutation operation 84 var fragmentNode = new FragmentNode(node ) {93 var fragmentNode = new FragmentNode(node.Data) { 85 94 SubtreeIndex = index, 86 95 FragmentIndex = fragment.Index1 … … 91 100 fragmentNode.AddArc(arc); 92 101 } 102 if (node == parents[0]) 103 throw new InvalidOperationException("Node == parents[0]"); 104 93 105 node = parents[0]; 94 if (subtreeIndex == fragment.Index1) { 106 107 if (index == fragment.Index1) { 95 108 // index stays the same 96 } else if (fragment.Index1 < subtreeIndex) {97 if ( subtreeIndex < fragment.Index1 + fragmentLength) {109 } else if (fragment.Index1 < index) { 110 if (index < fragment.Index1 + fragmentLength) { 98 111 // in the case of mutation, the fragment could have been introduced via a node replacement 99 112 // there is no guarantee the subtree exists above this level, therefore the index is set to the fragment index … … 103 116 index += node.Data.NodeAt(fragment.Index1).GetLength() - fragmentLength; 104 117 } 105 } else if ( subtreeIndex < fragment.Index1) {106 if (fragment.Index1 < subtreeIndex + subtreeLength) {118 } else if (index < fragment.Index1) { 119 if (fragment.Index1 < index + subtreeLength) { 107 120 // subtree contains fragment 108 121 index = fragment.Index1; … … 111 124 } 112 125 } 113 114 126 yield return fragmentNode; 115 var up = Trace(node, index, fragmentNode); // force immediate query execution 127 if (node == graphNode && index == subtreeIndex) 128 throw new ArgumentException("Infinite recursion detected"); 129 var up = Trace(node, index, fragmentNode).ToList(); // force immediate query execution 116 130 foreach (var v in up) { yield return v; } 117 131 break; … … 121 135 #region crossover tracing 122 136 if (parents.Count == 2) { 137 if (node == parents[0]) 138 throw new InvalidOperationException("Node == parents[0]"); 139 if (node == parents[1]) 140 throw new InvalidOperationException("Node == parents[1]"); 123 141 // we are tracing a crossover operation 124 142 if (fragment.Index1 == index) { … … 143 161 if (fragment.Index1 < index + subtreeLength) { 144 162 // subtree contains fragment => branching point in the fragment graph 145 var fragmentNode = new FragmentNode(node ) {163 var fragmentNode = new FragmentNode(node.Data) { 146 164 SubtreeIndex = index, 147 165 FragmentIndex = fragment.Index1 148 166 }; 149 167 if (parent != null) { 150 var arc = new Arc(parent, fragmentNode);168 var arc = new GenealogyGraphArc(parent, fragmentNode); 151 169 parent.AddArc(arc); 152 170 fragmentNode.AddArc(arc); … … 176 194 return writer.ToString(); 177 195 } 178 #region some helper methods for shortening the tracing code179 private static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {180 return NodeAt(tree.Root, position);181 }182 private static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {183 return root.IterateNodesPrefix().ElementAt(position);184 }185 #endregion186 196 } 187 197 }
Note: See TracChangeset
for help on using the changeset viewer.