Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10462


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

Location:
branches/HeuristicLab.EvolutionTracking
Files:
4 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      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Operators/AfterCrossoverOperator.cs

    r10347 r10462  
    2929  [Item("AfterCrossoverOperator", "A generic operator that can record genealogical relationships between crossover parents and children.")]
    3030  public class AfterCrossoverOperator<T> : EvolutionTrackingOperator, ICrossoverOperator<T> where T : class,IItem {
    31     private const string defaultParentsParameterName = "Parents";
    32     private const string defaultChildParameterName = "Child";
     31    private const string DefaultParentsParameterName = "Parents";
     32    private const string DefaultChildParameterName = "Child";
    3333    public IScopeTreeLookupParameter<T> ParentsParameter { get; set; }
    3434    public ILookupParameter<T> ChildParameter { get; set; }
     
    4242
    4343    public AfterCrossoverOperator() {
    44       ParentsParameter = new ScopeTreeLookupParameter<T>(defaultParentsParameterName);
    45       ChildParameter = new LookupParameter<T>(defaultChildParameterName);
     44      ParentsParameter = new ScopeTreeLookupParameter<T>(DefaultParentsParameterName);
     45      ChildParameter = new LookupParameter<T>(DefaultChildParameterName);
    4646      Parameters.Add(ParentsParameter);
    4747      Parameters.Add(ChildParameter);
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/SymbolicDataAnalysisGenealogyGraphView.cs

    r10347 r10462  
    1717      var visualNode = (VisualGenealogyGraphNode)sender;
    1818      var graphNode = (IGenealogyGraphNode<ISymbolicExpressionTree>)visualNode.Data;
    19       var fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
    20       var treeView = (GraphicalSymbolicExpressionTreeView)viewHost.ActiveView;
    21       treeView.HighlightSubtree(fragment.Root, Color.RoyalBlue);
     19      IFragment<ISymbolicExpressionTreeNode> fragment = null;
     20      if (graphNode.InArcs != null)
     21        fragment = (IFragment<ISymbolicExpressionTreeNode>)graphNode.InArcs.Last().Data;
     22      if (fragment != null) {
     23        var treeView = (GraphicalSymbolicExpressionTreeView)viewHost.ActiveView;
     24        treeView.HighlightSubtree(fragment.Root, Color.RoyalBlue);
     25      }
    2226    }
    2327  }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs

    r10459 r10462  
    356356      // add tracking analyzer
    357357      foreach (var op in operators.OfType<SymbolicDataAnalysisGenealogyAnalyzer>()) {
    358         //        op.BeforeCrossoverOperator = new SymbolicDataAnalysisExpressionBeforeCrossoverOperator();
    359         //        op.AfterCrossoverOperator = new SymbolicDataAnalysisExpressionAfterCrossoverOperator();
    360         op.BeforeCrossoverOperator = new BeforeCrossoverOperator<ISymbolicExpressionTree>();
    361         op.AfterCrossoverOperator = new AfterCrossoverOperator<ISymbolicExpressionTree>();
     358        op.BeforeCrossoverOperator = new SymbolicDataAnalysisExpressionBeforeCrossoverOperator();
     359        op.AfterCrossoverOperator = new SymbolicDataAnalysisExpressionAfterCrossoverOperator();
    362360        op.BeforeManipulatorOperator = new BeforeManipulatorOperator<ISymbolicExpressionTree>();
    363361        op.AfterManipulatorOperator = new AfterManipulatorOperator<ISymbolicExpressionTree>();
     
    373371          op.ManipulatorChildParameterName = manipulator.SymbolicExpressionTreeParameter.Name;
    374372        }
    375         op.PopulationParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     373        var creator = operators.OfType<ISymbolicExpressionTreeCreator>().FirstOrDefault();
     374        if (creator != null) {
     375          op.PopulationParameter.ActualName = creator.SymbolicExpressionTreeParameter.ActualName;
     376        }
    376377      }
    377378    }
Note: See TracChangeset for help on using the changeset viewer.