Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/26/14 01:56:35 (10 years ago)
Author:
bburlacu
Message:

#1772: Introduced separate class for FragmentNodes and adjusted tracing code. Fixed small bug creating the genealogy graph.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r10886 r10888  
    252252        for (int i = 0; i < population.Count; ++i) {
    253253          var individual = population[i];
    254           var vertex = new GenealogyGraphNode<T> { Content = individual, Rank = Generations.Value, };
     254          var vertex = new GenealogyGraphNode<T> { Content = individual, Rank = Generations.Value };
    255255          GenealogyGraph.AddVertex(vertex);
    256256          // save the vertex id in the individual scope (so that we can identify graph indices)
     
    258258        }
    259259      } else {
    260         // TODO: eliminate from the graph extra vertices added by the recombination operators when filling the pool of offspring with offspring selection
    261 
    262260        var elite = population.FirstOrDefault(x => GenealogyGraph.Contains(x));
    263261        if (elite != null) {
     262          var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph[elite];
     263          prevVertex.IsElite = true; // mark elites in the graph retroactively
     264
     265          var clone = (T)elite.Clone();
     266
    264267          var vertex = new GenealogyGraphNode<T> {
    265             Content = (T)elite.Clone(),
     268            Content = prevVertex.Content,
    266269            Rank = Generations.Value,
    267             IsElite = true
     270            Quality = prevVertex.Quality,
     271            IsElite = false
    268272          };
     273
     274          GenealogyGraph.SetContent(prevVertex, clone);
     275          // swap id
     276          var id = prevVertex.Id;
     277          GenealogyGraph.SetId(prevVertex, vertex.Id);
     278
    269279          // add new vertex to the graph
     280          vertex.Id = id;
    270281          GenealogyGraph.AddVertex(vertex);
    271           // swap content between current vertex and previous vertex
    272           var previousVertex = (IGenealogyGraphNode<T>)GenealogyGraph[elite];
    273           var tmp = vertex.Content;
    274           vertex.Content = previousVertex.Content;
    275           previousVertex.Content = tmp;
    276           // adjust graph content map
    277           GenealogyGraph[vertex.Content] = vertex;
    278           GenealogyGraph[previousVertex.Content] = previousVertex;
    279           GenealogyGraph.AddArc(previousVertex, vertex); // connect current elite with previous elite
    280           vertex.Id = previousVertex.Id; // another way would be to introduce the vertex.Id into the scope of the elite
    281           vertex.Quality = previousVertex.Quality;
    282 
    283           if (previousVertex.InArcs.Any()) {
    284             vertex.InArcs.Last().Data = previousVertex.InArcs.Last().Data; // save the fragment in case there is one
     282
     283          GenealogyGraph.AddArc(prevVertex, vertex); // connect current elite with previous elite
     284
     285          if (prevVertex.InArcs.Any()) {
     286            vertex.InArcs.Last().Data = prevVertex.InArcs.Last().Data; // save the fragment in case there is one
    285287          }
    286288        }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Arc.cs

    r10278 r10888  
    5454    public Arc() { }
    5555
     56    public Arc(IVertex source, IVertex target) {
     57      Source = source;
     58      Target = target;
     59    }
     60
    5661    protected Arc(Arc original, Cloner cloner)
    5762      : base(original, cloner) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs

    r10884 r10888  
    3232  [StorableClass]
    3333  public class DirectedGraph : Item, IDirectedGraph {
     34    public bool AllowDuplicateContent { get; set; }
     35
    3436    [Storable]
    3537    private readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove)
     
    101103    }
    102104    public virtual void AddVertex(IVertex vertex) {
    103       if (contentMap.ContainsKey(vertex.Content)) {
     105      if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) {
    104106        throw new Exception("Content already exists in the graph.");
    105107      }
     
    109111    }
    110112
     113    public virtual IArc AddArc(IVertex source, IVertex target) {
     114      var arc = new Arc(source, target);
     115      source.AddForwardArc(arc);
     116      target.AddReverseArc(arc);
     117      return arc;
     118    }
     119
    111120    public virtual void RemoveVertex(IVertex vertex) {
    112121      nodes.Remove(vertex);
    113122    }
     123
    114124    public override Image ItemImage {
    115125      get { return Common.Resources.VSImageLibrary.Graph; }
    116126    }
     127
     128    public void SetContent(IVertex vertex, object content) {
     129      var oldContent = vertex.Content;
     130      contentMap.Remove(oldContent);
     131      vertex.Content = content;
     132      contentMap[content] = vertex;
     133    }
     134
     135    public void SetId(IVertex vertex, string id) {
     136      var oldId = vertex.Id;
     137      idMap.Remove(oldId);
     138      vertex.Id = id;
     139      idMap[id] = vertex;
     140    }
    117141  }
    118142}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IArc.cs

    r10278 r10888  
    2020#endregion
    2121
     22
    2223namespace HeuristicLab.EvolutionTracking {
    2324  public interface IArc {
    2425    IVertex Source { get; set; }
    2526    IVertex Target { get; set; }
     27    string Label { get; set; }
     28    double Weight { get; set; }
     29    object Data { get; set; }
     30  }
     31
     32  public interface IArc<T> : IArc where T : class,IVertex {
     33    new T Source { get; set; }
     34    new T Target { get; set; }
    2635  }
    2736}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs

    r10884 r10888  
    3030    void Clear();
    3131    void AddVertex(IVertex vertex);
     32    IArc AddArc(IVertex source, IVertex target);
    3233    void RemoveVertex(IVertex vertex);
    3334    IEnumerable<IVertex> Nodes { get; }
     
    3536    bool Contains(object content);
    3637    IVertex GetVertex(string id);
     38    void SetContent(IVertex vertex, object content);
     39    void SetId(IVertex vertex, string id);
    3740  }
    3841}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs

    r10886 r10888  
    2525namespace HeuristicLab.EvolutionTracking {
    2626  public interface IVertex : IItem {
    27     string Id { get; }
    28     IEnumerable<IArc> InArcs { get; set; }
    29     IEnumerable<IArc> OutArcs { get; set; }
     27    string Id { get; set; }
     28    IEnumerable<IArc> InArcs { get; }
     29    IEnumerable<IArc> OutArcs { get; }
    3030
    3131    int InDegree { get; }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs

    r10886 r10888  
    4646    public IEnumerable<IArc> InArcs {
    4747      get { return inArcs ?? Enumerable.Empty<IArc>(); }
    48       set { inArcs = value.ToList(); }
    4948    }
    5049    [Storable]
     
    5251    public IEnumerable<IArc> OutArcs {
    5352      get { return outArcs ?? Enumerable.Empty<IArc>(); }
    54       set { outArcs = value.ToList(); }
    5553    }
    56 
    5754    [Storable]
    5855    public object Content { get; set; }
     
    6966      Id = Guid.NewGuid().ToString();
    7067      Label = original.Label;
    71       inArcs = new List<IArc>(original.InArcs);
    72       outArcs = new List<IArc>(original.OutArcs);
     68      inArcs = new List<IArc>(original.inArcs);
     69      outArcs = new List<IArc>(original.outArcs);
    7370    }
    7471
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r10886 r10888  
    2929namespace HeuristicLab.EvolutionTracking {
    3030  [StorableClass]
    31   [Item("GenealogyGraph", "")]
     31  [Item("GenealogyGraph", "A class representing a genealogy graph")]
    3232  public class GenealogyGraph : DirectedGraph, IGenealogyGraph {
    3333    [Storable]
     
    4141    }
    4242
    43     public IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
    44       var arc = new GenealogyGraphArc(source, target);
    45       source.AddForwardArc(arc);
    46       target.AddReverseArc(arc);
    47       return arc;
    48     }
    49 
    5043    protected GenealogyGraph(GenealogyGraph original, Cloner cloner)
    5144      : base(original, cloner) {
     45      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks);
    5246    }
    5347    public override IDeepCloneable Clone(Cloner cloner) {
     
    5953      Ranks = new Dictionary<double, List<IGenealogyGraphNode>>();
    6054    }
     55
     56    public override IArc AddArc(IVertex source, IVertex target) {
     57      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
     58      source.AddForwardArc(arc);
     59      target.AddReverseArc(arc);
     60      return arc;
     61    }
     62
    6163    public override void AddVertex(IVertex vertex) {
     64      base.AddVertex(vertex);
    6265      var node = (IGenealogyGraphNode)vertex;
    6366      if (!Ranks.ContainsKey(node.Rank))
    6467        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
    6568      Ranks[node.Rank].Add(node);
    66       base.AddVertex(vertex);
    6769    }
    6870    public override void RemoveVertex(IVertex vertex) {
     
    9496
    9597  [StorableClass]
    96   [Item("GenealogyGraph", "")]
     98  [Item("GenealogyGraph", "A class representing a genealogy graph")]
    9799  public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem {
    98100    // members and properties
     
    106108      get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; }
    107109    }
    108 
    109     public IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target) {
    110       var arc = new GenealogyGraphArc(source, target);
    111       source.AddForwardArc(arc);
    112       target.AddReverseArc(arc);
    113       return arc;
    114     }
    115 
    116110    // contructors
    117111    protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner)
     
    128122    // methods
    129123    public override void AddVertex(IVertex vertex) {
     124      base.AddVertex(vertex);
    130125      var node = (IGenealogyGraphNode<T>)vertex;
    131126      if (!Ranks.ContainsKey(node.Rank)) {
     
    133128      }
    134129      Ranks[node.Rank].Add(node);
    135       base.AddVertex(vertex);
    136130    }
    137131    public override void RemoveVertex(IVertex vertex) {
     
    142136      base.RemoveVertex(vertex);
    143137    }
    144 
     138    public override IArc AddArc(IVertex source, IVertex target) {
     139      var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target);
     140      source.AddForwardArc(arc);
     141      target.AddReverseArc(arc);
     142      return arc;
     143    }
    145144    IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes {
    146145      get { return Nodes; }
    147146    }
    148 
    149147    public event EventHandler GraphUpdated;
    150148    private void OnGraphUpdated(object sender, EventArgs args) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphArc.cs

    r10886 r10888  
    6969    protected GenealogyGraphArc(GenealogyGraphArc<T> original, Cloner cloner)
    7070      : base(original, cloner) {
    71       Data = (T)original.Data.Clone();
     71      Data = (T)original.Data;
    7272    }
    7373
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r10886 r10888  
    4242
    4343    public new IEnumerable<IGenealogyGraphArc> InArcs {
    44       get { return base.InArcs.Cast<IGenealogyGraphArc>().ToList(); }
    45       set { base.InArcs = value; }
     44      get { return base.InArcs.Cast<IGenealogyGraphArc>(); }
    4645    }
    4746    public new IEnumerable<IGenealogyGraphArc> OutArcs {
    48       get { return base.OutArcs.Cast<IGenealogyGraphArc>().ToList(); }
    49       set { base.InArcs = value; }
     47      get { return base.OutArcs.Cast<IGenealogyGraphArc>(); }
    5048    }
    5149    public IEnumerable<IGenealogyGraphNode> Ancestors {
     
    131129    }
    132130    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
    133       get { return base.Parents.Select(p => (IGenealogyGraphNode<T>)p); }
     131      get { return base.Parents.Cast<IGenealogyGraphNode<T>>(); }
    134132    }
    135133    public new IEnumerable<IGenealogyGraphNode<T>> Children {
    136       get { return base.Children.Select(c => (IGenealogyGraphNode<T>)c); }
     134      get { return base.Children.Cast<IGenealogyGraphNode<T>>(); }
    137135    }
    138136    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
    139       get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
     137      get { return base.Ancestors.Cast<IGenealogyGraphNode<T>>(); }
    140138    }
    141139    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
    142       get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
     140      get { return base.Descendants.Cast<IGenealogyGraphNode<T>>(); }
    143141    }
    144142  }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs

    r10886 r10888  
    2727    Dictionary<double, List<IGenealogyGraphNode>> Ranks { get; }
    2828    new IEnumerable<IGenealogyGraphNode> Nodes { get; }
    29     IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target);
    3029  }
    3130
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs

    r10347 r10888  
    2626    new IGenealogyGraphNode Source { get; set; }
    2727    new IGenealogyGraphNode Target { get; set; }
    28     object Data { get; set; }
    2928  }
    3029
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs

    r10677 r10888  
    3333    IEnumerable<IGenealogyGraphNode> Ancestors { get; }
    3434    IEnumerable<IGenealogyGraphNode> Descendants { get; }
    35     new IEnumerable<IGenealogyGraphArc> InArcs { get; set; }
    36     new IEnumerable<IGenealogyGraphArc> OutArcs { get; set; }
     35    new IEnumerable<IGenealogyGraphArc> InArcs { get; }
     36    new IEnumerable<IGenealogyGraphArc> OutArcs { get; }
    3737  }
    3838
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs

    r10886 r10888  
    4343    }
    4444
     45    public IValueParameter<IntValue> ExecutionCountParameter {
     46      get { return (IValueParameter<IntValue>)Parameters["ExecutionCount"]; }
     47    }
     48
    4549    protected BeforeCrossoverOperator(BeforeCrossoverOperator<T> original, Cloner cloner)
    4650      : base(original, cloner) {
     
    5357      Parameters.Add(new ScopeTreeLookupParameter<T>(ParentsParameterName));
    5458      Parameters.Add(new LookupParameter<T>(ChildParameterName));
     59      Parameters.Add(new ValueParameter<IntValue>("ExecutionCount", new IntValue(0)));
    5560    }
    5661
     
    6570    private readonly Func<IScope, string> getScopeId = s => ((StringValue)s.Variables["Id"].Value).Value;
    6671    public override IOperation Apply() {
    67       if (CurrentGeneration == null) throw new Exception();
     72      ExecutionCountParameter.Value.Value++;
     73
    6874      var subScopes = ExecutionContext.Scope.SubScopes;
    6975      var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetVertex(getScopeId(x))).ToList();
     
    7581        // but the first parent is actually the future child so we use this
    7682        Content = parents[0],
     83        //        Rank = Generations.Value + 1
    7784        Rank = parentVertices[0].Rank + 1
    7885      };
     86
    7987      GenealogyGraph.AddVertex(childVertex);
     88
    8089      foreach (var parentVertex in parentVertices) {
    8190        GenealogyGraph.AddArc(parentVertex, childVertex);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs

    r10886 r10888  
    2020#endregion
    2121
    22 using System.Collections.Generic;
    2322using System.Linq;
    2423using HeuristicLab.Common;
     
    5049
    5150    public override IOperation Apply() {
    52       if (GenealogyGraph.Contains(ChildParameter.ActualValue)) {
     51      var vChild = (IGenealogyGraphNode<T>)GenealogyGraph[ChildParameter.ActualValue];
     52      if (vChild.Rank.IsAlmost(Generations.Value + 1)) {
    5353        // if the graph already contains a vertex representing the child, it means that the child is a product of crossover
    5454        // when mutation follows after crossover, some changes need to be made to the graph to maintain consistency
    55         var child = ChildParameter.ActualValue;
    56         var clone = (T)child.Clone();
    57         var vChild = (IGenealogyGraphNode<T>)GenealogyGraph[child];
    58         var vClone = new GenealogyGraphNode<T> { Content = clone, Rank = vChild.Rank - 0.5 };
     55        var vClone = new GenealogyGraphNode<T> {
     56          Content = (T)ChildParameter.ActualValue.Clone(),
     57          Rank = vChild.Parents.Last().Rank + 0.5
     58        };
    5959        GenealogyGraph.AddVertex(vClone);
    60         var parents = vChild.Parents; // parents of child become parents of clone
    61         foreach (var p in parents) {
    62           var arc = p.OutArcs.First(a => a.Target == vChild);
     60
     61        foreach (var arc in vChild.InArcs) {
    6362          arc.Target = vClone;
    6463          vClone.AddReverseArc(arc);
     
    6665
    6766        vClone.InArcs.Last().Data = vChild.InArcs.Last().Data; // fragment of child becomes fragment of clone
    68         vChild.InArcs = new List<IGenealogyGraphArc>(); // child will be connected to clone
    6967        GenealogyGraph.AddArc(vClone, vChild);
    7068
     
    7573        GenealogyGraph[parent.Content] = parent;
    7674
    77       } else { // this needs to be checked
    78         var vChild = new GenealogyGraphNode<T> { Content = ChildParameter.ActualValue, Rank = Generations.Value };
    79         GenealogyGraph.AddVertex(vChild);
     75      } else if (vChild.Rank.IsAlmost(Generations.Value)) {
     76        var clone = (T)ChildParameter.ActualValue.Clone();
     77        GenealogyGraph.SetContent(vChild, clone);
     78        var xx = new GenealogyGraphNode<T> { Content = ChildParameter.ActualValue, Rank = Generations.Value + 1 };
     79        GenealogyGraph.AddVertex(xx);
     80        GenealogyGraph.AddArc(vChild, xx);
    8081      }
    8182      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.