Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10897 for branches


Ignore:
Timestamp:
05/27/14 16:33:17 (10 years ago)
Author:
bburlacu
Message:

#1772: Adjusted namespace in Plugin.cs.frame for HeuristicLab.EvolutionTracking.Views. Simplified DirectedGraph and GenealogyGraph API. Added public events for the Vertex content (so that the graph itself can be notified when the content changes and can adjust it's content-to-vertex map. Adjusted instrumented operators code to reflect api changes.

Location:
branches/HeuristicLab.EvolutionTracking
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking.Views/3.4/Plugin.cs.frame

    r10458 r10897  
    2222using HeuristicLab.PluginInfrastructure;
    2323
    24 namespace HeuristicLab.EvolutionaryTracking.Views {
     24namespace HeuristicLab.EvolutionTracking.Views {
    2525  [Plugin("HeuristicLab.EvolutionTracking.Views", "Provides controls and views for the evolution graph and related structures.", "3.4.2.$WCREV$")]
    2626  [PluginFile("HeuristicLab.EvolutionTracking.Views-3.4.dll", PluginFileType.Assembly)]
     
    3333  [PluginDependency("HeuristicLab.EvolutionTracking", "3.4")]
    3434
    35   public class HeuristicLabEvolutionaryTrackingPlugin : PluginBase {
     35  public class HeuristicLabEvolutionTrackingPlugin : PluginBase {
    3636  }
    3737}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r10888 r10897  
    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>(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         var elite = population.FirstOrDefault(x => GenealogyGraph.Contains(x));
     260        int index = 0;
     261        T elite = null;
     262        for (int i = 0; i < population.Count; ++i) {
     263          if (GenealogyGraph.Contains(population[i])) {
     264            elite = population[i];
     265            index = i;
     266          }
     267          break;
     268        }
     269
    261270        if (elite != null) {
    262           var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph[elite];
     271          var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph.GetVertex(elite);
    263272          prevVertex.IsElite = true; // mark elites in the graph retroactively
    264273
    265274          var clone = (T)elite.Clone();
    266275
    267           var vertex = new GenealogyGraphNode<T> {
    268             Content = prevVertex.Content,
     276          var vertex = new GenealogyGraphNode<T>(prevVertex.Content) {
    269277            Rank = Generations.Value,
    270278            Quality = prevVertex.Quality,
     
    272280          };
    273281
    274           GenealogyGraph.SetContent(prevVertex, clone);
    275           // swap id
    276           var id = prevVertex.Id;
    277           GenealogyGraph.SetId(prevVertex, vertex.Id);
    278 
    279           // add new vertex to the graph
    280           vertex.Id = id;
     282          prevVertex.Content = clone;
     283
     284          ExecutionContext.Scope.SubScopes[index].Variables["Id"].Value = new StringValue(vertex.Id);
     285
    281286          GenealogyGraph.AddVertex(vertex);
    282287
     
    290295      // update qualities
    291296      for (int i = 0; i < population.Count; ++i) {
    292         var vertex = (IGenealogyGraphNode)GenealogyGraph[population[i]];
     297        var vertex = (IGenealogyGraphNode)GenealogyGraph.GetVertex(population[i]);
    293298        vertex.Quality = qualities[i].Value;
    294299      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Arc.cs

    r10888 r10897  
    3131  public class Arc : Item, IArc {
    3232    [Storable]
    33     private IVertex source;
    34     public IVertex Source { get { return source; } set { source = value; } }
     33    public IVertex Source { get; set; }
     34
    3535    [Storable]
    36     private IVertex target;
    37     public IVertex Target { get { return target; } set { target = value; } }
     36    public IVertex Target { get; set; }
     37
    3838    [Storable]
    39     private string label;
    40     public string Label { get { return label; } set { label = value; } }
     39    public string Label { get; set; }
     40
    4141    [Storable]
    42     private double weight;
    43     public double Weight { get { return weight; } set { weight = value; } }
     42    public double Weight { get; set; }
     43
    4444    [Storable]
    45     private object data;
    46     public object Data { get { return data; } set { data = value; } }
     45    public object Data { get; set; }
    4746
    4847    [StorableConstructor]
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs

    r10888 r10897  
    4545    [Storable]
    4646    private readonly Dictionary<object, IVertex> contentMap;
     47
     48    [StorableConstructor]
     49    protected DirectedGraph(bool serializing)
     50      : base(serializing) {
     51    }
     52
    4753    public DirectedGraph() {
    4854      nodes = new List<IVertex>();
     
    5056      idMap = new Dictionary<string, IVertex>();
    5157    }
    52     [StorableConstructor]
    53     protected DirectedGraph(bool serializing)
    54       : base(serializing) {
    55     }
     58
    5659    protected DirectedGraph(DirectedGraph original, Cloner cloner)
    5760      : base(original, cloner) {
     
    6063      idMap = new Dictionary<string, IVertex>(original.idMap);
    6164    }
     65
    6266    public override IDeepCloneable Clone(Cloner cloner) {
    6367      return new DirectedGraph(this, cloner);
    6468    }
     69
    6570    public bool Contains(IVertex t) {
    6671      return nodes.Contains(t);
     
    7782    }
    7883
    79     public IVertex this[object key] {
    80       get {
    81         IVertex result;
    82         contentMap.TryGetValue(key, out result);
    83         return result;
    84       }
    85       set {
    86         if (contentMap.ContainsKey(key)) {
    87           idMap.Remove(contentMap[key].Id);
    88         }
    89         contentMap[key] = value;
    90         idMap[value.Id] = value;
    91       }
     84    public IVertex GetVertex(object content) {
     85      IVertex result;
     86      contentMap.TryGetValue(content, out result);
     87      return result;
    9288    }
     89
    9390    public virtual bool Any(Func<IVertex, bool> predicate) {
    9491      return nodes.Any(predicate);
    9592    }
     93
    9694    public virtual bool IsEmpty {
    9795      get { return !nodes.Any(); }
    9896    }
     97
    9998    public virtual void Clear() {
    10099      nodes.Clear();
     
    102101      idMap.Clear();
    103102    }
     103
    104104    public virtual void AddVertex(IVertex vertex) {
    105       if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) {
    106         throw new Exception("Content already exists in the graph.");
     105      if (vertex.Content != null) {
     106        if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) {
     107          throw new Exception("Content already exists in the graph.");
     108        }
     109        contentMap.Add(vertex.Content, vertex);
    107110      }
    108       contentMap[vertex.Content] = vertex;
    109       idMap[vertex.Id] = vertex;
     111      idMap.Add(vertex.Id, vertex);
    110112      nodes.Add(vertex);
     113
     114      vertex.PreContentChanged += Vertex_PreContentChanged;
     115      vertex.PostContentChanged += Vertex_PostContentChanged;
     116    }
     117
     118    public virtual void RemoveVertex(IVertex vertex) {
     119      contentMap.Remove(vertex.Content);
     120      idMap.Remove(vertex.Id);
     121      nodes.Remove(vertex);
     122
     123      vertex.PreContentChanged -= Vertex_PreContentChanged;
     124      vertex.PostContentChanged -= Vertex_PostContentChanged;
    111125    }
    112126
     
    118132    }
    119133
    120     public virtual void RemoveVertex(IVertex vertex) {
    121       nodes.Remove(vertex);
     134    private void Vertex_PreContentChanged(object sender, EventArgs args) {
     135      var vertex = (IVertex)sender;
     136      if (contentMap.ContainsKey(vertex.Content)) {
     137        contentMap.Remove(vertex.Content);
     138      }
     139    }
     140
     141    private void Vertex_PostContentChanged(object sender, EventArgs args) {
     142      var vertex = (IVertex)sender;
     143      if (vertex.Content != null)
     144        contentMap.Add(vertex.Content, vertex);
    122145    }
    123146
     
    125148      get { return Common.Resources.VSImageLibrary.Graph; }
    126149    }
    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     }
    141150  }
    142151}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs

    r10888 r10897  
    3333    void RemoveVertex(IVertex vertex);
    3434    IEnumerable<IVertex> Nodes { get; }
    35     IVertex this[object content] { get; set; }
    3635    bool Contains(object content);
    3736    IVertex GetVertex(string id);
    38     void SetContent(IVertex vertex, object content);
    39     void SetId(IVertex vertex, string id);
     37    IVertex GetVertex(object content);
    4038  }
    4139}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs

    r10888 r10897  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Core;
     
    2526namespace HeuristicLab.EvolutionTracking {
    2627  public interface IVertex : IItem {
    27     string Id { get; set; }
    28     IEnumerable<IArc> InArcs { get; }
    29     IEnumerable<IArc> OutArcs { get; }
     28    event EventHandler PreContentChanged;
     29    event EventHandler PostContentChanged;
     30
     31    string Id { get; }
     32    IEnumerable<IArc> InArcs { get; set; }
     33    IEnumerable<IArc> OutArcs { get; set; }
    3034
    3135    int InDegree { get; }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs

    r10890 r10897  
    3030  [StorableClass]
    3131  public class Vertex : Item, IVertex {
     32    public event EventHandler PreContentChanged;
     33    protected virtual void OnPreContentChanged(object sender, EventArgs args) {
     34      var changed = PreContentChanged;
     35      if (changed != null) {
     36        changed(sender, args);
     37      }
     38    }
     39
     40    public event EventHandler PostContentChanged;
     41    protected virtual void OnPostContentChanged(object sender, EventArgs args) {
     42      var changed = PostContentChanged;
     43      if (changed != null) {
     44        changed(sender, args);
     45      }
     46    }
     47
    3248    [Storable]
    3349    private string id;
     50
    3451    public string Id {
    3552      get { return id; }
    36       set { id = value; }
    3753    }
     54
    3855    [Storable]
    39     private string label;
    40     public string Label { get { return label; } set { label = value; } }
     56    public string Label { get; set; }
     57
    4158    [Storable]
    42     private double weight;
    43     public double Weight { get { return weight; } set { weight = value; } }
     59    public double Weight { get; set; }
     60
     61    [Storable]
     62    protected object content;
     63    public object Content {
     64      get { return content; }
     65      set {
     66        OnPreContentChanged(this, EventArgs.Empty);
     67        content = value;
     68        OnPostContentChanged(this, EventArgs.Empty);
     69      }
     70    }
     71
    4472    [Storable]
    4573    private List<IArc> inArcs;
     
    4876      set { inArcs = value.ToList(); }
    4977    }
     78
    5079    [Storable]
    5180    private List<IArc> outArcs;
     
    5483      set { outArcs = value.ToList(); }
    5584    }
    56     [Storable]
    57     public object Content { get; set; }
    5885
    5986    [StorableConstructor]
     
    6491    }
    6592
     93    public Vertex(object content)
     94      : this() {
     95      this.content = content;
     96    }
     97
    6698    protected Vertex(Vertex original, Cloner cloner)
    6799      : base(original, cloner) {
    68       Id = Guid.NewGuid().ToString();
     100      id = Guid.NewGuid().ToString();
     101      content = original.content;
    69102      Label = original.Label;
     103      Weight = original.Weight;
    70104      inArcs = new List<IArc>(original.inArcs);
    71105      outArcs = new List<IArc>(original.outArcs);
     
    79113    protected void AfterDeserialization() {
    80114      if (Id == null) {
    81         Id = Guid.NewGuid().ToString();
     115        id = Guid.NewGuid().ToString();
    82116      }
    83117    }
     
    117151    protected Vertex(Vertex<T> original, Cloner cloner)
    118152      : base(original, cloner) {
    119       Content = original.Content; // not sure if to Clone()
    120153    }
    121154
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs

    r10888 r10897  
    8181    }
    8282
    83     public new IGenealogyGraphNode this[object content] {
    84       get {
    85         var result = base[content];
    86         return result == null ? null : (IGenealogyGraphNode)result;
    87       }
    88       set { base[content] = value; }
    89     }
    90 
    9183    public override void Clear() {
    9284      base.Clear();
     
    123115    public override void AddVertex(IVertex vertex) {
    124116      base.AddVertex(vertex);
    125       var node = (IGenealogyGraphNode<T>)vertex;
     117      var node = (IGenealogyGraphNode)vertex;
    126118      if (!Ranks.ContainsKey(node.Rank)) {
    127119        Ranks[node.Rank] = new List<IGenealogyGraphNode>();
     
    150142      if (updated != null) updated(sender, args);
    151143    }
    152     public new IGenealogyGraphNode<T> this[object content] {
    153       get {
    154         var result = base[content];
    155         return result == null ? null : (IGenealogyGraphNode<T>)result;
    156       }
    157     }
    158144  }
    159145}
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs

    r10890 r10897  
    4040
    4141    public GenealogyGraphNode() { }
     42    public GenealogyGraphNode(object content) : base(content) { }
    4243
    4344    public new IEnumerable<IGenealogyGraphArc> InArcs {
     
    122123      set { base.Content = value; }
    123124    }
     125
    124126    public GenealogyGraphNode() { }
     127    public GenealogyGraphNode(object content) : base(content) { }
     128
    125129    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
    126130      : base(original, cloner) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs

    r10890 r10897  
    7777      var parents = ParentsParameter.ActualValue.ToList();
    7878
    79       var childVertex = new GenealogyGraphNode<T> {
    80         // the child parameter does not have a value yet (it will be assigned after crossover),
    81         // but the first parent is actually the future child so we use this
    82         Content = parents[0],
    83         Rank = parentVertices[0].Rank + 1
    84       };
     79      var childVertex = new GenealogyGraphNode<T>(parents[0]) { Rank = parentVertices[0].Rank + 1 };
    8580
    8681      GenealogyGraph.AddVertex(childVertex);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs

    r10890 r10897  
    5151    public override IOperation Apply() {
    5252      // since mutation always takes place after crossover, the vertex for the current child is already in the tree
    53       var v = (IGenealogyGraphNode<T>)GenealogyGraph[ChildParameter.ActualValue];
     53      var v = (IGenealogyGraphNode<T>)GenealogyGraph.GetVertex(ChildParameter.ActualValue);
    5454
    5555      if (!v.Rank.IsAlmost(Generations.Value + 1)) {
     
    5858      var clone = (T)ChildParameter.ActualValue.Clone();
    5959
    60       var c = new GenealogyGraphNode<T> {
    61         Rank = v.Rank - 0.5,
    62         Content = clone
    63       };
     60      var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 };
    6461
    6562      foreach (var a in v.InArcs) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/Tracking/SymboldDataAnalysisGenealogyView.cs

    r10888 r10897  
    109109        var matchingTrees = trees.Where(x => x.Root.ContainsSubtree(subtree, comparer));
    110110
    111         var matchingVertices = matchingTrees.Select(x => Content[x]).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
     111        var matchingVertices = matchingTrees.Select(x => Content.GetVertex(x)).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
    112112        graphChart_highlightMatchingVertices(matchingVertices);
    113113      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs

    r10890 r10897  
    3030  public class SymbolicDataAnalysisExpressionAfterCrossoverOperator : AfterCrossoverOperator<ISymbolicExpressionTree> {
    3131    public override IOperation Apply() {
    32       var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue];
     32      var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetVertex(ChildParameter.ActualValue);
    3333      var arcs = childVertex.InArcs.ToList();
    3434      var nodes0 = (List<ISymbolicExpressionTreeNode>)arcs[0].Data;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs

    r10837 r10897  
    4040
    4141    public override IOperation Apply() {
    42       var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue];
     42      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetVertex(ChildParameter.ActualValue);
    4343      var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs.First().Data;
    4444      var nodesAfter = ChildParameter.ActualValue.IterateNodesPrefix().ToList();
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeCrossoverOperator.cs

    r10888 r10897  
    3030      var result = base.Apply(); // the base operator will add the child to the graph before the actual crossover operation takes place
    3131      var parents = ParentsParameter.ActualValue.ToList();
    32       var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[parents[0]]; // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet)
     32      var childVertex = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetVertex(parents[0]); // use the parent since it is actually the child before crossover (and the ChildParameter doesn't have a value yet)
    3333      var arcs = childVertex.InArcs.ToList();
    3434
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionBeforeManipulatorOperator.cs

    r10888 r10897  
    3030      var result = base.Apply(); // add the vertex in the genealogy graph
    3131
    32       var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph[ChildParameter.ActualValue];
     32      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetVertex(ChildParameter.ActualValue);
    3333      var vClone = vChild.Parents.Last();
    3434      vChild.InArcs.First().Data = vClone.Content.IterateNodesPrefix().ToList();
Note: See TracChangeset for help on using the changeset viewer.