Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9238


Ignore:
Timestamp:
02/21/13 16:02:47 (11 years ago)
Author:
bburlacu
Message:

#1772: Added base class for the fragment analyzers. Improved analyzers, added SymbolicExpressionTreeRelativeFragmentDepthAnalyzer. Added LineageExplorer.

Location:
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4
Files:
3 added
1 deleted
6 edited
4 moved

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeFragmentLengthsAnalyzer.cs

    r9082 r9238  
    6363      UpdateCounterParameter.Hidden = true;
    6464      UpdateIntervalParameter.Hidden = true;
    65 
    66 
    6765    }
    6866    #region After deserialization code
    69 
    7067    [StorableHook(HookType.AfterDeserialization)]
    7168    private void AfterDeserialization() {
     
    9794          InitializeRows();
    9895
    99           var parents = SecondaryTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()).ToList(); // parents of individuals at generation N-1
    100           var offspring = SecondaryTraceMap.Keys.Cast<ISymbolicExpressionTree>().ToList(); // individuals at generation N-1
    101           var fragments = SecondaryFragmentMap.Values.Cast<IFragment>().ToList();
     96          var fragmentLengths = (from m in SecondaryFragmentMap
     97                                 let fragment = (IFragment)m.Value
     98                                 where fragment.Root != null
     99                                 select fragment.Length
     100                               ).ToList();
    102101
     102          // fragments of selected offspring
     103          var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
     104          var goodFragmentlengths = (from m in SecondaryFragmentMap
     105                                     let tree = (ISymbolicExpressionTree)m.Key
     106                                     let fragment = (IFragment)m.Value
     107                                     where fragment.Root != null
     108                                     where selected.Contains(tree)
     109                                     select fragment.Length
     110                                    ).ToList();
    103111
    104           // a hash of the offspring trees that got selected for reproduction at generation N
    105           var selected = new HashSet<ISymbolicExpressionTree>(GlobalTraceMap.Values.SelectMany(list => list.Cast<ISymbolicExpressionTree>()));
    106 
    107           var selectedOffspringFragments = SecondaryFragmentMap.Where(m => selected.Contains((ISymbolicExpressionTree)m.Key)).Select(m => m.Key).ToList();
    108 
    109           var selectedOffspring = offspring.Where(selected.Contains).ToList();
    110           var selectedOffspringParents = selectedOffspring.SelectMany(x => SecondaryTraceMap[x].Cast<ISymbolicExpressionTree>()).ToList();
    111 
    112           var rows = FragmentLengths.Rows;
    113 
    114           double avgParentsLength = parents.Count > 0 ? parents.Average(p => p.Length) : 0;
    115           double avgGoodParentsLength = selectedOffspringParents.Count > 0 ? selectedOffspringParents.Average(p => p.Length) : 0;
    116           rows["Parent lengths (all)"].Values.Add(avgParentsLength);
    117           rows["Parent lengths (good)"].Values.Add(avgGoodParentsLength);
    118           double avgOffspringLength = offspring.Count > 0 ? offspring.Average(o => o.Length) : 0;
    119           double avgGoodOffspringLength = selectedOffspring.Count > 0 ? selectedOffspring.Average(o => o.Length) : 0;
    120           rows["Offspring lengths (good)"].Values.Add(avgOffspringLength);
    121           rows["Offspring lengths (all)"].Values.Add(avgGoodOffspringLength);
    122           double avgFragmentLength = fragments.Count > 0 ? fragments.Average(f => f.Length) : 0;
    123           double avgGoodFragmentLength = selectedOffspringFragments.Count > 0 ? fragments.Average(f => f.Length) : 0;
    124           rows["Fragment lengths (good)"].Values.Add(avgGoodFragmentLength);
    125           rows["Fragment lengths (all)"].Values.Add(avgFragmentLength);
     112          FragmentLengths.Rows["Average fragment length (all)"].Values.Add(fragmentLengths.Count > 0 ? fragmentLengths.Average() : 0.0);
     113          FragmentLengths.Rows["Average fragment length (good)"].Values.Add(goodFragmentlengths.Count > 0 ? goodFragmentlengths.Average() : 0.0);
    126114        }
    127115      }
    128 
    129116      return base.Apply();
    130117    }
     
    132119    protected override void InitializeParameters() {
    133120      var results = ResultsParameter.ActualValue;
    134       if (!results.ContainsKey("Fragment Lengths")) {
    135         FragmentLengths = FragmentLengths ?? new DataTable("Fragment Lengths") { VisualProperties = { YAxisTitle = "Fragment length" } };
    136         results.Add(new Result("Fragment Lengths", FragmentLengths));
     121      if (!results.ContainsKey("Fragment lengths")) {
     122        FragmentLengths = FragmentLengths ?? new DataTable("Fragment lengths") { VisualProperties = { YAxisTitle = "Number of nodes" } };
     123        results.Add(new Result("Fragment lengths", FragmentLengths));
    137124      }
    138125      base.InitializeParameters();
     
    142129      string[] rowNames =
    143130        {
    144           "Parent lengths (good)", "Parent lengths (all)",
    145           "Offspring lengths (good)", "Offspring lengths (all)",
    146           "Fragment lengths (good)", "Fragment lengths (all)"
     131          "Average fragment length (good)", "Average fragment length (all)"
    147132        };
    148133      foreach (var rowName in rowNames.Where(rowName => !FragmentLengths.Rows.ContainsKey(rowName))) {
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeFragmentsAnalyzer.cs

    r9082 r9238  
    272272        foreach (var m in GlobalFragmentMap)
    273273          SecondaryFragmentMap.Add(m.Key, m.Value);
     274
    274275      return base.Apply();
    275276    }
     
    294295    /// </summary>
    295296    /// <param name="fragments">The symbolic expression tree fragments</param>
    296     /// <param name="mode">The similarity mode (0 - exact, 1 - high, 2 - relaxed)</param>
     297    /// <param name="comparer">The node similarity comparer</param>
    297298    /// <returns>The average number of similar fragments</returns>
    298299    private static double CalculateSimilarity(IList<IFragment> fragments, SymbolicExpressionTreeNodeSimilarityComparer comparer) {
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/Fragments/SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs

    r9082 r9238  
    4242  public sealed class SymbolicExpressionTreeRelativeFragmentDepthAnalyzer : SymbolicExpressionTreeFragmentsAnalyzer {
    4343    private const string RelativeFragmentDepthParameterName = "Relative fragment depth";
    44     // impact values calculator
    4544    public ILookupParameter<DataTable> RelativeFragmentDepthParameter { get { return (ILookupParameter<DataTable>)Parameters[RelativeFragmentDepthParameterName]; } }
    4645
     
    131130
    132131    private void InitializeRows() {
    133       string[] rowNames =
    134         {
    135           "Average relative fragment depth (good)", "Average relative fragment depth (all)"
    136         };
     132      string[] rowNames = { "Average relative fragment depth (good)", "Average relative fragment depth (all)" };
    137133      foreach (var rowName in rowNames.Where(rowName => !RelativeFragmentDepths.Rows.ContainsKey(rowName))) {
    138134        RelativeFragmentDepths.Rows.Add(new DataRow(rowName) { VisualProperties = { StartIndexZero = true } });
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/GeneticOperatorImprovementAnalyzer.cs

    r9236 r9238  
    1010using HeuristicLab.Parameters;
    1111using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     12using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    1213
    1314namespace HeuristicLab.EvolutionaryTracking {
    1415  /// An analyzer that looks at individual lineages in the genealogy graph
    1516  /// </summary>
    16   [Item("GeneticOperatorsAverageImprovementAnalyzer", "An operator that records the average success of genetic operators such as crossover and mutation.")]
     17  [Item("GeneticOperatorImprovementAnalyzer", "An operator that records the average success of genetic operators such as crossover and mutation.")]
    1718  [StorableClass]
    18   class GeneticOperatorsAverageImprovementAnalyzer : SingleSuccessorOperator, IAnalyzer {
     19  class GeneticOperatorImprovementAnalyzer : SingleSuccessorOperator, IAnalyzer {
    1920    #region Parameter names
    2021    private const string UpdateIntervalParameterName = "UpdateInterval";
     
    2425    private const string PopulationGraphParameterName = "PopulationGraph";
    2526    private const string GeneticOperatorsAverageImprovementResultName = "GeneticOperatorsAverageImprovement";
     27    private const string UpdateConstantsInIntermediateNodesParameterName = "UpdateConstantsInIntermediateNodes";
    2628    #endregion
    2729
     
    3234    public ValueParameter<IntValue> UpdateCounterParameter {
    3335      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
     36    }
     37    public ValueParameter<BoolValue> UpdateConstantsInIntermediateNodesParameter {
     38      get { return (ValueParameter<BoolValue>)Parameters[UpdateConstantsInIntermediateNodesParameterName]; }
    3439    }
    3540    public LookupParameter<ResultCollection> ResultsParameter {
     
    4853      get { return UpdateCounterParameter.Value; }
    4954    }
     55    public BoolValue UpdateConstantsInIntermediateNodes {
     56      get { return UpdateConstantsInIntermediateNodesParameter.Value; }
     57    }
    5058    public IntValue Generations {
    5159      get { return GenerationsParameter.ActualValue; }
     
    5967
    6068    [StorableConstructor]
    61     private GeneticOperatorsAverageImprovementAnalyzer(bool deserializing) : base(deserializing) { }
    62     private GeneticOperatorsAverageImprovementAnalyzer(GeneticOperatorsAverageImprovementAnalyzer original, Cloner cloner) : base(original, cloner) { }
     69    private GeneticOperatorImprovementAnalyzer(bool deserializing) : base(deserializing) { }
     70    private GeneticOperatorImprovementAnalyzer(GeneticOperatorImprovementAnalyzer original, Cloner cloner) : base(original, cloner) { }
    6371    public override IDeepCloneable Clone(Cloner cloner) {
    64       return new GeneticOperatorsAverageImprovementAnalyzer(this, cloner);
     72      return new GeneticOperatorImprovementAnalyzer(this, cloner);
    6573    }
    66     public GeneticOperatorsAverageImprovementAnalyzer() {
     74    public GeneticOperatorImprovementAnalyzer() {
    6775      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
    6876      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
    6977      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
    7078      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far"));
     79      Parameters.Add(new ValueParameter<BoolValue>(UpdateConstantsInIntermediateNodesParameterName, "Controls whether or not the constant values of the intermediate nodes should be updated.", new BoolValue(false)));
    7180
    7281      UpdateCounterParameter.Hidden = true;
     
    7786    [StorableHook(HookType.AfterDeserialization)]
    7887    private void AfterDeserialization() {
     88      if (!Parameters.ContainsKey(UpdateConstantsInIntermediateNodesParameterName))
     89        Parameters.Add(new ValueParameter<BoolValue>(UpdateConstantsInIntermediateNodesParameterName, "Controls whether or not the constant values of the intermediate nodes should be updated."));
    7990    }
    8091    #endregion
     
    106117        var mutationImprovements = new List<double>();
    107118
    108         foreach (var node in nodes) {
    109           if (node.InEdges == null) continue; // it means the corresponding individual did not reproduce
    110           var parentsQuality = node.InEdges.Select(e => e.Source).Max(v => ((SymbolicExpressionGenealogyGraphNode)v).Quality);
    111           var childQuality = node.Quality;
    112           var improvement = childQuality / parentsQuality;
     119        // in case we need to use it
     120        var evaluator = new SymbolicRegressionConstantOptimizationEvaluator();
     121        evaluator.EvaluationPartitionParameter.ActualName = "FitnessCalculationPartition";
     122
     123        foreach (var node in nodes.Where(n => n.InEdges != null).OrderByDescending(n => n.InEdges.Count)) {
     124          double parentsQuality = node.InEdges.Select(e => e.Source).Max(v => ((SymbolicExpressionGenealogyGraphNode)v).Quality);
     125          double childQuality = node.Quality;
     126          double improvement = childQuality - parentsQuality;
    113127          if (double.IsNaN(improvement) || double.IsInfinity(improvement) || parentsQuality.IsAlmost(0.0)) improvement = 0;
    114128          switch (node.InEdges.Count) {
     
    117131              break;
    118132            case 1: // mutation
     133              // determine if we should update the quality
    119134              mutationImprovements.Add(improvement);
    120135              break;
     
    139154          mutMaxImprovement = mutationImprovements.Max();
    140155        }
     156
    141157
    142158        if (!table.Rows.ContainsKey("Average crossover improvement")) {
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreeFrequentPatternsAnalyzer.cs

    r9082 r9238  
    9696    }
    9797    #endregion
    98 
    9998    #region Properties
    10099    public bool EnabledByDefault {
     
    361360          // adjust weights according to the probability a child will find itself on a certain argument position
    362361          for (int j = 0; j != possibleChildConnections.Count; ++j) {
    363             var childSymbolNode = possibleChildConnections[j].Target as SymbolNode;
     362            var childSymbolNode = (SymbolNode)possibleChildConnections[j].Target;
    364363            double sumPos = childSymbolNode.Positions.Sum(x => x.Value);
    365364            int v; childSymbolNode.Positions.TryGetValue(i, out v);
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Analyzers/SymbolicExpressionTreePopulationDiversityAnalyzer.cs

    r9082 r9238  
    3030using HeuristicLab.Parameters;
    3131using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.Problems.DataAnalysis.Symbolic;
    3233
    3334// type definitions for ease of use
    34 using HeuristicLab.Problems.DataAnalysis.Symbolic;
     35using CloneMapType = HeuristicLab.Core.ItemDictionary<HeuristicLab.Core.IItem, HeuristicLab.Core.IItem>;
    3536
    3637namespace HeuristicLab.EvolutionaryTracking {
     
    4647    private const string UpdateCounterParameterName = "UpdateCounter";
    4748    private const string ResultsParameterName = "Results";
    48     #endregion
    49 
    50     #region Parameter properties
     49    private const string GenerationsParameterName = "Generations";
     50    private const string StoreHistoryParameterName = "StoreHistory";
     51    // comparer parameters
     52    private const string MatchVariablesParameterName = "MatchVariableNames";
     53    private const string MatchVariableWeightsParameterName = "MatchVariableWeights";
     54    private const string MatchConstantValuesParameterName = "MatchConstantValues";
     55    private const string SortSubtreesParameterName = "SortSubtrees";
     56    // population graph
     57    private const string PopulationGraphParameterName = "PopulationGraph";
     58    // clone map
     59    private const string GlobalCloneMapParameterName = "GlobalCloneMap";
     60    #endregion
     61
     62    #region Parameters
    5163    public ValueParameter<IntValue> UpdateIntervalParameter {
    5264      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
     
    5870      get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
    5971    }
     72    public LookupParameter<IntValue> GenerationsParameter {
     73      get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
     74    }
     75    public ValueParameter<BoolValue> StoreHistoryParameter {
     76      get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
     77    }
    6078    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
    6179      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    6280    }
    63     #endregion
    64 
    65     #region Parameters
     81    public ValueParameter<BoolValue> MatchVariableNamesParameter {
     82      get { return (ValueParameter<BoolValue>)Parameters[MatchVariablesParameterName]; }
     83    }
     84    public ValueParameter<BoolValue> MatchVariableWeightsParameter {
     85      get { return (ValueParameter<BoolValue>)Parameters[MatchVariableWeightsParameterName]; }
     86    }
     87    public ValueParameter<BoolValue> MatchConstantValuesParameter {
     88      get { return (ValueParameter<BoolValue>)Parameters[MatchConstantValuesParameterName]; }
     89    }
     90    public ValueParameter<BoolValue> SortSubtreesParameter {
     91      get { return (ValueParameter<BoolValue>)Parameters[SortSubtreesParameterName]; }
     92    }
     93    public LookupParameter<CloneMapType> GlobalCloneMapParameter {
     94      get { return (LookupParameter<CloneMapType>)Parameters[GlobalCloneMapParameterName]; }
     95    }
     96    #endregion
     97
     98    #region Parameter properties
    6699    public IntValue UpdateInterval {
    67100      get { return UpdateIntervalParameter.Value; }
     
    72105    public ResultCollection Results {
    73106      get { return ResultsParameter.ActualValue; }
     107    }
     108    public CloneMapType GlobalCloneMap {
     109      get { return GlobalCloneMapParameter.ActualValue; }
     110    }
     111    public IntValue Generations {
     112      get { return GenerationsParameter.ActualValue; }
     113    }
     114    public BoolValue StoreHistory {
     115      get { return StoreHistoryParameter.Value; }
    74116    }
    75117    #endregion
     
    87129      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
    88130      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
    89 
     131      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
     132      Parameters.Add(new ValueParameter<BoolValue>(MatchVariablesParameterName, "Specify if the symbolic expression tree comparer should match variable names."));
     133      Parameters.Add(new ValueParameter<BoolValue>(MatchVariableWeightsParameterName, "Specify if the symbolic expression tree comparer should match variable weights."));
     134      Parameters.Add(new ValueParameter<BoolValue>(MatchConstantValuesParameterName, "Specify if the symbolic expression tree comparer should match constant values."));
     135      Parameters.Add(new ValueParameter<BoolValue>(SortSubtreesParameterName, "Specifies whether the subtrees of a tree should be sorted before comparison."));
     136      Parameters.Add(new LookupParameter<CloneMapType>(GlobalCloneMapParameterName, "A global map keeping track of trees and their clones (made during selection)."));
     137      Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
    90138      UpdateCounterParameter.Hidden = true;
    91139      UpdateIntervalParameter.Hidden = true;
     
    106154    #endregion
    107155
     156    public bool EnabledByDefault { get { return true; } }
     157
    108158    public override IOperation Apply() {
    109159      UpdateCounter.Value++;
     
    113163
    114164        var trees = SymbolicExpressionTreeParameter.ActualValue.ToList();
    115         var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter();
    116         foreach (var t in trees) canonicalSorter.SortSubtrees(t);
    117 
    118         var comparer = new SymbolicExpressionTreeNodeSimilarityComparer { MatchConstantValues = false, MatchVariableNames = false, MatchVariableWeights = false };
    119 
    120         var distinct = 1.0 - trees.Select(x => x.Root).Distinct(comparer).Count() / (double)trees.Count;
    121 
    122         var results = ResultsParameter.ActualValue;
    123 
    124         if (!results.ContainsKey("PopulationDiversity"))
    125           results.Add(new Result("PopulationDiversity", new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Count" } }));
    126         var populationDiversityTable = results["PopulationDiversity"].Value as DataTable;
    127 
    128         if (populationDiversityTable == null) return base.Apply(); // should never happen
    129 
     165        if (SortSubtreesParameter.Value.Value) {
     166          var canonicalSorter = new SymbolicExpressionTreeCanonicalSorter();
     167          foreach (var t in trees) canonicalSorter.SortSubtrees(t);
     168        }
     169
     170        bool matchConstants = MatchConstantValuesParameter.Value.Value;
     171        bool matchVarNames = MatchVariableNamesParameter.Value.Value;
     172        bool matchVarWeights = MatchVariableWeightsParameter.Value.Value;
     173
     174        var comparer = new SymbolicExpressionTreeEqualityComparer {
     175          SimilarityComparer = new SymbolicExpressionTreeNodeSimilarityComparer {
     176            MatchConstantValues = matchConstants, MatchVariableNames = matchVarNames, MatchVariableWeights = matchVarWeights
     177          }
     178        };
     179        ResultCollection results = ResultsParameter.ActualValue;
     180        // population diversity
     181        DataTable populationDiversityTable;
     182        if (!results.ContainsKey("PopulationDiversity")) {
     183          populationDiversityTable = new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Diversity" } };
     184          results.Add(new Result("PopulationDiversity", populationDiversityTable));
     185        }
     186        populationDiversityTable = (DataTable)results["PopulationDiversity"].Value;
    130187        if (!populationDiversityTable.Rows.ContainsKey("Diversity"))
    131188          populationDiversityTable.Rows.Add(new DataRow("Diversity") { VisualProperties = { StartIndexZero = true } });
    132 
     189        double distinct = trees.Distinct(comparer).Count() / (double)trees.Count;
    133190        populationDiversityTable.Rows["Diversity"].Values.Add(distinct);
     191        // selection diversity
     192        if (GlobalCloneMap == null) return base.Apply();
     193
     194        DataTable relativeSelectionCountsTable;
     195        if (!results.ContainsKey("SelectedIndividuals")) {
     196          relativeSelectionCountsTable = new DataTable("SelectedIndividuals") { VisualProperties = { YAxisTitle = "% Selected Individuals" } };
     197          results.Add(new Result("SelectedIndividuals", relativeSelectionCountsTable));
     198        }
     199        relativeSelectionCountsTable = (DataTable)Results["SelectedIndividuals"].Value;
     200        if (!relativeSelectionCountsTable.Rows.ContainsKey("SelectedIndividuals")) {
     201          relativeSelectionCountsTable.Rows.Add(new DataRow("SelectedIndividuals") { VisualProperties = { StartIndexZero = true } });
     202        }
     203        double relativeSelectionCount = GlobalCloneMap.Values.Distinct().Count() / (double)trees.Count;
     204        relativeSelectionCountsTable.Rows["SelectedIndividuals"].Values.Add(relativeSelectionCount);
     205
     206        // do a histogram of selection frequency for each individual in the population
     207        DataTable selectionFrequenciesTable;
     208        if (!results.ContainsKey("SelectionFrequencies")) {
     209          selectionFrequenciesTable = new DataTable("Selection Frequencies") { VisualProperties = { YAxisTitle = "Selection Count" } };
     210          results.Add(new Result("SelectionFrequencies", selectionFrequenciesTable));
     211        }
     212        selectionFrequenciesTable = (DataTable)results["SelectionFrequencies"].Value;
     213        DataRow histoRow;
     214        if (!selectionFrequenciesTable.Rows.ContainsKey("SelectionFrequencies")) {
     215          histoRow = new DataRow("SelectionFrequencies") { VisualProperties = { StartIndexZero = true } };
     216          selectionFrequenciesTable.Rows.Add(histoRow);
     217        }
     218        histoRow = selectionFrequenciesTable.Rows["SelectionFrequencies"];
     219        var frequencies = new double[trees.Count];
     220
     221        var graph = (SymbolicExpressionTreeGenealogyGraph)results[PopulationGraphParameterName].Value;
     222        // get graph nodes corresponding to individuals of the previous generation
     223        var prevGen = graph.Nodes.Where(node => node.Rank == Generations.Value - 1).OrderByDescending(n => n.Quality).ToList();
     224        for (int i = 0; i != prevGen.Count; ++i) {
     225          int selFreq = GlobalCloneMap.Values.Count(t => t == prevGen[i].SymbolicExpressionTree);
     226          frequencies[i] = selFreq;
     227        }
     228        histoRow.Values.Replace(frequencies);
     229
     230        bool storeHistory = StoreHistory.Value;
     231        if (storeHistory) {
     232          DataTableHistory selectionFrequenciesHistory;
     233          if (!results.ContainsKey("SelectionFrequenciesHistory")) {
     234            selectionFrequenciesHistory = new DataTableHistory();
     235            results.Add(new Result("SelectionFrequenciesHistory", selectionFrequenciesHistory));
     236          }
     237          selectionFrequenciesHistory = (DataTableHistory)results["SelectionFrequenciesHistory"].Value;
     238          selectionFrequenciesHistory.Add((DataTable)selectionFrequenciesTable.Clone());
     239        }
    134240      }
    135241      return base.Apply();
    136242    }
    137 
    138     public bool EnabledByDefault { get { return true; } }
    139243  }
    140244}
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/DirectedGraph/Vertex.cs

    r9082 r9238  
    100100      return list;
    101101    }
    102 
    103102    // this method can be used to add arcs towards targets that are not visible in the graph
    104103    // (they do not appear in the nodes Dictionary). It is the programmers responsibility to
     
    114113      if (OutEdges == null) { OutEdges = new List<Arc> { arc }; } else { OutEdges.Add(arc); }
    115114    }
    116 
    117115    public void AddReverseArc(Arc arc) {
    118116      if (arc.Target == null) { arc.Target = this; }
     
    120118      if (InEdges == null) { InEdges = new List<Arc> { arc }; } else { InEdges.Add(arc); }
    121119    }
    122 
    123 
    124120    public void AddReverseArc(IVertex source, double weight = 0.0, object data = null) {
    125121      var e = new Arc { Source = source, Target = this, Data = data, Weight = weight };
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/HeuristicLab.EvolutionaryTracking-3.4.csproj

    r9082 r9238  
    8787  </ItemGroup>
    8888  <ItemGroup>
    89     <Compile Include="Analyzers\SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs" />
    90     <Compile Include="Analyzers\SymbolicExpressionTreeFragmentLengthsAnalyzer.cs" />
     89    <Compile Include="Analyzers\SymbolicExpressionTreeShapeAnalyzer.cs" />
     90    <Compile Include="Analyzers\SymbolicExpressionTreeEvolvabilityAnalyzer.cs" />
     91    <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeRelativeFragmentDepthAnalyzer.cs" />
     92    <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeFragmentLengthsAnalyzer.cs" />
    9193    <Compile Include="Analyzers\GeneticOperatorsAverageImprovementAnalyzer.cs" />
    92     <Compile Include="Analyzers\SymbolicExpressionTreeEvolvabilityAnalyzer.cs" />
    9394    <Compile Include="Analyzers\SymbolicExpressionTreeFrequentPatternsAnalyzer.cs" />
    94     <Compile Include="Analyzers\SymbolicExpressionTreeFragmentsAnalyzer.cs" />
     95    <Compile Include="Analyzers\Fragments\SymbolicExpressionTreeFragmentsAnalyzer.cs" />
    9596    <Compile Include="FPGraph.cs" />
     97    <Compile Include="LineageExplorer.cs" />
     98    <Compile Include="Operators\LineageAggregator.cs" />
    9699    <Compile Include="Operators\SymbolicExpressionTreeFPBuilder.cs" />
    97100    <Compile Include="Operators\SymbolicExpressionTreeGenealogyGraphBuilder.cs" />
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/CleanupOperator.cs

    r9082 r9238  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
    2426using HeuristicLab.Data;
     27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2528using HeuristicLab.Operators;
    2629using HeuristicLab.Optimization;
     
    4750    private const string MaximumGenerationsParameterName = "MaximumGenerations";
    4851    private const string ResultsParameterName = "Results";
    49     private const string DeleteGenealogyGraphParameterName = "DeleteGenealogyGraph";
     52    private const string CleanGenealogyGraphParameterName = "CleanGenealogyGraph";
     53    private const string UpdateGenealogyGraphParameterName = "UpdateGenealogyGraph";
     54    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
     55    private const string SymbolicExpressionTreeQualityParameterName = "Quality";
    5056    #endregion
    5157
     
    7278      get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
    7379    }
    74     public ValueParameter<BoolValue> DeleteGenealogyGraphParameter {
    75       get { return (ValueParameter<BoolValue>)Parameters[DeleteGenealogyGraphParameterName]; }
     80    public ValueParameter<BoolValue> CleanGenealogyGraphParameter {
     81      get { return (ValueParameter<BoolValue>)Parameters[CleanGenealogyGraphParameterName]; }
     82    }
     83    public ValueParameter<BoolValue> UpdateGenealogyGraphParameter {
     84      get { return (ValueParameter<BoolValue>)Parameters[UpdateGenealogyGraphParameterName]; }
     85    }
     86    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
     87      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     88    }
     89    public IScopeTreeLookupParameter<DoubleValue> SymbolicExpressionTreeQualityParameter {
     90      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[SymbolicExpressionTreeQualityParameterName]; }
    7691    }
    7792    #endregion
    7893
    7994    #region Properties
    80     public CloneMapType GlobalCloneMap {
    81       get { return GlobalCloneMapParameter.ActualValue; }
    82     }
    83     public TraceMapType GlobalTraceMap {
    84       get { return GlobalTraceMapParameter.ActualValue; }
    85     }
    86     public CloneMapType GlobalFragmentMap {
    87       get { return GlobalFragmentMapParameter.ActualValue; }
    88     }
    89     public GeneticExchangeMapType GlobalGeneticExchangeMap {
    90       get { return GlobalGeneticExchangeMapParameter.ActualValue; }
    91     }
    92     public IntValue Generations {
    93       get { return GenerationsParameter.ActualValue; }
    94     }
    95     public IntValue MaximumGenerations {
    96       get { return MaximumGenerationsParameter.ActualValue; }
    97     }
    98     public ResultCollection Results {
    99       get { return ResultsParameter.ActualValue; }
    100     }
     95    private CloneMapType GlobalCloneMap { get { return GlobalCloneMapParameter.ActualValue; } }
     96    private TraceMapType GlobalTraceMap { get { return GlobalTraceMapParameter.ActualValue; } }
     97    private CloneMapType GlobalFragmentMap { get { return GlobalFragmentMapParameter.ActualValue; } }
     98    private GeneticExchangeMapType GlobalGeneticExchangeMap { get { return GlobalGeneticExchangeMapParameter.ActualValue; } }
     99    private IntValue Generations { get { return GenerationsParameter.ActualValue; } }
     100    private IntValue MaximumGenerations { get { return MaximumGenerationsParameter.ActualValue; } }
     101    private ResultCollection Results { get { return ResultsParameter.ActualValue; } }
     102    public BoolValue CleanGenealogyGraph { get { return CleanGenealogyGraphParameter.Value; } }
     103    public BoolValue UpdateGenealogyGraph { get { return UpdateGenealogyGraphParameter.Value; } }
     104    private IEnumerable<DoubleValue> Quality { get { return SymbolicExpressionTreeQualityParameter.ActualValue; } }
     105    private IEnumerable<ISymbolicExpressionTree> SymbolicExpressionTree { get { return SymbolicExpressionTreeParameter.ActualValue; } }
    101106    #endregion
    102107
     
    105110      if (!Parameters.ContainsKey(GlobalGeneticExchangeMapParameterName))
    106111        Parameters.Add(new LookupParameter<GeneticExchangeMapType>(GlobalGeneticExchangeMapParameterName));
     112      if (!Parameters.ContainsKey(CleanGenealogyGraphParameterName))
     113        Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName));
     114      if (!Parameters.ContainsKey(UpdateGenealogyGraphParameterName))
     115        Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName));
     116      if (!Parameters.ContainsKey(SymbolicExpressionTreeParameterName))
     117        Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
     118      if (!Parameters.ContainsKey(SymbolicExpressionTreeQualityParameterName))
     119        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees"));
    107120    }
    108121
     
    121134      Parameters.Add(new LookupParameter<IntValue>(MaximumGenerationsParameterName));
    122135      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
     136      Parameters.Add(new ValueParameter<BoolValue>(CleanGenealogyGraphParameterName));
     137      Parameters.Add(new ValueParameter<BoolValue>(UpdateGenealogyGraphParameterName));
     138      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
     139      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(SymbolicExpressionTreeQualityParameterName, "The qualities of the symbolic expression trees"));
    123140    }
    124141
     
    129146      if (GlobalGeneticExchangeMap != null) GlobalGeneticExchangeMap.Clear();
    130147
     148      if (!Results.ContainsKey(PopulationGraphParameterName)) return base.Apply();
     149      var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;
     150      if (graph == null) return base.Apply();
     151
     152      if (UpdateGenealogyGraph.Value) {
     153        // update vertex qualities if the trees in the scope were optimized by the ConstantOptimizationAnalyzer
     154        ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray();
     155        double[] qualities = Quality.Select(x => x.Value).ToArray();
     156
     157        for (int i = 0; i != trees.Length; ++i) {
     158          foreach (var graphNode in graph.GetGraphNodes(trees[i])) {
     159            graphNode.Quality = qualities[i];
     160          }
     161        }
     162      }
     163
    131164      // if we are at the end of the run, then we clear the genealogy graph (to save memory)
    132       if (Generations.Value == MaximumGenerations.Value)
    133         if (Results.ContainsKey(PopulationGraphParameterName)) {
    134           var graph = (SymbolicExpressionTreeGenealogyGraph)Results[PopulationGraphParameterName].Value;
    135           Results.Remove(PopulationGraphParameterName);
    136           graph.Dispose();
    137         }
    138 
     165      if (CleanGenealogyGraph.Value) {
     166        if (Generations.Value == MaximumGenerations.Value)
     167          if (Results.ContainsKey(PopulationGraphParameterName)) {
     168            Results.Remove(PopulationGraphParameterName);
     169            graph.Dispose();
     170          }
     171      }
    139172      return base.Apply();
    140173    }
  • branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking/3.4/Operators/SymbolicExpressionTreeGenealogyGraphBuilder.cs

    r9082 r9238  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using System.Threading;
     
    6362    private const string SymbolicExpressionTreeQualityParameterName = "Quality";
    6463    #endregion
    65     #region Parameter properties
     64    #region Parameters
    6665    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
    6766      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
     
    109108    }
    110109    #endregion
    111     #region Properties
     110    #region Parameter properties
    112111    public bool EnabledByDefault {
    113112      get { return true; }
     
    189188
    190189    public override IOperation Apply() {
    191       if (GlobalTraceMap == null) return base.Apply(); // no trace map, no genealogy graph
    192 
    193190      if (!Parameters.ContainsKey("Results")) return base.Apply();
    194191
     
    211208      var pairs = trees.Zip(qualities, (t, q) => new { Tree = t, Quality = q.Value }).OrderByDescending(x => x.Quality).ToList();
    212209
    213       var graphNodes = new List<SymbolicExpressionGenealogyGraphNode>(
    214         (from i in Enumerable.Range(0, trees.Length)
    215          let tree = pairs[i].Tree
    216          let quality = pairs[i].Quality
    217          select new SymbolicExpressionGenealogyGraphNode { SymbolicExpressionTree = tree, Quality = quality, Rank = generation, IsElite = i < Elites.Value }
    218         ).Concat
    219         (from t in trees
    220          where GlobalTraceMap.ContainsKey(t)
    221          let parents = GlobalTraceMap[t]
    222          where parents.Count == 1 // 1 parent means mutation
    223          let p = (ISymbolicExpressionTree)parents[0]
    224          select new SymbolicExpressionGenealogyGraphNode { SymbolicExpressionTree = p, Quality = Evaluate(p), Rank = generation - 0.5 }
    225         )).ToList();
     210      if (generation == 0) {
     211        // add the trees in the graph and return
     212        foreach (var pair in pairs) {
     213          var tree = pair.Tree;
     214          var quality = pair.Quality;
     215          graph.AddNode(new SymbolicExpressionGenealogyGraphNode {
     216            SymbolicExpressionTree = tree, Quality = quality, Rank = generation
     217          });
     218        }
     219        return base.Apply();
     220      }
     221
     222      var graphNodes = (from i in Enumerable.Range(0, trees.Length)
     223                        let tree = pairs[i].Tree
     224                        let quality = pairs[i].Quality
     225                        select new SymbolicExpressionGenealogyGraphNode {
     226                          SymbolicExpressionTree = tree,
     227                          Quality = quality,
     228                          Rank = generation,
     229                          IsElite = i < Elites.Value
     230                        }).Concat
     231        // the following query addds the intermediate nodes that were created when
     232        // crossover was followed by mutation (they are not present in the scope)
     233                        (from t in trees
     234                         where GlobalTraceMap.ContainsKey(t)
     235                         let parents = GlobalTraceMap[t]
     236                         where parents.Count == 1 // 1 parent means mutation
     237                         let p = (ISymbolicExpressionTree)parents[0]
     238                         select new SymbolicExpressionGenealogyGraphNode {
     239                           SymbolicExpressionTree = p,
     240                           Quality = Evaluate(p),
     241                           Rank = generation - 0.5 // an intermediate parent that would normale be 'invisible' to the evolutionary process
     242                         }
     243                        ).ToList();
    226244
    227245      foreach (var node in graphNodes) {
     
    229247      }
    230248
     249      graphNodes.Sort((a, b) => b.Rank.CompareTo(a.Rank));
     250
    231251      foreach (var node in graphNodes) {
    232252        var tree = node.SymbolicExpressionTree;
    233         if (!GlobalTraceMap.ContainsKey(tree)) continue;
     253        if (!GlobalTraceMap.ContainsKey(tree)) {
     254          continue;
     255        }
    234256        var parents = GlobalTraceMap[tree].Cast<ISymbolicExpressionTree>();
    235257        foreach (var p in parents) {
    236258          var nodes = graph.GetGraphNodes(p);
    237           if (nodes == null) continue;
     259          if (nodes == null) {
     260            throw new Exception("Node cannot have no parents");
     261          }
    238262          var sourceNode = nodes.LastOrDefault(n => n.Rank < node.Rank);
    239           if (sourceNode == null) continue;
     263          if (sourceNode == null) {
     264            throw new Exception("Source node cannot be null");
     265          }
    240266          var arc = new Arc { Source = sourceNode, Target = node, Data = GlobalFragmentMap[tree] };
    241267          sourceNode.AddForwardArc(arc);
     
    249275    /// Evaluate a symbolic expression tree. We perform evaluation by adding a temporary subscope with the tree in it, and calling evaluator.Apply()
    250276    /// </summary>
    251     /// <param name="tree"></param>
    252     /// <returns></returns>
     277    /// <param name="tree">The symbolic expression tree to evaluate</param>
     278    /// <returns>A double value representing the fitness</returns>
    253279    private double Evaluate(IItem tree) {
    254280      var subScope = new Scope();
Note: See TracChangeset for help on using the changeset viewer.