Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs @ 18082

Last change on this file since 18082 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.3 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 System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HEAL.Attic;
28using HeuristicLab.Random;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableType("A968620F-339E-4C96-B39A-8FC8E42D6509")]
31  public sealed class FactorVariableTreeNode : SymbolicExpressionTreeTerminalNode, IVariableTreeNode {
32    public new FactorVariable Symbol {
33      get { return (FactorVariable)base.Symbol; }
34    }
35    [Storable]
36    private double[] weights;
37    public double[] Weights {
38      get { return weights; }
39      set { weights = value; }
40    }
41    [Storable]
42    private string variableName;
43    public string VariableName {
44      get { return variableName; }
45      set { variableName = value; }
46    }
47
48    public override Type DataType {
49      get { return typeof(double); }
50    }
51
52    [StorableConstructor]
53    private FactorVariableTreeNode(StorableConstructorFlag _) : base(_) { }
54    private FactorVariableTreeNode(FactorVariableTreeNode original, Cloner cloner)
55      : base(original, cloner) {
56      variableName = original.variableName;
57      if (original.weights != null) {
58        this.weights = new double[original.Weights.Length];
59        Array.Copy(original.Weights, weights, weights.Length);
60      }
61    }
62
63    public FactorVariableTreeNode(FactorVariable variableSymbol)
64      : base(variableSymbol) {
65    }
66
67    public override bool HasLocalParameters {
68      get { return true; }
69    }
70
71    public override void ResetLocalParameters(IRandom random) {
72      base.ResetLocalParameters(random);
73      variableName = Symbol.VariableNames.SampleRandom(random);
74      weights =
75        Symbol.GetVariableValues(variableName)
76        .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray();
77    }
78
79    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
80      // mutate only one randomly selected weight
81      var idx = random.Next(weights.Length);
82      // 50% additive & 50% multiplicative
83      if (random.NextDouble() < 0.5) {
84        double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu,
85          Symbol.WeightManipulatorSigma);
86        weights[idx] = weights[idx] + x * shakingFactor;
87      } else {
88        double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
89        weights[idx] = weights[idx] * x;
90      }
91      if (random.NextDouble() < Symbol.VariableChangeProbability) {
92        VariableName = Symbol.VariableNames.SampleRandom(random);
93        if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
94          // if the length of the weight array does not match => re-initialize weights
95          weights =
96            Symbol.GetVariableValues(variableName)
97              .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1))
98              .ToArray();
99        }
100      }
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new FactorVariableTreeNode(this, cloner);
105    }
106
107    public double GetValue(string cat) {
108      return weights[Symbol.GetIndexForValue(VariableName, cat)];
109    }
110
111    public override string ToString() {
112      var weightStr = string.Join("; ",
113        Symbol.GetVariableValues(VariableName).Select(value => value + ": " + GetValue(value).ToString("E4")));
114      return VariableName + " (factor) "
115        + "[" + weightStr + "]";
116    }
117  }
118}
119
Note: See TracBrowser for help on using the repository browser.