Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/ConstantTreeNode.cs @ 17830

Last change on this file since 17830 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: 3.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("247DBD04-18F2-4184-B6F5-6E283BF06FD0")]
30  public sealed class ConstantTreeNode : SymbolicExpressionTreeTerminalNode {
31    public new Constant Symbol {
32      get { return (Constant)base.Symbol; }
33    }
34
35    private double constantValue;
36    [Storable]
37    public double Value {
38      get { return constantValue; }
39      set { constantValue = value; }
40    }
41
42    public override Type DataType {
43      get { return typeof(double); }
44    }
45
46    [StorableConstructor]
47    private ConstantTreeNode(StorableConstructorFlag _) : base(_) { }
48
49    private ConstantTreeNode(ConstantTreeNode original, Cloner cloner)
50      : base(original, cloner) {
51      constantValue = original.constantValue;
52    }
53
54    private ConstantTreeNode() : base() { }
55    public ConstantTreeNode(Constant constantSymbol) : base(constantSymbol) { }
56
57    public override bool HasLocalParameters {
58      get {
59        return true;
60      }
61    }
62    public override void ResetLocalParameters(IRandom random) {
63      base.ResetLocalParameters(random);
64      var range = Symbol.MaxValue - Symbol.MinValue;
65      Value = random.NextDouble() * range + Symbol.MinValue;
66    }
67
68    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
69      base.ShakeLocalParameters(random, shakingFactor);
70      // 50% additive & 50% multiplicative
71      if (random.NextDouble() < 0.5) {
72        double x = NormalDistributedRandom.NextDouble(random, Symbol.ManipulatorMu, Symbol.ManipulatorSigma);
73        Value = Value + x * shakingFactor;
74      } else {
75        double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeManipulatorSigma);
76        Value = Value * x;
77      }
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new ConstantTreeNode(this, cloner);
82    }
83
84    public override string ToString() {
85      return constantValue.ToString("E4");
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.