[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;
|
---|
[5686] | 28 | using HeuristicLab.Parameters;
|
---|
[3294] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 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()
|
---|
[5792] | 126 | where node.Subtrees.Count() > 0 &&
|
---|
| 127 | !node.IterateNodesPrefix().OfType<ArgumentTreeNode>().Any() &&
|
---|
| 128 | !node.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>().Any()
|
---|
[5733] | 129 | from subtree in node.Subtrees
|
---|
[5686] | 130 | select new CutPoint(node, subtree)).ToList();
|
---|
[3294] | 131 |
|
---|
| 132 | if (cutPoints.Count() == 0)
|
---|
| 133 | // no cut point found => abort;
|
---|
| 134 | return false;
|
---|
| 135 | var selectedCutPoint = cutPoints[random.Next(cutPoints.Count)];
|
---|
| 136 | // replace the branch at the cut point with an argument node
|
---|
[5686] | 137 | var replacedBranch = selectedCutPoint.Child;
|
---|
[5733] | 138 | selectedCutPoint.Parent.RemoveSubtree(selectedCutPoint.ChildIndex);
|
---|
| 139 | selectedCutPoint.Parent.InsertSubtree(selectedCutPoint.ChildIndex, newArgumentNode);
|
---|
[4106] | 140 |
|
---|
[4524] | 141 | // find all old invocations of the selected ADF and attach a cloned version of the replaced branch (with all argument-nodes expanded)
|
---|
| 142 | // iterate in post-fix order to make sure that the subtrees of n are already adapted when n is processed
|
---|
| 143 | var invocationNodes = (from node in tree.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
|
---|
| 144 | where node.Symbol.FunctionName == defunBranch.FunctionName
|
---|
[5733] | 145 | where node.Subtrees.Count() == defunBranch.NumberOfArguments
|
---|
[4477] | 146 | select node).ToList();
|
---|
[4106] | 147 | // do this repeatedly until no matching invocations are found
|
---|
[4524] | 148 | while (invocationNodes.Count > 0) {
|
---|
[5510] | 149 | List<ISymbolicExpressionTreeNode> newlyAddedBranches = new List<ISymbolicExpressionTreeNode>();
|
---|
[4106] | 150 | foreach (var invocationNode in invocationNodes) {
|
---|
[4524] | 151 | // check that the invocation node really has the correct number of arguments
|
---|
[5733] | 152 | if (invocationNode.Subtrees.Count() != defunBranch.NumberOfArguments) throw new InvalidOperationException();
|
---|
[4106] | 153 | // append a new argument branch after expanding all argument nodes
|
---|
[5510] | 154 | var clonedBranch = (ISymbolicExpressionTreeNode)replacedBranch.Clone();
|
---|
[5733] | 155 | clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.Subtrees);
|
---|
| 156 | invocationNode.InsertSubtree(newArgumentNode.Symbol.ArgumentIndex, clonedBranch);
|
---|
[4106] | 157 | newlyAddedBranches.Add(clonedBranch);
|
---|
| 158 | }
|
---|
[4524] | 159 | // iterate in post-fix order to make sure that the subtrees of n are already adapted when n is processed
|
---|
[4477] | 160 | invocationNodes = (from newlyAddedBranch in newlyAddedBranches
|
---|
[4524] | 161 | from node in newlyAddedBranch.IterateNodesPostfix().OfType<InvokeFunctionTreeNode>()
|
---|
| 162 | where node.Symbol.FunctionName == defunBranch.FunctionName
|
---|
[5733] | 163 | where node.Subtrees.Count() == defunBranch.NumberOfArguments
|
---|
[4477] | 164 | select node).ToList();
|
---|
[3294] | 165 | }
|
---|
[3338] | 166 | // increase expected number of arguments of function defining branch
|
---|
| 167 | // it's possible that the number of actually referenced arguments was reduced (all references were replaced by a single new argument)
|
---|
| 168 | // but the number of expected arguments is increased anyway
|
---|
[4524] | 169 | defunBranch.NumberOfArguments++;
|
---|
| 170 | defunBranch.Grammar.AddSymbol(newArgumentNode.Symbol);
|
---|
[5686] | 171 | defunBranch.Grammar.SetSubtreeCount(newArgumentNode.Symbol, 0, 0);
|
---|
[3338] | 172 | // allow the argument as child of any other symbol
|
---|
[5792] | 173 | GrammarModifier.SetAllowedParentSymbols(defunBranch.Grammar, selectedCutPoint.Child.Symbol, newArgumentNode.Symbol);
|
---|
| 174 |
|
---|
[5733] | 175 | foreach (var subtree in tree.Root.Subtrees) {
|
---|
[3338] | 176 | // when the changed function is known in the branch then update the number of arguments
|
---|
[4524] | 177 | var matchingSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == defunBranch.FunctionName).SingleOrDefault();
|
---|
[3338] | 178 | if (matchingSymbol != null) {
|
---|
[5686] | 179 | subtree.Grammar.SetSubtreeCount(matchingSymbol, defunBranch.NumberOfArguments, defunBranch.NumberOfArguments);
|
---|
[5792] | 180 | foreach (var symb in subtree.Grammar.Symbols) {
|
---|
| 181 | if (symb is StartSymbol || symb is ProgramRootSymbol) continue;
|
---|
[6918] | 182 | if (symb.Name == matchingSymbol.Name) continue; //don't allow invoke as child of invoke
|
---|
[5792] | 183 | if (subtree.Grammar.IsAllowedChildSymbol(selectedCutPoint.Parent.Symbol, symb, selectedCutPoint.ChildIndex))
|
---|
| 184 | subtree.Grammar.AddAllowedChildSymbol(matchingSymbol, symb, newArgumentNode.Symbol.ArgumentIndex);
|
---|
[3360] | 185 | }
|
---|
[3294] | 186 | }
|
---|
| 187 | }
|
---|
[4524] | 188 |
|
---|
[3294] | 189 | return true;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[5510] | 192 | private static ISymbolicExpressionTreeNode ReplaceArgumentsInBranch(ISymbolicExpressionTreeNode branch, IEnumerable<ISymbolicExpressionTreeNode> argumentTrees) {
|
---|
[4524] | 193 | ArgumentTreeNode argNode = branch as ArgumentTreeNode;
|
---|
| 194 | if (argNode != null) {
|
---|
[3360] | 195 | // replace argument nodes by a clone of the original subtree that provided the result for the argument node
|
---|
[5510] | 196 | return (SymbolicExpressionTreeNode)argumentTrees.ElementAt(argNode.Symbol.ArgumentIndex).Clone();
|
---|
[3360] | 197 | } else {
|
---|
| 198 | // call recursively for all subtree
|
---|
[5733] | 199 | List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(branch.Subtrees);
|
---|
| 200 | while (branch.Subtrees.Count() > 0) branch.RemoveSubtree(0);
|
---|
[3360] | 201 | foreach (var subtree in subtrees) {
|
---|
[5733] | 202 | branch.AddSubtree(ReplaceArgumentsInBranch(subtree, argumentTrees));
|
---|
[3294] | 203 | }
|
---|
[3360] | 204 | return branch;
|
---|
[3294] | 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[4524] | 208 | private static ArgumentTreeNode MakeArgumentNode(int argIndex) {
|
---|
[3338] | 209 | var node = (ArgumentTreeNode)(new Argument(argIndex)).CreateTreeNode();
|
---|
[3294] | 210 | return node;
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|