1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
33 | /// <summary>
|
---|
34 | /// Analyzer to use in combination with eps-lexicase selection
|
---|
35 | /// </summary>
|
---|
36 | [Item("EpsLexicaseAnalyzer", "")]
|
---|
37 | [StorableClass]
|
---|
38 | public sealed class EpsLexicaseAnalyzer : SymbolicDataAnalysisSingleObjectiveAnalyzer {
|
---|
39 |
|
---|
40 | [StorableConstructor]
|
---|
41 | private EpsLexicaseAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
42 | private EpsLexicaseAnalyzer(EpsLexicaseAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
44 | return new EpsLexicaseAnalyzer(this, cloner);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public EpsLexicaseAnalyzer()
|
---|
48 | : base() {
|
---|
49 | Parameters.Add(new LookupParameter<IRegressionProblemData>("ProblemData"));
|
---|
50 | Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter"));
|
---|
51 | Parameters.Add(new LookupParameter<DoubleLimit>("EstimationLimits"));
|
---|
52 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Errors", 1));
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override IOperation Apply() {
|
---|
56 | var problemData = (IRegressionProblemData)Parameters["ProblemData"].ActualValue;
|
---|
57 | var ds = problemData.Dataset;
|
---|
58 | var trainingRows = problemData.TrainingIndices.ToArray();
|
---|
59 | var y = problemData.TargetVariableTrainingValues.ToArray();
|
---|
60 | var trees = SymbolicExpressionTree;
|
---|
61 | var interpreter = (ISymbolicDataAnalysisExpressionTreeInterpreter)Parameters["SymbolicExpressionTreeInterpreter"].ActualValue;
|
---|
62 | var limits = (DoubleLimit)Parameters["EstimationLimits"].ActualValue;
|
---|
63 |
|
---|
64 | var errors = new ItemArray<DoubleArray>(trees.Length);
|
---|
65 | int i = 0;
|
---|
66 | foreach (var tree in trees) {
|
---|
67 | var model = new SymbolicRegressionModel(problemData.TargetVariable, tree, (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter.Clone(), limits.Lower, limits.Upper);
|
---|
68 | if (ApplyLinearScalingParameter.ActualValue.Value)
|
---|
69 | model.Scale(problemData);
|
---|
70 |
|
---|
71 | errors[i] = new DoubleArray(y.Zip(model.GetEstimatedValues(ds, trainingRows), (yi, pi) => Math.Abs(yi - pi)).ToArray());
|
---|
72 | i++;
|
---|
73 | }
|
---|
74 | Parameters["Errors"].ActualValue = errors;
|
---|
75 |
|
---|
76 | return base.Apply();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|