Changeset 14717


Ignore:
Timestamp:
03/06/17 16:40:33 (4 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
Files:
4 edited

Legend:

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

    r14540 r14717  
    228228      // create a set of (virtual) values to match the number of weights
    229229      t.Symbol.VariableNames = new string[] { t.VariableName };
    230       t.Symbol.VariableValues = new KeyValuePair<string, List<string>>[] { new KeyValuePair<string, List<string>>(t.VariableName, weights.Select((_, i) => "X" + i).ToList()) };
     230      t.Symbol.VariableValues = new[]
     231      { new KeyValuePair<string, Dictionary<string,int>>(
     232        t.VariableName,
     233        weights.Select((_, i) => Tuple.Create(_,i)).ToDictionary(tup=>"X" + tup.Item2, tup=>tup.Item2)) };
    231234      return t;
    232235    }
  • branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs

    r14249 r14717  
    159159    [StorableHook(HookType.AfterDeserialization)]
    160160    private void AfterDeserialization() {
    161       if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
     161      if(!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
    162162        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
    163163        ApplyLinearScalingParameter.Hidden = true;
     
    165165        //it is assumed that for all symbolic regression algorithms linear scaling was set to true
    166166        //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator
    167         if (GetType().Name.Contains("SymbolicRegression"))
     167        if(GetType().Name.Contains("SymbolicRegression"))
    168168          ApplyLinearScaling.Value = true;
    169169      }
     
    213213      grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
    214214      grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
    215       foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableBase>()) {
    216         if (!varSymbol.Fixed) {
     215      foreach(var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableBase>()) {
     216        if(!varSymbol.Fixed) {
    217217          varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x));
    218218          varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<double>(x));
    219219        }
    220220      }
    221       foreach (var factorSymbol in grammar.Symbols.OfType<BinaryFactorVariable>()) {
    222         if (!factorSymbol.Fixed) {
     221      foreach(var factorSymbol in grammar.Symbols.OfType<BinaryFactorVariable>()) {
     222        if(!factorSymbol.Fixed) {
    223223          factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x));
    224224          factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x));
     
    226226            .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList());
    227227        }
    228       } foreach (var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) {
    229         if (!factorSymbol.Fixed) {
     228      }
     229      foreach(var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) {
     230        if(!factorSymbol.Fixed) {
    230231          factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x));
    231232          factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x));
    232233          factorSymbol.VariableValues = factorSymbol.VariableNames
    233             .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList());
     234            .ToDictionary(varName => varName,
     235            varName => ds.GetStringValues(varName).Distinct()
     236            .Select((n, i) => Tuple.Create(n, i))
     237            .ToDictionary(tup => tup.Item1, tup => tup.Item2));
    234238        }
    235239      }
     
    276280
    277281    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
    278       if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
     282      if(MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
    279283        MaximumSymbolicExpressionTreeDepth.Value = 3;
    280284    }
     
    304308
    305309      var handler = ProblemDataChanged;
    306       if (handler != null) handler(this, EventArgs.Empty);
     310      if(handler != null) handler(this, EventArgs.Empty);
    307311
    308312      OnReset();
     
    313317      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
    314318
    315       foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
     319      foreach(var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
    316320        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
    317321      }
    318       foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
     322      foreach(var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
    319323        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
    320324        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
    321325      }
    322       foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
     326      foreach(var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
    323327        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
    324328        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
    325329      }
    326       foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
     330      foreach(var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
    327331        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
    328332        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     
    331335        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
    332336      }
    333       foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
     337      foreach(var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
    334338        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    335339        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    336340      }
    337       foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
    338         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    339       }
    340       foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
    341         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    342       }
    343       foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
     341      foreach(var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
     342        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     343      }
     344      foreach(var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
     345        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     346      }
     347      foreach(var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
    344348        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
    345349      }
    346       foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
     350      foreach(var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
    347351        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
    348352      }
    349       foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
    350         op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
    351       }
    352       foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
     353      foreach(var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
     354        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
     355      }
     356      foreach(var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
    353357        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
    354358        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
    355359      }
    356       foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
     360      foreach(var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
    357361        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
    358362      }
    359       foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
     363      foreach(var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
    360364        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
    361365        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
  • 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.