Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/13/10 20:44:31 (15 years ago)
Author:
gkronber
Message:

Fixed bugs related to dynamic symbol constraints with ADFs. #290 (Implement ADFs)

File:
1 edited

Legend:

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

    r3297 r3338  
    4949    }
    5050
    51     protected override SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
     51    protected override SymbolicExpressionTree Cross(IRandom random,
    5252      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
    5353      IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
    54       return Cross(random, grammar, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
     54      return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
    5555    }
    5656
    57     public static SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
     57    public static SymbolicExpressionTree Cross(IRandom random,
    5858      SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
    5959      double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) {
     
    6767      int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
    6868
    69       var allowedBranches = from branch in IterateNodes(parent1.Root)
     69      var allowedBranches = from branch in parent1.Root.IterateNodesPrefix()
    7070                            where branch.GetSize() < maxInsertedBranchSize
    7171                            where branch.GetHeight() < maxInsertedBranchHeight
    72                             where grammar.GetAllowedSymbols(crossoverPoint0.Symbol, replacedSubtreeIndex).Contains(branch.Symbol)
    73                             where IsMatchingPointType(parent0, crossoverPoint0, branch)
     72                            // where crossoverPoint0.GetAllowedSymbols(replacedSubtreeIndex).Contains(branch.Symbol)
     73                            where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch)
    7474                            select branch;
    7575
     
    8989    }
    9090
    91     private static bool IsMatchingPointType(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent, SymbolicExpressionTreeNode newBranch) {
    92       var functionCalls = (from node in IterateNodes(newBranch)
    93                            let invokeNode = node as InvokeFunctionTreeNode
    94                            let argNode = node as ArgumentTreeNode
    95                            where invokeNode != null || argNode != null
    96                            let name = invokeNode != null ? invokeNode.InvokedFunctionName : "ARG" + argNode.ArgumentIndex
    97                            let argCount = invokeNode != null ? invokeNode.SubTrees.Count : 0
    98                            select new { FunctionName = name, FunctionArgumentCount = argCount }).Distinct();
    99       var definingBranch = GetDefiningBranch(tree, parent);
    100       if (definingBranch == null) return false;
    101       foreach (var functionCall in functionCalls) {
    102         if (!definingBranch.DynamicSymbols.Contains(functionCall.FunctionName) ||
    103           definingBranch.GetDynamicSymbolArgumentCount(functionCall.FunctionName) != functionCall.FunctionArgumentCount)
    104           return false;
     91    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();
     96
     97      // 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;
    105113      }
     114
    106115      return true;
    107116    }
    108117
    109     private static SymbolicExpressionTreeNode GetDefiningBranch(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent) {
    110       foreach (var treeNode in tree.Root.SubTrees) {
    111         if (IterateNodes(treeNode).Contains(parent)) return treeNode;
    112       }
    113       return null;
    114     }
    115 
    116118    private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
    117       var crossoverPoints = from branch in IterateNodes(parent0.Root)
     119      var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix()
    118120                            where branch.SubTrees.Count > 0
    119                             where !(branch.Symbol is ProgramRootSymbol)
     121                            where branch != parent0.Root
    120122                            from index in Enumerable.Range(0, branch.SubTrees.Count)
    121123                            let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 }
     
    172174    }
    173175
    174     private static IEnumerable<SymbolicExpressionTreeNode> IterateNodes(SymbolicExpressionTreeNode root) {
    175       foreach (var subTree in root.SubTrees) {
    176         foreach (var branch in IterateNodes(subTree)) {
    177           yield return branch;
    178         }
    179       }
    180       yield return root;
    181     }
    182 
    183176    private static int GetBranchLevel(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode point) {
    184177      if (root == point) return 0;
Note: See TracChangeset for help on using the changeset viewer.