Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMultyPointsMutatorNodeTypeSaving.cs @ 16899

Last change on this file since 16899 was 16899, checked in by msemenki, 5 years ago

#2988: New version of class structure.

File size: 6.0 KB
Line 
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
22using HEAL.Attic;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Problems.DataAnalysis.Symbolic;
29using HeuristicLab.Random;
30using System.Collections.Generic;
31using System.Linq;
32
33namespace HeuristicLab.Algorithms.EvolvmentModelsOfModels {
34  [Item("EMMMultyPointMutatorNodeTypeSaving", "Selects a random tree node and changes the symbol.")]
35  [StorableType("8A24C715-BEFD-4396-9264-31F949330B1A")]
36  public sealed class EMMMultyPointsMutatorNodeTypeSaving : SymbolicExpressionTreeManipulator {
37
38    private const string MapParameterName = "Map";
39    private const string MutationProbabilityParameterName = "MutationProbability";
40    public ILookupParameter<EMMMapBase<ISymbolicExpressionTree>> MapParameter {
41      get { return (ILookupParameter<EMMMapBase<ISymbolicExpressionTree>>)Parameters[MapParameterName]; }
42    }
43    public List<ISymbolicExpressionTree> ModelSet => MapParameter.ActualValue.ModelSet;
44    public List<int> ClusterNumber => MapParameter.ActualValue.ClusterNumber;
45    public List<List<int>> Map => MapParameter.ActualValue.Map;
46
47    public ILookupParameter<PercentValue> MutationProbabilityParameter {
48      get { return (ILookupParameter<PercentValue>)Parameters[MutationProbabilityParameterName]; }
49    }
50
51    public PercentValue MutationProbability => MutationProbabilityParameter.ActualValue;
52
53    [StorableConstructor]
54    private EMMMultyPointsMutatorNodeTypeSaving(StorableConstructorFlag _) : base(_) { }
55    private EMMMultyPointsMutatorNodeTypeSaving(EMMMultyPointsMutatorNodeTypeSaving original, Cloner cloner) : base(original, cloner) { }
56    public EMMMultyPointsMutatorNodeTypeSaving() : base() {
57      Parameters.Add(new LookupParameter<EMMMapBase<ISymbolicExpressionTree>>(MapParameterName));
58      Parameters.Add(new LookupParameter<PercentValue>(MutationProbabilityParameterName));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new EMMMultyPointsMutatorNodeTypeSaving(this, cloner);
63    }
64
65    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
66      EMMOnePointMutatorPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map, MutationProbability);
67    }
68    private static bool SymbolTypeCheck(ISymbol nSymbol, IEnumerable<ISymbol> allSymbols) {
69      foreach (var pSymbol in allSymbols) {
70        if (nSymbol == pSymbol)
71          return true;
72      }
73      return false;
74    }
75    public static void EMMOnePointMutatorPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<int> clusterNumber, List<List<int>> map, PercentValue mutationProbability) {
76
77      List<ISymbol> allowedSymbols = new List<ISymbol>();
78      var probability = (1 / mutationProbability.Value) / (symbolicExpressionTree.Length); // probability for edch node to mutate
79      var allAllowedSymbols = symbolicExpressionTree.Root.Grammar.AllowedSymbols.Where(s =>
80        !(s is Defun) &&
81        !(s is GroupSymbol) &&
82        !(s is ProgramRootSymbol) &&
83        !(s is StartSymbol)
84      );
85
86      symbolicExpressionTree.Root.ForEachNodePostfix(node => {//for each node in tree - try to mutate
87
88        if (SymbolTypeCheck(node.Symbol, allAllowedSymbols)) { // we do not want to toch StartSymbol
89          if (random.NextDouble() < probability) { // ok. this node decide to mutate
90            if (node.Symbol.MaximumArity > 0) {
91              foreach (var symbol in allAllowedSymbols) {// add in list all nodes with the same arity
92                if ((symbol.MaximumArity == node.Symbol.MaximumArity) && (symbol.MinimumArity == node.Symbol.MinimumArity)) { allowedSymbols.Add(symbol); }
93              }
94            } else { // for terminal nodes add  the same type of nodes
95              allowedSymbols.Add(node.Symbol);
96            }
97            int pTemp = random.Next(map.Count);
98            if (node is TreeModelTreeNode treeNode) { pTemp = treeNode.ClusterNumer; } //remeber the cluster number
99            var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); // set up probabilities
100
101#pragma warning disable CS0618 // Type or member is obsolete
102            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.
103#pragma warning restore CS0618 // Type or member is obsolete
104            node = newSymbol.CreateTreeNode();
105
106            if (node is TreeModelTreeNode treeNode2) { // make rigth mutation for tree model
107              treeNode2.TreeNumber = map[pTemp].SampleRandom(random);
108              treeNode2.Tree = (ISymbolicExpressionTree)modelSet[treeNode2.TreeNumber].Clone();
109              treeNode2.ClusterNumer = pTemp;
110              treeNode2.SetLocalParameters(random, 1);
111            } else if (node.HasLocalParameters) { // make local parametrs set up for other node types
112              node.ResetLocalParameters(random);
113            }
114            allowedSymbols.Clear(); // clean before the next node
115          }
116
117        }
118      });
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.