Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/11 13:51:04 (13 years ago)
Author:
gkronber
Message:

#1418 Fixed compiler errors in symbolic expression tree encoding

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/ArgumentCreater.cs

    r5499 r5510  
    2727using HeuristicLab.Data;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.Parameters;
    2930
    3031namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    3334  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 106
    3435  /// </summary>
    35   [Item("ArgumentCreater", "Manipulates a symbolic expression by creating a new argument within one function-defining branch.")]
     36  [Item("ArgumentCreater", "Manipulates a symbolic expression by creating a new argument within one function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 106")]
    3637  [StorableClass]
    37   public sealed class ArgumentCreater : SymbolicExpressionTreeArchitectureManipulator {
     38  public sealed class ArgumentCreater : SymbolicExpressionTreeArchitectureManipulator, ISymbolicExpressionTreeSizeConstraintOperator {
     39    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     40    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     41    #region Parameter Properties
     42    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     43      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
     44    }
     45    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
     46      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     47    }
     48    #endregion
     49    #region Properties
     50    public IntValue MaximumSymbolicExpressionTreeLength {
     51      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
     52    }
     53    public IntValue MaximumSymbolicExpressionTreeDepth {
     54      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
     55    }
     56    #endregion
    3857    [StorableConstructor]
    3958    private ArgumentCreater(bool deserializing) : base(deserializing) { }
    4059    private ArgumentCreater(ArgumentCreater original, Cloner cloner) : base(original, cloner) { }
    41     public ArgumentCreater() : base() { }
     60    public ArgumentCreater()
     61      : base() {
     62      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
     63      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     64    }
     65
    4266    public override sealed void ModifyArchitecture(
    4367      IRandom random,
    44       SymbolicExpressionTree symbolicExpressionTree,
    45       ISymbolicExpressionGrammar grammar,
    46       IntValue maxTreeSize, IntValue maxTreeHeight,
    47       IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
    48       out bool success) {
    49       success = CreateNewArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
     68      ISymbolicExpressionTree symbolicExpressionTree,
     69      IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
     70      CreateNewArgument(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
    5071    }
    5172
     
    5677    public static bool CreateNewArgument(
    5778      IRandom random,
    58       SymbolicExpressionTree symbolicExpressionTree,
    59       ISymbolicExpressionGrammar grammar,
    60       int maxTreeSize, int maxTreeHeight,
    61       int maxFunctionDefiningBranches, int maxFunctionArguments) {
     79      ISymbolicExpressionTree symbolicExpressionTree,
     80      int maxTreeLength, int maxTreeDepth,
     81      int maxFunctionDefinitions, int maxFunctionArguments) {
    6282      // work on a copy in case we find out later that the tree would be too big
    6383      // in this case it's easiest to simply return the original tree.
    64       SymbolicExpressionTree clonedTree = (SymbolicExpressionTree)symbolicExpressionTree.Clone();
     84      ISymbolicExpressionTree clonedTree = (ISymbolicExpressionTree)symbolicExpressionTree.Clone();
    6585
    6686      var functionDefiningBranches = clonedTree.IterateNodesPrefix().OfType<DefunTreeNode>();
     
    83103      // this operation potentially creates very big trees so the access to the size property might throw overflow exception
    84104      try {
    85         if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Size <= maxTreeSize && clonedTree.Height <= maxTreeHeight) {
     105        if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Size <= maxTreeLength && clonedTree.Height <= maxTreeDepth) {
    86106
    87107          // size constraints are fulfilled
     
    100120    }
    101121
    102     private static bool CreateNewArgumentForDefun(IRandom random, SymbolicExpressionTree tree, DefunTreeNode defunBranch, ArgumentTreeNode newArgumentNode) {
     122    private static bool CreateNewArgumentForDefun(IRandom random, ISymbolicExpressionTree tree, DefunTreeNode defunBranch, ArgumentTreeNode newArgumentNode) {
    103123      // select a random cut point in the function defining branch
    104124      // the branch at the cut point is to be replaced by a new argument node
    105125      var cutPoints = (from node in defunBranch.IterateNodesPrefix()
    106                        where node.SubTrees.Count > 0
     126                       where node.SubTrees.Count() > 0
    107127                       from subtree in node.SubTrees
    108                        select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).ToList();
     128                       select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).ToList();
    109129
    110130      if (cutPoints.Count() == 0)
     
    121141      var invocationNodes = (from node in tree.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
    122142                             where node.Symbol.FunctionName == defunBranch.FunctionName
    123                              where node.SubTrees.Count == defunBranch.NumberOfArguments
     143                             where node.SubTrees.Count() == defunBranch.NumberOfArguments
    124144                             select node).ToList();
    125145      // do this repeatedly until no matching invocations are found     
    126146      while (invocationNodes.Count > 0) {
    127         List<SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>();
     147        List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>();
    128148        foreach (var invocationNode in invocationNodes) {
    129149          // check that the invocation node really has the correct number of arguments
    130           if (invocationNode.SubTrees.Count != defunBranch.NumberOfArguments) throw new InvalidOperationException();
     150          if (invocationNode.SubTrees.Count() != defunBranch.NumberOfArguments) throw new InvalidOperationException();
    131151          // append a new argument branch after expanding all argument nodes
    132           var clonedBranch = (SymbolicExpressionTreeNode)replacedBranch.Clone();
     152          var clonedBranch = (ISymbolicExpressionTreeNode)replacedBranch.Clone();
    133153          clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees);
    134154          invocationNode.InsertSubTree(newArgumentNode.Symbol.ArgumentIndex, clonedBranch);
     
    139159                           from node in newlyAddedBranch.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
    140160                           where node.Symbol.FunctionName == defunBranch.FunctionName
    141                            where node.SubTrees.Count == defunBranch.NumberOfArguments
     161                           where node.SubTrees.Count() == defunBranch.NumberOfArguments
    142162                           select node).ToList();
    143163      }
     
    171191    }
    172192
    173     private static SymbolicExpressionTreeNode ReplaceArgumentsInBranch(SymbolicExpressionTreeNode branch, IList<SymbolicExpressionTreeNode> argumentTrees) {
     193    private static ISymbolicExpressionTreeNode ReplaceArgumentsInBranch(ISymbolicExpressionTreeNode branch, IEnumerable<ISymbolicExpressionTreeNode> argumentTrees) {
    174194      ArgumentTreeNode argNode = branch as ArgumentTreeNode;
    175195      if (argNode != null) {
    176196        // replace argument nodes by a clone of the original subtree that provided the result for the argument node
    177         return (SymbolicExpressionTreeNode)argumentTrees[argNode.Symbol.ArgumentIndex].Clone();
     197        return (SymbolicExpressionTreeNode)argumentTrees.ElementAt(argNode.Symbol.ArgumentIndex).Clone();
    178198      } else {
    179199        // call recursively for all subtree
    180         List<SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(branch.SubTrees);
    181         while (branch.SubTrees.Count > 0) branch.RemoveSubTree(0);
     200        List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(branch.SubTrees);
     201        while (branch.SubTrees.Count() > 0) branch.RemoveSubTree(0);
    182202        foreach (var subtree in subtrees) {
    183203          branch.AddSubTree(ReplaceArgumentsInBranch(subtree, argumentTrees));
Note: See TracChangeset for help on using the changeset viewer.