Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/08/11 10:42:57 (13 years ago)
Author:
mkommend
Message:

#1540: Added min and max arity to symbols and renamed SymbolicExpressionTreeNode.SubtreesCount to SymbolicExpressionTreeNode.SubtreeCount.

Location:
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Analyzers/SymbolicExpressionSymbolFrequencyAnalyzer.cs

    r5983 r6387  
    126126          string symbolName;
    127127          if (aggregateDifferentNumberOfSubtrees) symbolName = node.Symbol.Name;
    128           else symbolName = node.Symbol.Name + "-" + node.SubtreesCount;
     128          else symbolName = node.Symbol.Name + "-" + node.SubtreeCount;
    129129          if (symbolFrequencies.ContainsKey(symbolName)) symbolFrequencies[symbolName] += 1;
    130130          else symbolFrequencies.Add(symbolName, 1);
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs

    r5809 r6387  
    6262      foreach (var node in branch.IterateNodesPrefix()) {
    6363        Instruction instr = new Instruction();
    64         int subtreesCount = node.SubtreesCount;
     64        int subtreesCount = node.SubtreeCount;
    6565        if (subtreesCount > 255) throw new ArgumentException("Number of subtrees is too big (>255)");
    6666        instr.nArguments = (byte)subtreesCount;
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Crossovers/SubtreeCrossover.cs

    r6284 r6387  
    133133      if (newChild == null) {
    134134        // make sure that one subtree can be removed and that only the last subtree is removed
    135         return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreesCount &&
    136           cutPoint.ChildIndex == parent.SubtreesCount - 1;
     135        return parent.Grammar.GetMinimumSubtreeCount(parent.Symbol) < parent.SubtreeCount &&
     136          cutPoint.ChildIndex == parent.SubtreeCount - 1;
    137137      } else {
    138138        // check syntax constraints of direct parent - child relation
     
    146146            result &&
    147147            parent.Grammar.ContainsSymbol(n.Symbol) &&
    148             n.SubtreesCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
    149             n.SubtreesCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
     148            n.SubtreeCount >= parent.Grammar.GetMinimumSubtreeCount(n.Symbol) &&
     149            n.SubtreeCount <= parent.Grammar.GetMaximumSubtreeCount(n.Symbol);
    150150        });
    151151        return result;
     
    169169          }
    170170          // add one additional extension point if the number of sub trees for the symbol is not full
    171           if (n.SubtreesCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
     171          if (n.SubtreeCount < n.Grammar.GetMaximumSubtreeCount(n.Symbol)) {
    172172            // empty extension point
    173             internalCrossoverPoints.Add(new CutPoint(n, n.SubtreesCount));
     173            internalCrossoverPoints.Add(new CutPoint(n, n.SubtreeCount));
    174174          }
    175175        }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbol.cs

    r6296 r6387  
    3030    bool Fixed { get; set; }
    3131
     32    int MinimumArity { get; }
     33    int MaximumArity { get; }
     34
    3235    event EventHandler Changed;
    3336  }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionTreeNode.cs

    r5809 r6387  
    3939
    4040    IEnumerable<ISymbolicExpressionTreeNode> Subtrees { get; }
    41     int SubtreesCount { get; }
     41    int SubtreeCount { get; }
    4242    ISymbolicExpressionTreeNode GetSubtree(int index);
    4343    int IndexOfSubtree(ISymbolicExpressionTreeNode tree);
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6377 r6387  
    143143    private void AddSymbolToDictionaries(ISymbol symbol) {
    144144      symbols.Add(symbol.Name, symbol);
    145       symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0));
     145      symbolSubtreeCount.Add(symbol.Name, Tuple.Create(symbol.MinimumArity, symbol.MaximumArity));
    146146      RegisterSymbolEvents(symbol);
    147147    }
     
    315315    protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) {
    316316      var groupSymbol = symbol as GroupSymbol;
    317       if (groupSymbol != null)
    318         foreach (ISymbol s in groupSymbol.Flatten().Where(s => !(s is GroupSymbol)))
     317      if (groupSymbol != null) {
     318        var symbols = groupSymbol.Flatten().Where(s => !(s is GroupSymbol));
     319        if (symbols.Any(s => s.MinimumArity > minimumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     320        if (symbols.Any(s => s.MaximumArity < maximumSubtreeCount)) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     321        foreach (ISymbol s in symbols)
    319322          SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount);
    320       else
     323      } else {
     324        if (symbol.MinimumArity > minimumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
     325        if (symbol.MaximumArity < maximumSubtreeCount) throw new ArgumentException("Invalid minimum subtree count " + minimumSubtreeCount + " for " + symbol);
    321326        SetSubTreeCountInDictionaries(symbol, minimumSubtreeCount, maximumSubtreeCount);
     327      }
    322328
    323329      ClearCaches();
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs

    r6377 r6387  
    126126    public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
    127127
    128     public int SubtreesCount {
     128    public int SubtreeCount {
    129129      get {
    130130        if (subtrees == null) return 0;
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs

    r6233 r6387  
    3232    public const string ArgumentName = "Argument";
    3333    public const string ArgumentDescription = "Symbol that represents a function argument.";
     34    private const int minimumArity = 0;
     35    private const int maximumArity = 0;
     36
     37    public override int MinimumArity {
     38      get { return minimumArity; }
     39    }
     40    public override int MaximumArity {
     41      get { return maximumArity; }
     42    }
     43
    3444    [Storable]
    3545    private int argumentIndex;
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Defun.cs

    r5809 r6387  
    3232    public const string DefunName = "Defun";
    3333    public const string DefunDescription = "Symbol that represents a function defining node.";
     34    private const int minimumArity = 1;
     35    private const int maximumArity = 1;
     36
     37    public override int MinimumArity {
     38      get { return minimumArity; }
     39    }
     40    public override int MaximumArity {
     41      get { return maximumArity; }
     42    }
    3443
    3544    [StorableConstructor]
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs

    r6377 r6387  
    2828namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
    2929  public sealed class GroupSymbol : Symbol {
     30    private const int minimumArity = 0;
     31    private const int maximumArity = 0;
     32
     33    public override int MinimumArity {
     34      get { return minimumArity; }
     35    }
     36    public override int MaximumArity {
     37      get { return maximumArity; }
     38    }
     39
    3040    private ObservableSet<ISymbol> symbols;
    3141    public IObservableSet<ISymbol> SymbolsCollection {
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs

    r6233 r6387  
    3232    public const string InvokeFunctionName = "InvokeFunction";
    3333    public const string InvokeFunctionDescription = "Symbol that the invocation of another function.";
     34    private const int minimumArity = 0;
     35    private const int maximumArity = byte.MaxValue;
     36
     37    public override int MinimumArity {
     38      get { return minimumArity; }
     39    }
     40    public override int MaximumArity {
     41      get { return maximumArity; }
     42    }
    3443
    3544    [Storable]
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/ProgramRootSymbol.cs

    r5809 r6387  
    2929    public const string ProgramRootSymbolName = "ProgramRootSymbol";
    3030    public const string ProgramRootSymbolDescription = "Special symbol that represents the program root node of a symbolic expression tree.";
     31    private const int minimumArity = 1;
     32    private const int maximumArity = byte.MaxValue;
     33
     34    public override int MinimumArity {
     35      get { return minimumArity; }
     36    }
     37    public override int MaximumArity {
     38      get { return maximumArity; }
     39    }
    3140
    3241    [StorableConstructor]
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/StartSymbol.cs

    r5809 r6387  
    2929    public const string StartSymbolName = "StartSymbol";
    3030    public const string StartSymbolDescription = "Special symbol that represents the starting node of the result producing branch of a symbolic expression tree.";
     31    private const int minimumArity = 1;
     32    private const int maximumArity = 1;
     33
     34    public override int MinimumArity {
     35      get { return minimumArity; }
     36    }
     37    public override int MaximumArity {
     38      get { return maximumArity; }
     39    }
    3140
    3241    [StorableConstructor]
    33     private StartSymbol(bool deserializing) : base(deserializing) { }
     42    private StartSymbol(bool deserializing)
     43      : base(deserializing) {
     44    }
    3445    private StartSymbol(StartSymbol original, Cloner cloner) : base(original, cloner) { }
    3546    public StartSymbol() : base(StartSymbol.StartSymbolName, StartSymbol.StartSymbolDescription) { }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs

    r6299 r6387  
    7373      get { return false; }
    7474    }
     75
     76    public abstract int MinimumArity { get; }
     77    public abstract int MaximumArity { get; }
    7578    #endregion
    7679
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs

    r6377 r6387  
    2727namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
    2828  public static class Grammars {
    29 
    3029    [StorableClass]
    3130    private class Addition : Symbol {
     31      private const int minimumArity = 1;
     32      private const int maximumArity = byte.MaxValue;
     33
     34      public override int MinimumArity {
     35        get { return minimumArity; }
     36      }
     37      public override int MaximumArity {
     38        get { return maximumArity; }
     39      }
     40
    3241      [StorableConstructor]
    3342      protected Addition(bool deserializing) : base(deserializing) { }
     
    4150    [StorableClass]
    4251    private class Subtraction : Symbol {
     52      private const int minimumArity = 1;
     53      private const int maximumArity = byte.MaxValue;
     54
     55      public override int MinimumArity {
     56        get { return minimumArity; }
     57      }
     58      public override int MaximumArity {
     59        get { return maximumArity; }
     60      }
     61
    4362      [StorableConstructor]
    4463      protected Subtraction(bool deserializing) : base(deserializing) { }
     
    5271    [StorableClass]
    5372    private class Multiplication : Symbol {
     73      private const int minimumArity = 1;
     74      private const int maximumArity = byte.MaxValue;
     75
     76      public override int MinimumArity {
     77        get { return minimumArity; }
     78      }
     79      public override int MaximumArity {
     80        get { return maximumArity; }
     81      }
     82
    5483      [StorableConstructor]
    5584      protected Multiplication(bool deserializing) : base(deserializing) { }
     
    6392    [StorableClass]
    6493    private class Division : Symbol {
     94      private const int minimumArity = 1;
     95      private const int maximumArity = byte.MaxValue;
     96
     97      public override int MinimumArity {
     98        get { return minimumArity; }
     99      }
     100      public override int MaximumArity {
     101        get { return maximumArity; }
     102      }
     103
    65104      [StorableConstructor]
    66105      protected Division(bool deserializing) : base(deserializing) { }
     
    74113    [StorableClass]
    75114    private class Terminal : Symbol {
     115      private const int minimumArity = 0;
     116      private const int maximumArity = 0;
     117
     118      public override int MinimumArity {
     119        get { return minimumArity; }
     120      }
     121      public override int MaximumArity {
     122        get { return maximumArity; }
     123      }
     124
    76125      [StorableConstructor]
    77126      protected Terminal(bool deserializing) : base(deserializing) { }
  • branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs

    r5809 r6387  
    122122
    123123        foreach (var argTreenode in defunTreeNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()) {
    124           Assert.IsTrue(argTreenode.SubtreesCount == 0);
     124          Assert.IsTrue(argTreenode.SubtreeCount == 0);
    125125          Assert.IsTrue(((Argument)argTreenode.Symbol).ArgumentIndex < arity);
    126126        }
Note: See TracChangeset for help on using the changeset viewer.