Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs @ 14259

Last change on this file since 14259 was 14259, checked in by gkronber, 8 years ago

#2650: added support for factor variables to Excel formatter and Excel exporter as well as to the Latex formatter and consequently the mathematical representation view.

File size: 4.5 KB
RevLine 
[14249]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Random;
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  [StorableClass]
31  public 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    protected FactorVariableTreeNode(bool deserializing) : base(deserializing) { }
50    protected 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      if (random.NextDouble() < 0.2) {
77        VariableName = Symbol.VariableNames.SampleRandom(random);
78        if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
79          // if the length of the weight array does not match => re-initialize weights
80          weights =
81            Symbol.GetVariableValues(variableName)
82              .Select(_ => NormalDistributedRandom.NextDouble(random, 0, 1))
83              .ToArray();
84        }
85      } else {
86        // mutate only one randomly selected weight
87        var idx = random.Next(weights.Length);
88        // 50% additive & 50% multiplicative
89        if (random.NextDouble() < 0.5) {
90          double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu,
91            Symbol.WeightManipulatorSigma);
92          weights[idx] = weights[idx] + x * shakingFactor;
93        } else {
94          double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
95          weights[idx] = weights[idx] * x;
96        }
97      }
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new FactorVariableTreeNode(this, cloner);
102    }
103
104    public double GetValue(string cat) {
105      // TODO: perf
106      var s = Symbol;
107      int idx = 0;
108      foreach (var val in s.GetVariableValues(VariableName)) {
109        if (cat == val) return weights[idx];
110        idx++;
111      }
112      throw new ArgumentOutOfRangeException("Found unknown value " + cat + " for variable " + VariableName);
113    }
114
115    public override string ToString() {
[14259]116      var weightStr = string.Join("; ",
117        Symbol.GetVariableValues(VariableName).Select(value => value + ": " + GetValue(value).ToString("E4")));
118      return VariableName + " (factor) "
119        + "[" + weightStr + "]";
[14249]120    }
121  }
122}
123
Note: See TracBrowser for help on using the repository browser.