Changeset 14717 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols
- Timestamp:
- 03/06/17 16:40:33 (8 years ago)
- Location:
- branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariable.cs
r14249 r14717 31 31 [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")] 32 32 public class FactorVariable : VariableBase { 33 private readonly Dictionary<string, List<string>> variableValues;33 private readonly Dictionary<string, Dictionary<string, int>> variableValues; // for each variable value also store a zero-based index 34 34 [Storable] 35 public IEnumerable<KeyValuePair<string, List<string>>> VariableValues {35 public IEnumerable<KeyValuePair<string, Dictionary<string, int>>> VariableValues { 36 36 get { return variableValues; } 37 37 set { 38 if 38 if(value == null) throw new ArgumentNullException(); 39 39 variableValues.Clear(); 40 foreach 41 variableValues.Add(kvp.Key, new List<string>(kvp.Value));40 foreach(var kvp in value) { 41 variableValues.Add(kvp.Key, new Dictionary<string, int>(kvp.Value)); 42 42 } 43 43 } … … 47 47 protected FactorVariable(bool deserializing) 48 48 : base(deserializing) { 49 variableValues = new Dictionary<string, List<string>>();49 variableValues = new Dictionary<string, Dictionary<string, int>>(); 50 50 } 51 51 protected FactorVariable(FactorVariable original, Cloner cloner) 52 52 : base(original, cloner) { 53 53 variableValues = 54 original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new List<string>(kvp.Value));54 original.variableValues.ToDictionary(kvp => kvp.Key, kvp => new Dictionary<string, int>(kvp.Value)); 55 55 } 56 56 public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { } 57 57 public FactorVariable(string name, string description) 58 58 : base(name, description) { 59 variableValues = new Dictionary<string, List<string>>();59 variableValues = new Dictionary<string, Dictionary<string,int>>(); 60 60 } 61 61 … … 69 69 70 70 public IEnumerable<string> GetVariableValues(string variableName) { 71 return variableValues[variableName]; 71 return variableValues[variableName].Keys; 72 } 73 74 public int GetIndexForValue(string variableName, string variableValue) { 75 return variableValues[variableName][variableValue]; 72 76 } 73 77 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs
r14554 r14717 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Common; … … 51 52 : base(original, cloner) { 52 53 variableName = original.variableName; 53 if 54 if(original.weights != null) { 54 55 this.weights = new double[original.Weights.Length]; 55 56 Array.Copy(original.Weights, weights, weights.Length); … … 74 75 75 76 public override void ShakeLocalParameters(IRandom random, double shakingFactor) { 76 if 77 if(random.NextDouble() < 0.2) { 77 78 VariableName = Symbol.VariableNames.SampleRandom(random); 78 if 79 if(weights.Length != Symbol.GetVariableValues(VariableName).Count()) { 79 80 // if the length of the weight array does not match => re-initialize weights 80 81 weights = … … 87 88 var idx = random.Next(weights.Length); 88 89 // 50% additive & 50% multiplicative 89 if 90 if(random.NextDouble() < 0.5) { 90 91 double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu, 91 92 Symbol.WeightManipulatorSigma); … … 103 104 104 105 public double GetValue(string cat) { 105 // TODO: perf 106 var s = Symbol; 107 int idx = 0; 108 foreach (var val in s.GetVariableValues(VariableName)) { 109 if (cat == val) return weights[idx]; 110 idx++; 111 } 112 throw new ArgumentOutOfRangeException("Found unknown value " + cat + " for variable " + VariableName); 106 return weights[Symbol.GetIndexForValue(VariableName, cat)]; 113 107 } 114 108
Note: See TracChangeset
for help on using the changeset viewer.