Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs @ 11858

Last change on this file since 11858 was 11858, checked in by bburlacu, 9 years ago

#1772: Micro optimization inside the TraceCalculator and inside the GenealogyGraphNode (Children and Parents properties), resulting in maybe 25% speed gain. Corrected item name for the BeforeCrossoverOperator, very minor refactoring inside the BeforeManipulatorOperator. Updated project file.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.EvolutionTracking {
30  [StorableClass]
31  [Item("GenealogGraphNode", "A class representing a node in the GenealogyGraph")]
32  public class GenealogyGraphNode : Vertex, IGenealogyGraphNode {
33    [StorableConstructor]
34    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
35
36    public override IDeepCloneable Clone(Cloner cloner) {
37      return new GenealogyGraphNode(this, cloner);
38    }
39
40    protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner)
41      : base(original, cloner) {
42      Quality = original.Quality;
43      Rank = original.Rank;
44      IsElite = original.IsElite;
45      Id = Guid.NewGuid().ToString();
46    }
47
48    public GenealogyGraphNode(IDeepCloneable data)
49      : base(data) {
50      Id = Guid.NewGuid().ToString();
51    }
52
53    public new IEnumerable<IGenealogyGraphArc> InArcs {
54      get { return base.InArcs.Select(x => (IGenealogyGraphArc)x); }
55    }
56
57    public new IEnumerable<IGenealogyGraphArc> OutArcs {
58      get { return base.OutArcs.Select(x => (IGenealogyGraphArc)x); }
59    }
60
61    public IEnumerable<IGenealogyGraphNode> Parents {
62      get { return base.InArcs.Select(x => (IGenealogyGraphNode)x.Source); }
63    }
64
65    public IEnumerable<IGenealogyGraphNode> Children {
66      get { return base.OutArcs.Select(x => (IGenealogyGraphNode)x.Target); }
67    }
68
69    public IEnumerable<IGenealogyGraphNode> Ancestors {
70      get {
71        if (!Parents.Any())
72          return Enumerable.Empty<IGenealogyGraphNode>();
73        // for performance, we use a hashset for lookup and a list for iteration
74        var nodes = new HashSet<IGenealogyGraphNode>(Parents);
75        var list = new List<IGenealogyGraphNode>(Parents);
76        int i = 0;
77        while (i != list.Count) {
78          foreach (var e in list[i].InArcs) {
79            if (nodes.Contains(e.Source)) continue;
80            nodes.Add(e.Source);
81            list.Add(e.Source);
82          }
83          ++i;
84        }
85        return list;
86      }
87    }
88
89    public IEnumerable<IGenealogyGraphNode> Descendants {
90      get {
91        var nodes = new HashSet<IGenealogyGraphNode>(Children);
92        var list = new List<IGenealogyGraphNode>(Children);
93        int i = 0;
94        while (i != list.Count) {
95          foreach (var e in list[i].OutArcs) {
96            if (nodes.Contains(e.Target)) continue;
97            nodes.Add(e.Target);
98            list.Add(e.Target);
99          }
100          ++i;
101        }
102        return list;
103      }
104    }
105
106    [Storable]
107    public string Id { get; private set; }
108
109    [Storable]
110    private double rank;
111    public double Rank {
112      get { return rank; }
113      set { rank = value; }
114    }
115    [Storable]
116    private double quality;
117    public double Quality {
118      get { return quality; }
119      set { quality = value; }
120    }
121    [Storable]
122    private bool isElite;
123    public bool IsElite {
124      get { return isElite; }
125      set { isElite = value; }
126    }
127    public int CompareTo(IGenealogyGraphNode other) {
128      return Quality.CompareTo(other.Quality);
129    }
130  }
131
132  [StorableClass]
133  [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")]
134  public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem {
135    public new T Data {
136      get { return (T)base.Data; }
137      set { base.Data = value; }
138    }
139
140    [StorableConstructor]
141    protected GenealogyGraphNode(bool deserializing) : base(deserializing) { }
142
143    public GenealogyGraphNode(IDeepCloneable content) : base(content) { }
144
145    protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner)
146      : base(original, cloner) {
147    }
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new GenealogyGraphNode<T>(this, cloner);
150    }
151    public new IEnumerable<IGenealogyGraphNode<T>> Parents {
152      get { return base.Parents.Select(x => (IGenealogyGraphNode<T>)x); }
153    }
154    public new IEnumerable<IGenealogyGraphNode<T>> Children {
155      get { return base.Children.Select(x => (IGenealogyGraphNode<T>)x); }
156    }
157    public new IEnumerable<IGenealogyGraphNode<T>> Ancestors {
158      get { return base.Ancestors.Select(x => (IGenealogyGraphNode<T>)x); }
159    }
160    public new IEnumerable<IGenealogyGraphNode<T>> Descendants {
161      get { return base.Descendants.Select(x => (IGenealogyGraphNode<T>)x); }
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.