Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/28/14 16:23:40 (11 years ago)
Author:
bburlacu
Message:

#1772: Improved usage of GenealogyGraph and GenealogyGraphNode classes. Made some progress on building block tracing.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeTile.cs

    r10655 r10677  
    5858    }
    5959    public ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> LayoutEngine { get; set; }
    60     private Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitiveMap;
     60    private readonly Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitiveMap;
    6161
    6262    public SymbolicExpressionTreeTile(IChart chart)
    6363      : base(chart) {
    6464      primitiveMap = new Dictionary<IPrimitive, ISymbolicExpressionTreeNode>();
     65
     66      Group = new Group(chart);
    6567    }
    6668    public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree)
    6769      : this(chart) {
    68       SymbolicExpressionTree = tree;
    6970      PreferredNodeWidth = 80;
    7071      PreferredNodeHeight = 40;
     72      SymbolicExpressionTree = tree;
    7173    }
    7274    private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) {
    7375      Clear();
    74       ISymbolicExpressionTreeNode root = Root;
    75       if (root.Symbol is ProgramRootSymbol && root.SubtreeCount == 1) { root = root.GetSubtree(0); }
    76       var visualNodes = LayoutEngine.CalculateLayout(root);
     76      var actualRoot = Root;
     77      if (Root.Symbol is ProgramRootSymbol && Root.SubtreeCount == 1) { actualRoot = Root.GetSubtree(0); }
     78      var visualNodes = LayoutEngine.CalculateLayout(actualRoot);
    7779
    7880      var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r10675 r10677  
    230230          previousVertex.AddForwardArc(vertex);
    231231          vertex.AddReverseArc(previousVertex);
    232           vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one
    233           vertex.Id = previousVertex.Id;
     232          vertex.Id = previousVertex.Id; // another way would be to introduce the vertex.Id into the scope of the elite
     233          if (previousVertex.InArcs.Any()) {
     234            vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one
     235          }
    234236        }
    235237      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs

    r10300 r10677  
    7777    public virtual void Clear() {
    7878      nodes.Clear();
     79      contentMap.Clear();
    7980    }
    8081    public virtual void AddVertex(IVertex vertex) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs

    r10650 r10677  
    2626  public interface IVertex : IItem {
    2727    string Id { get; }
    28     List<IArc> InArcs { get; }
    29     List<IArc> OutArcs { get; }
     28    IEnumerable<IArc> InArcs { get; set; }
     29    IEnumerable<IArc> OutArcs { get; set; }
    3030
    3131    int InDegree { get; }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs

    r10300 r10677  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    4344    [Storable]
    4445    private List<IArc> inArcs;
    45     public List<IArc> InArcs { get { return inArcs; } protected set { inArcs = value; } }
     46    public IEnumerable<IArc> InArcs {
     47      get { return inArcs ?? Enumerable.Empty<IArc>(); }
     48      set { inArcs = value.ToList(); }
     49    }
    4650    [Storable]
    4751    private List<IArc> outArcs;
    48     public List<IArc> OutArcs { get { return outArcs; } protected set { outArcs = value; } }
     52    public IEnumerable<IArc> OutArcs {
     53      get { return outArcs ?? Enumerable.Empty<IArc>(); }
     54      set { outArcs = value.ToList(); }
     55    }
    4956
    5057    [StorableConstructor]
     
    6168      Id = Guid.NewGuid().ToString();
    6269      Label = original.Label;
    63       InArcs = new List<IArc>(original.InArcs);
    64       OutArcs = new List<IArc>(original.OutArcs);
     70      inArcs = new List<IArc>(original.InArcs);
     71      outArcs = new List<IArc>(original.OutArcs);
    6572    }
    6673
     
    8188    public void AddForwardArc(IVertex target, double w = 0.0, object data = null) {
    8289      var arc = new Arc { Source = this, Target = target, Data = data, Weight = w };
    83       if (OutArcs == null) OutArcs = new List<IArc> { arc };
    84       else OutArcs.Add(arc);
     90      if (outArcs == null) outArcs = new List<IArc> { arc };
     91      else outArcs.Add(arc);
    8592    }
    8693    public void AddForwardArc(IArc arc) {
    8794      if (arc.Source == null) { arc.Source = this; }
    8895      if (arc.Source != this) { throw new Exception("AddForwardArc: Source should be equal to this."); }
    89       if (OutArcs == null) { OutArcs = new List<IArc> { arc }; } else { OutArcs.Add(arc); }
     96      if (outArcs == null) { outArcs = new List<IArc> { arc }; } else { outArcs.Add(arc); }
     97    }
     98    public void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
     99      var arc = new Arc { Source = source, Target = this, Data = data, Weight = w };
     100      if (inArcs == null) inArcs = new List<IArc> { arc };
     101      else inArcs.Add(arc);
    90102    }
    91103    public void AddReverseArc(IArc arc) {
    92104      if (arc.Target == null) { arc.Target = this; }
    93105      if (arc.Target != this) { throw new Exception("AddReverseArc: Target should be equal to this."); };
    94       if (InArcs == null) { InArcs = new List<IArc> { arc }; } else { InArcs.Add(arc); }
    95     }
    96     public void AddReverseArc(IVertex source, double w = 0.0, object data = null) {
    97       var arc = new Arc { Source = source, Target = this, Data = data, Weight = w };
    98       if (InArcs == null) InArcs = new List<IArc> { arc };
    99       else InArcs.Add(arc);
     106      if (inArcs == null) { inArcs = new List<IArc> { arc }; } else { inArcs.Add(arc); }
    100107    }
    101108
    102     public int InDegree { get { return InArcs == null ? 0 : InArcs.Count; } }
    103     public int OutDegree { get { return OutArcs == null ? 0 : OutArcs.Count; } }
     109    public int InDegree { get { return InArcs.Count(); } }
     110    public int OutDegree { get { return OutArcs.Count(); } }
    104111    public int Degree { get { return InDegree + OutDegree; } }
    105112  }
    106 
    107113
    108114  [StorableClass]
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r10650 r10677  
    3737      set { ranks = value; }
    3838    }
     39    public new IEnumerable<IGenealogyGraphNode> Nodes {
     40      get { return from n in base.Nodes select (IGenealogyGraphNode)n; }
     41    }
    3942    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
    4043      : base(original, cloner) {
     
    7477      }
    7578    }
     79
     80    public override void Clear() {
     81      base.Clear();
     82      ranks.Clear();
     83    }
    7684  }
    7785
     
    7987  [Item("GenealogyGraph", "")]
    8088  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
     89    // members and properties
    8190    [Storable]
    8291    private Dictionary<double, LinkedList<IGenealogyGraphNode>> ranks;
     
    8594      set { ranks = value; }
    8695    }
     96    public new IEnumerable<IGenealogyGraphNode<T>> Nodes {
     97      get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
     98    }
     99    // contructors
    87100    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
    88101      : base(original, cloner) {
     
    96109      Ranks = new Dictionary<double, LinkedList<IGenealogyGraphNode>>();
    97110    }
     111    // methods
    98112    public override void AddVertex(IVertex vertex) {
    99113      var node = (IGenealogyGraphNode<T>)vertex;
     
    110124      base.RemoveVertex(vertex);
    111125    }
     126
     127    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
     128      get { return Nodes; }
     129    }
     130
    112131    public event EventHandler GraphUpdated;
    113132    private void OnGraphUpdated(object sender, EventArgs args) {
     
    115134      if (updated != null) updated(sender, args);
    116135    }
    117 
    118136    public new List<IGenealogyGraphNode<T>> this[object content] {
    119137      get {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r10300 r10677  
    4141    public GenealogyGraphNode() { }
    4242
    43     public new List<IGenealogyGraphArc> InArcs {
     43    public new IEnumerable<IGenealogyGraphArc> InArcs {
    4444      get {
    45         return base.InArcs == null ? null : base.InArcs.Cast<IGenealogyGraphArc>().ToList();
     45        return base.InArcs.Cast<IGenealogyGraphArc>().ToList();
    4646      }
     47      set { base.InArcs = value; }
    4748    }
    48     public new List<IGenealogyGraphArc> OutArcs {
     49    public new IEnumerable<IGenealogyGraphArc> OutArcs {
    4950      get {
    50         return base.OutArcs == null ? null : base.OutArcs.Cast<IGenealogyGraphArc>().ToList();
     51        return base.OutArcs.Cast<IGenealogyGraphArc>().ToList();
    5152      }
     53      set { base.InArcs = value; }
    5254    }
    53     public IEnumerable<IGenealogyGraphNode> Ancestors() {
    54       // for performance, we use a hashset for lookup and a list for iteration
    55       var nodes = new HashSet<IGenealogyGraphNode> { this };
    56       var list = new List<IGenealogyGraphNode> { this };
    57       int i = 0;
    58       while (i != list.Count) {
    59         if (list[i].InArcs != null) {
     55    public IEnumerable<IGenealogyGraphNode> Ancestors {
     56      get {
     57        // for performance, we use a hashset for lookup and a list for iteration
     58        var nodes = new HashSet<IGenealogyGraphNode> { this };
     59        var list = new List<IGenealogyGraphNode> { this };
     60        int i = 0;
     61        while (i != list.Count) {
    6062          foreach (var e in list[i].InArcs) {
    6163            if (nodes.Contains(e.Source)) continue;
     
    6365            list.Add(e.Source);
    6466          }
     67          ++i;
    6568        }
    66         ++i;
     69        return list;
    6770      }
    68       return list;
    6971    }
    7072    /// <summary>
     
    7274    /// </summary>
    7375    /// <returns>All the descendants of the current node</returns>
    74     public IEnumerable<IGenealogyGraphNode> Descendants() {
    75       var nodes = new HashSet<IGenealogyGraphNode> { this };
    76       var list = new List<IGenealogyGraphNode> { this };
    77       int i = 0;
    78       while (i != list.Count) {
    79         if (list[i].OutArcs != null) {
     76    public IEnumerable<IGenealogyGraphNode> Descendants {
     77      get {
     78        var nodes = new HashSet<IGenealogyGraphNode> { this };
     79        var list = new List<IGenealogyGraphNode> { this };
     80        int i = 0;
     81        while (i != list.Count) {
    8082          foreach (var e in list[i].OutArcs) {
    8183            if (nodes.Contains(e.Target)) continue;
     
    8385            list.Add(e.Target);
    8486          }
     87          ++i;
    8588        }
    86         ++i;
     89        return list;
    8790      }
    88       return list;
    8991    }
    9092    [Storable]
     
    118120      base.AddReverseArc(arc);
    119121    }
     122
     123    public IEnumerable<IGenealogyGraphNode> Parents {
     124      get {
     125        return InArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : InArcs.Select(a => a.Source);
     126      }
     127    }
     128
     129    public IEnumerable<IGenealogyGraphNode> Children {
     130      get { return OutArcs == null ? Enumerable.Empty<IGenealogyGraphNode>() : OutArcs.Select(a => a.Source); }
     131    }
    120132  }
    121133
     
    135147      return new GenealogyGraphNode<T>(this, cloner);
    136148    }
     149
     150    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
     151      get {
     152        return base.Parents.Select(p => (IGenealogyGraphNode<T>)p);
     153      }
     154    }
     155
     156    public new IEnumerable<IGenealogyGraphNode<T>> Children {
     157      get {
     158        return base.Children.Select(c => (IGenealogyGraphNode<T>)c);
     159      }
     160    }
     161
     162    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
     163      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
     164    }
     165
     166    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
     167      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
     168    }
    137169  }
    138170}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs

    r10650 r10677  
    2626  public interface IGenealogyGraph : IDirectedGraph {
    2727    Dictionary<double, LinkedList<IGenealogyGraphNode>> Ranks { get; }
     28    new IEnumerable<IGenealogyGraphNode> Nodes { get; }
    2829  }
    2930
    30   public interface IGenealogyGraph<T> : IGenealogyGraph where T : class,IItem { }
     31  public interface IGenealogyGraph<T> : IGenealogyGraph where T : class, IItem {
     32    new IEnumerable<IGenealogyGraphNode<T>> Nodes { get; }
     33  }
    3134}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs

    r10650 r10677  
    2222using System;
    2323using System.Collections.Generic;
    24 using HeuristicLab.Core;
    2524
    2625namespace HeuristicLab.EvolutionTracking {
    2726  public interface IGenealogyGraphNode : IVertex, IComparable<IGenealogyGraphNode> {
    28     IEnumerable<IGenealogyGraphNode> Ancestors();
    29     IEnumerable<IGenealogyGraphNode> Descendants();
    30     new List<IGenealogyGraphArc> InArcs { get; }
    31     new List<IGenealogyGraphArc> OutArcs { get; }
    32 
    3327    double Rank { get; set; }
    3428    double Quality { get; set; }
    3529    bool IsElite { get; set; }
     30
     31    IEnumerable<IGenealogyGraphNode> Parents { get; }
     32    IEnumerable<IGenealogyGraphNode> Children { get; }
     33    IEnumerable<IGenealogyGraphNode> Ancestors { get; }
     34    IEnumerable<IGenealogyGraphNode> Descendants { get; }
     35    new IEnumerable<IGenealogyGraphArc> InArcs { get; set; }
     36    new IEnumerable<IGenealogyGraphArc> OutArcs { get; set; }
    3637  }
    3738
    38   public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class { }
     39  public interface IGenealogyGraphNode<T> : IGenealogyGraphNode, IVertex<T> where T : class {
     40    new IEnumerable<IGenealogyGraphNode<T>> Parents { get; }
     41    new IEnumerable<IGenealogyGraphNode<T>> Children { get; }
     42
     43    new IEnumerable<IGenealogyGraphNode<T>> Ancestors { get; }
     44    new IEnumerable<IGenealogyGraphNode<T>> Descendants { get; }
     45  }
    3946}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs

    r10650 r10677  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Common;
     
    5556        // adjust parent-child(clone) relationship in the graph
    5657        var parents = vChild.InArcs.Select(a => a.Source);
    57         vChild.InArcs.Clear();
     58        vChild.InArcs = new List<IGenealogyGraphArc>();
    5859        foreach (var p in parents) {
    5960          foreach (var a in p.OutArcs) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/FragmentGraphView.cs

    r10656 r10677  
    2020    public new IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>> Content {
    2121      get { return (IGenealogyGraph<IFragment<ISymbolicExpressionTreeNode>>)base.Content; }
    22       set {
    23         base.Content = value;
    24         MakeTiles();
    25         Draw();
    26       }
     22      set { base.Content = value; }
    2723    }
    2824
     
    3834      var chart = symbolicExpressionChartControl.Chart;
    3935      tileDictionary.Clear();
    40       foreach (var node in Content.Nodes.Cast<IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>>()) {
     36      foreach (var node in Content.Nodes) {
    4137        var tile = new SymbolicExpressionTreeTile(chart);
    4238        tile.LayoutEngine = symbolicExpressionEngine;
     
    4844      }
    4945
    50       foreach (var node in Content.Nodes.Where(n => n.InArcs != null && n.InArcs.Count > 0)) {
    51         var graphNode = (IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)node;
    52         var layoutNode = tileDictionary[graphNode];
     46
     47      foreach (var node in Content.Nodes.Where(n => n.Children.Any())) {
     48        var layoutNode = tileDictionary[node];
    5349        layoutNode.Children = new List<TileLayoutNode>();
    54         foreach (var c in node.InArcs.Select(x => x.Source as IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)) {
    55           layoutNode.Children.Add(tileDictionary[c]);
     50        foreach (var child in node.Children) {
     51          layoutNode.Children.Add(tileDictionary[child]);
    5652        }
    5753      }
     
    5955
    6056    private void Draw() {
    61       var root = tileDictionary[(IGenealogyGraphNode<IFragment<ISymbolicExpressionTreeNode>>)Content.Nodes[0]];
    62       var visualNodes = layoutEngine.CalculateLayout(root);
     57      var root = Content.Nodes.First();
     58
     59      var fragmentRoot = tileDictionary[root];
     60      var visualNodes = layoutEngine.CalculateLayout(fragmentRoot);
    6361
    6462      symbolicExpressionChartControl.UpdateEnabled = false;
     
    8280
    8381    #region Event Handlers (Content)
    84 
    8582    // TODO: Put event handlers of the content here
    86 
    87     #endregion
    88 
    8983    protected override void OnContentChanged() {
    9084      base.OnContentChanged();
    91       if (Content == null) {
    92         // TODO: Add code when content has been changed and is null
    93       } else {
    94         // TODO: Add code when content has been changed and is not null
     85      if (Content != null) {
     86        MakeTiles();
     87        Draw();
    9588      }
    9689    }
    97 
     90    #endregion
    9891
    9992    protected override void SetEnabledStateOfControls() {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymboldDataAnalysisGenealogyView.cs

    r10674 r10677  
    2323using System.Collections.Generic;
    2424using System.Drawing;
     25using System.IO;
    2526using System.Linq;
    2627using System.Windows.Forms;
     
    3435using FragmentGraph = HeuristicLab.EvolutionTracking.IGenealogyGraph<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;
    3536
    36 
    3737using FragmentNode = HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.EvolutionTracking.IFragment<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTreeNode>>;
    3838
     
    102102        // perform fragment tracing
    103103        var fragmentGraph = TraceSubtree(subtree);
    104         var fragmentGraphView = MainFormManager.CreateView(typeof(FragmentGraphView));
    105         fragmentGraphView.Content = fragmentGraph;
    106         fragmentGraphView.Show();
     104        MainFormManager.MainForm.ShowContent(fragmentGraph);
     105        //        var fragmentGraphView = MainFormManager.MainForm.ShowContent(fragmentGraph);
     106        //        fragmentGraphView.Content = fragmentGraph;
     107        //        fragmentGraphView.Show();
    107108
    108109        // open a FragmentGraphView displaying this fragmentGraph
     
    166167
    167168    #region fragment tracing
     169    #region helper methods to shorten the code
     170
     171    #endregion
     172
    168173    private FragmentGraph TraceSubtree(ISymbolicExpressionTreeNode subtree) {
    169174      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
     
    174179
    175180    private void Trace(IGenealogyGraphNode<ISymbolicExpressionTree> graphNode, ISymbolicExpressionTreeNode subtree, FragmentGraph fragmentGraph, FragmentNode parentNode = null) {
    176       if (graphNode.InArcs == null) return;
    177       // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node
    178       var tree = graphNode.Content;
    179       var subtreeIndex = tree.IterateNodesPrefix().ToList().IndexOf(subtree);
    180       var subtreeLength = subtree.GetLength();
    181 
    182       var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
    183       if (fragment == null) return;
    184       var fragmentLength = fragment.Root.GetLength();
    185 
    186       FragmentNode node = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank };
    187       if (parentNode != null) {
    188         AddChild(parentNode, node);
    189       }
    190       fragmentGraph.AddVertex(node);
    191 
    192       // below, we consider three cases:
    193       // 1) the selected subtree is the same as the fragment
    194       // 2) the fragment contains the selected subtree
    195       // 3) the fragment is contained by the selected subtree
    196       // In each case, the idea is to isolate and individually track the fragment and the rest of the subtree (for cases 2 and 3)
    197       if (fragment.Index == subtreeIndex) {
    198         // the selected subtree is the actual fragment
    199         if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!");
    200 
    201         var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
    202         var s = g.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
    203         Trace(g, s, fragmentGraph, node);
    204 
    205       } else if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) {
    206         // the fragment contains the selected subtree
    207         if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length.");
    208 
    209         var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs.Last().Source;
    210         var i = fragment.Root.IterateNodesPrefix().ToList().IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root
    211         var s = g.Content.IterateNodesPrefix().ToList()[fragment.OldIndex + i];
    212         Trace(g, s, fragmentGraph, node);
    213 
    214       } else if (subtreeIndex < fragment.Index && fragment.Index < subtreeIndex + subtreeLength) {
    215         // the selected subtree contains the fragment
    216         if (fragmentLength >= subtreeLength) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length.");
    217 
    218         var g0 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source;
    219         var s0 = g0.Content.IterateNodesPrefix().ToList()[subtreeIndex];
    220         Trace(g0, s0, fragmentGraph, node);
    221 
    222         if (graphNode.InArcs.Count > 1) {
    223           var g1 = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[1].Source;
    224           var s1 = g1.Content.IterateNodesPrefix().ToList()[fragment.OldIndex];
    225           Trace(g1, s1, fragmentGraph, node);
    226         }
    227       } else {
    228         // fragment and subtree are completely distinct, therefore we will track the subtree
    229         var g = (IGenealogyGraphNode<ISymbolicExpressionTree>)graphNode.InArcs[0].Source;
    230         var s = graphNode.Content.IterateNodesPrefix().ToList()[subtreeIndex];
    231         Trace(g, s, fragmentGraph, node);
     181      while (true) {
     182        if (!graphNode.InArcs.Any()) return;
     183
     184        var parentVertices = graphNode.Parents.ToList();
     185        // the subtree must belong to the currently displayed tree which in turn must belong to the currently selected graph node
     186        var tree = graphNode.Content;
     187        var subtreeIndex = tree.IndexOf(subtree);
     188        var subtreeLength = subtree.GetLength();
     189
     190        var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
     191        if (fragment == null) return;
     192        var fragmentLength = fragment.Root.GetLength();
     193
     194        FragmentNode node = new FragmentNode { Content = new Fragment<ISymbolicExpressionTreeNode> { Root = subtree }, Rank = graphNode.Rank };
     195        if (parentNode != null) {
     196          AddChild(parentNode, node);
     197        }
     198        fragmentGraph.AddVertex(node);
     199
     200        // if the selected subtree is the actual fragment
     201        if (fragment.Index == subtreeIndex) {
     202          if (fragmentLength != subtreeLength) throw new Exception("Fragment and subtree lengths should be the same!");
     203          graphNode = parentVertices.Last();
     204          tree = graphNode.Content;
     205          subtree = tree.NodeAt(fragment.OldIndex);
     206          parentNode = node;
     207          continue;
     208        }
     209        // if the fragment contains the selected subtree => track fragment, then track subtree
     210        if (fragment.Index < subtreeIndex && subtreeIndex < fragment.Index + fragmentLength) {
     211          if (subtreeLength >= fragmentLength) throw new Exception("Fragment contains subtree, so subtree length should be less than the fragment length.");
     212
     213          graphNode = parentVertices.Last();
     214          tree = graphNode.Content;
     215          var i = fragment.Root.IndexOf(subtree); // get the index of the selected subtree, relative to the fragment root
     216          subtree = tree.NodeAt(fragment.OldIndex + i);
     217          parentNode = node;
     218          continue;
     219        }
     220        // if the selected subtree contains the fragment => track fragment and subtree
     221        if (subtreeIndex < fragment.Index && fragment.Index < subtreeIndex + subtreeLength) {
     222          if (fragmentLength >= subtreeLength) throw new Exception("Subtree contains fragment, so fragment length should be less than the subtree length.");
     223
     224          graphNode = parentVertices[0];
     225          tree = graphNode.Content;
     226          subtree = tree.NodeAt(subtreeIndex);
     227          // track subtree
     228          Trace(graphNode, subtree, fragmentGraph, node);
     229
     230          // track fragment
     231          if (parentVertices.Count > 1) {
     232            graphNode = parentVertices[1];
     233            tree = graphNode.Content;
     234            subtree = tree.NodeAt(fragment.OldIndex);
     235            parentNode = node;
     236            continue;
     237          }
     238        } else {
     239          // fragment and subtree are completely distinct => we only track the subtree
     240          graphNode = parentVertices[0];
     241          tree = graphNode.Content;
     242          subtree = tree.NodeAt(subtreeIndex);
     243          parentNode = node;
     244          continue;
     245        }
     246        break;
    232247      }
    233248    }
     
    240255    #endregion
    241256  }
     257
     258  internal static class Util {
     259    private static string ViewAsText(this ISymbolicExpressionTreeNode root) {
     260      var writer = new StringWriter();
     261      SymbolicExpressionTreeHierarchicalFormatter.RenderNode(writer, root, string.Empty);
     262      return writer.ToString();
     263    }
     264    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTree tree, int position) {
     265      return NodeAt(tree.Root, position);
     266    }
     267    internal static ISymbolicExpressionTreeNode NodeAt(this ISymbolicExpressionTreeNode root, int position) {
     268      return root.IterateNodesPrefix().ElementAt(position);
     269    }
     270    internal static int IndexOf(this ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node) {
     271      return IndexOf(tree.Root, node);
     272    }
     273    internal static int IndexOf(this ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node) {
     274      return root.IterateNodesPrefix().ToList().IndexOf(node); // not too worried about efficiency here
     275    }
     276  }
    242277}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs

    r10654 r10677  
    1010    public override IOperation Apply() {
    1111      var child = ChildParameter.ActualValue;
    12       var childVertex = GenealogyGraph[child].Last();
    13       var arc0 = (IGenealogyGraphArc)childVertex.InArcs[0];
    14       var arc1 = (IGenealogyGraphArc)childVertex.InArcs[1];
    15       var nodes0 = (List<ISymbolicExpressionTreeNode>)arc0.Data;
    16       var nodes1 = (List<ISymbolicExpressionTreeNode>)arc1.Data;
     12      var childVertex = (IGenealogyGraphNode)GenealogyGraph[child].Last();
     13      var arcs = childVertex.InArcs.ToList();
     14      var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data;
     15      var nodes1 = (List<ISymbolicExpressionTreeNode>)arcs[1].Data;
    1716      var childNodes = child.IterateNodesPrefix().ToList();
    1817      IFragment<ISymbolicExpressionTreeNode> fragment = null;
     
    2827      if (fragment == null) throw new Exception("Could not determine fragment!");
    2928
    30       arc0.Data = null;
    31       arc1.Data = fragment;
     29      arcs[0].Data = null;
     30      arcs[1].Data = fragment;
    3231
    3332      return base.Apply();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs

    r10650 r10677  
    2020    public override IOperation Apply() {
    2121      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue].First();
    22       var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs[0].Data;
     22      var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs.First().Data;
    2323      var nodesAfter = ChildParameter.ActualValue.IterateNodesBreadth().ToList();
    2424      IFragment<ISymbolicExpressionTreeNode> fragment = null;
     
    3232      }
    3333
    34       vChild.InArcs[0].Data = fragment;
     34      vChild.InArcs.First().Data = fragment;
    3535      return base.Apply();
    3636    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs

    r10674 r10677  
    3232      var parents = ParentsParameter.ActualValue.ToList();
    3333      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)
     34      var arcs = childVertex.InArcs.ToList();
    3435
    3536      for (int i = 0; i < parents.Count; ++i) {
    3637        var nodes = parents[i].IterateNodesPrefix().ToList();
    37         var arc = childVertex.InArcs[i];
    38         arc.Data = nodes;
     38        arcs[i].Data = nodes;
    3939      }
    4040      var parentVertices = childVertex.InArcs.Select(x => (IGenealogyGraphNode<ISymbolicExpressionTree>)x.Source).ToList();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs

    r10347 r10677  
    1010
    1111      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue].Last();
    12       vChild.InArcs[0].Data = vChild.Content.IterateNodesBreadth().ToList();
     12      vChild.InArcs.First().Data = vChild.Content.IterateNodesBreadth().ToList();
    1313
    1414      return result;
Note: See TracChangeset for help on using the changeset viewer.