Ignore:
Timestamp:
05/29/20 12:48:39 (2 years ago)
Author:
pfleck
Message:

#3040 Added first draft for WindowedSymbol.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/WindowedSymbolTreeNode.cs

    r17551 r17573  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2526using HEAL.Attic;
    2627using HeuristicLab.Random;
     28
    2729namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    28   [StorableType("B92EF904-18EC-41FC-9123-9B9293246815")]
    29   public abstract class VariableTreeNodeBase : SymbolicExpressionTreeTerminalNode, IVariableTreeNode {
    30     public new VariableBase Symbol {
    31       get { return (VariableBase)base.Symbol; }
     30  [StorableType("E79432BB-414D-4535-8751-D0139AF048E1")]
     31  public class WindowedSymbolTreeNode : SymbolicExpressionTreeNode, IWindowedSymbolTreeNode {
     32    public new WindowedSymbol Symbol {
     33      get { return (WindowedSymbol)base.Symbol; }
    3234    }
    3335    [Storable]
    34     private double weight;
    35     public double Weight {
    36       get { return weight; }
    37       set { weight = value; }
     36    private double offset;
     37    public double Offset {
     38      get { return offset; }
     39      set { offset = Math.Min(Math.Max(value, 0.0), 1.0); }     
    3840    }
    3941    [Storable]
    40     private string variableName;
    41     public string VariableName {
    42       get { return variableName; }
    43       set { variableName = value; }
     42    private double length;
     43    public double Length {
     44      get { return length; }
     45      set { length = Math.Min(Math.Max(value, 0.0), 1.0); }
    4446    }
    4547
    4648    [StorableConstructor]
    47     protected VariableTreeNodeBase(StorableConstructorFlag _) : base(_) { }
    48     protected VariableTreeNodeBase(VariableTreeNodeBase original, Cloner cloner)
     49    protected WindowedSymbolTreeNode(StorableConstructorFlag _) : base(_) { }
     50    protected WindowedSymbolTreeNode(WindowedSymbolTreeNode original, Cloner cloner)
    4951      : base(original, cloner) {
    50       weight = original.weight;
    51       variableName = original.variableName;
     52      offset = original.offset;
     53      length = original.length;
    5254    }
    53     protected VariableTreeNodeBase() { }
    54     protected VariableTreeNodeBase(VariableBase variableSymbol) : base(variableSymbol) { }
     55    public override IDeepCloneable Clone(Cloner cloner) {
     56      return new WindowedSymbolTreeNode(this, cloner);
     57    }
     58
     59    public WindowedSymbolTreeNode(WindowedSymbol windowedSymbol) : base(windowedSymbol) {
     60      if (!windowedSymbol.EnableWindowing) {
     61        Offset = 0.0;
     62        Length = 1.0;
     63      }
     64    }
    5565
    5666    public override bool HasLocalParameters {
    57       get { return true; }
     67      get { return Symbol.EnableWindowing; }
    5868    }
    5969
    6070    public override void ResetLocalParameters(IRandom random) {
    6171      base.ResetLocalParameters(random);
    62       weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);
    6372
    64 #pragma warning disable 612, 618
    65       variableName = Symbol.VariableNames.SelectRandom(random);
    66 #pragma warning restore 612, 618
     73      if (Symbol.EnableWindowing) {
     74        Offset = NormalDistributedRandom.NextDouble(random, Symbol.OffsetMu, Symbol.OffsetSigma);
     75        Length = NormalDistributedRandom.NextDouble(random, Symbol.LengthMu, Symbol.LengthSigma);
     76      }
    6777    }
    6878
     
    7080      base.ShakeLocalParameters(random, shakingFactor);
    7181
    72       // 50% additive & 50% multiplicative (TODO: BUG in if statement below -> fix in HL 4.0!)
    73       if (random.NextDouble() < 0) {
    74         double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma);
    75         weight = weight + x * shakingFactor;
    76       } else {
    77         double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
    78         weight = weight * x;
    79       }
    80 
    81       if (Symbol.VariableChangeProbability >= 1.0) {
    82         // old behaviour for backwards compatibility
    83         #region Backwards compatible code, remove with 3.4
    84 #pragma warning disable 612, 618
    85         variableName = Symbol.VariableNames.SelectRandom(random);
    86 #pragma warning restore 612, 618
    87         #endregion
    88       } else if (random.NextDouble() < Symbol.VariableChangeProbability) {
    89         var oldName = variableName;
    90         variableName = Symbol.VariableNames.SampleRandom(random);
    91         if (oldName != variableName) {
    92           // re-initialize weight if the variable is changed
    93           weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);
    94         }
     82      if (Symbol.EnableWindowing) {
     83        Offset += NormalDistributedRandom.NextDouble(random, Symbol.ManipulatorOffsetMu, Symbol.ManipulatorOffsetSigma) * shakingFactor;
     84        Length += NormalDistributedRandom.NextDouble(random, Symbol.ManipulatorLengthMu, Symbol.ManipulatorLengthSigma) * shakingFactor;
    9585      }
    9686    }
    9787
    9888    public override string ToString() {
    99       if (weight.IsAlmost(1.0)) return variableName;
    100       else return weight.ToString("E4") + " " + variableName;
     89      return Symbol.EnableWindowing
     90        ? base.ToString() + $"[{Offset} : {Length}]"
     91        : base.ToString();
    10192    }
    10293  }
Note: See TracChangeset for help on using the changeset viewer.