Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/06/17 16:40:33 (8 years ago)
Author:
gkronber
Message:

#2650: use a dictionary of variable values instead of a list in FactorVariable (symbol) to remove sequential search for variable value in FactorVariableTreeNode

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  
    3131  [Item("FactorVariable", "Represents a categorical variable (comparable to factors as in R).")]
    3232  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
    3434    [Storable]
    35     public IEnumerable<KeyValuePair<string, List<string>>> VariableValues {
     35    public IEnumerable<KeyValuePair<string, Dictionary<string, int>>> VariableValues {
    3636      get { return variableValues; }
    3737      set {
    38         if (value == null) throw new ArgumentNullException();
     38        if(value == null) throw new ArgumentNullException();
    3939        variableValues.Clear();
    40         foreach (var kvp in value) {
    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));
    4242        }
    4343      }
     
    4747    protected FactorVariable(bool deserializing)
    4848      : base(deserializing) {
    49       variableValues = new Dictionary<string, List<string>>();
     49      variableValues = new Dictionary<string, Dictionary<string, int>>();
    5050    }
    5151    protected FactorVariable(FactorVariable original, Cloner cloner)
    5252      : base(original, cloner) {
    5353      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));
    5555    }
    5656    public FactorVariable() : this("FactorVariable", "Represents a categorical variable (comparable to factors as in R).") { }
    5757    public FactorVariable(string name, string description)
    5858      : base(name, description) {
    59       variableValues = new Dictionary<string, List<string>>();
     59      variableValues = new Dictionary<string, Dictionary<string,int>>();
    6060    }
    6161
     
    6969
    7070    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];
    7276    }
    7377  }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/FactorVariableTreeNode.cs

    r14554 r14717  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Common;
     
    5152      : base(original, cloner) {
    5253      variableName = original.variableName;
    53       if (original.weights != null) {
     54      if(original.weights != null) {
    5455        this.weights = new double[original.Weights.Length];
    5556        Array.Copy(original.Weights, weights, weights.Length);
     
    7475
    7576    public override void ShakeLocalParameters(IRandom random, double shakingFactor) {
    76       if (random.NextDouble() < 0.2) {
     77      if(random.NextDouble() < 0.2) {
    7778        VariableName = Symbol.VariableNames.SampleRandom(random);
    78         if (weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
     79        if(weights.Length != Symbol.GetVariableValues(VariableName).Count()) {
    7980          // if the length of the weight array does not match => re-initialize weights
    8081          weights =
     
    8788        var idx = random.Next(weights.Length);
    8889        // 50% additive & 50% multiplicative
    89         if (random.NextDouble() < 0.5) {
     90        if(random.NextDouble() < 0.5) {
    9091          double x = NormalDistributedRandom.NextDouble(random, Symbol.WeightManipulatorMu,
    9192            Symbol.WeightManipulatorSigma);
     
    103104
    104105    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)];
    113107    }
    114108
Note: See TracChangeset for help on using the changeset viewer.