Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2988_ModelsOfModels2/HeuristicLab.Algorithms.EMM/EMMMutators.cs @ 16911

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

#2988: New version of class structure.

File size: 5.6 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("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    private const string MapParameterName = "Map";
39
40    public ILookupParameter<EMMMapBase<ISymbolicExpressionTree>> MapParameter {
41      get { return (ILookupParameter<EMMMapBase<ISymbolicExpressionTree>>)Parameters[MapParameterName]; }
42    }
43
44    public List<ISymbolicExpressionTree> ModelSet => MapParameter.ActualValue.ModelSet;
45    public List<int> ClusterNumber => MapParameter.ActualValue.ClusterNumber;
46    public List<List<int>> Map => MapParameter.ActualValue.Map;
47
48    [StorableConstructor]
49    private EMMMutators(StorableConstructorFlag _) : base(_) { }
50    private EMMMutators(EMMMutators original, Cloner cloner) : base(original, cloner) { }
51    public EMMMutators() : base() {
52      Parameters.Add(new LookupParameter<EMMMapBase<ISymbolicExpressionTree>>(MapParameterName));
53    }
54
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new EMMMutators(this, cloner);
57    }
58
59    protected override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
60      EMMMutatorsPart(random, symbolicExpressionTree, ModelSet, ClusterNumber, Map);
61    }
62    public static void EMMMutatorsPart(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, List<ISymbolicExpressionTree> modelSet, List<int> clusterNumber, List<List<int>> map) {
63      List<ISymbol> allowedSymbols = new List<ISymbol>();
64      ISymbolicExpressionTreeNode parent;
65      int childIndex;
66      ISymbolicExpressionTreeNode child;
67      // repeat until a fitting parent and child are found (MAX_TRIES times)
68      int tries = 0;
69      do {
70
71#pragma warning disable 612, 618
72        parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random);
73#pragma warning restore 612, 618
74
75        childIndex = random.Next(parent.SubtreeCount);
76
77        child = parent.GetSubtree(childIndex);
78        int existingSubtreeCount = child.SubtreeCount;
79        allowedSymbols.Clear();
80        foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) {
81          // check basic properties that the new symbol must have
82          if (symbol.Name != child.Symbol.Name &&
83            symbol.InitialFrequency > 0 &&
84            existingSubtreeCount <= parent.Grammar.GetMinimumSubtreeCount(symbol) &&
85            existingSubtreeCount >= parent.Grammar.GetMaximumSubtreeCount(symbol)) {
86            // check that all existing subtrees are also allowed for the new symbol
87            bool allExistingSubtreesAllowed = true;
88            for (int existingSubtreeIndex = 0; existingSubtreeIndex < existingSubtreeCount && allExistingSubtreesAllowed; existingSubtreeIndex++) {
89              var existingSubtree = child.GetSubtree(existingSubtreeIndex);
90              allExistingSubtreesAllowed &= parent.Grammar.IsAllowedChildSymbol(symbol, existingSubtree.Symbol, existingSubtreeIndex);
91            }
92            if (allExistingSubtreesAllowed) {
93              allowedSymbols.Add(symbol);
94            }
95          }
96        }
97        tries++;
98      } while (tries < MAX_TRIES && allowedSymbols.Count == 0);
99
100      if (tries < MAX_TRIES) {
101        var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList();
102#pragma warning disable 612, 618
103        var newSymbol = allowedSymbols.SelectRandom(weights, random);
104#pragma warning restore 612, 618
105
106        // replace the old node with the new node
107
108        var newNode = newSymbol.CreateTreeNode();
109
110        if (newNode is TreeModelTreeNode treeNode) {
111          int p = random.Next(map.Count);
112          if (child is TreeModelTreeNode chNode) { p = chNode.ClusterNumer; }
113          treeNode.TreeNumber = map[p].SampleRandom(random);
114          treeNode.Tree = (ISymbolicExpressionTree)modelSet[treeNode.TreeNumber].Clone();
115          treeNode.ClusterNumer = p;
116          treeNode.SetLocalParameters(random, 0.5);
117        } else if (newNode.HasLocalParameters)
118          newNode.ResetLocalParameters(random);
119        foreach (var subtree in child.Subtrees)
120          newNode.AddSubtree(subtree);
121        parent.RemoveSubtree(childIndex);
122        parent.InsertSubtree(childIndex, newNode);
123      }
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.