Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/11 13:51:04 (14 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/SubroutineCreater.cs

    r5499 r5510  
    2828using HeuristicLab.Data;
    2929using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.Parameters;
    3031
    3132namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
     
    3536  /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 97
    3637  /// </summary>
    37   [Item("SubroutineCreater", "Manipulates a symbolic expression by adding one new function-defining branch containing a proportion of a preexisting branch and by creating a reference to the new branch.")]
     38  [Item("SubroutineCreater", "Manipulates a symbolic expression by adding one new function-defining branch containing a proportion of a preexisting branch and by creating a reference to the new branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 97")]
    3839  [StorableClass]
    39   public sealed class SubroutineCreater : SymbolicExpressionTreeArchitectureManipulator {
     40  public sealed class SubroutineCreater : SymbolicExpressionTreeArchitectureManipulator, ISymbolicExpressionTreeSizeConstraintOperator {
    4041    private const double ARGUMENT_CUTOFF_PROBABILITY = 0.05;
    41 
     42    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
     43    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
     44    #region Parameter Properties
     45    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
     46      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
     47    }
     48    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
     49      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
     50    }
     51    #endregion
     52    #region Properties
     53    public IntValue MaximumSymbolicExpressionTreeLength {
     54      get { return MaximumSymbolicExpressionTreeLengthParameter.ActualValue; }
     55    }
     56    public IntValue MaximumSymbolicExpressionTreeDepth {
     57      get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; }
     58    }
     59    #endregion
    4260    [StorableConstructor]
    4361    private SubroutineCreater(bool deserializing) : base(deserializing) { }
    4462    private SubroutineCreater(SubroutineCreater original, Cloner cloner) : base(original, cloner) { }
    45     public SubroutineCreater() : base() { }
     63    public SubroutineCreater() : base() {
     64      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, "The maximal length (number of nodes) of the symbolic expression tree."));
     65      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
     66    }
    4667
    4768    public override IDeepCloneable Clone(Cloner cloner) {
     
    5172    public override sealed void ModifyArchitecture(
    5273      IRandom random,
    53       SymbolicExpressionTree symbolicExpressionTree,
    54       ISymbolicExpressionGrammar grammar,
    55       IntValue maxTreeSize, IntValue maxTreeHeight,
    56       IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
    57       out bool success) {
    58       success = CreateSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
     74      ISymbolicExpressionTree symbolicExpressionTree,
     75      IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
     76      CreateSubroutine(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
    5977    }
    6078
    6179    public static bool CreateSubroutine(
    6280      IRandom random,
    63       SymbolicExpressionTree symbolicExpressionTree,
    64       ISymbolicExpressionGrammar grammar,
    65       int maxTreeSize, int maxTreeHeight,
    66       int maxFunctionDefiningBranches, int maxFunctionArguments) {
     81      ISymbolicExpressionTree symbolicExpressionTree,
     82      int maxTreeLength, int maxTreeDepth,
     83      int maxFunctionDefinitions, int maxFunctionArguments) {
    6784      var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
    68       if (functionDefiningBranches.Count() >= maxFunctionDefiningBranches)
     85      if (functionDefiningBranches.Count() >= maxFunctionDefinitions)
    6986        // allowed maximum number of ADF reached => abort
    7087        return false;
    71       if (symbolicExpressionTree.Size + 4 > maxTreeSize)
     88      if (symbolicExpressionTree.Size + 4 > maxTreeLength)
    7289        // defining a new function causes an size increase by 4 nodes (max) if the max tree size is reached => abort
    7390        return false;
    74       string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefiningBranches * 10 - 1)).ToString(); // >= 100 functions => ###
    75       var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefiningBranches)
     91      string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ###
     92      var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
    7693                                 select "ADF" + index.ToString(formatString);
    7794
     
    8299      int r = random.Next(totalNumberOfBodyNodes);
    83100      int aggregatedNumberOfBodyNodes = 0;
    84       SymbolicExpressionTreeNode selectedBody = null;
     101      ISymbolicExpressionTreeNode selectedBody = null;
    85102      foreach (var body in bodies) {
    86103        aggregatedNumberOfBodyNodes += body.Size;
     
    94111      var allCutPoints = from parent in selectedBody.IterateNodesPrefix()
    95112                         from subtree in parent.SubTrees
    96                          select new { Parent = parent, ReplacedBranchIndex = parent.SubTrees.IndexOf(subtree), ReplacedBranch = subtree };
     113                         select new { Parent = parent, ReplacedBranchIndex = parent.IndexOfSubTree(subtree), ReplacedBranch = subtree };
    97114      if (allCutPoints.Count() == 0)
    98115        // no cut points => abort
     
    101118      var selectedCutPoint = allCutPoints.SelectRandom(random);
    102119      // select random branches as argument cut-off points (replaced by argument terminal nodes in the function)
    103       List<SymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);
    104       SymbolicExpressionTreeNode functionBody = selectedCutPoint.ReplacedBranch;
     120      List<ISymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);
     121      ISymbolicExpressionTreeNode functionBody = selectedCutPoint.ReplacedBranch;
    105122      // disconnect the function body from the tree
    106123      selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedBranchIndex);
     
    120137      symbolicExpressionTree.Root.AddSubTree(defunNode);
    121138      // the grammar in the newly defined function is a clone of the grammar of the originating branch
    122       defunNode.SetGrammar((ISymbolicExpressionGrammar)selectedBody.Grammar.Clone());
     139      defunNode.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBody.Grammar.Clone());
    123140      // remove all argument symbols from grammar
    124141      var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList();
     
    164181    }
    165182
    166     private static SymbolicExpressionTreeNode DisconnectBranches(SymbolicExpressionTreeNode node, List<SymbolicExpressionTreeNode> argumentBranches) {
     183    private static ISymbolicExpressionTreeNode DisconnectBranches(ISymbolicExpressionTreeNode node, List<ISymbolicExpressionTreeNode> argumentBranches) {
    167184      if (argumentBranches.Contains(node)) {
    168185        var argumentIndex = argumentBranches.IndexOf(node);
     
    171188      }
    172189      // remove the subtrees so that we can clone only the root node
    173       List<SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(node.SubTrees);
    174       while (node.SubTrees.Count > 0) node.RemoveSubTree(0);
     190      List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.SubTrees);
     191      while (node.SubTrees.Count() > 0) node.RemoveSubTree(0);
    175192      // recursively apply function for subtrees or append a argument terminal node
    176193      foreach (var subtree in subtrees) {
     
    180197    }
    181198
    182     private static List<SymbolicExpressionTreeNode> SelectRandomArgumentBranches(SymbolicExpressionTreeNode selectedRoot,
     199    private static List<ISymbolicExpressionTreeNode> SelectRandomArgumentBranches(ISymbolicExpressionTreeNode selectedRoot,
    183200      IRandom random,
    184201      double cutProbability,
     
    186203      // breadth first determination of argument cut-off points
    187204      // we must make sure that we cut off all original argument nodes and that the number of new argument is smaller than the limit
    188       List<SymbolicExpressionTreeNode> argumentBranches = new List<SymbolicExpressionTreeNode>();
     205      List<ISymbolicExpressionTreeNode> argumentBranches = new List<ISymbolicExpressionTreeNode>();
    189206      if (selectedRoot is ArgumentTreeNode) {
    190207        argumentBranches.Add(selectedRoot);
     
    202219        }
    203220        // cut-off in the sub-trees in random order
    204         var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count)
     221        var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count())
    205222                             select new { Index = index, OrderValue = random.NextDouble() }).OrderBy(x => x.OrderValue).Select(x => x.Index);
    206223        foreach (var subtreeIndex in randomIndexes) {
    207           var subtree = selectedRoot.SubTrees[subtreeIndex];
     224          var subtree = selectedRoot.GetSubTree(subtreeIndex);
    208225          minNewArgumentsForSubtrees[subtreeIndex] = 0;
    209226          // => cut-off at 0..n points somewhere in the current sub-tree
Note: See TracChangeset for help on using the changeset viewer.