Changeset 17573 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols
- Timestamp:
- 05/29/20 12:48:39 (5 years ago)
- 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 28 28 [StorableType("C6C245BF-C44A-4207-A268-55641483F27F")] 29 29 [Item("Sum", "Symbol that represents the sum function.")] 30 public sealed class Sum : Symbol {30 public sealed class Sum : WindowedSymbol { 31 31 private const int minimumArity = 1; 32 32 private const int maximumArity = 1; -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/WindowedSymbol.cs
r17551 r17573 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 25 using HEAL.Attic; 26 27 27 namespace 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 { 30 30 #region Properties 31 31 [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; } 35 35 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; 38 50 OnChanged(EventArgs.Empty); 39 51 } … … 41 53 } 42 54 [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; } 46 58 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; 50 62 OnChanged(EventArgs.Empty); 51 63 } … … 53 65 } 54 66 [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; } 58 70 set { 59 if (value != weightManipulatorMu) {60 weightManipulatorMu = value;71 if (value != lengthMu) { 72 lengthMu = value; 61 73 OnChanged(EventArgs.Empty); 62 74 } … … 64 76 } 65 77 [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; } 69 81 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; 73 97 OnChanged(EventArgs.Empty); 74 98 } 75 99 } 76 100 } 77 [Storable (DefaultValue = 0.0)]78 private double m ultiplicativeWeightManipulatorSigma;79 public double M ultiplicativeWeightManipulatorSigma {80 get { return m ultiplicativeWeightManipulatorSigma; }101 [Storable] 102 private double manipulatorOffsetSigma; 103 public double ManipulatorOffsetSigma { 104 get { return manipulatorOffsetSigma; } 81 105 set { 82 if (m ultiplicativeWeightManipulatorSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");83 if (value != m ultiplicativeWeightManipulatorSigma) {84 m ultiplicativeWeightManipulatorSigma = value;106 if (manipulatorOffsetSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed."); 107 if (value != manipulatorOffsetSigma) { 108 manipulatorOffsetSigma = value; 85 109 OnChanged(EventArgs.Empty); 86 110 } 87 111 } 88 112 } 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; } 95 117 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 } 98 122 } 99 123 } 100 101 private List<string> variableNames;102 124 [Storable] 103 public IEnumerable<string> VariableNames { 104 get { return variableNames; } 125 private double manipulatorLengthSigma; 126 public double ManipulatorLengthSigma { 127 get { return manipulatorLengthSigma; } 105 128 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 } 110 134 } 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; }143 135 } 144 136 #endregion 145 137 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; 151 168 } 152 169 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); 179 172 } 180 173 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/WindowedSymbolTreeNode.cs
r17551 r17573 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 25 26 using HEAL.Attic; 26 27 using HeuristicLab.Random; 28 27 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 28 [StorableType(" B92EF904-18EC-41FC-9123-9B9293246815")]29 public abstract class VariableTreeNodeBase : SymbolicExpressionTreeTerminalNode, IVariableTreeNode {30 public new VariableBaseSymbol {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; } 32 34 } 33 35 [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); } 38 40 } 39 41 [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); } 44 46 } 45 47 46 48 [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) 49 51 : base(original, cloner) { 50 weight = original.weight;51 variableName = original.variableName;52 offset = original.offset; 53 length = original.length; 52 54 } 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 } 55 65 56 66 public override bool HasLocalParameters { 57 get { return true; }67 get { return Symbol.EnableWindowing; } 58 68 } 59 69 60 70 public override void ResetLocalParameters(IRandom random) { 61 71 base.ResetLocalParameters(random); 62 weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);63 72 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 } 67 77 } 68 78 … … 70 80 base.ShakeLocalParameters(random, shakingFactor); 71 81 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; 95 85 } 96 86 } 97 87 98 88 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(); 101 92 } 102 93 }
Note: See TracChangeset
for help on using the changeset viewer.