Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs @ 17180

Last change on this file since 17180 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.2 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    [StorableConstructor]
49    private FactorVariableTreeNode(StorableConstructorFlag _) : base(_) { }
50    private FactorVariableTreeNode(FactorVariableTreeNode original, Cloner cloner)
51      : base(original, cloner) {
52      variableName = original.variableName;
53      if (original.weights != null) {
54        this.weights = new double[original.Weights.Length];
55        Array.Copy(original.Weights, weights, weights.Length);
56      }
57    }
58
59    public FactorVariableTreeNode(FactorVariable variableSymbol)
60      : base(variableSymbol) {
61    }
62
63    public override bool HasLocalParameters {
64      get { return true; }
65    }
66
67    public override void ResetLocalParameters(IRandom random) {
68      base.ResetLocalParameters(random);
69      variableName = Symbol.VariableNames.SampleRandom(random);
70      weights =
71        Symbol.GetVariableValues(variableName)
72        .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1)).ToArray();
73    }
74
75    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
76      // mutate only one randomly selected weight
77      var idx = random.Next(weights.Length);
78      // 50% additive & 50% multiplicative
79      if (random.NextDouble() < 0.5) {
80        double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu,
81          Symbol.WeightManipulatorSigma);
82        weights[idx] = weights[idx] + x * shakingFactor;
83      } else {
84        double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
85        weights[idx] = weights[idx] * x;
86      }
87      if (random.NextDouble() < Symbol.VariableChangeProbability) {
88        VariableName = Symbol.VariableNames.SampleRandom(random);
89        if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
90          // if the length of the weight array does not match => re-initialize weights
91          weights =
92            Symbol.GetVariableValues(variableName)
93              .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1))
94              .ToArray();
95        }
96      }
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new FactorVariableTreeNode(this, cloner);
101    }
102
103    public double GetValue(string cat) {
104      return weights[Symbol.GetIndexForValue(VariableName, cat)];
105    }
106
107    public override string ToString() {
108      var weightStr = string.Join("; ",
109        Symbol.GetVariableValues(VariableName).Select(value => value + ": " + GetValue(value).ToString("E4")));
110      return VariableName + " (factor) "
111        + "[" + weightStr + "]";
112    }
113  }
114}
115
Note: See TracBrowser for help on using the repository browser.