Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableTreeNodeBase.cs @ 17604

Last change on this file since 17604 was 17604, checked in by pfleck, 4 years ago

#3040 Stores the datatype of a tree node (e.g. variable nodes) in the tree itself for the interpreter to derive the datatypes for subtrees. This way, the interpreter (and simplifier) do not need an actual dataset to figure out datatypes for subtrees.

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
26using HEAL.Attic;
27using HeuristicLab.Random;
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
29  [StorableType("B92EF904-18EC-41FC-9123-9B9293246815")]
30  public abstract class VariableTreeNodeBase : SymbolicExpressionTreeTerminalNode, IVariableTreeNode {
31    public new VariableBase Symbol {
32      get { return (VariableBase)base.Symbol; }
33    }
34    [Storable]
35    private double weight;
36    public double Weight {
37      get { return weight; }
38      set { weight = value; }
39    }
40    [Storable]
41    private string variableName;
42    public string VariableName {
43      get { return variableName; }
44      set { variableName = value; }
45    }
46
47    public override Type DataType {
48      get { return Symbol.VariableDataType; }
49    }
50
51    [StorableConstructor]
52    protected VariableTreeNodeBase(StorableConstructorFlag _) : base(_) { }
53    protected VariableTreeNodeBase(VariableTreeNodeBase original, Cloner cloner)
54      : base(original, cloner) {
55      weight = original.weight;
56      variableName = original.variableName;
57    }
58    protected VariableTreeNodeBase() { }
59    protected VariableTreeNodeBase(VariableBase variableSymbol) : base(variableSymbol) { }
60
61    public override bool HasLocalParameters {
62      get { return true; }
63    }
64
65    public override void ResetLocalParameters(IRandom random) {
66      base.ResetLocalParameters(random);
67      weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);
68
69#pragma warning disable 612, 618
70      variableName = Symbol.VariableNames.SelectRandom(random);
71#pragma warning restore 612, 618
72    }
73
74    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
75      base.ShakeLocalParameters(random, shakingFactor);
76
77      // 50% additive & 50% multiplicative (TODO: BUG in if statement below -> fix in HL 4.0!)
78      if (random.NextDouble() < 0) {
79        double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma);
80        weight = weight + x * shakingFactor;
81      } else {
82        double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
83        weight = weight * x;
84      }
85
86      if (Symbol.VariableChangeProbability >= 1.0) {
87        // old behaviour for backwards compatibility
88        #region Backwards compatible code, remove with 3.4
89#pragma warning disable 612, 618
90        variableName = Symbol.VariableNames.SelectRandom(random);
91#pragma warning restore 612, 618
92        #endregion
93      } else if (random.NextDouble() < Symbol.VariableChangeProbability) {
94        var oldName = variableName;
95        variableName = Symbol.VariableNames.SampleRandom(random);
96        if (oldName != variableName) {
97          // re-initialize weight if the variable is changed
98          weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);
99        }
100      }
101    }
102
103    public override string ToString() {
104      if (weight.IsAlmost(1.0)) return variableName;
105      else return weight.ToString("E4") + " " + variableName;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.