Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/16/18 08:35:59 (6 years ago)
Author:
bburlacu
Message:

#1772: Refactoring and speed optimization of diversification operators

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateQualityOperator.cs

    r13876 r15906  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2121
    2222using System;
     23using System.Collections;
     24using System.Collections.Generic;
    2325using System.Linq;
    2426using HeuristicLab.Common;
     
    4042    private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
    4143
     44    #region parameters
    4245    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
    4346      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
     
    5558      get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
    5659    }
     60    #endregion
    5761
    5862    public UpdateQualityOperator() {
     
    7680    public override IOperation Apply() {
    7781      var tree = SymbolicExpressionTreeParameter.ActualValue;
    78       FixParentLinks(tree);
    7982      var problemData = ProblemDataParameter.ActualValue;
    8083      var estimationLimits = EstimationLimitsParameter.ActualValue;
    8184      var interpreter = InterpreterParameter.ActualValue;
    8285
    83       var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices).ToArray();
    84       var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray();
     86      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndices);
     87      var targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
    8588
    86       if (estimatedValues.Length != targetValues.Length)
    87         throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
     89      var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue.Value;
    8890
    89       var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
     91      IEnumerable<double> scaled;
     92      if (scaleEstimatedValues) {
     93        var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
    9094
    91       for (int i = 0; i < estimatedValues.Length; ++i) {
    92         var estimated = estimatedValues[i];
    93         var target = targetValues[i];
    94         if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
    95           linearScalingCalculator.Add(estimated, target);
     95        var e1 = estimatedValues.GetEnumerator();
     96        var e2 = targetValues.GetEnumerator();
     97
     98        int count = 0;
     99        while (e1.MoveNext() && e2.MoveNext()) {
     100          var estimated = e1.Current;
     101          var target = e2.Current;
     102          if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
     103            linearScalingCalculator.Add(estimated, target);
     104          ++count;
     105        }
     106        if (e1.MoveNext() || e2.MoveNext())
     107          throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
     108
     109        double alpha = linearScalingCalculator.Alpha;
     110        double beta = linearScalingCalculator.Beta;
     111        if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
     112          alpha = 0.0;
     113          beta = 1.0;
     114        }
     115        scaled = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
     116      } else {
     117        scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper);
    96118      }
    97       double alpha = linearScalingCalculator.Alpha;
    98       double beta = linearScalingCalculator.Beta;
    99       if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
    100         alpha = 0.0;
    101         beta = 1.0;
    102       }
    103       var scaled = estimatedValues.Select(x => x * beta + alpha).LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
     119
    104120      OnlineCalculatorError error;
    105121      var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
     
    111127
    112128      ((DoubleValue)variables["Quality"].Value).Value = r2;
    113       GenealogyGraph.GetByContent(tree).Quality = r2;
    114 
    115       var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue;
    116       if (!scaleEstimatedValues.Value)
    117         scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
     129      //GenealogyGraph.GetByContent(tree).Quality = r2;
    118130
    119131      if (variables.ContainsKey("EstimatedValues")) {
    120         variables["EstimatedValues"].Value = new DoubleArray(scaled);
     132        variables["EstimatedValues"].Value = new DoubleArray(scaled.ToArray());
    121133      } else {
    122         variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled)));
     134        variables.Add(new Core.Variable("EstimatedValues", new DoubleArray(scaled.ToArray())));
    123135      }
    124136      return base.Apply();
    125137    }
    126 
    127     private static void FixParentLinks(ISymbolicExpressionTree tree) {
    128       foreach (var node in tree.IterateNodesPrefix().Where(x => x.SubtreeCount > 0)) {
    129         foreach (var s in node.Subtrees) {
    130           if (s.Parent != node)
    131             s.Parent = node;
    132         }
    133       }
    134     }
    135138  }
    136139}
Note: See TracChangeset for help on using the changeset viewer.