Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11253


Ignore:
Timestamp:
07/31/14 17:11:39 (10 years ago)
Author:
bburlacu
Message:

#1772: Ported the rest of the changes to the DirectedGraph and Vertex to the GenealogyGraph and GenealogyGraphNode. Adapted tracking operators, analyzers and views.

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  
    4545    // TODO: Put event handlers of child controls here.
    4646    public virtual void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs args) {
    47       var content = ((VisualGenealogyGraphNode)sender).Data.Content;
     47      var content = ((VisualGenealogyGraphNode)sender).Data.Data;
    4848      if (content != null) {
    4949        viewHost.Content = (IContent)content;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking.Views/3.4/HeuristicLab.EvolutionTracking.Views-3.4.csproj

    r11227 r11253  
    9797      <DependentUpon>FrequentFragmentsDialog.cs</DependentUpon>
    9898    </Compile>
    99     <Compile Include="GenealogyGraphChart.cs">
    100       <SubType>UserControl</SubType>
    101     </Compile>
     99    <Compile Include="GenealogyGraphChart.cs" />
    102100    <Compile Include="GenealogyGraphChart.Designer.cs">
    103101      <DependentUpon>GenealogyGraphChart.cs</DependentUpon>
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r11233 r11253  
    2020#endregion
    2121
    22 using System.Collections.Generic;
     22using System;
    2323using System.Linq;
    2424using HeuristicLab.Analysis;
     
    259259        int index = 0;
    260260        T elite = null;
     261
     262        int psum1 = GenealogyGraph.Ranks[generation].Sum(x => x.Parents.Count());
     263
    261264        for (int i = 0; i < population.Length; ++i) {
    262           if (GenealogyGraph.Contains(population[i])) {
     265          if (GenealogyGraph.GetByContent(population[i]).Rank.Equals(generation - 1)) {
    263266            elite = population[i];
    264267            index = i;
     
    271274          var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(elite);
    272275          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) {
    277278            Rank = generation,
    278279            Quality = prevVertex.Quality,
     
    280281          };
    281282
    282           var w = new GenealogyGraphNode<T>(clone) {
    283             Rank = generation - 1,
    284             Quality = prevVertex.Quality,
    285             IsElite = true
    286           };
    287 
    288           // replace previous elite with vertex w
    289           List<IGenealogyGraphNode<T>> parents = null;
    290283          object data = null;
    291           if (prevVertex.InArcs.Any()) {
     284          if (prevVertex.InArcs.Any())
    292285            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
    298289          GenealogyGraph.RemoveVertex(prevVertex);
    299290          GenealogyGraph.AddVertex(w);
     
    301292          GenealogyGraph.AddArc(w, v); // connect current elite with previous elite
    302293
     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
    303298          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;
    308302
    309303          // inject the graph node unique id to the scope
     
    311305        }
    312306        #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.");
    313311
    314312        ComputeSuccessRatios();
     
    316314      // update qualities
    317315      for (int i = 0; i < population.Length; ++i) {
    318         var vertex = (IGenealogyGraphNode)GenealogyGraph.GetByContent(population[i]);
     316        var vertex = GenealogyGraph.GetByContent(population[i]);
    319317        vertex.Quality = qualities[i].Value;
    320318      }
    321319
    322320      // 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();
    324322      foreach (var vertex in discardedOffspring.Select(individual => GenealogyGraph.GetByContent(individual))) {
    325323        GenealogyGraph.RemoveVertex(vertex);
     
    334332      // compute the weight of each genealogy graph node as the ratio (produced offspring) / (surviving offspring)
    335333      foreach (var ind in population) {
    336         var v = (IGenealogyGraphNode)GenealogyGraph.GetByContent(ind);
     334        var v = GenealogyGraph.GetByContent(ind);
    337335        foreach (var p in v.Parents)
    338336          p.Weight++;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r11233 r11253  
    6969    public override IArc AddArc(IVertex source, IVertex target) {
    7070      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
    71       source.AddArc(arc);
    72       target.AddArc(arc);
    73       arcs.Add(arc);
     71      base.AddArc(arc);
    7472      return arc;
    7573    }
     
    8280      Ranks[node.Rank].Add(node);
    8381
    84       if (contentMap.ContainsKey(node.Content))
     82      if (contentMap.ContainsKey(node.Data))
    8583        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
    86       contentMap[node.Content] = node;
     84      contentMap[node.Data] = node;
    8785
    8886      if (idMap.ContainsKey(node.Id))
    8987        throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph.");
    9088      idMap[node.Id] = node;
    91 
    92       vertex.Changed += OnVertexChanged;
    9389    }
    9490
    9591    public override void RemoveVertex(IVertex vertex) {
    9692      var node = (IGenealogyGraphNode)vertex;
    97       contentMap.Remove(node.Content);
     93      contentMap.Remove(node.Data);
    9894      idMap.Remove(node.Id);
    9995      if (Ranks.ContainsKey(node.Rank)) {
     
    129125      ranks.Clear();
    130126    }
    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 content
    138       }
    139     }
    140127  }
    141128
    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")]
    211130  [StorableClass]
    212 
    213   [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]
    214131  public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem {
    215132    public new IEnumerable<IGenealogyGraphNode<T>> Vertices {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r11233 r11253  
    3333    [StorableConstructor]
    3434    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
     35
    3536    public override IDeepCloneable Clone(Cloner cloner) {
    3637      return new GenealogyGraphNode(this, cloner);
    3738    }
     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
    3850    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
    3951      : base(original, cloner) {
     52      Quality = original.Quality;
     53      Rank = original.Rank;
     54      IsElite = original.IsElite;
     55      Id = Guid.NewGuid().ToString();
    4056    }
    4157
    42     public GenealogyGraphNode(object content)
    43       : base(content) {
     58    public GenealogyGraphNode(IDeepCloneable data)
     59      : base(data) {
    4460      Id = Guid.NewGuid().ToString();
    4561    }
     
    4864      get { return base.InArcs.Cast<IGenealogyGraphArc>(); }
    4965    }
     66
    5067    public new IEnumerable<IGenealogyGraphArc> OutArcs {
    5168      get { return base.OutArcs.Cast<IGenealogyGraphArc>(); }
    5269    }
     70
    5371    public IEnumerable<IGenealogyGraphNode> Ancestors {
    5472      get {
     
    123141  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
    124142  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; }
    128146    }
    129147
    130     public GenealogyGraphNode(object content) : base(content) { }
     148    public GenealogyGraphNode(IGenealogyGraphNode<T> original) : base(original) { }
     149
     150    public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
    131151
    132152    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
    133153      : base(original, cloner) {
    134       // we do not clone the content
    135       Content = original.Content;
    136154    }
    137155    public override IDeepCloneable Clone(Cloner cloner) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs

    r11233 r11253  
    2020#endregion
    2121
     22using HeuristicLab.Core;
     23
    2224namespace HeuristicLab.EvolutionTracking {
    2325  public interface IGenealogyGraphArc : IArc {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs

    r11233 r11253  
    2222using System;
    2323using System.Collections.Generic;
     24using HeuristicLab.Core;
    2425
    2526namespace HeuristicLab.EvolutionTracking {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/HeuristicLab.EvolutionTracking-3.4.csproj

    r11227 r11253  
    105105  <ItemGroup>
    106106    <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" />
    113107    <Compile Include="Fragment.cs" />
    114108    <Compile Include="Interfaces\IFragment.cs" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs

    r11233 r11253  
    7777      var subScopes = ExecutionContext.Scope.SubScopes;
    7878      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.");
    7981
    8082      var parents = ParentsParameter.ActualValue.ToList();
     
    8486      GenealogyGraph.AddVertex(childVertex);
    8587
    86       foreach (var parentVertex in parentVertices.Distinct()) {
     88      foreach (var parentVertex in parentVertices)
    8789        GenealogyGraph.AddArc(parentVertex, childVertex);
    88       }
     90
    8991      ExecutionContext.Scope.Variables.Add(new Variable("Id", new StringValue(childVertex.Id)));
    90 
    9192      return base.Apply();
    9293    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs

    r11233 r11253  
    5454      var v = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(ChildParameter.ActualValue);
    5555      var clone = (T)ChildParameter.ActualValue.Clone();
     56      var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 };
     57      GenealogyGraph.AddVertex(c);
    5658
    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) {
    6061        var p = arc.Source;
    6162        GenealogyGraph.RemoveArc(arc);
     
    6364      }
    6465
    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);
    7366      GenealogyGraph.AddArc(c, v);
    7467
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj

    r11208 r11253  
    283283      <DependentUpon>SymboldDataAnalysisGenealogyView.cs</DependentUpon>
    284284    </Compile>
    285     <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.cs">
    286       <SubType>UserControl</SubType>
    287     </Compile>
     285    <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.cs" />
    288286    <Compile Include="Tracking\SymbolicDataAnalysisExpressionGenealogyGraphChart.Designer.cs">
    289287      <DependentUpon>SymbolicDataAnalysisExpressionGenealogyGraphChart.cs</DependentUpon>
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/FragmentGraphView.cs

    r11015 r11253  
    7070      tileDictionary.Clear();
    7171      foreach (var node in Content.Vertices) {
    72         var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)node.Content;
     72        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)node.Data;
    7373        var tile = new SymbolicExpressionTreeTile(chart);
    7474        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;
    7878        var tileNode = new TileLayoutNode { Tile = tile };
    7979        tileDictionary.Add(node, tileNode);
     
    117117        var aSize = aTile.Size;
    118118        var aPos = aTile.Position;
    119         var graphNode = node.Content;
     119        var graphNode = node.Data;
    120120
    121121        if (node.SubtreeIndex > 0) {
    122           var subtree = graphNode.Content.Root.NodeAt(node.SubtreeIndex);
     122          var subtree = graphNode.Data.Root.NodeAt(node.SubtreeIndex);
    123123          foreach (var s in subtree.IterateNodesPrefix()) {
    124124            var primitive = aTile.GetPrimitive(s);
     
    132132        }
    133133        if (node.FragmentIndex > 0) {
    134           var subtree = graphNode.Content.Root.NodeAt(node.FragmentIndex);
     134          var subtree = graphNode.Data.Root.NodeAt(node.FragmentIndex);
    135135          foreach (var s in subtree.IterateNodesPrefix()) {
    136136            var primitive = aTile.GetPrimitive(s);
     
    149149            var index = node.SubtreeIndex + (parent.FragmentIndex - parent.SubtreeIndex);
    150150            // 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);
    153153              var primitive = aTile.GetPrimitive(subtree);
    154154              primitive.Brush = new SolidBrush(Color.LightCoral);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs

    r11233 r11253  
    7474      var visualNode = (VisualGenealogyGraphNode)sender;
    7575      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
    76       var tree = graphNode.Content;
     76      var tree = graphNode.Data;
    7777      symbolicExpressionTreeChart.Tree = tree;
    7878
     
    8080        var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
    8181        if (fragment != null) {
    82           treeChart_HighlightSubtree(graphNode.Content.NodeAt(fragment.Index1));
     82          treeChart_HighlightSubtree(graphNode.Data.NodeAt(fragment.Index1));
    8383        }
    8484      }
     
    9898        // perform fragment tracing
    9999        var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)genealogyGraphChart.SelectedGraphNode;
    100         var subtreeIndex = graphNode.Content.IterateNodesPrefix().ToList().IndexOf(subtree);
     100        var subtreeIndex = graphNode.Data.IterateNodesPrefix().ToList().IndexOf(subtree);
    101101        var fragmentGraph = SymbolicDataAnalysisExpressionTracing.TraceSubtree(graphNode, subtreeIndex);
    102102        if (fragmentGraph.Vertices.Any()) {
     
    106106        // perform matching like it was done before
    107107        // 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);
    109109        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
    110110
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymbolicDataAnalysisExpressionLineageExplorerView.cs

    r10746 r11253  
    6464        var treeNode = new TreeNode(g.Quality.ToString(CultureInfo.InvariantCulture));
    6565        treeView.Nodes.Add(treeNode);
    66         treeMap.Add(treeNode, (ISymbolicExpressionTree)g.Content);
     66        treeMap.Add(treeNode, (ISymbolicExpressionTree)g.Data);
    6767      }
    6868    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r11233 r11253  
    173173  </ItemGroup>
    174174  <ItemGroup>
    175     <Compile Include="Analyzers\SymbolicDataAnalysisInternalDiversityAnalyzer.cs" />
    176175    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" />
    177176    <Compile Include="Analyzers\SymbolicDataAnalysisGenealogyAnalyzer.cs" />
     
    207206    <Compile Include="SlidingWindow\SlidingWindowVisualizer.cs" />
    208207    <Compile Include="SymbolicDataAnalysisExpressionPruningOperator.cs" />
    209     <Compile Include="SymbolicDataAnalysisExpressionTreeSimilarityCalculator.cs" />
    210208    <Compile Include="SymbolicDataAnalysisSolutionPruningOptimizer.cs" />
    211209    <Compile Include="Analyzers\SymbolicDataAnalysisVariableFrequencyAnalyzer.cs" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs

    r11233 r11253  
    3434      var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data;
    3535      var nodes1 = (List<ISymbolicExpressionTreeNode>)arcs[1].Data;
    36       var childNodes = childVertex.Content.IterateNodesPrefix().ToList();
     36      var childNodes = childVertex.Data.IterateNodesPrefix().ToList();
    3737      IFragment<ISymbolicExpressionTreeNode> fragment = null;
    3838      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  
    2929namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    3030  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 = true
    38       };
    39     }
    40 
    4131    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;
    4434      var nodesAfter = ChildParameter.ActualValue.IterateNodesPrefix().ToList();
    4535      IFragment<ISymbolicExpressionTreeNode> fragment = null;
    4636
    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;
    5052
    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);
    6659        }
     60
     61        var lca = LowestCommonAncestor(root, list);
     62        int index = nodesAfter.IndexOf(lca);
    6763        fragment = new Fragment<ISymbolicExpressionTreeNode> {
    68           Root = a,
    69           Index1 = i,
    70           Index2 = i
     64          Root = lca,
     65          Index1 = index,
     66          Index2 = index
    7167        };
    72         break;
    7368      }
    7469
    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;
    7674      return base.Apply();
    7775    }
    7876
    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];
    82102    }
    83103  }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs

    r11233 r11253  
    3232      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetByContent(ChildParameter.ActualValue);
    3333      var vClone = vChild.Parents.Last();
    34       vChild.InArcs.First().Data = vClone.Content.IterateNodesPrefix().ToList();
     34      vChild.InArcs.First().Data = vClone.Data.IterateNodesPrefix().ToList();
    3535
    3636      var fragment = (IFragment<ISymbolicExpressionTreeNode>)vClone.InArcs.Last().Data;
     
    3838        // this step must be performed because the fragment root actually references the original child (not the clone).
    3939        // 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);
    4141      }
    4242
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionTracing.cs

    r11233 r11253  
    2424using System.IO;
    2525using System.Linq;
     26using HeuristicLab.Core;
    2627using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2728using HeuristicLab.EvolutionTracking;
     
    7475        }
    7576        var fragmentLength = fragment.Root.GetLength();
    76         var tree = node.Content;
     77        var tree = node.Data;
    7778        var subtree = tree.NodeAt(index);
    7879        var subtreeLength = subtree.GetLength();
     
    9798              // subtree contained in fragment, index stays the same
    9899            } else {
    99               index += node.Content.NodeAt(fragment.Index1).GetLength() - fragmentLength;
     100              index += node.Data.NodeAt(fragment.Index1).GetLength() - fragmentLength;
    100101            }
    101102          } else if (subtreeIndex < fragment.Index1) {
     
    131132              // fragment distinct from subtree
    132133              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 sizes
     134              index += node.Data.NodeAt(fragment.Index1).GetLength() - fragmentLength; // subtreeIndex must be adjusted according to swapped fragments sizes
    134135            }
    135136            continue;
     
    150151              yield return fragmentNode;
    151152
    152               if (parents[1].Content.NodeAt(fragment.Index2).GetLength() != fragmentLength) {
     153              if (parents[1].Data.NodeAt(fragment.Index2).GetLength() != fragmentLength) {
    153154                throw new Exception("Fragment lengths should match!");
    154155              }
Note: See TracChangeset for help on using the changeset viewer.