[5500] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5500] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
[9037] | 30 | using HeuristicLab.Optimization;
|
---|
[5500] | 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Random;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[5689] | 36 | [StorableClass]
|
---|
[10291] | 37 | public abstract class SymbolicDataAnalysisEvaluator<T> : InstrumentedOperator,
|
---|
[9037] | 38 | ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator, IStochasticOperator
|
---|
[5509] | 39 | where T : class, IDataAnalysisProblemData {
|
---|
[5500] | 40 | private const string RandomParameterName = "Random";
|
---|
| 41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
[5514] | 42 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
[5500] | 43 | private const string ProblemDataParameterName = "ProblemData";
|
---|
[5770] | 44 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
[5759] | 45 | private const string EvaluationPartitionParameterName = "EvaluationPartition";
|
---|
[5500] | 46 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
[8664] | 47 | private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
|
---|
[8798] | 48 | private const string ValidRowIndicatorParameterName = "ValidRowIndicator";
|
---|
[5500] | 49 |
|
---|
[5618] | 50 | public override bool CanChangeName { get { return false; } }
|
---|
| 51 |
|
---|
[5500] | 52 | #region parameter properties
|
---|
[9037] | 53 | ILookupParameter<IRandom> IStochasticOperator.RandomParameter {
|
---|
| 54 | get { return RandomParameter; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[5514] | 57 | public IValueLookupParameter<IRandom> RandomParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
[5500] | 59 | }
|
---|
| 60 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 61 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 62 | }
|
---|
[5649] | 63 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
|
---|
| 64 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
|
---|
[5500] | 65 | }
|
---|
[5514] | 66 | public IValueLookupParameter<T> ProblemDataParameter {
|
---|
| 67 | get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
[5500] | 68 | }
|
---|
| 69 |
|
---|
[5759] | 70 | public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
|
---|
| 71 | get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
|
---|
[5500] | 72 | }
|
---|
[5770] | 73 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 74 | get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
[5500] | 75 | }
|
---|
[5759] | 76 | public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
| 77 | get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
[5500] | 78 | }
|
---|
[8664] | 79 | public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
|
---|
| 80 | get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
|
---|
| 81 | }
|
---|
[8798] | 82 | public IValueLookupParameter<StringValue> ValidRowIndicatorParameter {
|
---|
| 83 | get { return (IValueLookupParameter<StringValue>)Parameters[ValidRowIndicatorParameterName]; }
|
---|
| 84 | }
|
---|
[5500] | 85 | #endregion
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | [StorableConstructor]
|
---|
| 89 | protected SymbolicDataAnalysisEvaluator(bool deserializing) : base(deserializing) { }
|
---|
[5509] | 90 | protected SymbolicDataAnalysisEvaluator(SymbolicDataAnalysisEvaluator<T> original, Cloner cloner)
|
---|
[5500] | 91 | : base(original, cloner) {
|
---|
| 92 | }
|
---|
| 93 | public SymbolicDataAnalysisEvaluator()
|
---|
| 94 | : base() {
|
---|
[5514] | 95 | Parameters.Add(new ValueLookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
[5649] | 96 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
|
---|
[5618] | 97 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
|
---|
[5514] | 98 | Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
[5759] | 99 | Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
|
---|
[5770] | 100 | 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."));
|
---|
[5759] | 101 | 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."));
|
---|
[8664] | 102 | Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating."));
|
---|
[8798] | 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)."));
|
---|
[5500] | 104 | }
|
---|
| 105 |
|
---|
[8664] | 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."));
|
---|
[8798] | 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)."));
|
---|
[8664] | 114 | }
|
---|
| 115 |
|
---|
[5500] | 116 | protected IEnumerable<int> GenerateRowsToEvaluate() {
|
---|
[5914] | 117 | return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
|
---|
| 121 | IEnumerable<int> rows;
|
---|
[5759] | 122 | int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
|
---|
| 123 | int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
|
---|
[5823] | 124 | int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
|
---|
| 125 | int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
|
---|
[5759] | 126 | if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
|
---|
[5914] | 127 |
|
---|
| 128 | if (percentageOfRows.IsAlmost(1.0))
|
---|
| 129 | rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
|
---|
| 130 | else {
|
---|
| 131 | int seed = RandomParameter.ActualValue.Next();
|
---|
| 132 | int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
|
---|
| 133 | if (count == 0) count = 1;
|
---|
| 134 | rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[8798] | 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;
|
---|
[5500] | 145 | }
|
---|
[8664] | 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) {
|
---|
[8974] | 152 | if (cache == null || cache.Length < maxRows) {
|
---|
[8664] | 153 | cache = new double[maxRows];
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[8838] | 156 | // calculate linear scaling
|
---|
[8664] | 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 | }
|
---|
| 187 | }
|
---|
[5500] | 188 | }
|
---|
| 189 | }
|
---|