Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs @ 4202

Last change on this file since 4202 was 4128, checked in by gkronber, 14 years ago

Implemented multi-objective version of symbolic regression problem. #1118

File size: 9.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
37using HeuristicLab.Problems.DataAnalysis.Symbolic;
38
39namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
40  [Item("Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem.")]
41  [Creatable("Problems")]
42  [StorableClass]
43  public class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveProblem {
44
45    #region Parameter Properties
46    public ValueParameter<BoolValue> MaximizationParameter {
47      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
48    }
49    IParameter ISingleObjectiveProblem.MaximizationParameter {
50      get { return MaximizationParameter; }
51    }
52    public new ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
53      get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
54    }
55    IParameter IProblem.EvaluatorParameter {
56      get { return EvaluatorParameter; }
57    }
58    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
59      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
60    }
61    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
62      get { return BestKnownQualityParameter; }
63    }
64    #endregion
65
66    #region Properties
67    public new ISymbolicRegressionEvaluator Evaluator {
68      get { return EvaluatorParameter.Value; }
69      set { EvaluatorParameter.Value = value; }
70    }
71    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
72      get { return EvaluatorParameter.Value; }
73    }
74    IEvaluator IProblem.Evaluator {
75      get { return EvaluatorParameter.Value; }
76    }
77    public DoubleValue BestKnownQuality {
78      get { return BestKnownQualityParameter.Value; }
79    }
80    #endregion
81
82    [StorableConstructor]
83    protected SymbolicRegressionProblem(bool deserializing) : base(deserializing) { }
84    public SymbolicRegressionProblem()
85      : base() {
86      var evaluator = new SymbolicRegressionScaledMeanSquaredErrorEvaluator();
87      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(false).AsReadOnly()));
88      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
89      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
90
91      evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
92
93      InitializeOperators();
94      ParameterizeEvaluator();
95
96      RegisterParameterEvents();
97      RegisterParameterValueEvents();
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
102      clone.RegisterParameterEvents();
103      clone.RegisterParameterValueEvents();
104      return clone;
105    }
106
107    private void RegisterParameterValueEvents() {
108      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
109    }
110
111    private void RegisterParameterEvents() { }
112
113    #region event handling
114    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
115      base.OnDataAnalysisProblemChanged(e);
116      BestKnownQualityParameter.Value = null;
117      // paritions could be changed
118      ParameterizeEvaluator();
119      ParameterizeAnalyzers();
120    }
121
122    protected override void OnSolutionParameterNameChanged(EventArgs e) {
123      base.OnSolutionParameterNameChanged(e);
124      ParameterizeEvaluator();
125      ParameterizeAnalyzers();
126    }
127
128    protected override void OnEvaluatorChanged(EventArgs e) {
129      base.OnEvaluatorChanged(e);
130      ParameterizeEvaluator();
131      ParameterizeAnalyzers();
132      RaiseEvaluatorChanged(e);
133    }
134    #endregion
135
136    #region event handlers
137    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
138      OnEvaluatorChanged(e);
139    }
140    #endregion
141
142    #region Helpers
143    [StorableHook(HookType.AfterDeserialization)]
144    private void AfterDeserializationHook() {
145      // BackwardsCompatibility3.3
146      #region Backwards compatible code (remove with 3.4)
147      if (Operators == null || Operators.Count() == 0) InitializeOperators();
148      #endregion
149      RegisterParameterEvents();
150      RegisterParameterValueEvents();
151    }
152
153    private void InitializeOperators() {
154      AddOperator(new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer());
155      ParameterizeAnalyzers();
156    }
157
158    private void ParameterizeEvaluator() {
159      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
160      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
161      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
162      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
163    }
164
165    private void ParameterizeAnalyzers() {
166      foreach (var analyzer in Analyzers) {
167        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
168        var fixedBestValidationSolutionAnalyzer = analyzer as FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer;
169        if (fixedBestValidationSolutionAnalyzer != null) {
170          fixedBestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
171          fixedBestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
172          fixedBestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
173          fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
174          fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
175          fixedBestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
176          fixedBestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
177          fixedBestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
178        }
179        var bestValidationSolutionAnalyzer = analyzer as ValidationBestScaledSymbolicRegressionSolutionAnalyzer;
180        if (bestValidationSolutionAnalyzer != null) {
181          bestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
182          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
183          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
184          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
185          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
186          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
187          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
188          bestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
189          bestValidationSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
190        }
191      }
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.