Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/17/12 15:18:03 (13 years ago)
Author:
bburlacu
Message:

#1682: New branch format: removed unnecessary files, merged remaining.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionTreeLengthAnalyzer.cs

    r6978 r7344  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    3030using HeuristicLab.Parameters;
    3131using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using System;
    3233
    3334namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    5253    }
    5354    public ValueLookupParameter<DataTable> SymbolicExpressionTreeLengthsParameter {
    54       get {
    55         return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName];
    56       }
     55      get { return (ValueLookupParameter<DataTable>)Parameters[SymbolicExpressionTreeLengthsParameterName]; }
    5756    }
    5857    public ValueLookupParameter<DataTableHistory> SymbolicExpressionTreeLengthsHistoryParameter {
    59       get {
    60         return (ValueLookupParameter<DataTableHistory>)Parameters[SymbolicExpressionTreeLengthsHistoryParameterName];
    61       }
     58      get { return (ValueLookupParameter<DataTableHistory>)Parameters[SymbolicExpressionTreeLengthsHistoryParameterName]; }
    6259    }
    6360    public ValueLookupParameter<ResultCollection> ResultsParameter {
     
    7168      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
    7269    }
    73     public LookupParameter<IntValue> UpdateCounterParameter {
    74       get { return (LookupParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
     70    public ValueParameter<IntValue> UpdateCounterParameter {
     71      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
    7572    }
    7673    #endregion
     74
     75    #region Properties
     76    public bool EnabledByDefault {
     77      get { return true; }
     78    }
     79    public IntValue UpdateInterval {
     80      get { return UpdateIntervalParameter.Value; }
     81    }
     82    public IntValue UpdateCounter {
     83      get { return UpdateCounterParameter.Value; }
     84    }
     85    public BoolValue StoreHistory {
     86      get { return StoreHistoryParameter.Value; }
     87    }
     88    #endregion
     89
    7790    [StorableConstructor]
    7891    private SymbolicExpressionTreeLengthAnalyzer(bool deserializing) : base() { }
    7992    private SymbolicExpressionTreeLengthAnalyzer(SymbolicExpressionTreeLengthAnalyzer original, Cloner cloner)
    8093      : base(original, cloner) {
    81       AfterDeserialization();
    8294    }
    8395    public override IDeepCloneable Clone(Cloner cloner) {
     
    91103      Parameters.Add(new ValueLookupParameter<DataTableHistory>(SymbolicExpressionTreeLengthsHistoryParameterName, "The data table to store the symbolic expression tree lengths history."));
    92104      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
    93 
    94       #region Add parameters for history
    95105      Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
    96106      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
    97       Parameters.Add(new LookupParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", "MinMaxAverageSymbolicExpressionTreeLengthAnalyzerUpdateCounter"));
    98       #endregion
    99 
     107      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
     108
     109      SymbolicExpressionTreeLengthsParameter.Hidden = true;
     110      SymbolicExpressionTreeLengthsHistoryParameter.Hidden = true;
     111      ResultsParameter.Hidden = true;
    100112      UpdateCounterParameter.Hidden = true;
    101 
    102       AfterDeserialization();
    103113    }
    104114
     
    112122        Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
    113123      }
     124      //necessary code to correct UpdateCounterParameter - type was changed from LookupParameter to ValueParameter
     125      if (Parameters.ContainsKey(UpdateCounterParameterName) && (Parameters[UpdateCounterParameterName] is LookupParameter<IntValue>))
     126        Parameters.Remove(UpdateCounterParameterName);
    114127      if (!Parameters.ContainsKey(UpdateCounterParameterName)) {
    115         Parameters.Add(new LookupParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", "MinMaxAverageSymbolicExpressionTreeLengthAnalyzerUpdateCounter"));
    116       }
    117     }
     128        Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
     129        UpdateCounterParameter.Hidden = true;
     130      }
     131    }
     132
     133    #region IStatefulItem members
     134    public override void InitializeState() {
     135      base.InitializeState();
     136      UpdateCounter.Value = 0;
     137    }
     138    public override void ClearState() {
     139      base.ClearState();
     140      UpdateCounter.Value = 0;
     141    }
     142    #endregion
    118143
    119144    public override IOperation Apply() {
    120       int updateInterval = UpdateIntervalParameter.Value.Value;
    121       IntValue updateCounter = UpdateCounterParameter.ActualValue;
    122       // if the counter doesn't exist yet, we initialize it here with the current update interval
    123       if (updateCounter == null) {
    124         updateCounter = new IntValue(updateInterval);
    125         UpdateCounterParameter.ActualValue = updateCounter;
    126       } else updateCounter.Value++;
    127 
     145      UpdateCounter.Value++;
    128146      // the analyzer runs periodically, every 'updateInterval' times
    129       if (updateCounter.Value == updateInterval) {
    130         updateCounter.Value = 0; // reset counter
     147      if (UpdateCounter.Value == UpdateInterval.Value) {
     148        UpdateCounter.Value = 0; // reset counter
    131149
    132150        // compute all tree lengths and store them in the lengthsTable
     
    136154        // if the table was not created yet, we create it here
    137155        if (treeLengthsTable == null) {
    138           treeLengthsTable = new DataTable("Tree Length Values");
     156          treeLengthsTable = new DataTable("Histogram");
    139157          SymbolicExpressionTreeLengthsParameter.ActualValue = treeLengthsTable;
    140158        }
    141159
    142160        // data table which stores tree length values
    143         DataRow dataRow;
    144 
    145         var treeLengths = solutions.Select(s => (double)s.Length);
    146 
    147         if (!treeLengthsTable.Rows.ContainsKey("Tree Lengths")) {
    148           dataRow = new DataRow("Tree Lengths", "", treeLengths);
    149           treeLengthsTable.Rows.Add(dataRow);
     161        DataRow treeLengthsTableRow;
     162
     163        const string treeLengthsTableRowName = "Symbolic expression tree lengths";
     164        const string treeLengthsTableRowDesc = "The distribution of symbolic expression tree lengths";
     165        const string xAxisTitle = "Symbolic expression tree lengths";
     166        const string yAxisTitle = "Frequency / Number of tree individuals";
     167
     168        var treeLengths = solutions.Select(s => (int)s.Length).ToList();
     169
     170        int maxLength = treeLengths.Max(t => t);
     171        int minLength = treeLengths.Min(t => t);
     172
     173        if (!treeLengthsTable.Rows.ContainsKey(treeLengthsTableRowName)) {
     174          treeLengthsTableRow = new DataRow(treeLengthsTableRowName, treeLengthsTableRowDesc, treeLengths.Select(x => (double)x));
     175          treeLengthsTable.Rows.Add(treeLengthsTableRow);
    150176        } else {
    151           dataRow = treeLengthsTable.Rows["Tree Lengths"];
    152           dataRow.Values.Replace(treeLengths);
     177          treeLengthsTableRow = treeLengthsTable.Rows[treeLengthsTableRowName];
     178          treeLengthsTableRow.Values.Replace(treeLengths.Select(x => (double)x));
    153179        }
    154180
    155181        double maximumAllowedTreeLength = ((LookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]).ActualValue.Value;
    156182
    157         dataRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
    158         dataRow.VisualProperties.ExactBins = false;
    159 
    160         // the following trick should result in an intervalWidth of 1,2,4,... (an int value)
    161         dataRow.VisualProperties.Bins = solutions.Max(s => s.Length) - solutions.Min(s => s.Length);
    162 
    163         int maxLength = solutions.Max(s => s.Length);
    164         if (maxLength <= 25)      // [0,25]
    165           dataRow.VisualProperties.ScaleFactor = 1.0;
    166         else if (maxLength <= 100) // [26,100])
    167           dataRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
    168         else if (maxLength <= 250) // [100,250]
    169           dataRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
     183        treeLengthsTableRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
     184        treeLengthsTableRow.VisualProperties.ExactBins = false;
     185
     186        int range = maxLength - minLength;
     187        if (range == 0) range = 1;
     188        // the following trick should result in an integer intervalWidth of 1,2,4,...
     189        treeLengthsTableRow.VisualProperties.Bins = range;
     190
     191        if (maxLength <= 25) // [0,25]
     192          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0;
     193        else if (maxLength <= 100) // [26,100]
     194          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 2.0;
     195        else if (maxLength <= 250) // [101,250]
     196          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 5.0;
    170197        else if (maxLength <= 500) // [251,500]
    171           dataRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
     198          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 10.0;
    172199        else
    173           dataRow.VisualProperties.ScaleFactor = 1.0 / 20.0;   // [501,inf]
     200          treeLengthsTableRow.VisualProperties.ScaleFactor = 1.0 / 20.0; // [501,inf]
     201
     202        treeLengthsTableRow.VisualProperties.IsVisibleInLegend = false;
    174203
    175204        // visual properties for the X-axis
     
    178207        treeLengthsTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
    179208        if (maxLength > maximumAllowedTreeLength + 1)
    180           treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = solutions.Max(s => s.Length) + 1; // +1 so the histogram column for the maximum length won't get trimmed
     209          treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
    181210        else
    182211          treeLengthsTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
    183         // visual properties for the Y-axis
     212        treeLengthsTable.VisualProperties.XAxisTitle = xAxisTitle;
     213        //visual properties for the Y-axis
    184214        treeLengthsTable.VisualProperties.YAxisMinimumAuto = false;
    185215        treeLengthsTable.VisualProperties.YAxisMaximumAuto = false;
    186216        treeLengthsTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
    187         treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = Math.Ceiling(solutions.Length / 2.0);
     217        int maxFreq = (int)Math.Round(solutions.GroupBy(s => s.Length).Max(g => g.Count()) / treeLengthsTableRow.VisualProperties.ScaleFactor);
     218        if (maxFreq % 5 != 0)
     219          maxFreq += (5 - maxFreq % 5);
     220        double yAxisMaximumFixedValue = maxFreq;
     221
     222        treeLengthsTable.VisualProperties.YAxisMaximumFixedValue = yAxisMaximumFixedValue;
     223        treeLengthsTable.VisualProperties.YAxisTitle = yAxisTitle;
    188224
    189225        var results = ResultsParameter.ActualValue;
    190226
    191         if (!results.ContainsKey("Tree Lengths")) {
    192           results.Add(new Result("Tree Lengths", treeLengthsTable));
     227        if (!results.ContainsKey(treeLengthsTableRowName)) {
     228          results.Add(new Result(treeLengthsTableRowName, treeLengthsTable));
    193229        } else {
    194           results["Tree Lengths"].Value = treeLengthsTable;
     230          results[treeLengthsTableRowName].Value = treeLengthsTable;
    195231        }
    196232
    197233        bool storeHistory = StoreHistoryParameter.Value.Value;
     234        const string treeLengthHistoryTableName = "Tree lengths history";
    198235
    199236        if (storeHistory) {
    200           // store tree lengths for each generation
    201           var historyDataRow = new DataRow("Tree Lengths " + results["Generations"].Value, "", dataRow.Values);
    202           historyDataRow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Histogram;
    203           historyDataRow.VisualProperties.ExactBins = false;
    204           historyDataRow.VisualProperties.Bins = solutions.Max(s => s.Length) - solutions.Min(s => s.Length);
    205           historyDataRow.VisualProperties.ScaleFactor = dataRow.VisualProperties.ScaleFactor;
    206           var historyTable = new DataTable("Lengths Table " + results["Generations"]);
    207           historyTable.Rows.Add(historyDataRow);
    208           // visual properties for the X-axis
    209           historyTable.VisualProperties.XAxisMinimumAuto = false;
    210           historyTable.VisualProperties.XAxisMaximumAuto = false;
    211           historyTable.VisualProperties.XAxisMinimumFixedValue = 0.0;
    212           if (solutions.Max(s => s.Length) > maximumAllowedTreeLength + 1)
    213             historyTable.VisualProperties.XAxisMaximumFixedValue = maxLength + 1; // +1 so the histogram column for the maximum length won't get trimmed
    214           else
    215             historyTable.VisualProperties.XAxisMaximumFixedValue = maximumAllowedTreeLength + 1;
    216           // visual properties for the Y-axis
    217           historyTable.VisualProperties.YAxisMinimumAuto = false;
    218           historyTable.VisualProperties.YAxisMaximumAuto = false;
    219           historyTable.VisualProperties.YAxisMinimumFixedValue = 0.0;
    220           historyTable.VisualProperties.YAxisMaximumFixedValue = Math.Ceiling(solutions.Length / 2.0);
    221 
    222237          var treeLengthsHistory = SymbolicExpressionTreeLengthsHistoryParameter.ActualValue;
    223238          if (treeLengthsHistory == null) {
     
    225240            SymbolicExpressionTreeLengthsHistoryParameter.ActualValue = treeLengthsHistory;
    226241          }
    227 
    228           treeLengthsHistory.Add(historyTable);
    229 
    230           if (!results.ContainsKey("Tree Lengths History")) {
    231             results.Add(new Result("Tree Lengths History", treeLengthsHistory));
     242          treeLengthsHistory.Add((DataTable)treeLengthsTable.Clone());
     243
     244          if (!results.ContainsKey(treeLengthHistoryTableName)) {
     245            results.Add(new Result(treeLengthHistoryTableName, treeLengthsHistory));
    232246          } else {
    233             results["Tree Lengths History"].Value = treeLengthsHistory;
     247            results[treeLengthHistoryTableName].Value = treeLengthsHistory;
    234248          }
    235249        }
    236250      }
    237 
    238251      return base.Apply();
    239252    }
Note: See TracChangeset for help on using the changeset viewer.