Changeset 11253
- Timestamp:
- 07/31/14 17:11:39 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking
- Files:
-
- 2 deleted
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking.Views/3.4/GenealogyGraphView.cs
r10514 r11253 45 45 // TODO: Put event handlers of child controls here. 46 46 public virtual void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) { 47 var content = ((VisualGenealogyGraphNode)sender).Data. Content;47 var content = ((VisualGenealogyGraphNode)sender).Data.Data; 48 48 if (content != null) { 49 49 viewHost.Content = (IContent)content; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking.Views/3.4/HeuristicLab.EvolutionTracking.Views-3.4.csproj
r11227 r11253 97 97 <DependentUpon>FrequentFragmentsDialog.cs</DependentUpon> 98 98 </Compile> 99 <Compile Include="GenealogyGraphChart.cs"> 100 <SubType>UserControl</SubType> 101 </Compile> 99 <Compile Include="GenealogyGraphChart.cs" /> 102 100 <Compile Include="GenealogyGraphChart.Designer.cs"> 103 101 <DependentUpon>GenealogyGraphChart.cs</DependentUpon> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r11233 r11253 20 20 #endregion 21 21 22 using System .Collections.Generic;22 using System; 23 23 using System.Linq; 24 24 using HeuristicLab.Analysis; … … 259 259 int index = 0; 260 260 T elite = null; 261 262 int psum1 = GenealogyGraph.Ranks[generation].Sum(x => x.Parents.Count()); 263 261 264 for (int i = 0; i < population.Length; ++i) { 262 if (GenealogyGraph. Contains(population[i])) {265 if (GenealogyGraph.GetByContent(population[i]).Rank.Equals(generation - 1)) { 263 266 elite = population[i]; 264 267 index = i; … … 271 274 var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(elite); 272 275 prevVertex.IsElite = true; // mark elites in the graph retroactively 273 274 var clone = (T)elite.Clone(); 275 276 var v = new GenealogyGraphNode<T>(prevVertex.Content) { 276 var w = new GenealogyGraphNode<T>(prevVertex); //shallow copy, arcs are not copied 277 var v = new GenealogyGraphNode<T>(prevVertex.Data) { 277 278 Rank = generation, 278 279 Quality = prevVertex.Quality, … … 280 281 }; 281 282 282 var w = new GenealogyGraphNode<T>(clone) {283 Rank = generation - 1,284 Quality = prevVertex.Quality,285 IsElite = true286 };287 288 // replace previous elite with vertex w289 List<IGenealogyGraphNode<T>> parents = null;290 283 object data = null; 291 if (prevVertex.InArcs.Any()) {284 if (prevVertex.InArcs.Any()) 292 285 data = prevVertex.InArcs.Last().Data; 293 parents = prevVertex.InArcs.Select(x => (IGenealogyGraphNode<T>)x.Source).ToList(); 294 // v.InArcs.Last().Data = prevVertex.InArcs.Last().Data; // save the fragment in case there is one 295 // var p = prevVertex.InArcs.First().Source; 296 // GenealogyGraph.AddArc(p, w); 297 } 286 var children = prevVertex.Children.ToList(); 287 var parents = prevVertex.Parents.ToList(); 288 298 289 GenealogyGraph.RemoveVertex(prevVertex); 299 290 GenealogyGraph.AddVertex(w); … … 301 292 GenealogyGraph.AddArc(w, v); // connect current elite with previous elite 302 293 294 // recreate connections after prevVertex was replaced with w 295 foreach (var c in children) GenealogyGraph.AddArc(w, c); 296 foreach (var p in parents) GenealogyGraph.AddArc(p, w); 297 303 298 v.InArcs.Last().Data = data; 304 if (parents != null) { 305 foreach (var p in parents) 306 GenealogyGraph.AddArc(p, w); 307 } 299 300 if (w.InArcs.Any()) 301 w.InArcs.Last().Data = data; 308 302 309 303 // inject the graph node unique id to the scope … … 311 305 } 312 306 #endregion 307 int psum2 = GenealogyGraph.Ranks[generation].Sum(x => x.Parents.Count()); 308 309 if (psum1 != psum2 - 1) // -1 because we have added an additional arc from the previous elite to the current one 310 throw new InvalidOperationException("Parent sum should remain the same."); 313 311 314 312 ComputeSuccessRatios(); … … 316 314 // update qualities 317 315 for (int i = 0; i < population.Length; ++i) { 318 var vertex = (IGenealogyGraphNode)GenealogyGraph.GetByContent(population[i]);316 var vertex = GenealogyGraph.GetByContent(population[i]); 319 317 vertex.Quality = qualities[i].Value; 320 318 } 321 319 322 320 // remove extra graph nodes (added by the instrumented operators in the case of offspring selection) 323 var discardedOffspring = GenealogyGraph.Ranks[generation].Select(x => (T)x. Content).Except(population).ToList();321 var discardedOffspring = GenealogyGraph.Ranks[generation].Select(x => (T)x.Data).Except(population).ToList(); 324 322 foreach (var vertex in discardedOffspring.Select(individual => GenealogyGraph.GetByContent(individual))) { 325 323 GenealogyGraph.RemoveVertex(vertex); … … 334 332 // compute the weight of each genealogy graph node as the ratio (produced offspring) / (surviving offspring) 335 333 foreach (var ind in population) { 336 var v = (IGenealogyGraphNode)GenealogyGraph.GetByContent(ind);334 var v = GenealogyGraph.GetByContent(ind); 337 335 foreach (var p in v.Parents) 338 336 p.Weight++; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r11233 r11253 69 69 public override IArc AddArc(IVertex source, IVertex target) { 70 70 var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 71 source.AddArc(arc); 72 target.AddArc(arc); 73 arcs.Add(arc); 71 base.AddArc(arc); 74 72 return arc; 75 73 } … … 82 80 Ranks[node.Rank].Add(node); 83 81 84 if (contentMap.ContainsKey(node. Content))82 if (contentMap.ContainsKey(node.Data)) 85 83 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 86 contentMap[node. Content] = node;84 contentMap[node.Data] = node; 87 85 88 86 if (idMap.ContainsKey(node.Id)) 89 87 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 90 88 idMap[node.Id] = node; 91 92 vertex.Changed += OnVertexChanged;93 89 } 94 90 95 91 public override void RemoveVertex(IVertex vertex) { 96 92 var node = (IGenealogyGraphNode)vertex; 97 contentMap.Remove(node. Content);93 contentMap.Remove(node.Data); 98 94 idMap.Remove(node.Id); 99 95 if (Ranks.ContainsKey(node.Rank)) { … … 129 125 ranks.Clear(); 130 126 } 131 132 protected override void OnVertexChanged(object sender, EventArgs args) {133 base.OnVertexChanged(sender, args);134 135 var vertex = (IGenealogyGraphNode)sender;136 if (!contentMap.ContainsKey(vertex.Content)) {137 // vertex has received new content138 }139 }140 127 } 141 128 142 // [StorableClass] 143 // [Item("GenealogyGraph", "A class representing a genealogy graph")] 144 // public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 145 // // members and properties 146 // [Storable] 147 // private Dictionary<double, List<IGenealogyGraphNode>> ranks; 148 // public Dictionary<double, List<IGenealogyGraphNode>> Ranks { 149 // get { return ranks; } 150 // set { ranks = value; } 151 // } 152 // public new IEnumerable<IGenealogyGraphNode<T>> Vertices { 153 // get { return from n in base.Vertices select (IGenealogyGraphNode<T>)n; } 154 // } 155 // // contructors 156 // protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) 157 // : base(original, cloner) { 158 // } 159 // public override IDeepCloneable Clone(Cloner cloner) { 160 // return new GenealogyGraph<T>(this, cloner); 161 // } 162 // 163 // [StorableConstructor] 164 // protected GenealogyGraph(bool deserializing) : base(deserializing) { } 165 // public GenealogyGraph() { 166 // Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 167 // } 168 // 169 // // methods 170 // public override void AddVertex(IVertex vertex) { 171 // base.AddVertex(vertex); 172 // var node = (IGenealogyGraphNode)vertex; 173 // if (!Ranks.ContainsKey(node.Rank)) { 174 // Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 175 // } 176 // Ranks[node.Rank].Add(node); 177 // } 178 // 179 // public override void RemoveVertex(IVertex vertex) { 180 // var node = (IGenealogyGraphNode<T>)vertex; 181 // if (Ranks.ContainsKey(node.Rank)) { 182 // Ranks[node.Rank].Remove(node); 183 // } 184 // base.RemoveVertex(vertex); 185 // } 186 // 187 // public override IArc AddArc(IVertex source, IVertex target) { 188 // var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 189 // source.AddArc(arc); 190 // target.AddArc(arc); 191 // arcs.Add(arc); 192 // return arc; 193 // } 194 // 195 // IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Vertices { 196 // get { return Vertices; } 197 // } 198 // 199 // public IGenealogyGraphNode GetByContent(object content) 200 // { 201 // IGenealogyGraphNode result; 202 // contentMap. 203 // } 204 // 205 // public event EventHandler GraphUpdated; 206 // private void OnGraphUpdated(object sender, EventArgs args) { 207 // var updated = GraphUpdated; 208 // if (updated != null) updated(sender, args); 209 // } 210 // } 129 [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")] 211 130 [StorableClass] 212 213 [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]214 131 public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem { 215 132 public new IEnumerable<IGenealogyGraphNode<T>> Vertices { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs
r11233 r11253 33 33 [StorableConstructor] 34 34 protected GenealogyGraphNode(bool deserializing) : base(deserializing) { } 35 35 36 public override IDeepCloneable Clone(Cloner cloner) { 36 37 return new GenealogyGraphNode(this, cloner); 37 38 } 39 40 // this constructor emulates the behavior of a copy constructor 41 // it returns a shallow copy in which the arcs are not cloned 42 protected GenealogyGraphNode(IGenealogyGraphNode original) 43 : base((IDeepCloneable)original.Data.Clone()) { 44 Quality = original.Quality; 45 Rank = original.Rank; 46 IsElite = original.IsElite; 47 Id = Guid.NewGuid().ToString(); 48 } 49 38 50 protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner) 39 51 : base(original, cloner) { 52 Quality = original.Quality; 53 Rank = original.Rank; 54 IsElite = original.IsElite; 55 Id = Guid.NewGuid().ToString(); 40 56 } 41 57 42 public GenealogyGraphNode( object content)43 : base( content) {58 public GenealogyGraphNode(IDeepCloneable data) 59 : base(data) { 44 60 Id = Guid.NewGuid().ToString(); 45 61 } … … 48 64 get { return base.InArcs.Cast<IGenealogyGraphArc>(); } 49 65 } 66 50 67 public new IEnumerable<IGenealogyGraphArc> OutArcs { 51 68 get { return base.OutArcs.Cast<IGenealogyGraphArc>(); } 52 69 } 70 53 71 public IEnumerable<IGenealogyGraphNode> Ancestors { 54 72 get { … … 123 141 [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")] 124 142 public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem { 125 public new T Content{126 get { return (T)base. Content; }127 set { base. Content= value; }143 public new T Data { 144 get { return (T)base.Data; } 145 set { base.Data = value; } 128 146 } 129 147 130 public GenealogyGraphNode(object content) : base(content) { } 148 public GenealogyGraphNode(IGenealogyGraphNode<T> original) : base(original) { } 149 150 public GenealogyGraphNode(IDeepCloneable content) : base(content) { } 131 151 132 152 protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner) 133 153 : base(original, cloner) { 134 // we do not clone the content135 Content = original.Content;136 154 } 137 155 public override IDeepCloneable Clone(Cloner cloner) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs
r11233 r11253 20 20 #endregion 21 21 22 using HeuristicLab.Core; 23 22 24 namespace HeuristicLab.EvolutionTracking { 23 25 public interface IGenealogyGraphArc : IArc { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs
r11233 r11253 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Core; 24 25 25 26 namespace HeuristicLab.EvolutionTracking { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/HeuristicLab.EvolutionTracking-3.4.csproj
r11227 r11253 105 105 <ItemGroup> 106 106 <Compile Include="Analyzers\GenealogyAnalyzer.cs" /> 107 <Compile Include="DirectedGraph\Arc.cs" />108 <Compile Include="DirectedGraph\DirectedGraph.cs" />109 <Compile Include="DirectedGraph\Interfaces\IArc.cs" />110 <Compile Include="DirectedGraph\Interfaces\IDirectedGraph.cs" />111 <Compile Include="DirectedGraph\Interfaces\IVertex.cs" />112 <Compile Include="DirectedGraph\Vertex.cs" />113 107 <Compile Include="Fragment.cs" /> 114 108 <Compile Include="Interfaces\IFragment.cs" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs
r11233 r11253 77 77 var subScopes = ExecutionContext.Scope.SubScopes; 78 78 var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetById(getScopeId(x))).ToList(); 79 if (!parentVertices.Any()) 80 throw new InvalidOperationException("Could not retrieve parent vertices."); 79 81 80 82 var parents = ParentsParameter.ActualValue.ToList(); … … 84 86 GenealogyGraph.AddVertex(childVertex); 85 87 86 foreach (var parentVertex in parentVertices .Distinct()) {88 foreach (var parentVertex in parentVertices) 87 89 GenealogyGraph.AddArc(parentVertex, childVertex); 88 } 90 89 91 ExecutionContext.Scope.Variables.Add(new Variable("Id", new StringValue(childVertex.Id))); 90 91 92 return base.Apply(); 92 93 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r11233 r11253 54 54 var v = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(ChildParameter.ActualValue); 55 55 var clone = (T)ChildParameter.ActualValue.Clone(); 56 var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 }; 57 GenealogyGraph.AddVertex(c); 56 58 57 var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 }; 58 59 foreach (var arc in v.InArcs) { 59 var arcs = v.InArcs; 60 foreach (var arc in arcs) { 60 61 var p = arc.Source; 61 62 GenealogyGraph.RemoveArc(arc); … … 63 64 } 64 65 65 // foreach (var a in v.InArcs) {66 // a.Target = c;67 // c.AddReverseArc(a);68 // }69 70 // v.InArcs = Enumerable.Empty<IGenealogyGraphArc>();71 72 GenealogyGraph.AddVertex(c);73 66 GenealogyGraph.AddArc(c, v); 74 67 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj
r11208 r11253 283 283 <DependentUpon>SymboldDataAnalysisGenealogyView.cs</DependentUpon> 284 284 </Compile> 285 <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.cs"> 286 <SubType>UserControl</SubType> 287 </Compile> 285 <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.cs" /> 288 286 <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.Designer.cs"> 289 287 <DependentUpon>SymbolicDataAnalysisExpressionGenealogyGraphChart.cs</DependentUpon> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs
r11015 r11253 70 70 tileDictionary.Clear(); 71 71 foreach (var node in Content.Vertices) { 72 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)node. Content;72 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)node.Data; 73 73 var tile = new SymbolicExpressionTreeTile(chart); 74 74 tile.LayoutEngine = symbolicExpressionEngine; 75 tile.Label = "Generation " + node. Content.Rank + Environment.NewLine +76 "Quality " + String.Format("{0:0.000}", node. Content.Quality);77 tile.Root = graphNode. Content.Root;75 tile.Label = "Generation " + node.Data.Rank + Environment.NewLine + 76 "Quality " + String.Format("{0:0.000}", node.Data.Quality); 77 tile.Root = graphNode.Data.Root; 78 78 var tileNode = new TileLayoutNode { Tile = tile }; 79 79 tileDictionary.Add(node, tileNode); … … 117 117 var aSize = aTile.Size; 118 118 var aPos = aTile.Position; 119 var graphNode = node. Content;119 var graphNode = node.Data; 120 120 121 121 if (node.SubtreeIndex > 0) { 122 var subtree = graphNode. Content.Root.NodeAt(node.SubtreeIndex);122 var subtree = graphNode.Data.Root.NodeAt(node.SubtreeIndex); 123 123 foreach (var s in subtree.IterateNodesPrefix()) { 124 124 var primitive = aTile.GetPrimitive(s); … … 132 132 } 133 133 if (node.FragmentIndex > 0) { 134 var subtree = graphNode. Content.Root.NodeAt(node.FragmentIndex);134 var subtree = graphNode.Data.Root.NodeAt(node.FragmentIndex); 135 135 foreach (var s in subtree.IterateNodesPrefix()) { 136 136 var primitive = aTile.GetPrimitive(s); … … 149 149 var index = node.SubtreeIndex + (parent.FragmentIndex - parent.SubtreeIndex); 150 150 // some mutations create discontinuities which invalidate the index 151 if (index >= 0 && index < graphNode. Content.Length) {152 var subtree = graphNode. Content.NodeAt(index);151 if (index >= 0 && index < graphNode.Data.Length) { 152 var subtree = graphNode.Data.NodeAt(index); 153 153 var primitive = aTile.GetPrimitive(subtree); 154 154 primitive.Brush = new SolidBrush(Color.LightCoral); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs
r11233 r11253 74 74 var visualNode = (VisualGenealogyGraphNode)sender; 75 75 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data; 76 var tree = graphNode. Content;76 var tree = graphNode.Data; 77 77 symbolicExpressionTreeChart.Tree = tree; 78 78 … … 80 80 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data; 81 81 if (fragment != null) { 82 treeChart_HighlightSubtree(graphNode. Content.NodeAt(fragment.Index1));82 treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1)); 83 83 } 84 84 } … … 98 98 // perform fragment tracing 99 99 var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode; 100 var subtreeIndex = graphNode. Content.IterateNodesPrefix().ToList().IndexOf(subtree);100 var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree); 101 101 var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex); 102 102 if (fragmentGraph.Vertices.Any()) { … … 106 106 // perform matching like it was done before 107 107 // currently there is no possibility to specify the subtree matching criteria 108 var trees = Content.Vertices.Select(x => x. Content);108 var trees = Content.Vertices.Select(x => x.Data); 109 109 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer)); 110 110 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymbolicDataAnalysisExpressionLineageExplorerView.cs
r10746 r11253 64 64 var treeNode = new TreeNode(g.Quality.ToString(CultureInfo.InvariantCulture)); 65 65 treeView.Nodes.Add(treeNode); 66 treeMap.Add(treeNode, (ISymbolicExpressionTree)g. Content);66 treeMap.Add(treeNode, (ISymbolicExpressionTree)g.Data); 67 67 } 68 68 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r11233 r11253 173 173 </ItemGroup> 174 174 <ItemGroup> 175 <Compile Include="Analyzers\SymbolicDataAnalysisInternalDiversityAnalyzer.cs" />176 175 <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" /> 177 176 <Compile Include="Analyzers\SymbolicDataAnalysisGenealogyAnalyzer.cs" /> … … 207 206 <Compile Include="SlidingWindow\SlidingWindowVisualizer.cs" /> 208 207 <Compile Include="SymbolicDataAnalysisExpressionPruningOperator.cs" /> 209 <Compile Include="SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs" />210 208 <Compile Include="SymbolicDataAnalysisSolutionPruningOptimizer.cs" /> 211 209 <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs
r11233 r11253 34 34 var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data; 35 35 var nodes1 = (List<ISymbolicExpressionTreeNode>)arcs[1].Data; 36 var childNodes = childVertex. Content.IterateNodesPrefix().ToList();36 var childNodes = childVertex.Data.IterateNodesPrefix().ToList(); 37 37 IFragment<ISymbolicExpressionTreeNode> fragment = null; 38 38 for (int i = 0; i < Math.Min(nodes0.Count, childNodes.Count); ++i) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs
r11233 r11253 29 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 30 30 public class SymbolicDataAnalysisExpressionAfterManipulatorOperator : AfterManipulatorOperator<ISymbolicExpressionTree> { 31 private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer;32 33 public SymbolicDataAnalysisExpressionAfterManipulatorOperator() {34 comparer = new SymbolicExpressionTreeNodeSimilarityComparer {35 MatchVariableNames = true,36 MatchVariableWeights = true,37 MatchConstantValues = true38 };39 }40 41 31 public override IOperation Apply() { 42 var vChild= (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetByContent(ChildParameter.ActualValue);43 var nodesBefore = (List<ISymbolicExpressionTreeNode>) vChild.InArcs.First().Data;32 var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetByContent(ChildParameter.ActualValue); 33 var nodesBefore = (List<ISymbolicExpressionTreeNode>)childVertex.InArcs.First().Data; 44 34 var nodesAfter = ChildParameter.ActualValue.IterateNodesPrefix().ToList(); 45 35 IFragment<ISymbolicExpressionTreeNode> fragment = null; 46 36 47 for (int i = 0; i < Math.Min(nodesAfter.Count, nodesBefore.Count); ++i) { 48 var a = nodesAfter[i]; 49 var b = nodesBefore[i]; 37 // try to find if a subtree was replaced by comparing references 38 for (int i = 0; i < Math.Min(nodesBefore.Count, nodesAfter.Count); ++i) { 39 if (nodesAfter[i] != nodesBefore[i]) { 40 fragment = new Fragment<ISymbolicExpressionTreeNode> { 41 Root = nodesAfter[i], 42 Index1 = i, 43 Index2 = i 44 }; 45 } 46 } 47 // if the fragment could not be identified by reference comparison, we try to identify it by comparing node labels 48 // the fragment root will be the lowest common ancestor of all the differing nodes 49 if (fragment == null) { 50 var list = new List<ISymbolicExpressionTreeNode>(); 51 var root = ChildParameter.ActualValue.Root; 50 52 51 bool found = false; 52 if (comparer.Equals(a, b)) { 53 int m = 0; 54 for (int j = 0; j < a.SubtreeCount; ++j) { 55 if (!AreSimilar(a.GetSubtree(j), b.GetSubtree(j), comparer)) { ++m; } 56 if (m > 1) { 57 found = true; 58 break; 59 } 60 } 61 if (m == 0) { 62 // everything is similar so we skip the whole subtree 63 i += a.GetLength(); 64 } 65 if (!found) continue; 53 for (int i = 0; i < Math.Min(nodesBefore.Count, nodesAfter.Count); ++i) { 54 var a = nodesAfter[i]; 55 var b = nodesBefore[i]; 56 57 if (!Equals(a.ToString(), b.ToString())) // label comparison is sufficient 58 list.Add(a); 66 59 } 60 61 var lca = LowestCommonAncestor(root, list); 62 int index = nodesAfter.IndexOf(lca); 67 63 fragment = new Fragment<ISymbolicExpressionTreeNode> { 68 Root = a,69 Index1 = i ,70 Index2 = i 64 Root = lca, 65 Index1 = index, 66 Index2 = index 71 67 }; 72 break;73 68 } 74 69 75 vChild.InArcs.First().Data = fragment; 70 if (fragment == null) 71 throw new ArgumentNullException("Could not identify fragment."); 72 73 childVertex.InArcs.First().Data = fragment; 76 74 return base.Apply(); 77 75 } 78 76 79 private bool AreSimilar(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, SymbolicExpressionTreeNodeSimilarityComparer comp) { 80 int length = a.GetLength(); 81 return comparer.Equals(a, b) && length.Equals(b.GetLength()) && SymbolicExpressionTreeMatching.Match(a, b, comp) == length; 77 private static ISymbolicExpressionTreeNode LowestCommonAncestor(ISymbolicExpressionTreeNode root, List<ISymbolicExpressionTreeNode> nodes) { 78 if (nodes.Count == 0) 79 throw new ArgumentException("The nodes list should contain at least one element."); 80 81 if (nodes.Count == 1) 82 return nodes[0]; 83 84 int lowestLevel = nodes.Min(x => root.GetBranchLevel(x)); 85 86 // bring the nodes in the nodes to the same level (relative to the root) 87 for (int i = 0; i < nodes.Count; ++i) { 88 var node = nodes[i]; 89 var level = root.GetBranchLevel(node); 90 for (int j = lowestLevel; j < level; ++j) 91 node = node.Parent; 92 nodes[i] = node; 93 } 94 95 // while not all the elements in the nodes are equal, go one level up 96 while (nodes.Any(x => x != nodes[0])) { 97 for (int i = 0; i < nodes.Count; ++i) 98 nodes[i] = nodes[i].Parent; 99 } 100 101 return nodes[0]; 82 102 } 83 103 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs
r11233 r11253 32 32 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetByContent(ChildParameter.ActualValue); 33 33 var vClone = vChild.Parents.Last(); 34 vChild.InArcs.First().Data = vClone. Content.IterateNodesPrefix().ToList();34 vChild.InArcs.First().Data = vClone.Data.IterateNodesPrefix().ToList(); 35 35 36 36 var fragment = (IFragment<ISymbolicExpressionTreeNode>)vClone.InArcs.Last().Data; … … 38 38 // this step must be performed because the fragment root actually references the original child (not the clone). 39 39 // then, a mutation operation (acting on the original child), could disrupt it 40 fragment.Root = vClone. Content.IterateNodesPrefix().ElementAt(fragment.Index1);40 fragment.Root = vClone.Data.IterateNodesPrefix().ElementAt(fragment.Index1); 41 41 } 42 42 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionTracing.cs
r11233 r11253 24 24 using System.IO; 25 25 using System.Linq; 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 28 using HeuristicLab.EvolutionTracking; … … 74 75 } 75 76 var fragmentLength = fragment.Root.GetLength(); 76 var tree = node. Content;77 var tree = node.Data; 77 78 var subtree = tree.NodeAt(index); 78 79 var subtreeLength = subtree.GetLength(); … … 97 98 // subtree contained in fragment, index stays the same 98 99 } else { 99 index += node. Content.NodeAt(fragment.Index1).GetLength() - fragmentLength;100 index += node.Data.NodeAt(fragment.Index1).GetLength() - fragmentLength; 100 101 } 101 102 } else if (subtreeIndex < fragment.Index1) { … … 131 132 // fragment distinct from subtree 132 133 node = parents[0]; // take first parent which contains the subtree 133 index += node. Content.NodeAt(fragment.Index1).GetLength() - fragmentLength; // subtreeIndex must be adjusted according to swapped fragments sizes134 index += node.Data.NodeAt(fragment.Index1).GetLength() - fragmentLength; // subtreeIndex must be adjusted according to swapped fragments sizes 134 135 } 135 136 continue; … … 150 151 yield return fragmentNode; 151 152 152 if (parents[1]. Content.NodeAt(fragment.Index2).GetLength() != fragmentLength) {153 if (parents[1].Data.NodeAt(fragment.Index2).GetLength() != fragmentLength) { 153 154 throw new Exception("Fragment lengths should match!"); 154 155 }
Note: See TracChangeset
for help on using the changeset viewer.