[16556] | 1 | #region License Information
|
---|
[5607] | 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5607] | 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 |
|
---|
[16556] | 22 | using System.Collections.Generic;
|
---|
[8723] | 23 | using System.Linq;
|
---|
[5607] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[11332] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[5914] | 28 | using HeuristicLab.Optimization;
|
---|
[5607] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[16556] | 30 | using HeuristicLab.Problems.DataAnalysis.Implementation;
|
---|
[5607] | 31 |
|
---|
[5624] | 32 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[5607] | 33 | /// <summary>
|
---|
| 34 | /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
|
---|
| 35 | /// </summary>
|
---|
| 36 | [StorableClass]
|
---|
| 37 | [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
|
---|
[5717] | 38 | public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
|
---|
[5975] | 39 | private const string ModelLengthResultName = "Model Length";
|
---|
| 40 | private const string ModelDepthResultName = "Model Depth";
|
---|
[5736] | 41 |
|
---|
[8723] | 42 | private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
|
---|
| 43 | private const string EstimationLimitsResultName = "Estimation Limits";
|
---|
| 44 | private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
|
---|
| 45 | private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
|
---|
| 46 | private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
|
---|
| 47 | private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
|
---|
| 48 | private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
|
---|
| 49 | private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
|
---|
| 50 |
|
---|
[16589] | 51 | private const string IntervalEvaluationResultName = "Interval Evaluation";
|
---|
[16556] | 52 | private const string EstimatedDerivationInterval = "Interval";
|
---|
| 53 |
|
---|
[5624] | 54 | public new ISymbolicRegressionModel Model {
|
---|
| 55 | get { return (ISymbolicRegressionModel)base.Model; }
|
---|
[5717] | 56 | set { base.Model = value; }
|
---|
[5607] | 57 | }
|
---|
[5624] | 58 | ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
|
---|
| 59 | get { return (ISymbolicDataAnalysisModel)base.Model; }
|
---|
[5607] | 60 | }
|
---|
[5736] | 61 | public int ModelLength {
|
---|
| 62 | get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
|
---|
| 63 | private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
|
---|
| 64 | }
|
---|
[5607] | 65 |
|
---|
[5736] | 66 | public int ModelDepth {
|
---|
| 67 | get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
|
---|
| 68 | private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[8723] | 71 | private ResultCollection EstimationLimitsResultCollection {
|
---|
| 72 | get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
|
---|
| 73 | }
|
---|
| 74 | public DoubleLimit EstimationLimits {
|
---|
| 75 | get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | public int TrainingUpperEstimationLimitHits {
|
---|
| 79 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
|
---|
| 80 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 81 | }
|
---|
| 82 | public int TestUpperEstimationLimitHits {
|
---|
| 83 | get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
|
---|
| 84 | private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 85 | }
|
---|
| 86 | public int TrainingLowerEstimationLimitHits {
|
---|
| 87 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
|
---|
| 88 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 89 | }
|
---|
| 90 | public int TestLowerEstimationLimitHits {
|
---|
| 91 | get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
|
---|
| 92 | private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 93 | }
|
---|
| 94 | public int TrainingNaNEvaluations {
|
---|
| 95 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
|
---|
| 96 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
|
---|
| 97 | }
|
---|
| 98 | public int TestNaNEvaluations {
|
---|
| 99 | get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
|
---|
| 100 | private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[16589] | 103 | private NamedIntervals IntervalEvaluaitonCollection =>
|
---|
| 104 | (NamedIntervals) this[IntervalEvaluationResultName].Value;
|
---|
[16556] | 105 |
|
---|
| 106 |
|
---|
| 107 |
|
---|
[5607] | 108 | [StorableConstructor]
|
---|
[5717] | 109 | private SymbolicRegressionSolution(bool deserializing) : base(deserializing) { }
|
---|
| 110 | private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
|
---|
[5607] | 111 | : base(original, cloner) {
|
---|
| 112 | }
|
---|
[5624] | 113 | public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
|
---|
| 114 | : base(model, problemData) {
|
---|
[11332] | 115 | foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
|
---|
| 116 | node.SetGrammar(null);
|
---|
| 117 |
|
---|
[5736] | 118 | Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
|
---|
| 119 | Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
|
---|
[8723] | 120 |
|
---|
| 121 | ResultCollection estimationLimitResults = new ResultCollection();
|
---|
| 122 | estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
|
---|
| 123 | estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 124 | estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 125 | estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 126 | estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 127 | estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 128 | estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 129 | Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
|
---|
[6588] | 130 | RecalculateResults();
|
---|
[16556] | 131 |
|
---|
[16589] | 132 | var namedIntervalCollection = new NamedIntervals();
|
---|
| 133 | Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", namedIntervalCollection));
|
---|
| 134 | GetIntervalEvaulations(namedIntervalCollection);
|
---|
[5607] | 135 | }
|
---|
| 136 |
|
---|
| 137 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 138 | return new SymbolicRegressionSolution(this, cloner);
|
---|
| 139 | }
|
---|
[5729] | 140 |
|
---|
[8723] | 141 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 142 | private void AfterDeserialization() {
|
---|
| 143 | if (!ContainsKey(EstimationLimitsResultsResultName)) {
|
---|
| 144 | ResultCollection estimationLimitResults = new ResultCollection();
|
---|
| 145 | estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
|
---|
| 146 | estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 147 | estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 148 | estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 149 | estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 150 | estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 151 | estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 152 | Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
|
---|
| 153 | CalculateResults();
|
---|
[16556] | 154 |
|
---|
[16589] | 155 | var namedIntervalCollection = new NamedIntervals();
|
---|
| 156 | Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", namedIntervalCollection));
|
---|
| 157 | GetIntervalEvaulations(namedIntervalCollection);
|
---|
[8723] | 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[6411] | 161 | protected override void RecalculateResults() {
|
---|
[6602] | 162 | base.RecalculateResults();
|
---|
[8723] | 163 | CalculateResults();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[16589] | 166 | private void GetIntervalEvaulations(NamedIntervals intervalEvaluation) {
|
---|
[16556] | 167 | var interpreter = new IntervalInterpreter();
|
---|
[16589] | 168 | var variableRanges = (ProblemData as RegressionProblemData)?.VariableRangesParameter.Value.VariableIntervals;
|
---|
[16556] | 169 |
|
---|
[16589] | 170 | if (variableRanges != null) {
|
---|
| 171 | intervalEvaluation.Add($"Target {ProblemData.TargetVariable}", new Interval(variableRanges[ProblemData.TargetVariable].LowerBound, variableRanges[ProblemData.TargetVariable].UpperBound));
|
---|
| 172 | intervalEvaluation.Add("Modell Interval", interpreter.GetSymbolicExressionTreeInterval(Model.SymbolicExpressionTree, variableRanges));
|
---|
[16556] | 173 |
|
---|
[16589] | 174 | foreach (var derivate in variableRanges) {
|
---|
| 175 | if (derivate.Key != ProblemData.TargetVariable) {
|
---|
| 176 | var derived = DerivativeCalculator.Derive(Model.SymbolicExpressionTree, derivate.Key);
|
---|
| 177 | var derivedResultInterval = interpreter.GetSymbolicExressionTreeInterval(derived, variableRanges);
|
---|
| 178 | intervalEvaluation.Add(" ∂f/∂" + derivate.Key,
|
---|
| 179 | new Interval(derivedResultInterval.LowerBound, derivedResultInterval.UpperBound));
|
---|
| 180 | }
|
---|
[16556] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[8723] | 185 | private void CalculateResults() {
|
---|
[5736] | 186 | ModelLength = Model.SymbolicExpressionTree.Length;
|
---|
| 187 | ModelDepth = Model.SymbolicExpressionTree.Depth;
|
---|
[8723] | 188 |
|
---|
| 189 | EstimationLimits.Lower = Model.LowerEstimationLimit;
|
---|
| 190 | EstimationLimits.Upper = Model.UpperEstimationLimit;
|
---|
| 191 |
|
---|
| 192 | TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
|
---|
| 193 | TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
|
---|
| 194 | TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
|
---|
| 195 | TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
|
---|
| 196 | TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
|
---|
| 197 | TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
|
---|
[5736] | 198 | }
|
---|
[5607] | 199 | }
|
---|
| 200 | }
|
---|