Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/15/11 14:54:43 (13 years ago)
Author:
abeham
Message:

#1465

  • updated branch with changes from trunk
  • fixed some bugs
  • introduced secondary x-axis option
Location:
branches/histogram
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/histogram

  • branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs

    r5809 r6011  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2728using HeuristicLab.Operators;
    2829using HeuristicLab.Optimization;
     
    4041    private const string ResultsParameterName = "Results";
    4142    private const string SymbolFrequenciesParameterName = "SymbolFrequencies";
     43    private const string AggregateSymbolsWithDifferentSubtreeCountParameterName = "AggregateSymbolsWithDifferentSubtreeCount";
    4244
    4345    #region parameter properties
     
    5153      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
    5254    }
     55    public IValueParameter<BoolValue> AggregateSymbolsWithDifferentSubtreeCountParameter {
     56      get { return (IValueParameter<BoolValue>)Parameters[AggregateSymbolsWithDifferentSubtreeCountParameterName]; }
     57    }
    5358    #endregion
    5459    #region properties
    55     public DataTable SymbolFrequencies {
    56       get { return SymbolFrequenciesParameter.ActualValue; }
    57       set { SymbolFrequenciesParameter.ActualValue = value; }
     60    public BoolValue AggregrateSymbolsWithDifferentSubtreeCount {
     61      get { return AggregateSymbolsWithDifferentSubtreeCountParameter.Value; }
     62      set { AggregateSymbolsWithDifferentSubtreeCountParameter.Value = value; }
    5863    }
    5964    #endregion
     
    6570      : base() {
    6671      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
    67       Parameters.Add(new ValueLookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));
     72      Parameters.Add(new LookupParameter<DataTable>(SymbolFrequenciesParameterName, "The data table to store the symbol frequencies."));
    6873      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the symbol frequencies should be stored."));
     74      Parameters.Add(new ValueParameter<BoolValue>(AggregateSymbolsWithDifferentSubtreeCountParameterName, "Flag that indicates if the frequencies of symbols with the same name but different number of sub-trees should be aggregated.", new BoolValue(true)));
    6975    }
    7076    public override IDeepCloneable Clone(Cloner cloner) {
     
    7278    }
    7379
     80    [StorableHook(HookType.AfterDeserialization)]
     81    private void AfterDeserialization() {
     82      #region remove with HL 3.4
     83      if (!Parameters.ContainsKey(AggregateSymbolsWithDifferentSubtreeCountParameterName))
     84        Parameters.Add(new ValueParameter<BoolValue>(AggregateSymbolsWithDifferentSubtreeCountParameterName, "Flag that indicates if the frequencies of symbols with the same name but different number of sub-trees should be aggregated.", new BoolValue(true)));
     85      #endregion
     86    }
     87
    7488    public override IOperation Apply() {
    7589      ItemArray<ISymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
    7690      ResultCollection results = ResultsParameter.ActualValue;
     91      DataTable symbolFrequencies = SymbolFrequenciesParameter.ActualValue;
     92      if (symbolFrequencies == null) {
     93        symbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population.");
     94        symbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency";
    7795
    78       if (SymbolFrequencies == null) {
    79         SymbolFrequencies = new DataTable("Symbol frequencies", "Relative frequency of symbols aggregated over the whole population.");
    80         SymbolFrequencies.VisualProperties.YAxisTitle = "Relative Symbol Frequency";
    81         results.Add(new Result("Symbol frequencies", SymbolFrequencies));
     96        SymbolFrequenciesParameter.ActualValue = symbolFrequencies;
     97        results.Add(new Result("Symbol frequencies", symbolFrequencies));
    8298      }
    8399
    84100      // all rows must have the same number of values so we can just take the first
    85       int numberOfValues = SymbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
     101      int numberOfValues = symbolFrequencies.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
    86102
    87       foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions)) {
    88         if (!SymbolFrequencies.Rows.ContainsKey(pair.Key)) {
     103      foreach (var pair in SymbolicExpressionSymbolFrequencyAnalyzer.CalculateSymbolFrequencies(expressions, AggregrateSymbolsWithDifferentSubtreeCount.Value)) {
     104        if (!symbolFrequencies.Rows.ContainsKey(pair.Key)) {
    89105          // initialize a new row for the symbol and pad with zeros
    90106          DataRow row = new DataRow(pair.Key, "", Enumerable.Repeat(0.0, numberOfValues));
    91107          row.VisualProperties.StartIndexZero = true;
    92           SymbolFrequencies.Rows.Add(row);
     108          symbolFrequencies.Rows.Add(row);
    93109        }
    94         SymbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);
     110        symbolFrequencies.Rows[pair.Key].Values.Add(pair.Value);
    95111      }
    96112
    97113      // add a zero for each data row that was not modified in the previous loop
    98       foreach (var row in SymbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1))
     114      foreach (var row in symbolFrequencies.Rows.Where(r => r.Values.Count != numberOfValues + 1))
    99115        row.Values.Add(0.0);
    100116
     
    102118    }
    103119
    104     public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees) {
     120    public static IEnumerable<KeyValuePair<string, double>> CalculateSymbolFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateDifferentNumberOfSubtrees = true) {
    105121      Dictionary<string, double> symbolFrequencies = new Dictionary<string, double>();
    106122      int totalNumberOfSymbols = 0;
     
    108124      foreach (var tree in trees) {
    109125        foreach (var node in tree.IterateNodesPrefix()) {
    110           if (symbolFrequencies.ContainsKey(node.Symbol.Name)) symbolFrequencies[node.Symbol.Name] += 1;
    111           else symbolFrequencies.Add(node.Symbol.Name, 1);
     126          string symbolName;
     127          if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name;
     128          else symbolName = node.Symbol.Name + "-" + node.SubtreesCount;
     129          if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1;
     130          else symbolFrequencies.Add(symbolName, 1);
    112131          totalNumberOfSymbols++;
    113132        }
  • branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs

    r5925 r6011  
    103103    public static void PTC2(IRandom random, ISymbolicExpressionTreeNode seedNode,
    104104      int maxLength, int maxDepth) {
     105      // make sure it is possible to create a trees smaller than maxLength and maxDepth
     106      if (seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol) > maxLength)
     107        throw new ArgumentException("Cannot create trees of length " + maxLength + " or shorter because of grammar constraints.", "maxLength");
     108      if (seedNode.Grammar.GetMinimumExpressionDepth(seedNode.Symbol) > maxDepth)
     109        throw new ArgumentException("Cannot create trees of depth " + maxDepth + " or smaller because of grammar constraints.", "maxDepth");
     110
    105111      // tree length is limited by the grammar and by the explicit size constraints
    106112      int allowedMinLength = seedNode.Grammar.GetMinimumExpressionLength(seedNode.Symbol);
     
    149155        int argumentIndex = nextExtension.ChildIndex;
    150156        int extensionDepth = nextExtension.ExtensionPointDepth;
    151         if (extensionDepth + parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth) {
     157        if (parent.Grammar.GetMinimumExpressionDepth(parent.Symbol) >= maxDepth - extensionDepth) {
    152158          ReplaceWithMinimalTree(random, root, parent, argumentIndex);
    153159        } else {
     
    155161                                where s.InitialFrequency > 0.0
    156162                                where parent.Grammar.IsAllowedChildSymbol(parent.Symbol, s, argumentIndex)
    157                                 where parent.Grammar.GetMinimumExpressionDepth(s) + extensionDepth - 1 < maxDepth
     163                                where parent.Grammar.GetMinimumExpressionDepth(s) < maxDepth - extensionDepth + 1
    158164                                where parent.Grammar.GetMaximumExpressionLength(s) > targetLength - totalListMinLength - currentLength
    159165                                select s)
  • branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r5809 r6011  
    229229
    230230    public virtual IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    231       return from s in Symbols where IsAllowedChildSymbol(parent, s) select s;
     231      return from s in AllowedSymbols where IsAllowedChildSymbol(parent, s) select s;
    232232    }
    233233
     
    265265        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    266266        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    267                                               let minForSlot = (long)(from s in Symbols
     267                                              let minForSlot = (long)(from s in AllowedSymbols
    268268                                                                      where IsAllowedChildSymbol(symbol, s, argIndex)
    269269                                                                      select GetMinimumExpressionLength(s)).DefaultIfEmpty(0).Min()
     
    282282        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
    283283        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaximumSubtreeCount(symbol))
    284                                   let maxForSlot = (long)(from s in Symbols
     284                                  let maxForSlot = (long)(from s in AllowedSymbols
    285285                                                          where IsAllowedChildSymbol(symbol, s, argIndex)
    286286                                                          select GetMaximumExpressionLength(s)).DefaultIfEmpty(0).Max()
    287287                                  select maxForSlot).DefaultIfEmpty(0).Sum();
    288         long limit = int.MaxValue;
    289         cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
     288        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, int.MaxValue);
    290289        return cachedMaxExpressionLength[symbol.Name];
    291290      }
     
    298297      if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) {
    299298        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
    300         cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
    301                                                      let minForSlot = (from s in Symbols
    302                                                                        where IsAllowedChildSymbol(symbol, s, argIndex)
    303                                                                        select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
    304                                                      select minForSlot).DefaultIfEmpty(0).Max();
     299        long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol))
     300                             let minForSlot = (long)(from s in AllowedSymbols
     301                                                     where IsAllowedChildSymbol(symbol, s, argIndex)
     302                                                     select GetMinimumExpressionDepth(s)).DefaultIfEmpty(0).Min()
     303                             select minForSlot).DefaultIfEmpty(0).Max();
     304        cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue);
    305305        return cachedMinExpressionDepth[symbol.Name];
    306306      }
Note: See TracChangeset for help on using the changeset viewer.