Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/08/13 15:36:13 (12 years ago)
Author:
mkommend
Message:

#1837: Updated sliding window GP branch with trunk changes.

Location:
branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4

    • Property svn:ignore
      •  

        old new  
         1*.user
         2Plugin.cs
        13bin
        2 *.user
        3 HeuristicLabProblemsDataAnalysisSymbolicPlugin.cs
        44obj
        5 *.vs10x
        6 Plugin.cs
  • branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs

    r7259 r9126  
    2828using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2929using HeuristicLab.Operators;
     30using HeuristicLab.Optimization;
    3031using HeuristicLab.Parameters;
    3132using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3536  [StorableClass]
    3637  public abstract class SymbolicDataAnalysisEvaluator<T> : SingleSuccessorOperator,
    37     ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator
     38    ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, IStochasticOperator
    3839  where T : class, IDataAnalysisProblemData {
    3940    private const string RandomParameterName = "Random";
     
    4445    private const string EvaluationPartitionParameterName = "EvaluationPartition";
    4546    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
     47    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
     48    private const string ValidRowIndicatorParameterName = "ValidRowIndicator";
    4649
    4750    public override bool CanChangeName { get { return false; } }
    4851
    4952    #region parameter properties
     53    ILookupParameter<IRandom> IStochasticOperator.RandomParameter {
     54      get { return RandomParameter; }
     55    }
     56
    5057    public IValueLookupParameter<IRandom> RandomParameter {
    5158      get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
     
    7077      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
    7178    }
     79    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
     80      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
     81    }
     82    public IValueLookupParameter<StringValue> ValidRowIndicatorParameter {
     83      get { return (IValueLookupParameter<StringValue>)Parameters[ValidRowIndicatorParameterName]; }
     84    }
    7285    #endregion
    7386
     
    87100      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."));
    88101      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."));
     102      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
     103      Parameters.Add(new ValueLookupParameter<StringValue>(ValidRowIndicatorParameterName, "An indicator variable in the data set that specifies which rows should be evaluated (those for which the indicator <> 0) (optional)."));
     104    }
     105
     106    [StorableHook(HookType.AfterDeserialization)]
     107    private void AfterDeserialization() {
     108      if (Parameters.ContainsKey(ApplyLinearScalingParameterName) && !(Parameters[ApplyLinearScalingParameterName] is LookupParameter<BoolValue>))
     109        Parameters.Remove(ApplyLinearScalingParameterName);
     110      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName))
     111        Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
     112      if (!Parameters.ContainsKey(ValidRowIndicatorParameterName))
     113        Parameters.Add(new ValueLookupParameter<StringValue>(ValidRowIndicatorParameterName, "An indicator variable in the data set that specifies which rows should be evaluated (those for which the indicator <> 0) (optional)."));
    89114    }
    90115
     
    94119
    95120    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
    96 
    97 
    98121      IEnumerable<int> rows;
    99122      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
     
    101124      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
    102125      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
    103 
    104126      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
    105127
     
    113135      }
    114136
    115       return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
     137      rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
     138      if (ValidRowIndicatorParameter.ActualValue != null) {
     139        string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
     140        var problemData = ProblemDataParameter.ActualValue;
     141        var indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
     142        rows = rows.Where(r => !indicatorRow[r].IsAlmost(0.0));
     143      }
     144      return rows;
     145    }
     146
     147    [ThreadStatic]
     148    private static double[] cache;
     149    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
     150      double lowerEstimationLimit, double upperEstimationLimit,
     151      IOnlineCalculator calculator, int maxRows) {
     152      if (cache == null || cache.Length < maxRows) {
     153        cache = new double[maxRows];
     154      }
     155
     156      // calculate linear scaling
     157      int i = 0;
     158      var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
     159      var targetValuesEnumerator = targetValues.GetEnumerator();
     160      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
     161      while (targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext()) {
     162        double target = targetValuesEnumerator.Current;
     163        double estimated = estimatedValuesEnumerator.Current;
     164        cache[i] = estimated;
     165        if (!double.IsNaN(estimated) && !double.IsInfinity(estimated))
     166          linearScalingCalculator.Add(estimated, target);
     167        i++;
     168      }
     169      if (linearScalingCalculator.ErrorState == OnlineCalculatorError.None && (targetValuesEnumerator.MoveNext() || estimatedValuesEnumerator.MoveNext()))
     170        throw new ArgumentException("Number of elements in target and estimated values enumeration do not match.");
     171
     172      double alpha = linearScalingCalculator.Alpha;
     173      double beta = linearScalingCalculator.Beta;
     174      if (linearScalingCalculator.ErrorState != OnlineCalculatorError.None) {
     175        alpha = 0.0;
     176        beta = 1.0;
     177      }
     178
     179      //calculate the quality by using the passed online calculator
     180      targetValuesEnumerator = targetValues.GetEnumerator();
     181      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
     182        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
     183
     184      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
     185        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
     186      }
    116187    }
    117188  }
Note: See TracChangeset for help on using the changeset viewer.