Free cookie consent management tool by TermsFeed Policy Generator

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/HeuristicLab.EvolutionTracking/3.4
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.