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("EMMMultyPointMutator", "Selects a random tree node and changes the symbol.")]
|
---|
35 | [StorableType("918B5F77-3B9E-4620-94A3-236913C4A3F2")]
|
---|
36 | public sealed class EMMMultyPointsMutator : SymbolicExpressionTreeManipulator {
|
---|
37 | private const string ModelSetParameterName = "Models";
|
---|
38 | private const string ClusterNumberParameterName = "ClusterNumber";
|
---|
39 | private const string MapParameterName = "Map";
|
---|
40 | private const string MutationProbabilityParameterName = "MutationProbability";
|
---|
41 | public ILookupParameter<ItemList<ISymbolicExpressionTree>> ModelSetParameter {
|
---|
42 | get { return (ILookupParameter<ItemList<ISymbolicExpressionTree>>)Parameters[ModelSetParameterName]; }
|
---|
43 | }
|
---|
44 | public ILookupParameter<ItemList<IntValue>> ClusterNumberParameter {
|
---|
45 | get { return (ILookupParameter<ItemList<IntValue>>)Parameters[ClusterNumberParameterName]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public ILookupParameter<ItemList<ItemList<IntValue>>> MapParameter {
|
---|
49 | get { return (ILookupParameter<ItemList<ItemList<IntValue>>>)Parameters[MapParameterName]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<PercentValue> MutationProbabilityParameter {
|
---|
52 | get { return (ILookupParameter<PercentValue>)Parameters[MutationProbabilityParameterName]; }
|
---|
53 | }
|
---|
54 | public ItemList<ISymbolicExpressionTree> ModelSet => ModelSetParameter.ActualValue;
|
---|
55 | public ItemList<IntValue> ClusterNumber => ClusterNumberParameter.ActualValue;
|
---|
56 | public ItemList<ItemList<IntValue>> Map => MapParameter.ActualValue;
|
---|
57 | public PercentValue MutationProbability => MutationProbabilityParameter.ActualValue;
|
---|
58 |
|
---|
59 | [StorableConstructor]
|
---|
60 | private EMMMultyPointsMutator(StorableConstructorFlag _) : base(_) { }
|
---|
61 | private EMMMultyPointsMutator(EMMMultyPointsMutator original, Cloner cloner) : base(original, cloner) { }
|
---|
62 | public EMMMultyPointsMutator() : 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 | Parameters.Add(new LookupParameter<PercentValue>(MutationProbabilityParameterName));
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
70 | return new EMMMultyPointsMutator(this, cloner);
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
|
---|
74 | EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map, MutationProbability);
|
---|
75 | }
|
---|
76 | private static bool SymbolTypeCheck(ISymbol nSymbol, IEnumerable<ISymbol> allSymbols) {
|
---|
77 | foreach (var pSymbol in allSymbols) {
|
---|
78 | if (nSymbol == pSymbol)
|
---|
79 | return true;
|
---|
80 | }
|
---|
81 | return false;
|
---|
82 | }
|
---|
83 | public static void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, ItemList<ISymbolicExpressionTree> modelSet, ItemList<IntValue> clusterNumber, ItemList<ItemList<IntValue>> map, PercentValue mutationProbability) {
|
---|
84 |
|
---|
85 | List<ISymbol> allowedSymbols = new List<ISymbol>();
|
---|
86 | var probability = (1 / mutationProbability.Value) / (symbolicExpressionTree.Length); // probability for edch node to mutate
|
---|
87 | var allAllowedSymbols = symbolicExpressionTree.Root.Grammar.AllowedSymbols.Where(s =>
|
---|
88 | !(s is Defun) &&
|
---|
89 | !(s is GroupSymbol) &&
|
---|
90 | !(s is ProgramRootSymbol) &&
|
---|
91 | !(s is StartSymbol)
|
---|
92 | );
|
---|
93 |
|
---|
94 | symbolicExpressionTree.Root.ForEachNodePostfix(node => {//for each node in tree - try to mutate
|
---|
95 |
|
---|
96 | if (SymbolTypeCheck(node.Symbol, allAllowedSymbols)) { // we do not want to toch StartSymbol
|
---|
97 | if (random.NextDouble() < probability) { // ok. this node decide to mutate
|
---|
98 |
|
---|
99 | foreach (var symbol in allAllowedSymbols) {// add in list all nodes with the same arity
|
---|
100 | if ((symbol.MaximumArity == node.Symbol.MaximumArity) && (symbol.MinimumArity == node.Symbol.MinimumArity)) { allowedSymbols.Add(symbol); }
|
---|
101 | }
|
---|
102 | if (node.Symbol.MaximumArity == 0) { // for terminal nodes add aditional the same type of nodes
|
---|
103 | allowedSymbols.Add(node.Symbol);
|
---|
104 | }
|
---|
105 | int pTemp = random.Next(map.Count);
|
---|
106 | if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.ClusterNumer; } //remeber the cluster number
|
---|
107 | var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); // set up probabilities
|
---|
108 |
|
---|
109 | #pragma warning disable CS0618 // Type or member is obsolete
|
---|
110 | var newSymbol = allowedSymbols.SelectRandom(weights, random); // create new node from the choosen symbol type. Ror terminal nodes it means that we have only one possible varint.
|
---|
111 | #pragma warning restore CS0618 // Type or member is obsolete
|
---|
112 | node = newSymbol.CreateTreeNode();
|
---|
113 |
|
---|
114 | if (node is TreeModelTreeNode treeNode2) { // make rigth mutation for tree model
|
---|
115 | treeNode2.TreeNumber = map[pTemp].SampleRandom(random).Value;
|
---|
116 | treeNode2.Tree = (ISymbolicExpressionTree)modelSet[treeNode2.TreeNumber].Clone();
|
---|
117 | treeNode2.ClusterNumer = pTemp;
|
---|
118 | treeNode2.SetLocalParameters(random, 1);
|
---|
119 | } else if (node.HasLocalParameters) { // make local parametrs set up for other node types
|
---|
120 | node.ResetLocalParameters(random);
|
---|
121 | }
|
---|
122 | allowedSymbols.Clear(); // clean before the next node
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|
126 | });
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } |
---|