Changeset 11253 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
- Timestamp:
- 07/31/14 17:11:39 (10 years ago)
- File:
-
- 1 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++;
Note: See TracChangeset
for help on using the changeset viewer.