1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.Random;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
35 | [StorableClass]
|
---|
36 | public abstract class SymbolicDataAnalysisEvaluator<T> : SingleSuccessorOperator,
|
---|
37 | ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator
|
---|
38 | where T : class, IDataAnalysisProblemData {
|
---|
39 | private const string RandomParameterName = "Random";
|
---|
40 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
41 | private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
42 | private const string ProblemDataParameterName = "ProblemData";
|
---|
43 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
44 | private const string EvaluationPartitionParameterName = "EvaluationPartition";
|
---|
45 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
46 | private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
|
---|
47 |
|
---|
48 | public override bool CanChangeName { get { return false; } }
|
---|
49 |
|
---|
50 | #region parameter properties
|
---|
51 | public IValueLookupParameter<IRandom> RandomParameter {
|
---|
52 | get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
55 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
56 | }
|
---|
57 | public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
|
---|
58 | get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
|
---|
59 | }
|
---|
60 | public IValueLookupParameter<T> ProblemDataParameter {
|
---|
61 | get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
|
---|
65 | get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
|
---|
66 | }
|
---|
67 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
68 | get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
69 | }
|
---|
70 | public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
71 | get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
72 | }
|
---|
73 | public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
|
---|
74 | get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
|
---|
75 | }
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 |
|
---|
79 | [StorableConstructor]
|
---|
80 | protected SymbolicDataAnalysisEvaluator(bool deserializing) : base(deserializing) { }
|
---|
81 | protected SymbolicDataAnalysisEvaluator(SymbolicDataAnalysisEvaluator<T> original, Cloner cloner)
|
---|
82 | : base(original, cloner) {
|
---|
83 | }
|
---|
84 | public SymbolicDataAnalysisEvaluator()
|
---|
85 | : base() {
|
---|
86 | Parameters.Add(new ValueLookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
87 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
|
---|
88 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
|
---|
89 | Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
|
---|
90 | Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
|
---|
91 | 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."));
|
---|
92 | 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."));
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected IEnumerable<int> GenerateRowsToEvaluate() {
|
---|
105 | return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows) {
|
---|
109 | IEnumerable<int> rows;
|
---|
110 | int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
|
---|
111 | int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
|
---|
112 | int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
|
---|
113 | int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
|
---|
114 |
|
---|
115 | if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
|
---|
116 |
|
---|
117 | if (percentageOfRows.IsAlmost(1.0))
|
---|
118 | rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
|
---|
119 | else {
|
---|
120 | int seed = RandomParameter.ActualValue.Next();
|
---|
121 | int count = (int)((samplesEnd - samplesStart) * percentageOfRows);
|
---|
122 | if (count == 0) count = 1;
|
---|
123 | rows = RandomEnumerable.SampleRandomNumbers(seed, samplesStart, samplesEnd, count);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
|
---|
127 | }
|
---|
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 | }
|
---|
172 | }
|
---|
173 | }
|
---|