Changeset 10462 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
- Timestamp:
- 02/19/14 08:25:37 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs
r10347 r10462 1 using System.Collections.Generic; 2 using System.Linq; 1 using System.Linq; 3 2 using HeuristicLab.Common; 4 3 using HeuristicLab.Core; … … 167 166 } 168 167 169 public override IOperation Apply() { 170 if (Generations.Value == 0) { 171 foreach (var individual in PopulationParameter.ActualValue) { 172 var vertex = new GenealogyGraphNode<T> { 173 Content = individual, 174 Rank = Generations.Value 175 }; 176 GenealogyGraph.AddVertex(vertex); 177 } 178 // at the beginning we add the before/after operators to the instrumented operators 168 private void ConfigureTrackingOperators() { 169 // at the beginning we add the before/after operators to the instrumented operators 170 if (Crossover != null) { 171 var instrumentedCrossover = (InstrumentedOperator)Crossover; 172 instrumentedCrossover.AfterExecutionOperators.Clear(); 173 instrumentedCrossover.BeforeExecutionOperators.Clear(); 174 179 175 if (EnableCrossoverTracking.Value) { 180 176 if (BeforeCrossoverOperator != null) { 181 177 BeforeCrossoverOperator.ParentsParameter.ActualName = CrossoverParentsParameterName; 182 178 BeforeCrossoverOperator.ChildParameter.ActualName = CrossoverChildParameterName; 183 var instrumentedCrossover = (InstrumentedOperator)Crossover;184 179 instrumentedCrossover.BeforeExecutionOperators.Add(BeforeCrossoverOperator); 185 180 } … … 187 182 AfterCrossoverOperator.ParentsParameter.ActualName = CrossoverParentsParameterName; 188 183 AfterCrossoverOperator.ChildParameter.ActualName = CrossoverChildParameterName; 189 var instrumentedCrossover = (InstrumentedOperator)Crossover;190 184 instrumentedCrossover.AfterExecutionOperators.Add(AfterCrossoverOperator); 191 185 } 192 186 } 193 187 } 188 189 if (Manipulator != null) { 190 var instrumentedManipulator = (InstrumentedOperator)Manipulator; 191 instrumentedManipulator.AfterExecutionOperators.Clear(); 192 instrumentedManipulator.BeforeExecutionOperators.Clear(); 194 193 if (EnableManipulatorTracking.Value && Manipulator != null) { 195 if (BeforeManipulatorOperator == null) { 196 BeforeManipulatorOperator = new BeforeManipulatorOperator<T>(); 194 if (BeforeManipulatorOperator != null) { 197 195 BeforeManipulatorOperator.ChildParameter.ActualName = ManipulatorChildParameterName; 198 var instrumentedManipulator = (InstrumentedOperator)Manipulator;199 196 instrumentedManipulator.BeforeExecutionOperators.Add(BeforeManipulatorOperator); 200 197 } 201 if (AfterManipulatorOperator == null) { 202 AfterManipulatorOperator = new AfterManipulatorOperator<T>(); 198 if (AfterManipulatorOperator != null) { 203 199 AfterManipulatorOperator.ChildParameter.ActualName = ManipulatorChildParameterName; 204 var instrumentedManipulator = (InstrumentedOperator)Manipulator;205 200 instrumentedManipulator.AfterExecutionOperators.Add(AfterManipulatorOperator); 206 201 } 207 202 } 203 } 204 } 205 206 public override IOperation Apply() { 207 var population = PopulationParameter.ActualValue.ToList(); 208 var qualities = QualityParameter.ActualValue.ToList(); 209 210 if (Generations.Value == 0) { 211 ConfigureTrackingOperators(); 212 213 foreach (var individual in population) { 214 var vertex = new GenealogyGraphNode<T> { Content = individual, Rank = Generations.Value, }; 215 GenealogyGraph.AddVertex(vertex); 216 } 208 217 } else { 209 // add missing elite individuals in the current generation 210 // TODO: optimize for speed 211 var currGen = new HashSet<T>(GenealogyGraph.Ranks[Generations.Value].Cast<IGenealogyGraphNode<T>>().Select(x => x.Content)); 212 foreach (var individual in PopulationParameter.ActualValue) { 213 if (currGen.Contains(individual)) continue; 214 // it is an elite which was already added to the graph in the previous generation 218 var elite = population.FirstOrDefault(x => GenealogyGraph.Contains(x)); 219 if (elite != null) { 215 220 var vertex = new GenealogyGraphNode<T> { 216 Content = individual,221 Content = elite, 217 222 Rank = Generations.Value, 218 223 IsElite = true 219 224 }; 225 var previousVertex = GenealogyGraph[elite].Last(); 220 226 GenealogyGraph.AddVertex(vertex); 221 }222 }223 // update qualities for the nodes in the graph224 var population = PopulationParameter.ActualValue.ToList();225 var qualities = QualityParameter.ActualValue.ToList();227 previousVertex.AddForwardArc(vertex); 228 vertex.AddReverseArc(previousVertex); 229 } 230 } 231 // update qualities 226 232 for (int i = 0; i < population.Count; ++i) { 227 foreach (var v in GenealogyGraph[population[i]].Cast<IGenealogyGraphNode<T>>()) { 228 v.Quality = qualities[i].Value; 233 var individual = population[i]; 234 foreach (var vertex in GenealogyGraph[individual].Cast<IGenealogyGraphNode<T>>()) { 235 vertex.Quality = qualities[i].Value; 229 236 } 230 237 }
Note: See TracChangeset
for help on using the changeset viewer.