Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/MultiObjective/ShapeConstrainedRegressionMultiObjectiveProblem.cs @ 18086

Last change on this file since 18086 was 18086, checked in by mkommend, 3 years ago

#2521: Merged trunk changes into branch.

File size: 7.5 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 System;
23using System.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
32  [Item("Shape-constrained symbolic regression problem (multi-objective)", "Represents a multi-objective shape-constrained regression problem.")]
33  [StorableType("2956C66F-4B71-4A62-998F-B52C5E8C02CD")]
34  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 150)]
35  public class ShapeConstrainedRegressionMultiObjectiveProblem : SymbolicDataAnalysisMultiObjectiveProblem<IRegressionProblemData, IMultiObjectiveConstraintsEvaluator>, IRegressionProblem {
36    private const double PunishmentFactor = 10;
37    private const int InitialMaximumTreeDepth = 8;
38    private const int InitialMaximumTreeLength = 25;
39    private const string EstimationLimitsParameterName = "EstimationLimits";
40    private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic regression model.";
41
42    #region parameter properties
43    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
44      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
45    }
46    #endregion
47
48    #region properties
49    public DoubleLimit EstimationLimits {
50      get { return EstimationLimitsParameter.Value; }
51    }
52
53    #endregion
54
55    [StorableConstructor]
56    protected ShapeConstrainedRegressionMultiObjectiveProblem(StorableConstructorFlag _) : base(_) { }
57    protected ShapeConstrainedRegressionMultiObjectiveProblem(ShapeConstrainedRegressionMultiObjectiveProblem original, Cloner cloner)
58      : base(original, cloner) {
59      RegisterEventHandlers();
60    }
61    public override IDeepCloneable Clone(Cloner cloner) { return new ShapeConstrainedRegressionMultiObjectiveProblem(this, cloner); }
62
63    public ShapeConstrainedRegressionMultiObjectiveProblem()
64      : base(new ShapeConstrainedRegressionProblemData(), new NMSEMultiObjectiveConstraintsEvaluator()) {
65
66      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
67      EstimationLimitsParameter.Hidden = true;
68
69      ApplyLinearScalingParameter.Value.Value = true;
70      SymbolicExpressionTreeGrammarParameter.Value = new LinearScalingGrammar();
71
72      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
73      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
74
75      InitializeOperators();
76      UpdateEstimationLimits();
77      UpdateMaximization();
78      RegisterEventHandlers();
79    }
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      RegisterEventHandlers();
84    }
85
86    private void RegisterEventHandlers() {
87      Evaluator.NumConstraintsParameter.Value.ValueChanged += NumConstraintsParameter_ValueChanged;
88    }
89
90    protected override void OnEvaluatorChanged() {
91      base.OnEvaluatorChanged();
92      UpdateEvaluatorObjectives(); // update objectives in evaluator based ProblemData
93      Evaluator.NumConstraintsParameter.Value.ValueChanged += NumConstraintsParameter_ValueChanged;
94    }
95    protected override void OnProblemDataChanged() {
96      base.OnProblemDataChanged();
97
98      UpdateEstimationLimits();
99      UpdateMaximization();
100      UpdateEvaluatorObjectives();
101    }
102
103    private void NumConstraintsParameter_ValueChanged(object sender, EventArgs e) {
104      UpdateMaximization();
105    }
106
107    private void UpdateMaximization() {
108      Maximization = new BoolArray(Evaluator.Maximization.ToArray());
109    }
110
111    private void UpdateEstimationLimits() {
112      if (ProblemData.TrainingIndices.Any()) {
113        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
114        var mean = targetValues.Average();
115        var range = targetValues.Max() - targetValues.Min();
116        EstimationLimits.Upper = mean + PunishmentFactor * range;
117        EstimationLimits.Lower = mean - PunishmentFactor * range;
118      } else {
119        EstimationLimits.Upper = double.MaxValue;
120        EstimationLimits.Lower = double.MinValue;
121      }
122    }
123    private void UpdateEvaluatorObjectives() {
124      if (ProblemData is ShapeConstrainedRegressionProblemData scProblemData) {
125        Evaluator.NumConstraintsParameter.Value.Value = scProblemData.ShapeConstraints.EnabledConstraints.Count();
126      } else {
127        Evaluator.NumConstraintsParameter.Value.Value = 0;
128      }
129    }
130
131    private void InitializeOperators() {
132      Operators.Add(new SymbolicRegressionMultiObjectiveTrainingBestSolutionAnalyzer());
133      Operators.Add(new SymbolicRegressionMultiObjectiveValidationBestSolutionAnalyzer());
134      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
135      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()));
136      ParameterizeOperators();
137    }
138
139    protected override void ParameterizeOperators() {
140      base.ParameterizeOperators();
141      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
142        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
143        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
144          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
145        }
146      }
147
148      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
149        //ToDo Change to encoding.name
150        //op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
151        op.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
152
153        if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
154          var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
155          phenotypicSimilarityCalculator.ProblemData = ProblemData;
156          phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
157        }
158      }
159    }
160
161
162    public override void Load(IRegressionProblemData data) {
163      var scProblemData = new ShapeConstrainedRegressionProblemData(data.Dataset, data.AllowedInputVariables, data.TargetVariable,
164                                                                    data.TrainingPartition, data.TestPartition) {
165        Name = data.Name,
166        Description = data.Description
167      };
168
169      base.Load(scProblemData);
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.