Changeset 10755
- Timestamp:
- 04/16/14 17:15:33 (11 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r10677 r10755 191 191 instrumentedManipulator.AfterExecutionOperators.Clear(); 192 192 instrumentedManipulator.BeforeExecutionOperators.Clear(); 193 if (EnableManipulatorTracking.Value && Manipulator != null) { 193 194 if (EnableManipulatorTracking.Value) { 194 195 if (BeforeManipulatorOperator != null) { 195 196 BeforeManipulatorOperator.ChildParameter.ActualName = ManipulatorChildParameterName; … … 222 223 if (elite != null) { 223 224 var vertex = new GenealogyGraphNode<T> { 224 Content = elite,225 Content = (T)elite.Clone(), 225 226 Rank = Generations.Value, 226 227 IsElite = true 227 228 }; 228 var previousVertex = (IGenealogyGraphNode<T>)GenealogyGraph[elite] .Last();229 var previousVertex = (IGenealogyGraphNode<T>)GenealogyGraph[elite]; 229 230 GenealogyGraph.AddVertex(vertex); 230 231 previousVertex.AddForwardArc(vertex); 231 232 vertex.AddReverseArc(previousVertex); 232 233 vertex.Id = previousVertex.Id; // another way would be to introduce the vertex.Id into the scope of the elite 234 vertex.Quality = previousVertex.Quality; 235 233 236 if (previousVertex.InArcs.Any()) { 234 237 vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one … … 238 241 // update qualities 239 242 for (int i = 0; i < population.Count; ++i) { 240 var individual = population[i]; 241 foreach (var vertex in GenealogyGraph[individual].Cast<IGenealogyGraphNode<T>>()) { 242 vertex.Quality = qualities[i].Value; 243 } 243 var vertex = (IGenealogyGraphNode)GenealogyGraph[population[i]]; 244 vertex.Quality = qualities[i].Value; 244 245 } 245 246 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs
r10677 r10755 38 38 } 39 39 [Storable] 40 private readonly Dictionary<object, List<IVertex>> contentMap;40 private readonly Dictionary<object, IVertex> contentMap; 41 41 public DirectedGraph() { 42 42 nodes = new List<IVertex>(); 43 contentMap = new Dictionary<object, List<IVertex>>();43 contentMap = new Dictionary<object, IVertex>(); 44 44 } 45 45 [StorableConstructor] … … 50 50 : base(original, cloner) { 51 51 nodes = new List<IVertex>(original.Nodes); 52 contentMap = new Dictionary<object, List<IVertex>>(original.contentMap);52 contentMap = new Dictionary<object, IVertex>(original.contentMap); 53 53 } 54 54 public override IDeepCloneable Clone(Cloner cloner) { … … 62 62 return contentMap.ContainsKey(content); 63 63 } 64 public List<IVertex>this[object key] {64 public IVertex this[object key] { 65 65 get { 66 List<IVertex>result;66 IVertex result; 67 67 contentMap.TryGetValue(key, out result); 68 68 return result; … … 80 80 } 81 81 public virtual void AddVertex(IVertex vertex) { 82 if (contentMap.ContainsKey(vertex.Content)) { 83 throw new Exception("Content already exists in the graph."); 84 } 85 contentMap[vertex.Content] = vertex; 82 86 Nodes.Add(vertex); 83 if (!contentMap.ContainsKey(vertex.Content))84 contentMap[vertex.Content] = new List<IVertex>();85 contentMap[vertex.Content].Add(vertex);86 87 } 87 88 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs
r10300 r10755 32 32 void RemoveVertex(IVertex vertex); 33 33 List<IVertex> Nodes { get; } 34 List<IVertex>this[object content] { get; }34 IVertex this[object content] { get; } 35 35 bool Contains(object content); 36 36 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Fragment.cs
r10650 r10755 8 8 public class Fragment : Item, IFragment { 9 9 [Storable] 10 public int Index { get; set; }10 public int Index1 { get; set; } 11 11 12 12 [Storable] 13 public int OldIndex{ get; set; }13 public int Index2 { get; set; } 14 14 15 15 [Storable] … … 23 23 : base(original, cloner) { 24 24 Root = original.Root; 25 Index = original.Index;26 OldIndex = original.OldIndex;25 Index1 = original.Index1; 26 Index2 = original.Index2; 27 27 } 28 28 public override IDeepCloneable Clone(Cloner cloner) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r10677 r10755 71 71 } 72 72 73 public new List<IGenealogyGraphNode>this[object content] {73 public new IGenealogyGraphNode this[object content] { 74 74 get { 75 75 var result = base[content]; 76 return result != null ? result.Cast<IGenealogyGraphNode>().ToList() : null;76 return result == null ? null : (IGenealogyGraphNode)result; 77 77 } 78 78 } … … 112 112 public override void AddVertex(IVertex vertex) { 113 113 var node = (IGenealogyGraphNode<T>)vertex; 114 if (!Ranks.ContainsKey(node.Rank)) 114 if (!Ranks.ContainsKey(node.Rank)) { 115 115 Ranks[node.Rank] = new LinkedList<IGenealogyGraphNode>(); 116 } 116 117 Ranks[node.Rank].AddLast(node); 117 118 base.AddVertex(vertex); … … 134 135 if (updated != null) updated(sender, args); 135 136 } 136 public new List<IGenealogyGraphNode<T>> this[object content] {137 public new IGenealogyGraphNode<T> this[object content] { 137 138 get { 138 139 var result = base[content]; 139 return result != null ? result.Cast<IGenealogyGraphNode<T>>().ToList() : null;140 return result == null ? null : (IGenealogyGraphNode<T>)result; 140 141 } 141 142 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Interfaces/IFragment.cs
r10650 r10755 25 25 public interface IFragment : IItem { 26 26 object Root { get; set; } 27 int Index { get; set; }28 int OldIndex{ get; set; }27 int Index1 { get; set; } 28 int Index2 { get; set; } 29 29 } 30 30 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r10677 r10755 49 49 public override IOperation Apply() { 50 50 if (GenealogyGraph.Contains(ChildParameter.ActualValue)) { 51 // if the graph already contains a vertex representing the child, it means that the child is a product of crossover 52 // when mutation follows after crossover, some changes need to be made 51 53 var child = ChildParameter.ActualValue; 52 54 var clone = (T)child.Clone(); 53 var vChild = (IGenealogyGraphNode<T>)GenealogyGraph[child] .Last();55 var vChild = (IGenealogyGraphNode<T>)GenealogyGraph[child]; 54 56 var vClone = new GenealogyGraphNode<T> { Content = clone, Rank = vChild.Rank - 0.5 }; 55 57 GenealogyGraph.AddVertex(vClone); 56 58 // adjust parent-child(clone) relationship in the graph 57 var parents = vChild.InArcs.Select(a => a.Source); 59 var parents = vChild.Parents; 60 // if there's a fragment, save it 61 IFragment fragment = null; 62 if (vChild.InArcs.Last().Data != null) { 63 fragment = (IFragment<T>)vChild.InArcs.Last().Data; 64 } 58 65 vChild.InArcs = new List<IGenealogyGraphArc>(); 59 66 foreach (var p in parents) { 60 foreach (var a in p.OutArcs) { 61 if (a.Target == vChild) 62 a.Target = vClone; 67 foreach (var a in p.OutArcs.Where(a => a.Target == vChild)) { 68 a.Target = vClone; 63 69 } 64 70 vClone.AddReverseArc(p); … … 66 72 vChild.AddReverseArc(vClone); 67 73 vClone.AddForwardArc(vChild); 74 vClone.InArcs.Last().Data = fragment; 68 75 } else { // this needs to be checked 69 76 var vChild = new GenealogyGraphNode<T> { Content = ChildParameter.ActualValue, Rank = Generations.Value }; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs
r10752 r10755 2 2 using System.Drawing; 3 3 using System.Linq; 4 using HeuristicLab.Common; 4 5 using HeuristicLab.Core.Views; 5 6 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 93 94 var aPos = aTile.Position; 94 95 95 if (node.Content.Index > 0) { 96 var subtree = node.Content.Root.NodeAt(node.Content.Index); 97 foreach (var s in subtree.IterateNodesPrefix()) { 96 if (node.Rank.IsAlmost(0)) { 97 foreach (var s in node.Content.Root.IterateNodesPrefix()) { 98 98 var primitive = aTile.GetPrimitive(s); 99 99 if (primitive != null) { 100 100 var rpb = primitive as RectangularPrimitiveBase; 101 101 if (rpb != null) { 102 rpb.Pen = new Pen(Color.RoyalBlue); 102 rpb.Pen = new Pen(Color.ForestGreen); 103 } 104 } 105 } 106 } else { 107 108 if (node.Content.Index1 > 0) { 109 var subtree = node.Content.Root.NodeAt(node.Content.Index1); 110 foreach (var s in subtree.IterateNodesPrefix()) { 111 var primitive = aTile.GetPrimitive(s); 112 if (primitive != null) { 113 var rpb = primitive as RectangularPrimitiveBase; 114 if (rpb != null) { 115 rpb.Pen = new Pen(Color.RoyalBlue); 116 } 103 117 } 104 118 } … … 115 129 116 130 if (child == node.Children.First()) { 117 if (node.Content.Index > 0) {118 var subtree = child.Content.Root.NodeAt(node.Content.Index );131 if (node.Content.Index1 > 0) { 132 var subtree = child.Content.Root.NodeAt(node.Content.Index1); 119 133 foreach (var s in subtree.IterateNodesPrefix()) { 120 134 var primitive = bTile.GetPrimitive(s); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs
r10752 r10755 81 81 var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data; 82 82 if (fragment != null) { 83 treeChart_HighlightSubtree( fragment.Root);83 treeChart_HighlightSubtree(graphNode.Content.NodeAt(fragment.Index1)); 84 84 } 85 85 } … … 101 101 var subtreeIndex = graphNode.Content.IterateNodesPrefix().ToList().IndexOf(subtree); 102 102 var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex); 103 MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen 103 if (fragmentGraph.Nodes.Any()) { 104 MainFormManager.MainForm.ShowContent(fragmentGraph); // display the fragment graph on the screen 105 } 104 106 } else { 105 107 // perform matching like it was done before … … 108 110 var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer)); 109 111 110 var matchingVertices = matchingTrees.Select Many(x => Content[x]).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();112 var matchingVertices = matchingTrees.Select(x => Content[x]).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>(); 111 113 graphChart_highlightMatchingVertices(matchingVertices); 112 114 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs
r10752 r10755 10 10 public override IOperation Apply() { 11 11 var child = ChildParameter.ActualValue; 12 var childVertex = (IGenealogyGraphNode)GenealogyGraph[child] .Last();12 var childVertex = (IGenealogyGraphNode)GenealogyGraph[child]; 13 13 var arcs = childVertex.InArcs.ToList(); 14 14 var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data; … … 20 20 fragment = new Fragment<ISymbolicExpressionTreeNode> { 21 21 Root = childNodes[i], 22 Index = i,22 Index1 = i, 23 23 }; 24 fragment. OldIndex= nodes1.IndexOf(fragment.Root);24 fragment.Index2 = nodes1.IndexOf(fragment.Root); 25 25 break; 26 26 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs
r10677 r10755 7 7 8 8 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 9 public class SymbolicDataAnalysisExpressionAfterManipulatorOperator : BeforeManipulatorOperator<ISymbolicExpressionTree> {9 public class SymbolicDataAnalysisExpressionAfterManipulatorOperator : AfterManipulatorOperator<ISymbolicExpressionTree> { 10 10 private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer; 11 11 … … 19 19 20 20 public override IOperation Apply() { 21 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue] .First();21 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue]; 22 22 var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs.First().Data; 23 var nodesAfter = ChildParameter.ActualValue.IterateNodes Breadth().ToList();23 var nodesAfter = ChildParameter.ActualValue.IterateNodesPrefix().ToList(); 24 24 IFragment<ISymbolicExpressionTreeNode> fragment = null; 25 25 26 26 for (int i = 0; i < Math.Min(nodesAfter.Count, nodesBefore.Count); ++i) { 27 if (comparer.Equals(nodesAfter[i], nodesBefore[i])) continue; 27 var a = nodesAfter[i]; 28 var b = nodesBefore[i]; 29 if (ReferenceEquals(a, b) && comparer.Equals(a, b)) continue; 28 30 fragment = new Fragment<ISymbolicExpressionTreeNode> { 29 Root = nodesAfter[i],30 Index = i31 Root = a, 32 Index1 = i 31 33 }; 32 34 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs
r10752 r10755 31 31 var result = base.Apply(); // the base operator will add the child to the graph before the actual crossover operation takes place 32 32 var parents = ParentsParameter.ActualValue.ToList(); 33 var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[parents[0]] .Last(); // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet)33 var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[parents[0]]; // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet) 34 34 var arcs = childVertex.InArcs.ToList(); 35 35 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs
r10677 r10755 7 7 public class SymbolicDataAnalysisExpressionBeforeManipulatorOperator : BeforeManipulatorOperator<ISymbolicExpressionTree> { 8 8 public override IOperation Apply() { 9 var result = base.Apply(); 9 var result = base.Apply(); // add the vertex in the genealogy graph 10 10 11 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue] .Last();12 vChild.InArcs.First().Data = vChild.Content.IterateNodes Breadth().ToList();11 var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue]; 12 vChild.InArcs.First().Data = vChild.Content.IterateNodesPrefix().ToList(); 13 13 14 14 return result; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionTracing.cs
r10752 r10755 1 1 2 using System; 2 3 using System.Collections.Generic; 3 4 using System.IO; … … 24 25 var index = subtreeIndex; // current index 25 26 var parent = parentNode; // current parent 27 26 28 while (true) { 29 if (!node.Parents.Any()) { 30 var fragmentNode = new FragmentNode { 31 Content = new Fragment<ISymbolicExpressionTreeNode> { Root = node.Content.NodeAt(index) }, 32 Rank = node.Rank 33 }; 34 if (parent != null) { 35 var arc = new GenealogyGraphArc { Source = parent, Target = fragmentNode }; 36 parent.AddForwardArc(arc); 37 // fragmentNode.Content.Index1 = parent.Content.Index1; 38 } 39 yield return fragmentNode; // no tracing if there are no parents 40 break; 41 } 42 43 if (node.IsElite) { 44 node = node.Parents.First(); 45 continue; 46 } // skip elite, go up the graph to original individual 47 27 48 var tree = node.Content; 28 49 var subtree = tree.NodeAt(index); 29 50 var subtreeLength = subtree.GetLength(); 30 var fragmentNode = new FragmentNode { 31 Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, 32 Rank = node.Rank 33 }; 34 if (parent != null) { 35 var arc = new GenealogyGraphArc { Source = parent, Target = fragmentNode }; 36 parent.AddForwardArc(arc); 37 } 38 parent = fragmentNode; 39 yield return fragmentNode; 40 if (!node.Parents.Any()) break; 41 if (node.IsElite) { 42 node = node.Parents.First(); 43 continue; 44 } 51 45 52 var fragment = (IFragment<ISymbolicExpressionTreeNode>)node.InArcs.Last().Data; 46 53 if (fragment == null) break; 47 54 var fragmentLength = fragment.Root.GetLength(); 48 if (fragment.Index == index) { 55 56 if (fragment.Index1 == index) { 49 57 node = node.Parents.Last(); // take second parent which contains the fragment 50 index = fragment.OldIndex; 51 } else if (fragment.Index < index) { 52 if (fragment.Index + fragmentLength > index) { // fragment contains subtree 53 node = node.Parents.Last(); // take second parent 54 index += fragment.OldIndex - fragment.Index; 55 } else { // fragment distinct from subtree 58 index = fragment.Index2; 59 } else if (fragment.Index1 < index) { 60 if (fragment.Index1 + fragmentLength > index) { 61 node = node.Parents.Last(); // fragment contains subtree, take second parent 62 index += fragment.Index2 - fragment.Index1; // the subtree has the same index relative to the fragment 63 } else { 64 // fragment distinct from subtree 56 65 node = node.Parents.First(); // take first parent which contains the subtree 57 index += node.Content.NodeAt(fragment.Index ).GetLength() - fragmentLength; // subtreeIndex must be adjusted according to swapped fragments sizes66 index += node.Content.NodeAt(fragment.Index1).GetLength() - fragmentLength; // subtreeIndex must be adjusted according to swapped fragments sizes 58 67 } 59 } else if (fragment.Index > index) { 60 if (fragment.Index < index + subtreeLength) { // subtree contains fragment => bifurcation point in the fragment graph 61 fragmentNode.Content.Index = fragment.Index - index; 68 } else if (fragment.Index1 > index) { 69 if (fragment.Index1 < index + subtreeLength) { 70 // subtree contains fragment => bifurcation point in the fragment graph 71 var fragmentNode = new FragmentNode { 72 Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, 73 Rank = node.Rank 74 }; 75 if (parent != null) { 76 var arc = new GenealogyGraphArc { Source = parent, Target = fragmentNode }; 77 parent.AddForwardArc(arc); 78 } 79 fragmentNode.Content.Index1 = fragment.Index1 - index; 80 yield return fragmentNode; 81 62 82 var left = Trace(node.Parents.First(), index, fragmentNode); // trace subtree 63 var right = Trace(node.Parents.Last(), fragment.OldIndex, fragmentNode); // trace fragment 64 foreach (var v in left.Concat(right)) { yield return v; } 83 84 if (node.Parents.Last().Content.NodeAt(fragment.Index2).GetLength() != fragmentLength) { 85 throw new Exception("Fragment lengths should match!"); 86 } 87 var right = Trace(node.Parents.Last(), fragment.Index2, fragmentNode); // trace fragment 88 foreach (var v in left.Concat(right)) { 89 yield return v; 90 } 65 91 break; 92 } else { 93 node = node.Parents.First(); // fragment distinct from subtree: update node, index remains unchanged 66 94 } 67 node = node.Parents.First(); // fragment distinct from subtree: update node, index remains unchanged68 95 } 69 96 }
Note: See TracChangeset
for help on using the changeset viewer.