[3294] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3294] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[4722] | 22 | using System;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[3294] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[3294] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5510] | 29 | using HeuristicLab.Parameters;
|
---|
[3294] | 30 |
|
---|
[5499] | 31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
[3294] | 32 | /// <summary>
|
---|
| 33 | /// Creates a new argument within one function-defining branch of a symbolic expression tree.
|
---|
| 34 | /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 106
|
---|
| 35 | /// </summary>
|
---|
[5510] | 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")]
|
---|
[3294] | 37 | [StorableClass]
|
---|
[5510] | 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
|
---|
[4722] | 57 | [StorableConstructor]
|
---|
| 58 | private ArgumentCreater(bool deserializing) : base(deserializing) { }
|
---|
| 59 | private ArgumentCreater(ArgumentCreater original, Cloner cloner) : base(original, cloner) { }
|
---|
[5510] | 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 |
|
---|
[3294] | 66 | public override sealed void ModifyArchitecture(
|
---|
| 67 | IRandom random,
|
---|
[5510] | 68 | ISymbolicExpressionTree symbolicExpressionTree,
|
---|
| 69 | IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
|
---|
| 70 | CreateNewArgument(random, symbolicExpressionTree, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
|
---|
[3294] | 71 | }
|
---|
| 72 |
|
---|
[4722] | 73 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 74 | return new ArgumentCreater(this, cloner);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3294] | 77 | public static bool CreateNewArgument(
|
---|
| 78 | IRandom random,
|
---|
[5510] | 79 | ISymbolicExpressionTree symbolicExpressionTree,
|
---|
| 80 | int maxTreeLength, int maxTreeDepth,
|
---|
| 81 | int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
[4524] | 82 | // work on a copy in case we find out later that the tree would be too big
|
---|
| 83 | // in this case it's easiest to simply return the original tree.
|
---|
[5510] | 84 | ISymbolicExpressionTree clonedTree = (ISymbolicExpressionTree)symbolicExpressionTree.Clone();
|
---|
[3294] | 85 |
|
---|
[4524] | 86 | var functionDefiningBranches = clonedTree.IterateNodesPrefix().OfType<DefunTreeNode>();
|
---|
[3294] | 87 | if (functionDefiningBranches.Count() == 0)
|
---|
| 88 | // no function defining branch found => abort
|
---|
| 89 | return false;
|
---|
[3338] | 90 |
|
---|
[3294] | 91 | // select a random function defining branch
|
---|
[3338] | 92 | var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
|
---|
| 93 | var definedArguments = (from symbol in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
|
---|
| 94 | select symbol.ArgumentIndex).Distinct();
|
---|
| 95 | if (definedArguments.Count() >= maxFunctionArguments)
|
---|
| 96 | // max number of arguments reached => abort
|
---|
| 97 | return false;
|
---|
[4524] | 98 |
|
---|
| 99 | var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
|
---|
| 100 | var newArgumentIndex = allowedArgumentIndexes.Except(definedArguments).First();
|
---|
| 101 | ArgumentTreeNode newArgumentNode = MakeArgumentNode(newArgumentIndex);
|
---|
| 102 |
|
---|
[5549] | 103 | // this operation potentially creates very big trees so the access to the length property might throw overflow exception
|
---|
[4524] | 104 | try {
|
---|
[5549] | 105 | if (CreateNewArgumentForDefun(random, clonedTree, selectedDefunBranch, newArgumentNode) && clonedTree.Length <= maxTreeLength && clonedTree.Depth <= maxTreeDepth) {
|
---|
[4524] | 106 |
|
---|
| 107 | // size constraints are fulfilled
|
---|
| 108 | // replace root of original tree with root of manipulated tree
|
---|
| 109 | symbolicExpressionTree.Root = clonedTree.Root;
|
---|
| 110 | return true;
|
---|
| 111 | } else {
|
---|
| 112 | // keep originalTree
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | catch (OverflowException) {
|
---|
| 117 | // keep original tree
|
---|
| 118 | return false;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[5510] | 122 | private static bool CreateNewArgumentForDefun(IRandom random, ISymbolicExpressionTree tree, DefunTreeNode defunBranch, ArgumentTreeNode newArgumentNode) {
|
---|
[3294] | 123 | // select a random cut point in the function defining branch
|
---|
| 124 | // the branch at the cut point is to be replaced by a new argument node
|
---|
[4524] | 125 | var cutPoints = (from node in defunBranch.IterateNodesPrefix()
|
---|
[5510] | 126 | where node.SubTrees.Count() > 0
|
---|
[3294] | 127 | from subtree in node.SubTrees
|
---|
[5510] | 128 | select new { Parent = node, ReplacedChildIndex = node.IndexOfSubTree(subtree), ReplacedChild = subtree }).ToList();
|
---|
[3294] | 129 |
|
---|
| 130 | if (cutPoints.Count() == 0)
|
---|
| 131 | // no cut point found => abort;
|
---|
| 132 | return false;
|
---|
| 133 | var selectedCutPoint = cutPoints[random.Next(cutPoints.Count)];
|
---|
| 134 | // replace the branch at the cut point with an argument node
|
---|
| 135 | var replacedBranch = selectedCutPoint.ReplacedChild;
|
---|
| 136 | selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedChildIndex);
|
---|
[4524] | 137 | selectedCutPoint.Parent.InsertSubTree(selectedCutPoint.ReplacedChildIndex, newArgumentNode);
|
---|
[4106] | 138 |
|
---|
[4524] | 139 | // find all old invocations of the selected ADF and attach a cloned version of the replaced branch (with all argument-nodes expanded)
|
---|
| 140 | // iterate in post-fix order to make sure that the subtrees of n are already adapted when n is processed
|
---|
| 141 | var invocationNodes = (from node in tree.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
|
---|
| 142 | where node.Symbol.FunctionName == defunBranch.FunctionName
|
---|
[5510] | 143 | where node.SubTrees.Count() == defunBranch.NumberOfArguments
|
---|
[4477] | 144 | select node).ToList();
|
---|
[4106] | 145 | // do this repeatedly until no matching invocations are found
|
---|
[4524] | 146 | while (invocationNodes.Count > 0) {
|
---|
[5510] | 147 | List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[4106] | 148 | foreach (var invocationNode in invocationNodes) {
|
---|
[4524] | 149 | // check that the invocation node really has the correct number of arguments
|
---|
[5510] | 150 | if (invocationNode.SubTrees.Count() != defunBranch.NumberOfArguments) throw new InvalidOperationException();
|
---|
[4106] | 151 | // append a new argument branch after expanding all argument nodes
|
---|
[5510] | 152 | var clonedBranch = (ISymbolicExpressionTreeNode)replacedBranch.Clone();
|
---|
[4106] | 153 | clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees);
|
---|
[4524] | 154 | invocationNode.InsertSubTree(newArgumentNode.Symbol.ArgumentIndex, clonedBranch);
|
---|
[4106] | 155 | newlyAddedBranches.Add(clonedBranch);
|
---|
| 156 | }
|
---|
[4524] | 157 | // iterate in post-fix order to make sure that the subtrees of n are already adapted when n is processed
|
---|
[4477] | 158 | invocationNodes = (from newlyAddedBranch in newlyAddedBranches
|
---|
[4524] | 159 | from node in newlyAddedBranch.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
|
---|
| 160 | where node.Symbol.FunctionName == defunBranch.FunctionName
|
---|
[5510] | 161 | where node.SubTrees.Count() == defunBranch.NumberOfArguments
|
---|
[4477] | 162 | select node).ToList();
|
---|
[3294] | 163 | }
|
---|
[3338] | 164 | // increase expected number of arguments of function defining branch
|
---|
| 165 | // it's possible that the number of actually referenced arguments was reduced (all references were replaced by a single new argument)
|
---|
| 166 | // but the number of expected arguments is increased anyway
|
---|
[4524] | 167 | defunBranch.NumberOfArguments++;
|
---|
| 168 | defunBranch.Grammar.AddSymbol(newArgumentNode.Symbol);
|
---|
| 169 | defunBranch.Grammar.SetMinSubtreeCount(newArgumentNode.Symbol, 0);
|
---|
| 170 | defunBranch.Grammar.SetMaxSubtreeCount(newArgumentNode.Symbol, 0);
|
---|
[3338] | 171 | // allow the argument as child of any other symbol
|
---|
[4524] | 172 | foreach (var symb in defunBranch.Grammar.Symbols)
|
---|
| 173 | for (int i = 0; i < defunBranch.Grammar.GetMaxSubtreeCount(symb); i++) {
|
---|
| 174 | defunBranch.Grammar.SetAllowedChild(symb, newArgumentNode.Symbol, i);
|
---|
[3338] | 175 | }
|
---|
[4524] | 176 | foreach (var subtree in tree.Root.SubTrees) {
|
---|
[3338] | 177 | // when the changed function is known in the branch then update the number of arguments
|
---|
[4524] | 178 | var matchingSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == defunBranch.FunctionName).SingleOrDefault();
|
---|
[3338] | 179 | if (matchingSymbol != null) {
|
---|
[4524] | 180 | subtree.Grammar.SetMinSubtreeCount(matchingSymbol, defunBranch.NumberOfArguments);
|
---|
| 181 | subtree.Grammar.SetMaxSubtreeCount(matchingSymbol, defunBranch.NumberOfArguments);
|
---|
[5529] | 182 | foreach (var child in subtree.Grammar.GetAllowedSymbols(subtree.Symbol, 0)) {
|
---|
[3360] | 183 | for (int i = 0; i < subtree.Grammar.GetMaxSubtreeCount(matchingSymbol); i++) {
|
---|
| 184 | subtree.Grammar.SetAllowedChild(matchingSymbol, child, i);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
[3294] | 187 | }
|
---|
| 188 | }
|
---|
[4524] | 189 |
|
---|
[3294] | 190 | return true;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[5510] | 193 | private static ISymbolicExpressionTreeNode ReplaceArgumentsInBranch(ISymbolicExpressionTreeNode branch, IEnumerable<ISymbolicExpressionTreeNode> argumentTrees) {
|
---|
[4524] | 194 | ArgumentTreeNode argNode = branch as ArgumentTreeNode;
|
---|
| 195 | if (argNode != null) {
|
---|
[3360] | 196 | // replace argument nodes by a clone of the original subtree that provided the result for the argument node
|
---|
[5510] | 197 | return (SymbolicExpressionTreeNode)argumentTrees.ElementAt(argNode.Symbol.ArgumentIndex).Clone();
|
---|
[3360] | 198 | } else {
|
---|
| 199 | // call recursively for all subtree
|
---|
[5510] | 200 | List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(branch.SubTrees);
|
---|
| 201 | while (branch.SubTrees.Count() > 0) branch.RemoveSubTree(0);
|
---|
[3360] | 202 | foreach (var subtree in subtrees) {
|
---|
| 203 | branch.AddSubTree(ReplaceArgumentsInBranch(subtree, argumentTrees));
|
---|
[3294] | 204 | }
|
---|
[3360] | 205 | return branch;
|
---|
[3294] | 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[4524] | 209 | private static ArgumentTreeNode MakeArgumentNode(int argIndex) {
|
---|
[3338] | 210 | var node = (ArgumentTreeNode)(new Argument(argIndex)).CreateTreeNode();
|
---|
[3294] | 211 | return node;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | }
|
---|