Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/10 12:12:29 (15 years ago)
Author:
gkronber
Message:

Changed way the grammar is stored in tree nodes to make it more efficient and fixed bugs in symbolic expression tree operators. #290 (Implement ADFs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs

    r3338 r3369  
    6161      SymbolicExpressionTreeNode crossoverPoint0;
    6262      int replacedSubtreeIndex;
    63       SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, out crossoverPoint0, out replacedSubtreeIndex);
     63      SelectCrossoverPoint(random, parent0, internalCrossoverPointProbability, maxTreeSize - 1, maxTreeHeight - 1, out crossoverPoint0, out replacedSubtreeIndex);
    6464
    6565      // calculate the max size and height that the inserted branch can have
     
    6767      int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
    6868
    69       var allowedBranches = from branch in parent1.Root.IterateNodesPrefix()
    70                             where branch.GetSize() < maxInsertedBranchSize
    71                             where branch.GetHeight() < maxInsertedBranchHeight
    72                             // where crossoverPoint0.GetAllowedSymbols(replacedSubtreeIndex).Contains(branch.Symbol)
    73                             where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch)
    74                             select branch;
     69      var allowedBranches = (from branch in parent1.Root.IterateNodesPrefix()
     70                             where branch.GetSize() < maxInsertedBranchSize
     71                             where branch.GetHeight() < maxInsertedBranchHeight
     72                             where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch)
     73                             select branch).ToList();
    7574
    7675      if (allowedBranches.Count() == 0) {
     
    9089
    9190    private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) {
    92       // cannot compare dynamic symbols by reference because new instances of Invoke/Argument are created on demand
    93       // instead we must check equality by names and number of arguments
    94       var branchSymbols = (from node in branch.IterateNodesPrefix()
    95                            select new { FunctionName = node.Symbol.Name, SubtreeCount = node.SubTrees.Count }).Distinct();
     91      // check point type for the whole branch
     92      foreach (var node in branch.IterateNodesPostfix()) {
     93        if (!parent.Grammar.ContainsSymbol(node.Symbol)) return false;
     94        else if (node.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(node.Symbol)) return false;
     95        else if (node.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(node.Symbol)) return false;
     96      }
    9697
    9798      // check syntax constraints of direct parent - child relation
    98       var matchingSymbolInParentGrammar = (from sym in parent.GetAllowedSymbols(replacedSubtreeIndex)
    99                                            where sym.Name == branch.Symbol.Name
    100                                            select sym).SingleOrDefault();
    101       if (matchingSymbolInParentGrammar == null ||
    102         branch.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) ||
    103         branch.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false;
    104 
    105       // check point type for the whole branch
    106       foreach (var branchSymbol in branchSymbols) {
    107         matchingSymbolInParentGrammar = (from sym in parent.Grammar.Symbols
    108                                          where sym.Name == branchSymbol.FunctionName
    109                                          select sym).SingleOrDefault();
    110         if (matchingSymbolInParentGrammar == null ||
    111           branchSymbol.SubtreeCount < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) ||
    112           branchSymbol.SubtreeCount > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false;
    113       }
     99      if (!parent.Grammar.IsAllowedChild(parent.Symbol, branch.Symbol, replacedSubtreeIndex)) return false;
    114100
    115101      return true;
    116102    }
    117103
    118     private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
     104    private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, int maxBranchSize, int maxBranchHeight, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
    119105      var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix()
    120106                            where branch.SubTrees.Count > 0
    121107                            where branch != parent0.Root
     108                            where branch.GetSize() < maxBranchSize
     109                            where branch.GetHeight() < maxBranchHeight
    122110                            from index in Enumerable.Range(0, branch.SubTrees.Count)
    123111                            let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 }
     
    151139    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<SymbolicExpressionTreeNode> branches, double internalNodeProbability) {
    152140      if (internalNodeProbability < 0.0 || internalNodeProbability > 1.0) throw new ArgumentException("internalNodeProbability");
    153       var groupedBranches = from branch in branches
    154                             group branch by branch.SubTrees.Count into g
    155                             select g;
     141      var groupedBranches = (from branch in branches
     142                             group branch by branch.SubTrees.Count into g
     143                             select g).ToList();
    156144      var allowedInternalBranches = (from g in groupedBranches
    157145                                     where g.Key > 0
     
    163151                                 select leaf).ToList();
    164152      if (allowedInternalBranches.Count == 0) {
    165         return allowedLeafBranches[random.Next(allowedLeafBranches.Count)];
     153        return allowedLeafBranches.SelectRandom(random);
    166154      } else if (allowedLeafBranches.Count == 0) {
    167         return allowedInternalBranches[random.Next(allowedInternalBranches.Count)];
     155        return allowedInternalBranches.SelectRandom(random);
    168156      } else if (random.NextDouble() < internalNodeProbability) {
    169157        // when leaf and internal nodes are possible then choose either a leaf or internal node with internalNodeProbability
    170         return allowedInternalBranches[random.Next(allowedInternalBranches.Count)];
     158        return allowedInternalBranches.SelectRandom(random);
    171159      } else {
    172         return allowedLeafBranches[random.Next(allowedLeafBranches.Count)];
     160        return allowedLeafBranches.SelectRandom(random);
    173161      }
    174162    }
Note: See TracChangeset for help on using the changeset viewer.