Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/17/12 11:18:40 (12 years ago)
Author:
mkommend
Message:

#1951:

  • Added linear scaling parameter to data analysis problems.
  • Adapted interfaces, evaluators and analyzers accordingly.
  • Added OnlineBoundedMeanSquaredErrorCalculator.
  • Adapted symbolic regression sample unit test.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs

    r7259 r8664  
    4444    private const string EvaluationPartitionParameterName = "EvaluationPartition";
    4545    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
     46    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    4647
    4748    public override bool CanChangeName { get { return false; } }
     
    7071      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
    7172    }
     73    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
     74      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     75    }
    7276    #endregion
    7377
     
    8791      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."));
    8892      Parameters.Add(new ValueLookupParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index."));
     93      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
     94    }
     95
     96    [StorableHook(HookType.AfterDeserialization)]
     97    private void AfterDeserialization() {
     98      if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>))
     99        Parameters.Remove(ApplyLinearScalingParameterName);
     100      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
     101        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
    89102    }
    90103
     
    94107
    95108    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
    96 
    97 
    98109      IEnumerable<int> rows;
    99110      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
     
    115126      return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
    116127    }
     128
     129    [ThreadStatic]
     130    private static double[] cache;
     131    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
     132      double lowerEstimationLimit, double upperEstimationLimit,
     133      IOnlineCalculator calculator, int maxRows) {
     134      if (cache == null || cache.GetLength(0) < maxRows) {
     135        cache = new double[maxRows];
     136      }
     137
     138      //calculate linear scaling
     139      //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements
     140      //this is not true if the cache is used
     141      int i = 0;
     142      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
     143      var targetValuesEnumerator = targetValues.GetEnumerator();
     144      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
     145      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
     146        double target = targetValuesEnumerator.Current;
     147        double estimated = estimatedValuesEnumerator.Current;
     148        cache[i] = estimated;
     149        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
     150          linearScalingCalculator.Add(estimated, target);
     151        i++;
     152      }
     153      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
     154        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
     155
     156      double alpha = linearScalingCalculator.Alpha;
     157      double beta = linearScalingCalculator.Beta;
     158      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
     159        alpha = 0.0;
     160        beta = 1.0;
     161      }
     162
     163      //calculate the quality by using the passed online calculator
     164      targetValuesEnumerator = targetValues.GetEnumerator();
     165      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
     166        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
     167
     168      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
     169        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
     170      }
     171    }
    117172  }
    118173}
Note: See TracChangeset for help on using the changeset viewer.