Free cookie consent management tool by TermsFeed Policy Generator

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

#3040 Added first draft for WindowedSymbol.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols
Files:
1 edited
2 copied

Legend:

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

    r17466 r17573  
    2828  [StorableType("C6C245BF-C44A-4207-A268-55641483F27F")]
    2929  [Item("Sum", "Symbol that represents the sum function.")]
    30   public sealed class Sum : Symbol {
     30  public sealed class Sum : WindowedSymbol {
    3131    private const int minimumArity = 1;
    3232    private const int maximumArity = 1;
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/WindowedSymbol.cs

    r17551 r17573  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using HeuristicLab.Common;
    2524using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2625using HEAL.Attic;
     26
    2727namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    28   [StorableType("F8A6AD96-28D9-4BEC-8392-8B7BA824B085")]
    29   public abstract class VariableBase : Symbol, IVariableSymbol {
     28  [StorableType("2E342E38-BE2F-4E49-A9C3-2E833B83FDBC")]
     29  public abstract class WindowedSymbol : Symbol, IWindowedSymbol {
    3030    #region Properties
    3131    [Storable]
    32     private double weightMu;
    33     public double WeightMu {
    34       get { return weightMu; }
     32    private bool enableWindowing;
     33    public bool EnableWindowing {
     34      get { return enableWindowing; }
    3535      set {
    36         if (value != weightMu) {
    37           weightMu = value;
     36        if (value != enableWindowing) {
     37          enableWindowing = value;
     38          OnChanged(EventArgs.Empty);
     39        }
     40      }
     41    }
     42
     43    [Storable]
     44    private double offsetMu;
     45    public double OffsetMu {
     46      get { return offsetMu; }
     47      set {
     48        if (value != offsetMu) {
     49          offsetMu = value;
    3850          OnChanged(EventArgs.Empty);
    3951        }
     
    4153    }
    4254    [Storable]
    43     private double weightSigma;
    44     public double WeightSigma {
    45       get { return weightSigma; }
     55    private double offsetSigma;
     56    public double OffsetSigma {
     57      get { return offsetSigma; }
    4658      set {
    47         if (weightSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    48         if (value != weightSigma) {
    49           weightSigma = value;
     59        if (offsetSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
     60        if (value != offsetSigma) {
     61          offsetSigma = value;
    5062          OnChanged(EventArgs.Empty);
    5163        }
     
    5365    }
    5466    [Storable]
    55     private double weightManipulatorMu;
    56     public double WeightManipulatorMu {
    57       get { return weightManipulatorMu; }
     67    private double lengthMu;
     68    public double LengthMu {
     69      get { return lengthMu; }
    5870      set {
    59         if (value != weightManipulatorMu) {
    60           weightManipulatorMu = value;
     71        if (value != lengthMu) {
     72          lengthMu = value;
    6173          OnChanged(EventArgs.Empty);
    6274        }
     
    6476    }
    6577    [Storable]
    66     private double weightManipulatorSigma;
    67     public double WeightManipulatorSigma {
    68       get { return weightManipulatorSigma; }
     78    private double lengthSigma;
     79    public double LengthSigma {
     80      get { return lengthSigma; }
    6981      set {
    70         if (weightManipulatorSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    71         if (value != weightManipulatorSigma) {
    72           weightManipulatorSigma = value;
     82        if (lengthSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
     83        if (value != lengthSigma) {
     84          lengthSigma = value;
     85          OnChanged(EventArgs.Empty); ;
     86        }
     87      }
     88    }
     89
     90    [Storable]
     91    private double manipulatorOffsetMu;
     92    public double ManipulatorOffsetMu {
     93      get { return manipulatorOffsetMu; }
     94      set {
     95        if (value != manipulatorOffsetMu) {
     96          manipulatorOffsetMu = value;
    7397          OnChanged(EventArgs.Empty);
    7498        }
    7599      }
    76100    }
    77     [Storable(DefaultValue = 0.0)]
    78     private double multiplicativeWeightManipulatorSigma;
    79     public double MultiplicativeWeightManipulatorSigma {
    80       get { return multiplicativeWeightManipulatorSigma; }
     101    [Storable]
     102    private double manipulatorOffsetSigma;
     103    public double ManipulatorOffsetSigma {
     104      get { return manipulatorOffsetSigma; }
    81105      set {
    82         if (multiplicativeWeightManipulatorSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    83         if (value != multiplicativeWeightManipulatorSigma) {
    84           multiplicativeWeightManipulatorSigma = value;
     106        if (manipulatorOffsetSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
     107        if (value != manipulatorOffsetSigma) {
     108          manipulatorOffsetSigma = value;
    85109          OnChanged(EventArgs.Empty);
    86110        }
    87111      }
    88112    }
    89 
    90     [Storable(DefaultValue = 1.0)]
    91     private double variableChangeProbability;
    92 
    93     public double VariableChangeProbability {
    94       get { return variableChangeProbability; }
     113    [Storable]
     114    private double manipulatorLengthMu;
     115    public double ManipulatorLengthMu {
     116      get { return manipulatorLengthMu; }
    95117      set {
    96         if (value < 0 || value > 1.0) throw new ArgumentException("Variable change probability must lie in the interval [0..1]");
    97         variableChangeProbability = value;
     118        if (value != manipulatorLengthMu) {
     119          manipulatorLengthMu = value;
     120          OnChanged(EventArgs.Empty);
     121        }
    98122      }
    99123    }
    100 
    101     private List<string> variableNames;
    102124    [Storable]
    103     public IEnumerable<string> VariableNames {
    104       get { return variableNames; }
     125    private double manipulatorLengthSigma;
     126    public double ManipulatorLengthSigma {
     127      get { return manipulatorLengthSigma; }
    105128      set {
    106         if (value == null) throw new ArgumentNullException();
    107         variableNames.Clear();
    108         variableNames.AddRange(value);
    109         OnChanged(EventArgs.Empty);
     129        if (manipulatorLengthSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
     130        if (value != manipulatorLengthSigma) {
     131          manipulatorLengthSigma = value;
     132          OnChanged(EventArgs.Empty);;
     133        }
    110134      }
    111     }
    112 
    113     private List<string> allVariableNames;
    114     [Storable]
    115     public IEnumerable<string> AllVariableNames {
    116       get { return allVariableNames; }
    117       set {
    118         if (value == null) throw new ArgumentNullException();
    119         allVariableNames.Clear();
    120         allVariableNames.AddRange(value);
    121       }
    122     }
    123 
    124     public override bool Enabled {
    125       get {
    126         if (variableNames.Count == 0) return false;
    127         return base.Enabled;
    128       }
    129       set {
    130         if (variableNames.Count == 0) base.Enabled = false;
    131         else base.Enabled = value;
    132       }
    133     }
    134 
    135     private const int minimumArity = 0;
    136     private const int maximumArity = 0;
    137 
    138     public override int MinimumArity {
    139       get { return minimumArity; }
    140     }
    141     public override int MaximumArity {
    142       get { return maximumArity; }
    143135    }
    144136    #endregion
    145137
    146     [StorableHook(HookType.AfterDeserialization)]
    147     private void AfterDeserialization() {
    148       if (allVariableNames == null || (allVariableNames.Count == 0 && variableNames.Count > 0)) {
    149         allVariableNames = variableNames;
    150       }
     138    [StorableConstructor]
     139    protected WindowedSymbol(StorableConstructorFlag _) : base(_) {
     140    }
     141    protected WindowedSymbol(WindowedSymbol original, Cloner cloner)
     142      : base(original, cloner) {
     143      enableWindowing = original.enableWindowing;
     144
     145      offsetMu = original.offsetMu;
     146      offsetSigma = original.offsetSigma;
     147      lengthMu = original.lengthMu;
     148      lengthSigma = original.lengthSigma;
     149
     150      manipulatorOffsetMu = original.manipulatorOffsetMu;
     151      manipulatorOffsetSigma = original.manipulatorOffsetSigma;
     152      manipulatorLengthMu = original.manipulatorLengthMu;
     153      manipulatorLengthSigma = original.manipulatorLengthSigma;
     154    }
     155    protected WindowedSymbol(string name, string description)
     156      : base(name, description) {
     157      enableWindowing = false;
     158
     159      offsetMu = 0.0;
     160      offsetSigma = 0.05;
     161      lengthMu = 1.0;
     162      lengthSigma = 0.05;
     163
     164      manipulatorOffsetMu = 0.0;
     165      manipulatorOffsetSigma = 0.05;
     166      manipulatorLengthMu = 0.0;
     167      manipulatorLengthSigma = 0.05;
    151168    }
    152169
    153     [StorableConstructor]
    154     protected VariableBase(StorableConstructorFlag _) : base(_) {
    155       variableNames = new List<string>();
    156       allVariableNames = new List<string>();
    157     }
    158     protected VariableBase(VariableBase original, Cloner cloner)
    159       : base(original, cloner) {
    160       weightMu = original.weightMu;
    161       weightSigma = original.weightSigma;
    162       variableNames = new List<string>(original.variableNames);
    163       allVariableNames = new List<string>(original.allVariableNames);
    164       weightManipulatorMu = original.weightManipulatorMu;
    165       weightManipulatorSigma = original.weightManipulatorSigma;
    166       multiplicativeWeightManipulatorSigma = original.multiplicativeWeightManipulatorSigma;
    167       variableChangeProbability = original.variableChangeProbability;
    168     }
    169     protected VariableBase(string name, string description)
    170       : base(name, description) {
    171       weightMu = 1.0;
    172       weightSigma = 1.0;
    173       weightManipulatorMu = 0.0;
    174       weightManipulatorSigma = 0.05;
    175       multiplicativeWeightManipulatorSigma = 0.03;
    176       variableChangeProbability = 0.2;
    177       variableNames = new List<string>();
    178       allVariableNames = new List<string>();
     170    public override ISymbolicExpressionTreeNode CreateTreeNode() {
     171      return new WindowedSymbolTreeNode(this);
    179172    }
    180173  }
  • 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.