Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Extensions/TrainingBestSolutionAnalyzer.cs @ 17714

Last change on this file since 17714 was 17327, checked in by gkronber, 5 years ago

#2994 introduced a new analyser which scales models under consideration of constraints

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
25using HeuristicLab.Parameters;
26using HEAL.Attic;
27using System;
28using HeuristicLab.Data;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression.Extensions {
31  [Item("TrainingBestSolutionAnalyzer (with constraints)", "An operator that analyzes the training best symbolic regression solution for single objective symbolic regression problems.")]
32  [StorableType("93A9331C-9E50-45DE-804B-21785A07EFB4")]
33  public sealed class TrainingBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<ISymbolicRegressionSolution>,
34  ISymbolicDataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {
35    private const string ProblemDataParameterName = "ProblemData";
36    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";
37    private const string EstimationLimitsParameterName = "EstimationLimits";
38    #region parameter properties
39    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
40      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
41    }
42    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
43      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
44    }
45    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
46      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
47    }
48    #endregion
49
50    [StorableConstructor]
51    private TrainingBestSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
52    private TrainingBestSolutionAnalyzer(TrainingBestSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
53    public TrainingBestSolutionAnalyzer()
54      : base() {
55      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution."));
56      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis tree interpreter for the symbolic expression tree."));
57      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model."));
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new TrainingBestSolutionAnalyzer(this, cloner);
61    }
62
63    protected override ISymbolicRegressionSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
64      if (!ApplyLinearScalingParameter.ActualValue.Value) throw new NotSupportedException("This analyzer only works if linear scaling of models is activated.");
65
66      var problemData = ProblemDataParameter.ActualValue;
67      var solTree = (ISymbolicExpressionTree)bestTree.Clone();
68      using (var nls = new ConstrainedNLSInternal("MMA", solTree, 100, ProblemDataParameter.ActualValue)) {
69        var originalConstraintValues = (double[])nls.BestConstraintValues.Clone(); // for debugging
70        nls.Optimize(ConstrainedNLSInternal.OptimizationMode.UpdateParametersAndKeepLinearScaling);
71
72        var model = new SymbolicRegressionModel(problemData.TargetVariable, solTree,
73          SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
74        var sol = new SymbolicRegressionSolution(model, (IRegressionProblemData)problemData.Clone());
75        // debugging
76        sol.AddOrUpdateResult("Constraint values (after optimization)", new DoubleArray(nls.BestConstraintValues));
77        sol.AddOrUpdateResult("Constraint values (before optimization)", new DoubleArray(originalConstraintValues));
78        sol.AddOrUpdateResult("Quality before optimization in analyzer", new DoubleValue(bestQuality));
79        sol.AddOrUpdateResult("Quality after optimization in analyzer", new DoubleValue(nls.BestError));
80        sol.AddOrUpdateResult("NLOpt result", new StringValue(nls.OptResult.ToString()));
81        return sol;
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.