Free cookie consent management tool by TermsFeed Policy Generator

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.