Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13747


Ignore:
Timestamp:
04/07/16 15:13:41 (8 years ago)
Author:
bburlacu
Message:

#1772: Extend the fragment length analyzer to calculate fragment size distributions for both the swapped and replaced fragments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisFragmentLengthAnalyzer.cs

    r13495 r13747  
    2020#endregion
    2121
     22using System.Collections.Generic;
    2223using System.Linq;
    2324using HeuristicLab.Analysis;
     
    2829using HeuristicLab.EvolutionTracking;
    2930using HeuristicLab.Optimization;
     31using HeuristicLab.Parameters;
    3032using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3133
     
    3436  [StorableClass]
    3537  public class SymbolicDataAnalysisFragmentLengthAnalyzer : EvolutionTrackingAnalyzer<ISymbolicExpressionTree> {
     38    private const string StoreHistoryParameterName = "StoryHistory";
     39
     40    public IFixedValueParameter<BoolValue> StoreHistoryParameter {
     41      get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
     42    }
     43
     44    public bool StoreHistory {
     45      get { return StoreHistoryParameter.Value.Value; }
     46      set { StoreHistoryParameter.Value.Value = value; }
     47    }
     48
    3649    [StorableConstructor]
    3750    protected SymbolicDataAnalysisFragmentLengthAnalyzer(bool deserializing) : base(deserializing) { }
     
    3952    public SymbolicDataAnalysisFragmentLengthAnalyzer() {
    4053      UpdateCounterParameter.ActualName = "FragmentLengthAnalyzerUpdateCounter";
     54
     55      Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, new BoolValue(false)));
    4156    }
    4257
     
    6580
    6681      // consider all fragments (including those of the intermediate crossover children)
    67       double averageCrossoverFragmentLength = 0;
    68       double averageMutationFragmentLength = 0;
    69       int crossoverChildren = 0;
    70       int mutationChildren = 0;
    71 
    7282      var vertices = PopulationGraph.Vertices.Where(x => x.Rank > Generation.Value - 1);
     83      var crossoverFragmentLengthDistribution = new List<double>();
     84      var replacedCrossoverFragmentLengthDistribution = new List<double>();
     85      var mutationFragmentLengthDistribution = new List<double>();
     86      var replacedMutationFragmentLengthDistribution = new List<double>();
    7387      foreach (var v in vertices) {
    7488        if (!v.InArcs.Any() || v.InArcs.Last().Data == null)
     
    7690        var fragment = (IFragment<ISymbolicExpressionTreeNode>)v.InArcs.Last().Data;
    7791        if (v.InDegree == 2) {
    78           averageCrossoverFragmentLength += fragment.Root.GetLength();
    79           crossoverChildren++;
     92          crossoverFragmentLengthDistribution.Add(fragment.Root.GetLength());
     93          int index = 0;
     94          foreach (var s in v.Parents.First().Data.IterateNodesPrefix()) {
     95            if (index == fragment.Index1) {
     96              replacedCrossoverFragmentLengthDistribution.Add(s.GetLength());
     97              break;
     98            }
     99            index++;
     100          }
    80101        } else {
    81           averageMutationFragmentLength += fragment.Root.GetLength();
    82           mutationChildren++;
     102          mutationFragmentLengthDistribution.Add(fragment.Root.GetLength());
     103          int index = 0;
     104          foreach (var s in v.Parents.First().Data.IterateNodesPrefix()) {
     105            if (index == fragment.Index1) {
     106              replacedMutationFragmentLengthDistribution.Add(s.GetLength());
     107              break;
     108            }
     109            index++;
     110          }
    83111        }
    84112      }
    85       var averageFragmentLength = (averageCrossoverFragmentLength + averageMutationFragmentLength) / (crossoverChildren + mutationChildren);
    86       averageCrossoverFragmentLength /= crossoverChildren;
    87       averageMutationFragmentLength /= mutationChildren;
    88 
     113      var averageFragmentLength = (crossoverFragmentLengthDistribution.Average() + mutationFragmentLengthDistribution.Average()) / 2d;
    89114      DataTable table;
    90115      if (!Results.ContainsKey("AverageFragmentLength")) {
     
    94119        table.Rows.Add(row);
    95120        row = new DataRow("Average crossover fragment length") { VisualProperties = { StartIndexZero = true } };
    96         row.Values.Add(averageCrossoverFragmentLength);
     121        row.Values.Add(crossoverFragmentLengthDistribution.Average());
     122        table.Rows.Add(row);
     123        row = new DataRow("Average replaced crossover fragment length") { VisualProperties = { StartIndexZero = true } };
     124        row.Values.Add(replacedCrossoverFragmentLengthDistribution.Average());
    97125        table.Rows.Add(row);
    98126        row = new DataRow("Average mutation fragment length") { VisualProperties = { StartIndexZero = true } };
    99         row.Values.Add(averageMutationFragmentLength);
     127        row.Values.Add(mutationFragmentLengthDistribution.Average());
     128        table.Rows.Add(row);
     129        row = new DataRow("Average replaced mutation fragment length") { VisualProperties = { StartIndexZero = true } };
     130        row.Values.Add(replacedMutationFragmentLengthDistribution.Average());
    100131        table.Rows.Add(row);
    101132        Results.Add(new Result("AverageFragmentLength", table));
     
    103134        table = (DataTable)Results["AverageFragmentLength"].Value;
    104135        table.Rows["Average fragment length"].Values.Add(averageFragmentLength);
    105         table.Rows["Average crossover fragment length"].Values.Add(averageCrossoverFragmentLength);
    106         table.Rows["Average mutation fragment length"].Values.Add(averageMutationFragmentLength);
     136        table.Rows["Average crossover fragment length"].Values.Add(crossoverFragmentLengthDistribution.Average());
     137        table.Rows["Average replaced crossover fragment length"].Values.Add(replacedCrossoverFragmentLengthDistribution.Average());
     138        table.Rows["Average mutation fragment length"].Values.Add(mutationFragmentLengthDistribution.Average());
     139        table.Rows["Average replaced mutation fragment length"].Values.Add(replacedMutationFragmentLengthDistribution.Average());
     140      }
     141      if (!Results.ContainsKey("FragmentLengthDistribution")) {
     142        table = new DataTable("Table length distribution");
     143        var row = new DataRow("Crossover fragment length distribution") { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };
     144        row.Values.Replace(crossoverFragmentLengthDistribution);
     145        table.Rows.Add(row);
     146        row = new DataRow("Replaced crossover fragment length distribution") { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };
     147        row.Values.Replace(replacedCrossoverFragmentLengthDistribution);
     148        table.Rows.Add(row);
     149        row = new DataRow("Mutation fragment length distribution") { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };
     150        row.Values.Replace(mutationFragmentLengthDistribution);
     151        table.Rows.Add(row);
     152        row = new DataRow("Replaced mutation fragment length distribution") { VisualProperties = { StartIndexZero = true, ChartType = DataRowVisualProperties.DataRowChartType.Histogram } };
     153        row.Values.Replace(replacedMutationFragmentLengthDistribution);
     154        table.Rows.Add(row);
     155        Results.Add(new Result("FragmentLengthDistribution", table));
     156      } else {
     157        table = (DataTable)Results["FragmentLengthDistribution"].Value;
     158        table.Rows["Crossover fragment length distribution"].Values.Replace(crossoverFragmentLengthDistribution);
     159        table.Rows["Mutation fragment length distribution"].Values.Replace(mutationFragmentLengthDistribution);
     160        table.Rows["Replaced crossover fragment length distribution"].Values.Replace(replacedCrossoverFragmentLengthDistribution);
     161        table.Rows["Replaced mutation fragment length distribution"].Values.Replace(replacedMutationFragmentLengthDistribution);
     162      }
     163      if (StoreHistory) {
     164        if (!Results.ContainsKey("FragmentLengthDistributionHistory")) {
     165          var history = new DataTableHistory();
     166          history.Add(table);
     167          Results.Add(new Result("FragmentLengthDistributionHistory", history));
     168        } else {
     169          var history = (DataTableHistory)Results["FragmentLengthDistributionHistory"].Value;
     170          history.Add(table);
     171        }
    107172      }
    108173      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.