1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 HEAL.Attic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 | using System.Collections.Generic;
|
---|
31 | using System.Linq;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
|
---|
34 | [Item("EMMChangeNodeTypeManipulation", "Selects a random tree node and changes the symbol.")]
|
---|
35 | [StorableType("990D3946-7F06-48B4-B8DB-F8E308D6304D")]
|
---|
36 | public sealed class EMMMutators : SymbolicExpressionTreeManipulator {
|
---|
37 | private const int MAX_TRIES = 100;
|
---|
38 |
|
---|
39 | private const string ModelSetParameterName = "Models";
|
---|
40 | private const string ClusterNumberParameterName = "ClusterNumber";
|
---|
41 | private const string MapParameterName = "Map";
|
---|
42 |
|
---|
43 | public ILookupParameter<ItemList<ISymbolicExpressionTree>> ModelSetParameter {
|
---|
44 | get { return (ILookupParameter<ItemList<ISymbolicExpressionTree>>)Parameters[ModelSetParameterName]; }
|
---|
45 | }
|
---|
46 |
|
---|
47 | public ILookupParameter<ItemList<IntValue>> ClusterNumberParameter {
|
---|
48 | get { return (ILookupParameter<ItemList<IntValue>>)Parameters[ClusterNumberParameterName]; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public ILookupParameter<ItemList<ItemList<IntValue>>> MapParameter {
|
---|
52 | get { return (ILookupParameter<ItemList<ItemList<IntValue>>>)Parameters[MapParameterName]; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public ItemList<ISymbolicExpressionTree> ModelSet => ModelSetParameter.ActualValue;
|
---|
56 | public ItemList<IntValue> ClusterNumber => ClusterNumberParameter.ActualValue;
|
---|
57 | public ItemList<ItemList<IntValue>> Map => MapParameter.ActualValue;
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | private EMMMutators(StorableConstructorFlag _) : base(_) { }
|
---|
61 | private EMMMutators(EMMMutators original, Cloner cloner) : base(original, cloner) { }
|
---|
62 | public EMMMutators() : base() {
|
---|
63 | Parameters.Add(new LookupParameter<ItemList<ISymbolicExpressionTree>>(ModelSetParameterName));
|
---|
64 | Parameters.Add(new LookupParameter<ItemList<IntValue>>(ClusterNumberParameterName));
|
---|
65 | Parameters.Add(new LookupParameter<ItemList<ItemList<IntValue>>>(MapParameterName));
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new EMMMutators(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
73 | EMMMutatorsPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map);
|
---|
74 | }
|
---|
75 | public static void EMMMutatorsPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, ItemList<ISymbolicExpressionTree> modelSet, ItemList<IntValue> clusterNumber, ItemList<ItemList<IntValue>> map) {
|
---|
76 | List<ISymbol> allowedSymbols = new List<ISymbol>();
|
---|
77 | ISymbolicExpressionTreeNode parent;
|
---|
78 | int childIndex;
|
---|
79 | ISymbolicExpressionTreeNode child;
|
---|
80 | // repeat until a fitting parent and child are found (MAX_TRIES times)
|
---|
81 | int tries = 0;
|
---|
82 | do {
|
---|
83 |
|
---|
84 | #pragma warning disable 612, 618
|
---|
85 | parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
|
---|
86 | #pragma warning restore 612, 618
|
---|
87 |
|
---|
88 | childIndex = random.Next(parent.SubtreeCount);
|
---|
89 |
|
---|
90 | child = parent.GetSubtree(childIndex);
|
---|
91 | int existingSubtreeCount = child.SubtreeCount;
|
---|
92 | allowedSymbols.Clear();
|
---|
93 | foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
|
---|
94 | // check basic properties that the new symbol must have
|
---|
95 | if (symbol.Name != child.Symbol.Name &&
|
---|
96 | symbol.InitialFrequency > 0 &&
|
---|
97 | existingSubtreeCount <= parent.Grammar.GetMinimumSubtreeCount(symbol) &&
|
---|
98 | existingSubtreeCount >= parent.Grammar.GetMaximumSubtreeCount(symbol)) {
|
---|
99 | // check that all existing subtrees are also allowed for the new symbol
|
---|
100 | bool allExistingSubtreesAllowed = true;
|
---|
101 | for (int existingSubtreeIndex = 0; existingSubtreeIndex < existingSubtreeCount && allExistingSubtreesAllowed; existingSubtreeIndex++) {
|
---|
102 | var existingSubtree = child.GetSubtree(existingSubtreeIndex);
|
---|
103 | allExistingSubtreesAllowed &= parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex);
|
---|
104 | }
|
---|
105 | if (allExistingSubtreesAllowed) {
|
---|
106 | allowedSymbols.Add(symbol);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | tries++;
|
---|
111 | } while (tries < MAX_TRIES && allowedSymbols.Count == 0);
|
---|
112 |
|
---|
113 | if (tries < MAX_TRIES) {
|
---|
114 | var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
|
---|
115 | #pragma warning disable 612, 618
|
---|
116 | var newSymbol = allowedSymbols.SelectRandom(weights, random);
|
---|
117 | #pragma warning restore 612, 618
|
---|
118 |
|
---|
119 | // replace the old node with the new node
|
---|
120 |
|
---|
121 | var newNode = newSymbol.CreateTreeNode();
|
---|
122 |
|
---|
123 | if (newNode is TreeModelTreeNode treeNode) {
|
---|
124 | int p = random.Next(map.Count);
|
---|
125 | if (child is TreeModelTreeNode chNode) { p = chNode.ClusterNumer; }
|
---|
126 | treeNode.TreeNumber = map[p].SampleRandom(random).Value;
|
---|
127 | treeNode.Tree = (ISymbolicExpressionTree)modelSet[treeNode.TreeNumber].Clone();
|
---|
128 | treeNode.ClusterNumer = p;
|
---|
129 | treeNode.SetLocalParameters(random, 0.5);
|
---|
130 | } else if (newNode.HasLocalParameters)
|
---|
131 | newNode.ResetLocalParameters(random);
|
---|
132 | foreach (var subtree in child.Subtrees)
|
---|
133 | newNode.AddSubtree(subtree);
|
---|
134 | parent.RemoveSubtree(childIndex);
|
---|
135 | parent.InsertSubtree(childIndex, newNode);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|