Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/19/14 08:25:37 (10 years ago)
Author:
bburlacu
Message:

#1772: Fixed bug in configuration of the tracking operators, fixed null reference exception when trying to access fragments in SymbolicDataAnalysisGenealogyGraphView

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;
     1using System.Linq;
    32using HeuristicLab.Common;
    43using HeuristicLab.Core;
     
    167166    }
    168167
    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
    179175        if (EnableCrossoverTracking.Value) {
    180176          if (BeforeCrossoverOperator != null) {
    181177            BeforeCrossoverOperator.ParentsParameter.ActualName = CrossoverParentsParameterName;
    182178            BeforeCrossoverOperator.ChildParameter.ActualName = CrossoverChildParameterName;
    183             var instrumentedCrossover = (InstrumentedOperator)Crossover;
    184179            instrumentedCrossover.BeforeExecutionOperators.Add(BeforeCrossoverOperator);
    185180          }
     
    187182            AfterCrossoverOperator.ParentsParameter.ActualName = CrossoverParentsParameterName;
    188183            AfterCrossoverOperator.ChildParameter.ActualName = CrossoverChildParameterName;
    189             var instrumentedCrossover = (InstrumentedOperator)Crossover;
    190184            instrumentedCrossover.AfterExecutionOperators.Add(AfterCrossoverOperator);
    191185          }
    192186        }
    193 
     187      }
     188
     189      if (Manipulator != null) {
     190        var instrumentedManipulator = (InstrumentedOperator)Manipulator;
     191        instrumentedManipulator.AfterExecutionOperators.Clear();
     192        instrumentedManipulator.BeforeExecutionOperators.Clear();
    194193        if (EnableManipulatorTracking.Value && Manipulator != null) {
    195           if (BeforeManipulatorOperator == null) {
    196             BeforeManipulatorOperator = new BeforeManipulatorOperator<T>();
     194          if (BeforeManipulatorOperator != null) {
    197195            BeforeManipulatorOperator.ChildParameter.ActualName = ManipulatorChildParameterName;
    198             var instrumentedManipulator = (InstrumentedOperator)Manipulator;
    199196            instrumentedManipulator.BeforeExecutionOperators.Add(BeforeManipulatorOperator);
    200197          }
    201           if (AfterManipulatorOperator == null) {
    202             AfterManipulatorOperator = new AfterManipulatorOperator<T>();
     198          if (AfterManipulatorOperator != null) {
    203199            AfterManipulatorOperator.ChildParameter.ActualName = ManipulatorChildParameterName;
    204             var instrumentedManipulator = (InstrumentedOperator)Manipulator;
    205200            instrumentedManipulator.AfterExecutionOperators.Add(AfterManipulatorOperator);
    206201          }
    207202        }
     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        }
    208217      } 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) {
    215220          var vertex = new GenealogyGraphNode<T> {
    216             Content = individual,
     221            Content = elite,
    217222            Rank = Generations.Value,
    218223            IsElite = true
    219224          };
     225          var previousVertex = GenealogyGraph[elite].Last();
    220226          GenealogyGraph.AddVertex(vertex);
    221         }
    222       }
    223       // update qualities for the nodes in the graph
    224       var population = PopulationParameter.ActualValue.ToList();
    225       var qualities = QualityParameter.ActualValue.ToList();
     227          previousVertex.AddForwardArc(vertex);
     228          vertex.AddReverseArc(previousVertex);
     229        }
     230      }
     231      // update qualities
    226232      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;
    229236        }
    230237      }
Note: See TracChangeset for help on using the changeset viewer.