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.

File:
1 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++;
Note: See TracChangeset for help on using the changeset viewer.