Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/09/10 17:28:32 (15 years ago)
Author:
gkronber
Message:

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs

    r3270 r3294  
    3737
    3838    protected override SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight) {
    39       return Apply(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);
     39      return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);
    4040    }
    4141
    42     public SymbolicExpressionTree Apply(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {
     42    public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {
    4343      // tree size is limited by the grammar and by the explicit size constraints
    44       int allowedMinSize = grammar.MinimalExpressionLength(grammar.StartSymbol);
    45       int allowedMaxSize = Math.Min(maxTreeSize, grammar.MaximalExpressionLength(grammar.StartSymbol));
     44      int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol);
     45      int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol));
    4646      // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
    4747      int treeSize = random.Next(allowedMinSize, allowedMaxSize);
     
    4949      do {
    5050        try {
    51           tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1);
     51          tree.Root = grammar.ProgramRootSymbol.CreateTreeNode();
     52          tree.Root.AddSubTree(PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1));
    5253        }
    5354        catch (ArgumentException) {
     
    5556          treeSize = random.Next(allowedMinSize, allowedMaxSize);
    5657        }
    57       } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
     58      } while (tree.Root.SubTrees.Count == 0 || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
     59      System.Diagnostics.Debug.Assert(grammar.IsValidExpression(tree));
    5860      return tree;
    5961    }
    6062
    61     private Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
     63    private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
    6264      var symbolList = symbols.ToList();
    63       var ticketsSum = symbolList.Select(x => x.Tickets.Value).Sum();
     65      var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
    6466      var r = random.NextDouble() * ticketsSum;
    6567      double aggregatedTickets = 0;
    6668      for (int i = 0; i < symbolList.Count; i++) {
    67         aggregatedTickets += symbolList[i].Tickets.Value;
     69        aggregatedTickets += symbolList[i].InitialFrequency;
    6870        if (aggregatedTickets >= r) {
    6971          return symbolList[i];
     
    7577
    7678
    77     private SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {
     79    public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {
    7880      SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode();
    7981      if (size <= 1 || maxDepth <= 1) return root;
    80       List<object[]> list = new List<object[]>();
     82      List<object[]> extensionPoints = new List<object[]>();
    8183      int currentSize = 1;
    82       int totalListMinSize = grammar.MinimalExpressionLength(rootSymbol) - 1;
     84      int totalListMinSize = grammar.GetMinExpressionLength(rootSymbol) - 1;
    8385
    8486      int actualArity = SampleArity(random, grammar, rootSymbol, size);
     
    8688        // insert a dummy sub-tree and add the pending extension to the list
    8789        root.AddSubTree(null);
    88         list.Add(new object[] { root, i, 2 });
     90        extensionPoints.Add(new object[] { root, i, 2 });
    8991      }
    9092
    9193      // while there are pending extension points and we have not reached the limit of adding new extension points
    92       while (list.Count > 0 && totalListMinSize + currentSize < size) {
    93         int randomIndex = random.Next(list.Count);
    94         object[] nextExtension = list[randomIndex];
    95         list.RemoveAt(randomIndex);
     94      while (extensionPoints.Count > 0 && totalListMinSize + currentSize < size) {
     95        int randomIndex = random.Next(extensionPoints.Count);
     96        object[] nextExtension = extensionPoints[randomIndex];
     97        extensionPoints.RemoveAt(randomIndex);
    9698        SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0];
    9799        int argumentIndex = (int)nextExtension[1];
    98100        int extensionDepth = (int)nextExtension[2];
    99         if (extensionDepth + grammar.MinimalExpressionDepth(parent.Symbol) >= maxDepth) {
     101        if (extensionDepth + grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
    100102          parent.RemoveSubTree(argumentIndex);
    101           SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar.AllowedSymbols(parent.Symbol, argumentIndex));
     103          SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, argumentIndex));
    102104          parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree
    103105          currentSize += branch.GetSize();
    104106          totalListMinSize -= branch.GetSize();
    105107        } else {
    106           var allowedSubFunctions = from s in grammar.AllowedSymbols(parent.Symbol, argumentIndex)
    107                                     where grammar.MinimalExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth
    108                                     where grammar.MaximalExpressionLength(s) > size - totalListMinSize - currentSize ||
     108          var allowedSubFunctions = from s in grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)
     109                                    where grammar.GetMinExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth
     110                                    where grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize ||
    109111                                          totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow
    110112                                    // terminals or terminal-branches
     
    121123            // insert a dummy sub-tree and add the pending extension to the list
    122124            newTree.AddSubTree(null);
    123             list.Add(new object[] { newTree, i, extensionDepth + 1 });
     125            extensionPoints.Add(new object[] { newTree, i, extensionDepth + 1 });
    124126          }
    125           totalListMinSize += grammar.MinimalExpressionLength(selectedSymbol);
     127          totalListMinSize += grammar.GetMinExpressionLength(selectedSymbol);
    126128        }
    127129      }
    128130      // fill all pending extension points
    129       while (list.Count > 0) {
    130         int randomIndex = random.Next(list.Count);
    131         object[] nextExtension = list[randomIndex];
    132         list.RemoveAt(randomIndex);
     131      while (extensionPoints.Count > 0) {
     132        int randomIndex = random.Next(extensionPoints.Count);
     133        object[] nextExtension = extensionPoints[randomIndex];
     134        extensionPoints.RemoveAt(randomIndex);
    133135        SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0];
    134136        int a = (int)nextExtension[1];
     
    136138        parent.RemoveSubTree(a);
    137139        parent.InsertSubTree(a,
    138           CreateMinimalTree(random, grammar, grammar.AllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height
     140          CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height
    139141      }
    140142      return root;
    141143    }
    142144
    143     private int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) {
     145    private static int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) {
    144146      // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
    145       int minArity = grammar.MinSubTrees(symbol);
    146       int maxArity = grammar.MaxSubTrees(symbol);
     147      int minArity = grammar.GetMinSubTreeCount(symbol);
     148      int maxArity = grammar.GetMaxSubTreeCount(symbol);
    147149      if (maxArity > targetSize) {
    148150        maxArity = targetSize;
     
    152154      long aggregatedLongestExpressionLength = 0;
    153155      for (int i = 0; i < maxArity; i++) {
    154         aggregatedLongestExpressionLength += (from s in grammar.AllowedSymbols(symbol, i)
    155                                               select grammar.MaximalExpressionLength(s)).Max();
     156        aggregatedLongestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i)
     157                                              select grammar.GetMaxExpressionLength(s)).Max();
    156158        if (aggregatedLongestExpressionLength < targetSize) minArity = i;
    157159        else break;
     
    162164      long aggregatedShortestExpressionLength = 0;
    163165      for (int i = 0; i < maxArity; i++) {
    164         aggregatedShortestExpressionLength += (from s in grammar.AllowedSymbols(symbol, i)
    165                                                select grammar.MinimalExpressionLength(s)).Min();
     166        aggregatedShortestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i)
     167                                               select grammar.GetMinExpressionLength(s)).Min();
    166168        if (aggregatedShortestExpressionLength > targetSize) {
    167169          maxArity = i;
     
    173175    }
    174176
    175     private SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) {
     177    private static SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) {
    176178      // determine possible symbols that will lead to the smallest possible tree
    177179      var possibleSymbols = (from s in symbols
    178                              group s by grammar.MinimalExpressionLength(s) into g
     180                             group s by grammar.GetMinExpressionLength(s) into g
    179181                             orderby g.Key
    180182                             select g).First();
     
    182184      // build minimal tree by recursive application
    183185      var tree = selectedSymbol.CreateTreeNode();
    184       for (int i = 0; i < grammar.MinSubTrees(selectedSymbol); i++)
    185         tree.AddSubTree(CreateMinimalTree(random, grammar, grammar.AllowedSymbols(selectedSymbol, i)));
     186      for (int i = 0; i < grammar.GetMinSubTreeCount(selectedSymbol); i++)
     187        tree.AddSubTree(CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(selectedSymbol, i)));
    186188      return tree;
    187189    }
Note: See TracChangeset for help on using the changeset viewer.