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