- Timestamp:
- 07/31/14 17:11:39 (10 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r11233 r11253 20 20 #endregion 21 21 22 using System .Collections.Generic;22 using System; 23 23 using System.Linq; 24 24 using HeuristicLab.Analysis; … … 259 259 int index = 0; 260 260 T elite = null; 261 262 int psum1 = GenealogyGraph.Ranks[generation].Sum(x => x.Parents.Count()); 263 261 264 for (int i = 0; i < population.Length; ++i) { 262 if (GenealogyGraph. Contains(population[i])) {265 if (GenealogyGraph.GetByContent(population[i]).Rank.Equals(generation - 1)) { 263 266 elite = population[i]; 264 267 index = i; … … 271 274 var prevVertex = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(elite); 272 275 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) { 277 278 Rank = generation, 278 279 Quality = prevVertex.Quality, … … 280 281 }; 281 282 282 var w = new GenealogyGraphNode<T>(clone) {283 Rank = generation - 1,284 Quality = prevVertex.Quality,285 IsElite = true286 };287 288 // replace previous elite with vertex w289 List<IGenealogyGraphNode<T>> parents = null;290 283 object data = null; 291 if (prevVertex.InArcs.Any()) {284 if (prevVertex.InArcs.Any()) 292 285 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 298 289 GenealogyGraph.RemoveVertex(prevVertex); 299 290 GenealogyGraph.AddVertex(w); … … 301 292 GenealogyGraph.AddArc(w, v); // connect current elite with previous elite 302 293 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 303 298 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; 308 302 309 303 // inject the graph node unique id to the scope … … 311 305 } 312 306 #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."); 313 311 314 312 ComputeSuccessRatios(); … … 316 314 // update qualities 317 315 for (int i = 0; i < population.Length; ++i) { 318 var vertex = (IGenealogyGraphNode)GenealogyGraph.GetByContent(population[i]);316 var vertex = GenealogyGraph.GetByContent(population[i]); 319 317 vertex.Quality = qualities[i].Value; 320 318 } 321 319 322 320 // 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(); 324 322 foreach (var vertex in discardedOffspring.Select(individual => GenealogyGraph.GetByContent(individual))) { 325 323 GenealogyGraph.RemoveVertex(vertex); … … 334 332 // compute the weight of each genealogy graph node as the ratio (produced offspring) / (surviving offspring) 335 333 foreach (var ind in population) { 336 var v = (IGenealogyGraphNode)GenealogyGraph.GetByContent(ind);334 var v = GenealogyGraph.GetByContent(ind); 337 335 foreach (var p in v.Parents) 338 336 p.Weight++; -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraph.cs
r11233 r11253 69 69 public override IArc AddArc(IVertex source, IVertex target) { 70 70 var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 71 source.AddArc(arc); 72 target.AddArc(arc); 73 arcs.Add(arc); 71 base.AddArc(arc); 74 72 return arc; 75 73 } … … 82 80 Ranks[node.Rank].Add(node); 83 81 84 if (contentMap.ContainsKey(node. Content))82 if (contentMap.ContainsKey(node.Data)) 85 83 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 86 contentMap[node. Content] = node;84 contentMap[node.Data] = node; 87 85 88 86 if (idMap.ContainsKey(node.Id)) 89 87 throw new InvalidOperationException("Duplicate content is not allowed in the genealogy graph."); 90 88 idMap[node.Id] = node; 91 92 vertex.Changed += OnVertexChanged;93 89 } 94 90 95 91 public override void RemoveVertex(IVertex vertex) { 96 92 var node = (IGenealogyGraphNode)vertex; 97 contentMap.Remove(node. Content);93 contentMap.Remove(node.Data); 98 94 idMap.Remove(node.Id); 99 95 if (Ranks.ContainsKey(node.Rank)) { … … 129 125 ranks.Clear(); 130 126 } 131 132 protected override void OnVertexChanged(object sender, EventArgs args) {133 base.OnVertexChanged(sender, args);134 135 var vertex = (IGenealogyGraphNode)sender;136 if (!contentMap.ContainsKey(vertex.Content)) {137 // vertex has received new content138 }139 }140 127 } 141 128 142 // [StorableClass] 143 // [Item("GenealogyGraph", "A class representing a genealogy graph")] 144 // public class GenealogyGraph<T> : DirectedGraph, IGenealogyGraph<T> where T : class, IItem { 145 // // members and properties 146 // [Storable] 147 // private Dictionary<double, List<IGenealogyGraphNode>> ranks; 148 // public Dictionary<double, List<IGenealogyGraphNode>> Ranks { 149 // get { return ranks; } 150 // set { ranks = value; } 151 // } 152 // public new IEnumerable<IGenealogyGraphNode<T>> Vertices { 153 // get { return from n in base.Vertices select (IGenealogyGraphNode<T>)n; } 154 // } 155 // // contructors 156 // protected GenealogyGraph(GenealogyGraph<T> original, Cloner cloner) 157 // : base(original, cloner) { 158 // } 159 // public override IDeepCloneable Clone(Cloner cloner) { 160 // return new GenealogyGraph<T>(this, cloner); 161 // } 162 // 163 // [StorableConstructor] 164 // protected GenealogyGraph(bool deserializing) : base(deserializing) { } 165 // public GenealogyGraph() { 166 // Ranks = new Dictionary<double, List<IGenealogyGraphNode>>(); 167 // } 168 // 169 // // methods 170 // public override void AddVertex(IVertex vertex) { 171 // base.AddVertex(vertex); 172 // var node = (IGenealogyGraphNode)vertex; 173 // if (!Ranks.ContainsKey(node.Rank)) { 174 // Ranks[node.Rank] = new List<IGenealogyGraphNode>(); 175 // } 176 // Ranks[node.Rank].Add(node); 177 // } 178 // 179 // public override void RemoveVertex(IVertex vertex) { 180 // var node = (IGenealogyGraphNode<T>)vertex; 181 // if (Ranks.ContainsKey(node.Rank)) { 182 // Ranks[node.Rank].Remove(node); 183 // } 184 // base.RemoveVertex(vertex); 185 // } 186 // 187 // public override IArc AddArc(IVertex source, IVertex target) { 188 // var arc = new GenealogyGraphArc((IGenealogyGraphNode)source, (IGenealogyGraphNode)target); 189 // source.AddArc(arc); 190 // target.AddArc(arc); 191 // arcs.Add(arc); 192 // return arc; 193 // } 194 // 195 // IEnumerable<IGenealogyGraphNode> IGenealogyGraph.Vertices { 196 // get { return Vertices; } 197 // } 198 // 199 // public IGenealogyGraphNode GetByContent(object content) 200 // { 201 // IGenealogyGraphNode result; 202 // contentMap. 203 // } 204 // 205 // public event EventHandler GraphUpdated; 206 // private void OnGraphUpdated(object sender, EventArgs args) { 207 // var updated = GraphUpdated; 208 // if (updated != null) updated(sender, args); 209 // } 210 // } 129 [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")] 211 130 [StorableClass] 212 213 [Item("GenealogyGraph", "A specialization of the genealogy graph with T as content for vertices")]214 131 public class GenealogyGraph<T> : GenealogyGraph, IGenealogyGraph<T> where T : class, IItem { 215 132 public new IEnumerable<IGenealogyGraphNode<T>> Vertices { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/GenealogyGraphNode.cs
r11233 r11253 33 33 [StorableConstructor] 34 34 protected GenealogyGraphNode(bool deserializing) : base(deserializing) { } 35 35 36 public override IDeepCloneable Clone(Cloner cloner) { 36 37 return new GenealogyGraphNode(this, cloner); 37 38 } 39 40 // this constructor emulates the behavior of a copy constructor 41 // it returns a shallow copy in which the arcs are not cloned 42 protected GenealogyGraphNode(IGenealogyGraphNode original) 43 : base((IDeepCloneable)original.Data.Clone()) { 44 Quality = original.Quality; 45 Rank = original.Rank; 46 IsElite = original.IsElite; 47 Id = Guid.NewGuid().ToString(); 48 } 49 38 50 protected GenealogyGraphNode(GenealogyGraphNode original, Cloner cloner) 39 51 : base(original, cloner) { 52 Quality = original.Quality; 53 Rank = original.Rank; 54 IsElite = original.IsElite; 55 Id = Guid.NewGuid().ToString(); 40 56 } 41 57 42 public GenealogyGraphNode( object content)43 : base( content) {58 public GenealogyGraphNode(IDeepCloneable data) 59 : base(data) { 44 60 Id = Guid.NewGuid().ToString(); 45 61 } … … 48 64 get { return base.InArcs.Cast<IGenealogyGraphArc>(); } 49 65 } 66 50 67 public new IEnumerable<IGenealogyGraphArc> OutArcs { 51 68 get { return base.OutArcs.Cast<IGenealogyGraphArc>(); } 52 69 } 70 53 71 public IEnumerable<IGenealogyGraphNode> Ancestors { 54 72 get { … … 123 141 [Item("GenealogyGraphNode", "A genealogy graph node which also has a Content")] 124 142 public class GenealogyGraphNode<T> : GenealogyGraphNode, IGenealogyGraphNode<T> where T : class, IItem { 125 public new T Content{126 get { return (T)base. Content; }127 set { base. Content= value; }143 public new T Data { 144 get { return (T)base.Data; } 145 set { base.Data = value; } 128 146 } 129 147 130 public GenealogyGraphNode(object content) : base(content) { } 148 public GenealogyGraphNode(IGenealogyGraphNode<T> original) : base(original) { } 149 150 public GenealogyGraphNode(IDeepCloneable content) : base(content) { } 131 151 132 152 protected GenealogyGraphNode(GenealogyGraphNode<T> original, Cloner cloner) 133 153 : base(original, cloner) { 134 // we do not clone the content135 Content = original.Content;136 154 } 137 155 public override IDeepCloneable Clone(Cloner cloner) { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphArc.cs
r11233 r11253 20 20 #endregion 21 21 22 using HeuristicLab.Core; 23 22 24 namespace HeuristicLab.EvolutionTracking { 23 25 public interface IGenealogyGraphArc : IArc { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/GenealogyGraph/Interfaces/IGenealogyGraphNode.cs
r11233 r11253 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Core; 24 25 25 26 namespace HeuristicLab.EvolutionTracking { -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/HeuristicLab.EvolutionTracking-3.4.csproj
r11227 r11253 105 105 <ItemGroup> 106 106 <Compile Include="Analyzers\GenealogyAnalyzer.cs" /> 107 <Compile Include="DirectedGraph\Arc.cs" />108 <Compile Include="DirectedGraph\DirectedGraph.cs" />109 <Compile Include="DirectedGraph\Interfaces\IArc.cs" />110 <Compile Include="DirectedGraph\Interfaces\IDirectedGraph.cs" />111 <Compile Include="DirectedGraph\Interfaces\IVertex.cs" />112 <Compile Include="DirectedGraph\Vertex.cs" />113 107 <Compile Include="Fragment.cs" /> 114 108 <Compile Include="Interfaces\IFragment.cs" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeCrossoverOperator.cs
r11233 r11253 77 77 var subScopes = ExecutionContext.Scope.SubScopes; 78 78 var parentVertices = subScopes.Select(x => (GenealogyGraphNode<T>)GenealogyGraph.GetById(getScopeId(x))).ToList(); 79 if (!parentVertices.Any()) 80 throw new InvalidOperationException("Could not retrieve parent vertices."); 79 81 80 82 var parents = ParentsParameter.ActualValue.ToList(); … … 84 86 GenealogyGraph.AddVertex(childVertex); 85 87 86 foreach (var parentVertex in parentVertices .Distinct()) {88 foreach (var parentVertex in parentVertices) 87 89 GenealogyGraph.AddArc(parentVertex, childVertex); 88 } 90 89 91 ExecutionContext.Scope.Variables.Add(new Variable("Id", new StringValue(childVertex.Id))); 90 91 92 return base.Apply(); 92 93 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/BeforeManipulatorOperator.cs
r11233 r11253 54 54 var v = (IGenealogyGraphNode<T>)GenealogyGraph.GetByContent(ChildParameter.ActualValue); 55 55 var clone = (T)ChildParameter.ActualValue.Clone(); 56 var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 }; 57 GenealogyGraph.AddVertex(c); 56 58 57 var c = new GenealogyGraphNode<T>(clone) { Rank = v.Rank - 0.5 }; 58 59 foreach (var arc in v.InArcs) { 59 var arcs = v.InArcs; 60 foreach (var arc in arcs) { 60 61 var p = arc.Source; 61 62 GenealogyGraph.RemoveArc(arc); … … 63 64 } 64 65 65 // foreach (var a in v.InArcs) {66 // a.Target = c;67 // c.AddReverseArc(a);68 // }69 70 // v.InArcs = Enumerable.Empty<IGenealogyGraphArc>();71 72 GenealogyGraph.AddVertex(c);73 66 GenealogyGraph.AddArc(c, v); 74 67
Note: See TracChangeset
for help on using the changeset viewer.