Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/05/16 14:25:28 (8 years ago)
Author:
gkronber
Message:

#2650: work in progress..

Location:
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r14232 r14237  
    200200    <Compile Include="Symbols\FactorVariable.cs" />
    201201    <Compile Include="Symbols\FactorVariableTreeNode.cs" />
     202    <Compile Include="Symbols\VariableBase.cs" />
     203    <Compile Include="Symbols\VariableTreeNodeBase.cs" />
    202204    <Compile Include="Symbols\Xor.cs" />
    203205    <Compile Include="Symbols\Erf.cs" />
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r14232 r14237  
    639639              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.VariableValue);
    640640              il.Emit(System.Reflection.Emit.OpCodes.Call, string_eq);
     641              throw new NotSupportedException();
    641642              // TODO: convert bool to 1 / 0?
    642643            } else {
     
    652653              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.VariableValue);
    653654              il.Emit(System.Reflection.Emit.OpCodes.Call, string_eq);
     655              throw new NotSupportedException();
    654656              // TODO: convert bool to 1 / 0?
    655657              il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r14232 r14237  
    462462            if (row < 0 || row >= dataset.Rows) return double.NaN;
    463463            var factorVarTreeNode = currentInstr.dynamicNode as FactorVariableTreeNode;
    464             return ((IList<string>)currentInstr.data)[row] == factorVarTreeNode.VariableValue ? 1 : 0;
     464            return ((IList<string>)currentInstr.data)[row] == factorVarTreeNode.VariableValue ? factorVarTreeNode.Weight : 0;
    465465          }
    466466        case OpCodes.LagVariable: {
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r14232 r14237  
    152152          else {
    153153            var factorTreeNode = instr.dynamicNode as FactorVariableTreeNode;
    154             instr.value = ((IList<string>)instr.data)[row] == factorTreeNode.VariableValue ? 1 : 0;
     154            instr.value = ((IList<string>)instr.data)[row] == factorTreeNode.VariableValue ? factorTreeNode.Weight : 0;
    155155          }
    156156        } else if (instr.opCode == OpCodes.LagVariable) {
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r14185 r14237  
    833833      var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
    834834      while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
    835       var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>()
     835      var groupedVarNodes = from node in subtrees.OfType<VariableTreeNodeBase>()
    836836                            let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0
    837837                            group node by node.VariableName + lag into g
    838838                            select g;
    839       var unchangedSubtrees = subtrees.Where(t => !(t is VariableTreeNode));
     839      var unchangedSubtrees = subtrees.Where(t => !(t is VariableTreeNodeBase));
    840840
    841841      foreach (var variableNodeGroup in groupedVarNodes) {
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariable.cs

    r14233 r14237  
    4141  [StorableClass]
    4242  [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")]
    43   public class FactorVariable : Symbol {
    44     #region Properties
    45     private List<string> variableNames;
    46     [Storable]
    47     public IEnumerable<string> VariableNames {
    48       get { return variableNames; }
    49       set {
    50         if (value == null) throw new ArgumentNullException();
    51         variableNames.Clear();
    52         variableNames.AddRange(value);
    53         OnChanged(EventArgs.Empty);
    54       }
    55     }
    56 
    57     private List<string> allVariableNames;
    58     [Storable]
    59     public IEnumerable<string> AllVariableNames {
    60       get { return allVariableNames; }
    61       set {
    62         if (value == null) throw new ArgumentNullException();
    63         allVariableNames.Clear();
    64         allVariableNames.AddRange(value);
    65       }
    66     }
     43  public class FactorVariable : VariableBase {
    6744
    6845    private Dictionary<string, List<string>> variableValues;
     
    8057    }
    8158
    82 
    83     public override bool Enabled {
    84       get {
    85         if (variableNames.Count == 0) return false;
    86         return base.Enabled;
    87       }
    88       set {
    89         if (variableNames.Count == 0) base.Enabled = false;
    90         else base.Enabled = value;
    91       }
    92     }
    93 
    94     private const int minimumArity = 0;
    95     private const int maximumArity = 0;
    96 
    97     public override int MinimumArity {
    98       get { return minimumArity; }
    99     }
    100     public override int MaximumArity {
    101       get { return maximumArity; }
    102     }
    103     #endregion
    104 
    10559    [StorableConstructor]
    10660    protected FactorVariable(bool deserializing)
    10761      : base(deserializing) {
    108       variableNames = new List<string>();
    109       allVariableNames = new List<string>();
    11062      variableValues = new Dictionary<string, List<string>>();
    11163    }
    11264    protected FactorVariable(FactorVariable original, Cloner cloner)
    11365      : base(original, cloner) {
    114       variableNames = new List<string>(original.variableNames);
    115       allVariableNames = new List<string>(original.allVariableNames);
    11666      variableValues =
    11767        original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new List<string>(kvp.Value));
     
    12070    public FactorVariable(string name, string description)
    12171      : base(name, description) {
    122       variableNames = new List<string>();
    123       allVariableNames = new List<string>();
    12472      variableValues = new Dictionary<string, List<string>>();
    12573    }
    12674
    127     [StorableHook(HookType.AfterDeserialization)]
    128     private void AfterDeserialization() {
    129       if (allVariableNames == null || (allVariableNames.Count == 0 && variableNames.Count > 0)) {
    130         allVariableNames = variableNames;
    131       }
    132     }
    13375    public override ISymbolicExpressionTreeNode CreateTreeNode() {
    13476      return new FactorVariableTreeNode(this);
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs

    r14233 r14237  
    2727namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2828  [StorableClass]
    29   public class FactorVariableTreeNode : SymbolicExpressionTreeTerminalNode {
     29  public class FactorVariableTreeNode : VariableTreeNodeBase {
    3030    public new FactorVariable Symbol {
    3131      get { return (FactorVariable)base.Symbol; }
    32     }
    33     [Storable]
    34     private string variableName;
    35     public string VariableName {
    36       get { return variableName; }
    37       set { variableName = value; }
    3832    }
    3933
     
    4943    protected FactorVariableTreeNode(FactorVariableTreeNode original, Cloner cloner)
    5044      : base(original, cloner) {
    51       variableName = original.variableName;
    5245      variableValue = original.variableValue;
    5346    }
     
    6154    public override void ResetLocalParameters(IRandom random) {
    6255      base.ResetLocalParameters(random);
    63       variableName = Symbol.VariableNames.SampleRandom(random);
    6456      variableValue = Symbol.GetVariableValues(VariableName).SampleRandom(random);
    6557    }
    6658
    6759    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
    68       base.ShakeLocalParameters(random, shakingFactor);
     60      // 50% additive & 50% multiplicative
     61      if (random.NextDouble() < 0.5) {
     62        double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma);
     63        Weight = Weight + x * shakingFactor;
     64      } else {
     65        double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
     66        Weight = Weight * x;
     67      }
    6968      if (random.NextDouble() < 0.2) {
    70         variableName = Symbol.VariableNames.SampleRandom(random);
     69        VariableName = Symbol.VariableNames.SampleRandom(random);
    7170      }
    7271      variableValue = Symbol.GetVariableValues(VariableName).SampleRandom(random);
     
    7877
    7978    public override string ToString() {
    80       return variableName + " = " + variableValue;
     79      return base.ToString() + " = " + variableValue;
    8180    }
    8281  }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/LaggedVariable.cs

    r14185 r14237  
    2727  [StorableClass]
    2828  [Item("LaggedVariable", "Represents a variable value with a time offset.")]
    29   public class LaggedVariable : Variable {
     29  public class LaggedVariable : VariableBase {
    3030    [Storable]
    3131    private int minLag;
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/LaggedVariableTreeNode.cs

    r14185 r14237  
    2626namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2727  [StorableClass]
    28   public sealed class LaggedVariableTreeNode : VariableTreeNode, ILaggedTreeNode {
     28  public sealed class LaggedVariableTreeNode : VariableTreeNodeBase, ILaggedTreeNode {
    2929    public new LaggedVariable Symbol {
    3030      get { return (LaggedVariable)base.Symbol; }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/Variable.cs

    r14185 r14237  
    2929  [StorableClass]
    3030  [Item("Variable", "Represents a variable value.")]
    31   public class Variable : Symbol {
    32     #region Properties
    33     [Storable]
    34     private double weightMu;
    35     public double WeightMu {
    36       get { return weightMu; }
    37       set {
    38         if (value != weightMu) {
    39           weightMu = value;
    40           OnChanged(EventArgs.Empty);
    41         }
    42       }
    43     }
    44     [Storable]
    45     private double weightSigma;
    46     public double WeightSigma {
    47       get { return weightSigma; }
    48       set {
    49         if (weightSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    50         if (value != weightSigma) {
    51           weightSigma = value;
    52           OnChanged(EventArgs.Empty);
    53         }
    54       }
    55     }
    56     [Storable]
    57     private double weightManipulatorMu;
    58     public double WeightManipulatorMu {
    59       get { return weightManipulatorMu; }
    60       set {
    61         if (value != weightManipulatorMu) {
    62           weightManipulatorMu = value;
    63           OnChanged(EventArgs.Empty);
    64         }
    65       }
    66     }
    67     [Storable]
    68     private double weightManipulatorSigma;
    69     public double WeightManipulatorSigma {
    70       get { return weightManipulatorSigma; }
    71       set {
    72         if (weightManipulatorSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    73         if (value != weightManipulatorSigma) {
    74           weightManipulatorSigma = value;
    75           OnChanged(EventArgs.Empty);
    76         }
    77       }
    78     }
    79     [Storable(DefaultValue = 0.0)]
    80     private double multiplicativeWeightManipulatorSigma;
    81     public double MultiplicativeWeightManipulatorSigma {
    82       get { return multiplicativeWeightManipulatorSigma; }
    83       set {
    84         if (multiplicativeWeightManipulatorSigma < 0.0) throw new ArgumentException("Negative sigma is not allowed.");
    85         if (value != multiplicativeWeightManipulatorSigma) {
    86           multiplicativeWeightManipulatorSigma = value;
    87           OnChanged(EventArgs.Empty);
    88         }
    89       }
    90     }
    91     private List<string> variableNames;
    92     [Storable]
    93     public IEnumerable<string> VariableNames {
    94       get { return variableNames; }
    95       set {
    96         if (value == null) throw new ArgumentNullException();
    97         variableNames.Clear();
    98         variableNames.AddRange(value);
    99         OnChanged(EventArgs.Empty);
    100       }
    101     }
    102 
    103     private List<string> allVariableNames;
    104     [Storable]
    105     public IEnumerable<string> AllVariableNames {
    106       get { return allVariableNames; }
    107       set {
    108         if (value == null) throw new ArgumentNullException();
    109         allVariableNames.Clear();
    110         allVariableNames.AddRange(value);
    111       }
    112     }
    113 
    114     public override bool Enabled {
    115       get {
    116         if (variableNames.Count == 0) return false;
    117         return base.Enabled;
    118       }
    119       set {
    120         if (variableNames.Count == 0) base.Enabled = false;
    121         else base.Enabled = value;
    122       }
    123     }
    124 
    125     private const int minimumArity = 0;
    126     private const int maximumArity = 0;
    127 
    128     public override int MinimumArity {
    129       get { return minimumArity; }
    130     }
    131     public override int MaximumArity {
    132       get { return maximumArity; }
    133     }
    134     #endregion
    135 
    136     [StorableHook(HookType.AfterDeserialization)]
    137     private void AfterDeserialization() {
    138       if (allVariableNames == null || (allVariableNames.Count == 0 && variableNames.Count > 0)) {
    139         allVariableNames = variableNames;
    140       }
    141     }
     31  public class Variable : VariableBase {
    14232
    14333    [StorableConstructor]
    14434    protected Variable(bool deserializing)
    14535      : base(deserializing) {
    146       variableNames = new List<string>();
    147       allVariableNames = new List<string>();
    14836    }
    14937    protected Variable(Variable original, Cloner cloner)
    15038      : base(original, cloner) {
    151       weightMu = original.weightMu;
    152       weightSigma = original.weightSigma;
    153       variableNames = new List<string>(original.variableNames);
    154       allVariableNames = new List<string>(original.allVariableNames);
    155       weightManipulatorMu = original.weightManipulatorMu;
    156       weightManipulatorSigma = original.weightManipulatorSigma;
    157       multiplicativeWeightManipulatorSigma = original.multiplicativeWeightManipulatorSigma;
    15839    }
    159     public Variable() : this("Variable", "Represents a variable value.") { }
    160     public Variable(string name, string description)
    161       : base(name, description) {
    162       weightMu = 1.0;
    163       weightSigma = 1.0;
    164       weightManipulatorMu = 0.0;
    165       weightManipulatorSigma = 0.05;
    166       multiplicativeWeightManipulatorSigma = 0.03;
    167       variableNames = new List<string>();
    168       allVariableNames = new List<string>();
    169     }
     40    public Variable() : base("Variable", "Represents a variable value.") { }
    17041
    17142    public override ISymbolicExpressionTreeNode CreateTreeNode() {
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableTreeNode.cs

    r14185 r14237  
    2121
    2222using HeuristicLab.Common;
    23 using HeuristicLab.Core;
    24 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2523using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    26 using HeuristicLab.Random;
    2724namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2825  [StorableClass]
    29   public class VariableTreeNode : SymbolicExpressionTreeTerminalNode {
     26  public class VariableTreeNode : VariableTreeNodeBase {
    3027    public new Variable Symbol {
    3128      get { return (Variable)base.Symbol; }
    3229    }
    33     [Storable]
    34     private double weight;
    35     public double Weight {
    36       get { return weight; }
    37       set { weight = value; }
    38     }
    39     [Storable]
    40     private string variableName;
    41     public string VariableName {
    42       get { return variableName; }
    43       set { variableName = value; }
    44     }
    45 
    4630    [StorableConstructor]
    4731    protected VariableTreeNode(bool deserializing) : base(deserializing) { }
    4832    protected VariableTreeNode(VariableTreeNode original, Cloner cloner)
    4933      : base(original, cloner) {
    50       weight = original.weight;
    51       variableName = original.variableName;
    5234    }
    5335    protected VariableTreeNode() { }
    5436    public VariableTreeNode(Variable variableSymbol) : base(variableSymbol) { }
    5537
    56     public override bool HasLocalParameters {
    57       get { return true; }
    58     }
    59 
    60     public override void ResetLocalParameters(IRandom random) {
    61       base.ResetLocalParameters(random);
    62       weight = NormalDistributedRandom.NextDouble(random, Symbol.WeightMu, Symbol.WeightSigma);
    63 
    64 #pragma warning disable 612, 618
    65       variableName = Symbol.VariableNames.SelectRandom(random);
    66 #pragma warning restore 612, 618
    67     }
    68 
    69     public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
    70       base.ShakeLocalParameters(random, shakingFactor);
    71       // 50% additive & 50% multiplicative
    72       if (random.NextDouble() < 0) {
    73         double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, Symbol.WeightManipulatorSigma);
    74         weight = weight + x * shakingFactor;
    75       } else {
    76         double x = NormalDistributedRandom.NextDouble(random, 1.0, Symbol.MultiplicativeWeightManipulatorSigma);
    77         weight = weight * x;
    78       }
    79 #pragma warning disable 612, 618
    80       variableName = Symbol.VariableNames.SelectRandom(random);
    81 #pragma warning restore 612, 618
    82     }
    83 
    8438    public override IDeepCloneable Clone(Cloner cloner) {
    8539      return new VariableTreeNode(this, cloner);
    8640    }
    87 
    88     public override string ToString() {
    89       if (weight.IsAlmost(1.0)) return variableName;
    90       else return weight.ToString("E4") + " " + variableName;
    91     }
    9241  }
    9342}
Note: See TracChangeset for help on using the changeset viewer.