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 System.Text;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
32 | /// <summary>
|
---|
33 | /// Manipulates a symbolic expression by duplicating a preexisting function-defining branch.
|
---|
34 | /// As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 88
|
---|
35 | /// </summary>
|
---|
36 | [Item("SubroutineDuplicater", "Manipulates a symbolic expression by duplicating a preexisting function-defining branch. As described in Koza, Bennett, Andre, Keane, Genetic Programming III - Darwinian Invention and Problem Solving, 1999, pp. 88")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class SubroutineDuplicater : SymbolicExpressionTreeArchitectureManipulator {
|
---|
39 | [StorableConstructor]
|
---|
40 | private SubroutineDuplicater(bool deserializing) : base(deserializing) { }
|
---|
41 | private SubroutineDuplicater(SubroutineDuplicater original, Cloner cloner)
|
---|
42 | : base(original, cloner) {
|
---|
43 | }
|
---|
44 | public SubroutineDuplicater() : base() { }
|
---|
45 |
|
---|
46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
47 | return new SubroutineDuplicater(this, cloner);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override sealed void ModifyArchitecture(
|
---|
51 | IRandom random,
|
---|
52 | ISymbolicExpressionTree symbolicExpressionTree,
|
---|
53 | IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
|
---|
54 | DuplicateSubroutine(random, symbolicExpressionTree, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public static bool DuplicateSubroutine(
|
---|
58 | IRandom random,
|
---|
59 | ISymbolicExpressionTree symbolicExpressionTree,
|
---|
60 | int maxFunctionDefinitions, int maxFunctionArguments) {
|
---|
61 | var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
|
---|
62 | if (functionDefiningBranches.Count() == 0 || functionDefiningBranches.Count() == maxFunctionDefinitions)
|
---|
63 | // no function defining branches to duplicate or already reached the max number of ADFs
|
---|
64 | return false;
|
---|
65 |
|
---|
66 | string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions) + 1).ToString(); // >= 100 functions => ###
|
---|
67 | var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
|
---|
68 | select "ADF" + index.ToString(formatString);
|
---|
69 | var selectedBranch = functionDefiningBranches.SelectRandom(random);
|
---|
70 | var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone();
|
---|
71 | string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
|
---|
72 | duplicatedDefunBranch.FunctionName = newFunctionName;
|
---|
73 | symbolicExpressionTree.Root.AddSubTree(duplicatedDefunBranch);
|
---|
74 | duplicatedDefunBranch.SetGrammar((ISymbolicExpressionTreeGrammar)selectedBranch.Grammar.Clone());
|
---|
75 | // add an invoke symbol for each branch that is allowed to invoke the original function
|
---|
76 | foreach (var subtree in symbolicExpressionTree.Root.SubTrees.OfType<SymbolicExpressionTreeTopLevelNode>()) {
|
---|
77 | var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
|
---|
78 | where symb.FunctionName == selectedBranch.FunctionName
|
---|
79 | select symb).SingleOrDefault();
|
---|
80 | if (matchingInvokeSymbol != null) {
|
---|
81 | GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, duplicatedDefunBranch.FunctionName, duplicatedDefunBranch.NumberOfArguments);
|
---|
82 | }
|
---|
83 | // in the current subtree:
|
---|
84 | // for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly
|
---|
85 | var originalFunctionInvocations = from node in subtree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
|
---|
86 | where node.Symbol.FunctionName == selectedBranch.FunctionName
|
---|
87 | select node;
|
---|
88 | foreach (var originalFunctionInvokeNode in originalFunctionInvocations) {
|
---|
89 | var newInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
|
---|
90 | where symb.FunctionName == duplicatedDefunBranch.FunctionName
|
---|
91 | select symb).Single();
|
---|
92 | // flip coin wether to replace with newly defined function
|
---|
93 | if (random.NextDouble() < 0.5) {
|
---|
94 | originalFunctionInvokeNode.Symbol = newInvokeSymbol;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | private static IEnumerable<string> UsedFunctionNames(ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
102 | return from node in symbolicExpressionTree.IterateNodesPrefix()
|
---|
103 | where node.Symbol is Defun
|
---|
104 | select ((DefunTreeNode)node).FunctionName;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|