[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;
|
---|
[16627] | 31 | using HEAL.Attic;
|
---|
[5607] | 32 |
|
---|
[5624] | 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[5607] | 34 | /// <summary>
|
---|
| 35 | /// Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity
|
---|
| 36 | /// </summary>
|
---|
[16627] | 37 | [StorableType("CC27F20C-5925-406B-9C38-3C4B2B781C9F")]
|
---|
[5607] | 38 | [Item(Name = "SymbolicRegressionSolution", Description = "Represents a symbolic regression solution (model + data) and attributes of the solution like accuracy and complexity.")]
|
---|
[5717] | 39 | public sealed class SymbolicRegressionSolution : RegressionSolution, ISymbolicRegressionSolution {
|
---|
[5975] | 40 | private const string ModelLengthResultName = "Model Length";
|
---|
| 41 | private const string ModelDepthResultName = "Model Depth";
|
---|
[5736] | 42 |
|
---|
[8723] | 43 | private const string EstimationLimitsResultsResultName = "Estimation Limits Results";
|
---|
| 44 | private const string EstimationLimitsResultName = "Estimation Limits";
|
---|
| 45 | private const string TrainingUpperEstimationLimitHitsResultName = "Training Upper Estimation Limit Hits";
|
---|
| 46 | private const string TestLowerEstimationLimitHitsResultName = "Test Lower Estimation Limit Hits";
|
---|
| 47 | private const string TrainingLowerEstimationLimitHitsResultName = "Training Lower Estimation Limit Hits";
|
---|
| 48 | private const string TestUpperEstimationLimitHitsResultName = "Test Upper Estimation Limit Hits";
|
---|
| 49 | private const string TrainingNaNEvaluationsResultName = "Training NaN Evaluations";
|
---|
| 50 | private const string TestNaNEvaluationsResultName = "Test NaN Evaluations";
|
---|
| 51 |
|
---|
[16589] | 52 | private const string IntervalEvaluationResultName = "Interval Evaluation";
|
---|
[16556] | 53 | private const string EstimatedDerivationInterval = "Interval";
|
---|
| 54 |
|
---|
[5624] | 55 | public new ISymbolicRegressionModel Model {
|
---|
| 56 | get { return (ISymbolicRegressionModel)base.Model; }
|
---|
[5717] | 57 | set { base.Model = value; }
|
---|
[5607] | 58 | }
|
---|
[5624] | 59 | ISymbolicDataAnalysisModel ISymbolicDataAnalysisSolution.Model {
|
---|
| 60 | get { return (ISymbolicDataAnalysisModel)base.Model; }
|
---|
[5607] | 61 | }
|
---|
[5736] | 62 | public int ModelLength {
|
---|
| 63 | get { return ((IntValue)this[ModelLengthResultName].Value).Value; }
|
---|
| 64 | private set { ((IntValue)this[ModelLengthResultName].Value).Value = value; }
|
---|
| 65 | }
|
---|
[5607] | 66 |
|
---|
[5736] | 67 | public int ModelDepth {
|
---|
| 68 | get { return ((IntValue)this[ModelDepthResultName].Value).Value; }
|
---|
| 69 | private set { ((IntValue)this[ModelDepthResultName].Value).Value = value; }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[8723] | 72 | private ResultCollection EstimationLimitsResultCollection {
|
---|
| 73 | get { return (ResultCollection)this[EstimationLimitsResultsResultName].Value; }
|
---|
| 74 | }
|
---|
| 75 | public DoubleLimit EstimationLimits {
|
---|
| 76 | get { return (DoubleLimit)EstimationLimitsResultCollection[EstimationLimitsResultName].Value; }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public int TrainingUpperEstimationLimitHits {
|
---|
| 80 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value; }
|
---|
| 81 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingUpperEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 82 | }
|
---|
| 83 | public int TestUpperEstimationLimitHits {
|
---|
| 84 | get { return ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value; }
|
---|
| 85 | private set { ((IntValue)EstimationLimitsResultCollection[TestUpperEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 86 | }
|
---|
| 87 | public int TrainingLowerEstimationLimitHits {
|
---|
| 88 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value; }
|
---|
| 89 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingLowerEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 90 | }
|
---|
| 91 | public int TestLowerEstimationLimitHits {
|
---|
| 92 | get { return ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value; }
|
---|
| 93 | private set { ((IntValue)EstimationLimitsResultCollection[TestLowerEstimationLimitHitsResultName].Value).Value = value; }
|
---|
| 94 | }
|
---|
| 95 | public int TrainingNaNEvaluations {
|
---|
| 96 | get { return ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value; }
|
---|
| 97 | private set { ((IntValue)EstimationLimitsResultCollection[TrainingNaNEvaluationsResultName].Value).Value = value; }
|
---|
| 98 | }
|
---|
| 99 | public int TestNaNEvaluations {
|
---|
| 100 | get { return ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value; }
|
---|
| 101 | private set { ((IntValue)EstimationLimitsResultCollection[TestNaNEvaluationsResultName].Value).Value = value; }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[16556] | 104 |
|
---|
[16627] | 105 | public NamedIntervals IntervalEvaluationCollection {
|
---|
| 106 | get { return (NamedIntervals)this[IntervalEvaluationResultName].Value; }
|
---|
| 107 | private set { this[IntervalEvaluationResultName].Value = value; }
|
---|
| 108 | }
|
---|
[16556] | 109 |
|
---|
| 110 |
|
---|
[16627] | 111 |
|
---|
[5607] | 112 | [StorableConstructor]
|
---|
[16627] | 113 | private SymbolicRegressionSolution(StorableConstructorFlag _) : base(_) { }
|
---|
[5717] | 114 | private SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner)
|
---|
[5607] | 115 | : base(original, cloner) {
|
---|
| 116 | }
|
---|
[5624] | 117 | public SymbolicRegressionSolution(ISymbolicRegressionModel model, IRegressionProblemData problemData)
|
---|
| 118 | : base(model, problemData) {
|
---|
[11332] | 119 | foreach (var node in model.SymbolicExpressionTree.Root.IterateNodesPrefix().OfType<SymbolicExpressionTreeTopLevelNode>())
|
---|
| 120 | node.SetGrammar(null);
|
---|
| 121 |
|
---|
[5736] | 122 | Add(new Result(ModelLengthResultName, "Length of the symbolic regression model.", new IntValue()));
|
---|
| 123 | Add(new Result(ModelDepthResultName, "Depth of the symbolic regression model.", new IntValue()));
|
---|
[8723] | 124 |
|
---|
| 125 | ResultCollection estimationLimitResults = new ResultCollection();
|
---|
| 126 | estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
|
---|
| 127 | estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 128 | estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 129 | estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 130 | estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 131 | estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 132 | estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 133 | Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
|
---|
[16635] | 134 | Add(new Result(IntervalEvaluationResultName, "Results concerning the derivation of symbolic regression solution", GetIntervalEvaluations()));
|
---|
[6588] | 135 | RecalculateResults();
|
---|
[16556] | 136 |
|
---|
[16627] | 137 | ;
|
---|
[5607] | 138 | }
|
---|
| 139 |
|
---|
| 140 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 141 | return new SymbolicRegressionSolution(this, cloner);
|
---|
| 142 | }
|
---|
[5729] | 143 |
|
---|
[8723] | 144 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 145 | private void AfterDeserialization() {
|
---|
| 146 | if (!ContainsKey(EstimationLimitsResultsResultName)) {
|
---|
| 147 | ResultCollection estimationLimitResults = new ResultCollection();
|
---|
| 148 | estimationLimitResults.Add(new Result(EstimationLimitsResultName, "", new DoubleLimit()));
|
---|
| 149 | estimationLimitResults.Add(new Result(TrainingUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 150 | estimationLimitResults.Add(new Result(TestUpperEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 151 | estimationLimitResults.Add(new Result(TrainingLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 152 | estimationLimitResults.Add(new Result(TestLowerEstimationLimitHitsResultName, "", new IntValue()));
|
---|
| 153 | estimationLimitResults.Add(new Result(TrainingNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 154 | estimationLimitResults.Add(new Result(TestNaNEvaluationsResultName, "", new IntValue()));
|
---|
| 155 | Add(new Result(EstimationLimitsResultsResultName, "Results concerning the estimation limits of symbolic regression solution", estimationLimitResults));
|
---|
| 156 | CalculateResults();
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[6411] | 160 | protected override void RecalculateResults() {
|
---|
[6602] | 161 | base.RecalculateResults();
|
---|
[8723] | 162 | CalculateResults();
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[16627] | 165 | private NamedIntervals GetIntervalEvaluations() {
|
---|
| 166 | var intervalEvaluation = new NamedIntervals();
|
---|
[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 | }
|
---|
[16627] | 183 | return intervalEvaluation;
|
---|
[16556] | 184 | }
|
---|
[16635] | 185 |
|
---|
[8723] | 186 | private void CalculateResults() {
|
---|
[5736] | 187 | ModelLength = Model.SymbolicExpressionTree.Length;
|
---|
| 188 | ModelDepth = Model.SymbolicExpressionTree.Depth;
|
---|
[8723] | 189 |
|
---|
| 190 | EstimationLimits.Lower = Model.LowerEstimationLimit;
|
---|
| 191 | EstimationLimits.Upper = Model.UpperEstimationLimit;
|
---|
| 192 |
|
---|
| 193 | TrainingUpperEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
|
---|
| 194 | TestUpperEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.UpperEstimationLimit));
|
---|
| 195 | TrainingLowerEstimationLimitHits = EstimatedTrainingValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
|
---|
| 196 | TestLowerEstimationLimitHits = EstimatedTestValues.Count(x => x.IsAlmost(Model.LowerEstimationLimit));
|
---|
| 197 | TrainingNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TrainingIndices).Count(double.IsNaN);
|
---|
| 198 | TestNaNEvaluations = Model.Interpreter.GetSymbolicExpressionTreeValues(Model.SymbolicExpressionTree, ProblemData.Dataset, ProblemData.TestIndices).Count(double.IsNaN);
|
---|
[16627] | 199 |
|
---|
| 200 | IntervalEvaluationCollection = GetIntervalEvaluations();
|
---|
[5736] | 201 | }
|
---|
[5607] | 202 | }
|
---|
| 203 | }
|
---|