Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/23/16 19:31:18 (8 years ago)
Author:
bburlacu
Message:

#2685: Add correction step for values miscalculated due to cyclical symbol dependencies in the grammar. Updated unit test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SymbolicRegressionPhenotypicDiversityAnalyzer.cs

    r14185 r14353  
    4242    private const string ProblemDataParameterName = "ProblemData";
    4343    private const string EstimationLimitsParameterName = "EstimationLimits";
     44    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4445    #endregion
    4546
     
    6061      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
    6162    }
     63    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
     64      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     65    }
    6266    #endregion
    6367
     
    7074      Parameters.Add(new ValueLookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
    7175      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
     76      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Whether or not to apply linear scaling to the estimated values"));
    7277      #endregion
    7378
     
    7984    protected SymbolicRegressionPhenotypicDiversityAnalyzer(bool deserializing)
    8085      : base(deserializing) {
     86    }
     87
     88    [StorableHook(HookType.AfterDeserialization)]
     89    private void AfterDeserialization() {
     90      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
     91        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Whether or not to apply linear scaling to the estimated values"));
    8192    }
    8293
     
    98109      }
    99110
    100       if (updateCounter.Value == updateInterval) {
    101         var trees = SymbolicExpressionTreeParameter.ActualValue;
     111      if (updateCounter.Value != updateInterval) return base.Apply();
     112
     113      var scopes = ExecutionContext.Scope.SubScopes;
     114      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
     115
     116      foreach (var scope in scopes.Where(x => !x.Variables.ContainsKey("EstimatedValues"))) {
     117        var tree = (ISymbolicExpressionTree)scope.Variables["SymbolicExpressionTree"].Value;
    102118        var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
    103119        var ds = ProblemDataParameter.ActualValue.Dataset;
    104120        var rows = ProblemDataParameter.ActualValue.TrainingIndices;
     121        var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, ds, rows).ToArray();
    105122
    106         var evaluatedValues = new ItemArray<DoubleArray>(trees.Select(t => new DoubleArray(interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray())));
    107         EvaluatedValuesParameter.ActualValue = evaluatedValues;
     123        var estimationLimits = EstimationLimitsParameter.ActualValue;
     124
     125        if (applyLinearScaling) {
     126          var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
     127          var targetValues = ds.GetDoubleValues(ProblemDataParameter.ActualValue.TargetVariable, rows);
     128          int i = 0;
     129          foreach (var target in targetValues) {
     130            var estimated = estimatedValues[i];
     131            if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
     132              linearScalingCalculator.Add(estimated, target);
     133            i++;
     134          }
     135          if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None) {
     136            var alpha = linearScalingCalculator.Alpha;
     137            var beta = linearScalingCalculator.Beta;
     138            for (i = 0; i < estimatedValues.Length; ++i) {
     139              estimatedValues[i] = estimatedValues[i] * beta + alpha;
     140            }
     141          }
     142        }
     143        // add estimated values to escope
     144        scope.Variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray())));
    108145      }
    109146      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.