[3294] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[4068] | 25 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
|
---|
[3294] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4106] | 27 | using System.Collections.Generic;
|
---|
[4524] | 28 | using System;
|
---|
[3294] | 29 |
|
---|
[3539] | 30 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators {
|
---|
[3294] | 31 | /// <summary>
|
---|
| 32 | /// Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch.
|
---|
| 33 | /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 94
|
---|
| 34 | /// </summary>
|
---|
| 35 | [Item("ArgumentDuplicater", "Manipulates a symbolic expression by duplicating an existing argument node of a function-defining branch.")]
|
---|
| 36 | [StorableClass]
|
---|
[3534] | 37 | public sealed class ArgumentDuplicater : SymbolicExpressionTreeArchitectureManipulator {
|
---|
[3294] | 38 | public override sealed void ModifyArchitecture(
|
---|
| 39 | IRandom random,
|
---|
| 40 | SymbolicExpressionTree symbolicExpressionTree,
|
---|
| 41 | ISymbolicExpressionGrammar grammar,
|
---|
| 42 | IntValue maxTreeSize, IntValue maxTreeHeight,
|
---|
| 43 | IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
|
---|
| 44 | out bool success) {
|
---|
| 45 | success = DuplicateArgument(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public static bool DuplicateArgument(
|
---|
| 49 | IRandom random,
|
---|
| 50 | SymbolicExpressionTree symbolicExpressionTree,
|
---|
| 51 | ISymbolicExpressionGrammar grammar,
|
---|
| 52 | int maxTreeSize, int maxTreeHeight,
|
---|
| 53 | int maxFunctionDefiningBranches, int maxFunctionArguments) {
|
---|
| 54 | var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
|
---|
| 55 |
|
---|
| 56 | var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
|
---|
| 57 | if (functionDefiningBranches.Count() == 0)
|
---|
| 58 | // no function defining branches => abort
|
---|
| 59 | return false;
|
---|
| 60 |
|
---|
[3360] | 61 | var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
|
---|
| 62 | var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>();
|
---|
| 63 | if (argumentSymbols.Count() == 0 || argumentSymbols.Count() >= maxFunctionArguments)
|
---|
[3294] | 64 | // when no argument or number of arguments is already at max allowed value => abort
|
---|
| 65 | return false;
|
---|
[3360] | 66 | var selectedArgumentSymbol = argumentSymbols.SelectRandom(random);
|
---|
| 67 | var takenIndexes = argumentSymbols.Select(s => s.ArgumentIndex);
|
---|
| 68 | var newArgumentIndex = allowedArgumentIndexes.Except(takenIndexes).First();
|
---|
| 69 |
|
---|
| 70 | var newArgSymbol = new Argument(newArgumentIndex);
|
---|
| 71 |
|
---|
[3294] | 72 | // replace existing references to the original argument with references to the new argument randomly in the selectedBranch
|
---|
[3360] | 73 | var argumentNodes = selectedDefunBranch.IterateNodesPrefix().OfType<ArgumentTreeNode>();
|
---|
[3294] | 74 | foreach (var argNode in argumentNodes) {
|
---|
[3360] | 75 | if (argNode.Symbol == selectedArgumentSymbol) {
|
---|
[3294] | 76 | if (random.NextDouble() < 0.5) {
|
---|
[3360] | 77 | argNode.Symbol = newArgSymbol;
|
---|
[3294] | 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | // find invocations of the functions and duplicate the matching argument branch
|
---|
[4477] | 82 | var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
|
---|
| 83 | where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 84 | where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments
|
---|
| 85 | select node).ToList();
|
---|
[4106] | 86 | // do this repeatedly until no matching invocations are found
|
---|
| 87 | while (invocationNodes.Count() > 0) {
|
---|
| 88 | List<SymbolicExpressionTreeNode> newlyAddedBranches = new List<SymbolicExpressionTreeNode>();
|
---|
| 89 | foreach (var invokeNode in invocationNodes) {
|
---|
[4524] | 90 | // check that the invocation node really has the correct number of arguments
|
---|
| 91 | if (invokeNode.SubTrees.Count != selectedDefunBranch.NumberOfArguments) throw new InvalidOperationException();
|
---|
[4106] | 92 | var argumentBranch = invokeNode.SubTrees[selectedArgumentSymbol.ArgumentIndex];
|
---|
| 93 | var clonedArgumentBranch = (SymbolicExpressionTreeNode)argumentBranch.Clone();
|
---|
| 94 | invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch);
|
---|
| 95 | newlyAddedBranches.Add(clonedArgumentBranch);
|
---|
| 96 | }
|
---|
[4477] | 97 | invocationNodes = (from newlyAddedBranch in newlyAddedBranches
|
---|
| 98 | from node in newlyAddedBranch.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
|
---|
| 99 | where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 100 | where node.SubTrees.Count == selectedDefunBranch.NumberOfArguments
|
---|
| 101 | select node).ToList();
|
---|
[3294] | 102 | }
|
---|
[3360] | 103 | // register the new argument symbol and increase the number of arguments of the ADF
|
---|
| 104 | selectedDefunBranch.Grammar.AddSymbol(newArgSymbol);
|
---|
| 105 | selectedDefunBranch.Grammar.SetMinSubtreeCount(newArgSymbol, 0);
|
---|
| 106 | selectedDefunBranch.Grammar.SetMaxSubtreeCount(newArgSymbol, 0);
|
---|
| 107 | // allow the argument as child of any other symbol
|
---|
| 108 | foreach (var symb in selectedDefunBranch.Grammar.Symbols)
|
---|
| 109 | for (int i = 0; i < selectedDefunBranch.Grammar.GetMaxSubtreeCount(symb); i++) {
|
---|
| 110 | selectedDefunBranch.Grammar.SetAllowedChild(symb, newArgSymbol, i);
|
---|
| 111 | }
|
---|
[3294] | 112 | selectedDefunBranch.NumberOfArguments++;
|
---|
[3360] | 113 |
|
---|
[3294] | 114 | // increase the arity of the changed ADF in all branches that can use this ADF
|
---|
| 115 | foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
|
---|
[3360] | 116 | var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
|
---|
| 117 | where symb.FunctionName == selectedDefunBranch.FunctionName
|
---|
| 118 | select symb).SingleOrDefault();
|
---|
| 119 | if (matchingInvokeSymbol != null) {
|
---|
| 120 | subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
| 121 | subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
|
---|
| 122 | foreach (var child in subtree.GetAllowedSymbols(0)) {
|
---|
| 123 | for (int i = 0; i < subtree.Grammar.GetMaxSubtreeCount(matchingInvokeSymbol); i++) {
|
---|
| 124 | subtree.Grammar.SetAllowedChild(matchingInvokeSymbol, child, i);
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
[3294] | 127 | }
|
---|
| 128 | }
|
---|
| 129 | return true;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|