Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Evaluators/SymbolicDataAnalysisEvaluator.cs @ 7615

Last change on this file since 7615 was 7615, checked in by gkronber, 12 years ago

#1081 merged r7462:7609 from trunk into time series branch

File size: 7.7 KB
RevLine 
[5500]1#region License Information
2/* HeuristicLab
[7268]3 * Copyright (C) 2002-2012 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Random;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[5689]35  [StorableClass]
[5509]36  public abstract class SymbolicDataAnalysisEvaluator<T> : SingleSuccessorOperator,
[5720]37    ISymbolicDataAnalysisEvaluator<T>, ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator
[5509]38  where T : class, IDataAnalysisProblemData {
[5500]39    private const string RandomParameterName = "Random";
40    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
[5514]41    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
[5500]42    private const string ProblemDataParameterName = "ProblemData";
[5770]43    private const string EstimationLimitsParameterName = "EstimationLimits";
[5759]44    private const string EvaluationPartitionParameterName = "EvaluationPartition";
[5500]45    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
[7615]46    private const string ValidRowIndicatorParameterName = "ValidRowIndicator";
[5500]47
[5618]48    public override bool CanChangeName { get { return false; } }
49
[5500]50    #region parameter properties
[5514]51    public IValueLookupParameter<IRandom> RandomParameter {
52      get { return (IValueLookupParameter<IRandom>)Parameters[RandomParameterName]; }
[5500]53    }
54    public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
55      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
56    }
[5649]57    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
58      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
[5500]59    }
[5514]60    public IValueLookupParameter<T> ProblemDataParameter {
61      get { return (IValueLookupParameter<T>)Parameters[ProblemDataParameterName]; }
[5500]62    }
63
[5759]64    public IValueLookupParameter<IntRange> EvaluationPartitionParameter {
65      get { return (IValueLookupParameter<IntRange>)Parameters[EvaluationPartitionParameterName]; }
[5500]66    }
[5770]67    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
68      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
[5500]69    }
[5759]70    public IValueLookupParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
71      get { return (IValueLookupParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
[5500]72    }
[7615]73    public IValueLookupParameter<StringValue> ValidRowIndicatorParameter {
74      get { return (IValueLookupParameter<StringValue>)Parameters[ValidRowIndicatorParameterName]; }
75    }
[5500]76    #endregion
77
78
79    [StorableConstructor]
80    protected SymbolicDataAnalysisEvaluator(bool deserializing) : base(deserializing) { }
[5509]81    protected SymbolicDataAnalysisEvaluator(SymbolicDataAnalysisEvaluator<T> original, Cloner cloner)
[5500]82      : base(original, cloner) {
83    }
84    public SymbolicDataAnalysisEvaluator()
85      : base() {
[5514]86      Parameters.Add(new ValueLookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
[5649]87      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
[5618]88      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic data analysis solution encoded as a symbolic expression tree."));
[5514]89      Parameters.Add(new ValueLookupParameter<T>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
[5759]90      Parameters.Add(new ValueLookupParameter<IntRange>(EvaluationPartitionParameterName, "The start index of the dataset partition on which the symbolic data analysis solution should be evaluated."));
[5770]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."));
[5759]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."));
[7615]93      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]94    }
95
[7615]96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      if(!Parameters.ContainsKey(ValidRowIndicatorParameterName))
99        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)."));
100    }
101
[5500]102    protected IEnumerable<int> GenerateRowsToEvaluate() {
[5914]103      return GenerateRowsToEvaluate(RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
104    }
105
[7615]106    protected IEnumerable<int> GenerateRowsToEvaluate(double percentageOfRows)
107    {
[5914]108
109      IEnumerable<int> rows;
[5759]110      int samplesStart = EvaluationPartitionParameter.ActualValue.Start;
111      int samplesEnd = EvaluationPartitionParameter.ActualValue.End;
[5823]112      int testPartitionStart = ProblemDataParameter.ActualValue.TestPartition.Start;
113      int testPartitionEnd = ProblemDataParameter.ActualValue.TestPartition.End;
[5618]114
[5759]115      if (samplesEnd < samplesStart) throw new ArgumentException("Start value is larger than end value.");
[5914]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
[7615]126      rows = rows.Where(i => i < testPartitionStart || testPartitionEnd <= i);
127     
128      if(ValidRowIndicatorParameter.ActualValue != null)
129      {
130        string indicatorVar = ValidRowIndicatorParameter.ActualValue.Value;
131        var problemData = ProblemDataParameter.ActualValue;
132        var indicatorRow = problemData.Dataset.GetReadOnlyDoubleValues(indicatorVar);
133        rows = rows.Where(r=>!indicatorRow[r].IsAlmost(0.0));
134      }
135      return rows;
[5500]136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.