[14249] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14249] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[16559] | 27 | using HEAL.Attic;
|
---|
[14249] | 28 | using HeuristicLab.Random;
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[16462] | 30 | [StorableType("A968620F-339E-4C96-B39A-8FC8E42D6509")]
|
---|
[14535] | 31 | public sealed class FactorVariableTreeNode : SymbolicExpressionTreeTerminalNode, IVariableTreeNode {
|
---|
[14249] | 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]
|
---|
[16462] | 49 | private FactorVariableTreeNode(StorableConstructorFlag _) : base(_) { }
|
---|
[14554] | 50 | private FactorVariableTreeNode(FactorVariableTreeNode original, Cloner cloner)
|
---|
[14249] | 51 | : base(original, cloner) {
|
---|
| 52 | variableName = original.variableName;
|
---|
[14826] | 53 | if (original.weights != null) {
|
---|
[14249] | 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) {
|
---|
[14758] | 76 | // mutate only one randomly selected weight
|
---|
| 77 | var idx = random.Next(weights.Length);
|
---|
| 78 | // 50% additive & 50% multiplicative
|
---|
[14826] | 79 | if (random.NextDouble() < 0.5) {
|
---|
[14758] | 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 | }
|
---|
[14826] | 87 | if (random.NextDouble() < Symbol.VariableChangeProbability) {
|
---|
[14249] | 88 | VariableName = Symbol.VariableNames.SampleRandom(random);
|
---|
[14826] | 89 | if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
|
---|
[14249] | 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) {
|
---|
[14717] | 104 | return weights[Symbol.GetIndexForValue(VariableName, cat)];
|
---|
[14249] | 105 | }
|
---|
| 106 |
|
---|
| 107 | public override string ToString() {
|
---|
[14259] | 108 | var weightStr = string.Join("; ",
|
---|
| 109 | Symbol.GetVariableValues(VariableName).Select(value => value + ": " + GetValue(value).ToString("E4")));
|
---|
| 110 | return VariableName + " (factor) "
|
---|
| 111 | + "[" + weightStr + "]";
|
---|
[14249] | 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|