Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14717 was 14717, checked in by gkronber, 7 years ago

#2650: use a dictionary of variable values instead of a list in FactorVariable (symbol) to remove sequential search for variable value in FactorVariableTreeNode

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