- Timestamp:
- 05/26/14 01:56:35 (10 years ago)
- 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 252 252 for (int i = 0; i < population.Count; ++i) { 253 253 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 }; 255 255 GenealogyGraph.AddVertex(vertex); 256 256 // save the vertex id in the individual scope (so that we can identify graph indices) … … 258 258 } 259 259 } else { 260 // TODO: eliminate from the graph extra vertices added by the recombination operators when filling the pool of offspring with offspring selection261 262 260 var elite = population.FirstOrDefault(x => GenealogyGraph.Contains(x)); 263 261 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 264 267 var vertex = new GenealogyGraphNode<T> { 265 Content = (T)elite.Clone(),268 Content = prevVertex.Content, 266 269 Rank = Generations.Value, 267 IsElite = true 270 Quality = prevVertex.Quality, 271 IsElite = false 268 272 }; 273 274 GenealogyGraph.SetContent(prevVertex, clone); 275 // swap id 276 var id = prevVertex.Id; 277 GenealogyGraph.SetId(prevVertex, vertex.Id); 278 269 279 // add new vertex to the graph 280 vertex.Id = id; 270 281 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 285 287 } 286 288 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Arc.cs
r10278 r10888 54 54 public Arc() { } 55 55 56 public Arc(IVertex source, IVertex target) { 57 Source = source; 58 Target = target; 59 } 60 56 61 protected Arc(Arc original, Cloner cloner) 57 62 : base(original, cloner) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/DirectedGraph.cs
r10884 r10888 32 32 [StorableClass] 33 33 public class DirectedGraph : Item, IDirectedGraph { 34 public bool AllowDuplicateContent { get; set; } 35 34 36 [Storable] 35 37 private readonly List<IVertex> nodes; // for performance reasons, maybe this should be a linked list (fast remove) … … 101 103 } 102 104 public virtual void AddVertex(IVertex vertex) { 103 if ( contentMap.ContainsKey(vertex.Content)) {105 if (!AllowDuplicateContent && contentMap.ContainsKey(vertex.Content)) { 104 106 throw new Exception("Content already exists in the graph."); 105 107 } … … 109 111 } 110 112 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 111 120 public virtual void RemoveVertex(IVertex vertex) { 112 121 nodes.Remove(vertex); 113 122 } 123 114 124 public override Image ItemImage { 115 125 get { return Common.Resources.VSImageLibrary.Graph; } 116 126 } 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 } 117 141 } 118 142 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IArc.cs
r10278 r10888 20 20 #endregion 21 21 22 22 23 namespace HeuristicLab.EvolutionTracking { 23 24 public interface IArc { 24 25 IVertex Source { get; set; } 25 26 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; } 26 35 } 27 36 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IDirectedGraph.cs
r10884 r10888 30 30 void Clear(); 31 31 void AddVertex(IVertex vertex); 32 IArc AddArc(IVertex source, IVertex target); 32 33 void RemoveVertex(IVertex vertex); 33 34 IEnumerable<IVertex> Nodes { get; } … … 35 36 bool Contains(object content); 36 37 IVertex GetVertex(string id); 38 void SetContent(IVertex vertex, object content); 39 void SetId(IVertex vertex, string id); 37 40 } 38 41 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Interfaces/IVertex.cs
r10886 r10888 25 25 namespace HeuristicLab.EvolutionTracking { 26 26 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; } 30 30 31 31 int InDegree { get; } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/DirectedGraph/Vertex.cs
r10886 r10888 46 46 public IEnumerable<IArc> InArcs { 47 47 get { return inArcs ?? Enumerable.Empty<IArc>(); } 48 set { inArcs = value.ToList(); }49 48 } 50 49 [Storable] … … 52 51 public IEnumerable<IArc> OutArcs { 53 52 get { return outArcs ?? Enumerable.Empty<IArc>(); } 54 set { outArcs = value.ToList(); }55 53 } 56 57 54 [Storable] 58 55 public object Content { get; set; } … … 69 66 Id = Guid.NewGuid().ToString(); 70 67 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); 73 70 } 74 71 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r10886 r10888 29 29 namespace HeuristicLab.EvolutionTracking { 30 30 [StorableClass] 31 [Item("GenealogyGraph", " ")]31 [Item("GenealogyGraph", "A class representing a genealogy graph")] 32 32 public class GenealogyGraph : DirectedGraph, IGenealogyGraph { 33 33 [Storable] … … 41 41 } 42 42 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 50 43 protected GenealogyGraph(GenealogyGraph original, Cloner cloner) 51 44 : base(original, cloner) { 45 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(original.Ranks); 52 46 } 53 47 public override IDeepCloneable Clone(Cloner cloner) { … … 59 53 Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 60 54 } 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 61 63 public override void AddVertex(IVertex vertex) { 64 base.AddVertex(vertex); 62 65 var node = (IGenealogyGraphNode)vertex; 63 66 if (!Ranks.ContainsKey(node.Rank)) 64 67 Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 65 68 Ranks[node.Rank].Add(node); 66 base.AddVertex(vertex);67 69 } 68 70 public override void RemoveVertex(IVertex vertex) { … … 94 96 95 97 [StorableClass] 96 [Item("GenealogyGraph", " ")]98 [Item("GenealogyGraph", "A class representing a genealogy graph")] 97 99 public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 98 100 // members and properties … … 106 108 get { return from n in base.Nodes select (IGenealogyGraphNode<T>)n; } 107 109 } 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 116 110 // contructors 117 111 protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) … … 128 122 // methods 129 123 public override void AddVertex(IVertex vertex) { 124 base.AddVertex(vertex); 130 125 var node = (IGenealogyGraphNode<T>)vertex; 131 126 if (!Ranks.ContainsKey(node.Rank)) { … … 133 128 } 134 129 Ranks[node.Rank].Add(node); 135 base.AddVertex(vertex);136 130 } 137 131 public override void RemoveVertex(IVertex vertex) { … … 142 136 base.RemoveVertex(vertex); 143 137 } 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 } 145 144 IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Nodes { 146 145 get { return Nodes; } 147 146 } 148 149 147 public event EventHandler GraphUpdated; 150 148 private void OnGraphUpdated(object sender, EventArgs args) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphArc.cs
r10886 r10888 69 69 protected GenealogyGraphArc(GenealogyGraphArc<T> original, Cloner cloner) 70 70 : base(original, cloner) { 71 Data = (T)original.Data .Clone();71 Data = (T)original.Data; 72 72 } 73 73 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs
r10886 r10888 42 42 43 43 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>(); } 46 45 } 47 46 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>(); } 50 48 } 51 49 public IEnumerable<IGenealogyGraphNode> Ancestors { … … 131 129 } 132 130 public new IEnumerable<IGenealogyGraphNode<T>> Parents { 133 get { return base.Parents. Select(p => (IGenealogyGraphNode<T>)p); }131 get { return base.Parents.Cast<IGenealogyGraphNode<T>>(); } 134 132 } 135 133 public new IEnumerable<IGenealogyGraphNode<T>> Children { 136 get { return base.Children. Select(c => (IGenealogyGraphNode<T>)c); }134 get { return base.Children.Cast<IGenealogyGraphNode<T>>(); } 137 135 } 138 136 public new IEnumerable<IGenealogyGraphNode<T>> Ancestors { 139 get { return base.Ancestors. Select(x => (IGenealogyGraphNode<T>)x); }137 get { return base.Ancestors.Cast<IGenealogyGraphNode<T>>(); } 140 138 } 141 139 public new IEnumerable<IGenealogyGraphNode<T>> Descendants { 142 get { return base.Descendants. Select(x => (IGenealogyGraphNode<T>)x); }140 get { return base.Descendants.Cast<IGenealogyGraphNode<T>>(); } 143 141 } 144 142 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraph.cs
r10886 r10888 27 27 Dictionary<double, List<IGenealogyGraphNode>> Ranks { get; } 28 28 new IEnumerable<IGenealogyGraphNode> Nodes { get; } 29 IGenealogyGraphArc AddArc(IGenealogyGraphNode source, IGenealogyGraphNode target);30 29 } 31 30 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs
r10347 r10888 26 26 new IGenealogyGraphNode Source { get; set; } 27 27 new IGenealogyGraphNode Target { get; set; } 28 object Data { get; set; }29 28 } 30 29 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs
r10677 r10888 33 33 IEnumerable<IGenealogyGraphNode> Ancestors { get; } 34 34 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; } 37 37 } 38 38 -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs
r10886 r10888 43 43 } 44 44 45 public IValueParameter<IntValue> ExecutionCountParameter { 46 get { return (IValueParameter<IntValue>)Parameters["ExecutionCount"]; } 47 } 48 45 49 protected BeforeCrossoverOperator(BeforeCrossoverOperator<T> original, Cloner cloner) 46 50 : base(original, cloner) { … … 53 57 Parameters.Add(new ScopeTreeLookupParameter<T>(ParentsParameterName)); 54 58 Parameters.Add(new LookupParameter<T>(ChildParameterName)); 59 Parameters.Add(new ValueParameter<IntValue>("ExecutionCount", new IntValue(0))); 55 60 } 56 61 … … 65 70 private readonly Func<IScope, string> getScopeId = s => ((StringValue)s.Variables["Id"].Value).Value; 66 71 public override IOperation Apply() { 67 if (CurrentGeneration == null) throw new Exception(); 72 ExecutionCountParameter.Value.Value++; 73 68 74 var subScopes = ExecutionContext.Scope.SubScopes; 69 75 var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetVertex(getScopeId(x))).ToList(); … … 75 81 // but the first parent is actually the future child so we use this 76 82 Content = parents[0], 83 // Rank = Generations.Value + 1 77 84 Rank = parentVertices[0].Rank + 1 78 85 }; 86 79 87 GenealogyGraph.AddVertex(childVertex); 88 80 89 foreach (var parentVertex in parentVertices) { 81 90 GenealogyGraph.AddArc(parentVertex, childVertex); -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r10886 r10888 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using System.Linq; 24 23 using HeuristicLab.Common; … … 50 49 51 50 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)) { 53 53 // if the graph already contains a vertex representing the child, it means that the child is a product of crossover 54 54 // 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 }; 59 59 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) { 63 62 arc.Target = vClone; 64 63 vClone.AddReverseArc(arc); … … 66 65 67 66 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 clone69 67 GenealogyGraph.AddArc(vClone, vChild); 70 68 … … 75 73 GenealogyGraph[parent.Content] = parent; 76 74 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); 80 81 } 81 82 return base.Apply();
Note: See TracChangeset
for help on using the changeset viewer.