Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.DataAnalysis.Classification/3.3/Symbolic/SymbolicClassificationProblem.cs @ 10015

Last change on this file since 10015 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 17.9 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.Encodings.SymbolicExpressionTreeEncoding.Symbols;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
37using HeuristicLab.Problems.DataAnalysis.Symbolic;
38using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
39
40namespace HeuristicLab.Problems.DataAnalysis.Classification {
41  [Item("Classification Problem", "Represents a classfication problem.")]
42  [StorableClass]
43  [Creatable("Problems")]
44  public sealed class SymbolicClassificationProblem : SingleObjectiveClassificationProblem<ISymbolicClassificationEvaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
45    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
46    private const string FunctionTreeGrammarParameterName = "FunctionTreeGrammar";
47    private const string MaxExpressionLengthParameterName = "MaxExpressionLength";
48    private const string MaxExpressionDepthParameterName = "MaxExpressionDepth";
49    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
50    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
51    private const string MaxFunctionDefiningBranchensParameterName = "MaxFunctionDefiningBranches";
52    private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
53
54    #region properties
55    public string Filename { get; set; }
56
57    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
58      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
59      private set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
60    }
61    public IValueParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
62      get { return (IValueParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
63    }
64
65    public ISymbolicExpressionGrammar FunctionTreeGrammar {
66      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
67      private set { FunctionTreeGrammarParameter.Value = value; }
68    }
69    public IValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
70      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters[FunctionTreeGrammarParameterName]; }
71    }
72
73    public IntValue MaxExpressionLength {
74      get { return MaxExpressionLengthParameter.Value; }
75      private set { MaxExpressionLengthParameter.Value = value; }
76    }
77    public IValueParameter<IntValue> MaxExpressionLengthParameter {
78      get { return (IValueParameter<IntValue>)Parameters[MaxExpressionLengthParameterName]; }
79    }
80
81    public IntValue MaxExpressionDepth {
82      get { return MaxExpressionDepthParameter.Value; }
83      private set { MaxExpressionDepthParameter.Value = value; }
84    }
85    public ValueParameter<IntValue> MaxExpressionDepthParameter {
86      get { return (ValueParameter<IntValue>)Parameters[MaxExpressionDepthParameterName]; }
87    }
88
89    public DoubleValue UpperEstimationLimit {
90      get { return UpperEstimationLimitParameter.Value; }
91      private set { UpperEstimationLimitParameter.Value = value; }
92    }
93    public IValueParameter<DoubleValue> UpperEstimationLimitParameter {
94      get { return (IValueParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
95    }
96
97    public DoubleValue LowerEstimationLimit {
98      get { return LowerEstimationLimitParameter.Value; }
99      private set { LowerEstimationLimitParameter.Value = value; }
100    }
101    public IValueParameter<DoubleValue> LowerEstimationLimitParameter {
102      get { return (IValueParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
103    }
104
105    public IntValue MaxFunctionDefiningBranches {
106      get { return MaxFunctionDefiningBranchesParameter.Value; }
107      private set { MaxFunctionDefiningBranchesParameter.Value = value; }
108    }
109    public IValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
110      get { return (IValueParameter<IntValue>)Parameters[MaxFunctionDefiningBranchensParameterName]; }
111    }
112
113    public IntValue MaxFunctionArguments {
114      get { return MaxFunctionArgumentsParameter.Value; }
115      private set { MaxFunctionArgumentsParameter.Value = value; }
116    }
117    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
118      get { return (IValueParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
119    }
120
121    public DoubleValue PunishmentFactor {
122      get { return new DoubleValue(10.0); }
123    }
124    public IntValue TrainingSamplesStart { get { return new IntValue(ClassificationProblemData.TrainingSamplesStart.Value); } }
125    public IntValue TrainingSamplesEnd {
126      get { return new IntValue((ClassificationProblemData.TrainingSamplesStart.Value + ClassificationProblemData.TrainingSamplesEnd.Value) / 2); }
127    }
128    public IntValue ValidationSamplesStart { get { return TrainingSamplesEnd; } }
129    public IntValue ValidationSamplesEnd { get { return new IntValue(ClassificationProblemData.TrainingSamplesEnd.Value); } }
130    public IntValue TestSamplesStart { get { return ClassificationProblemData.TestSamplesStart; } }
131    public IntValue TestSamplesEnd { get { return ClassificationProblemData.TestSamplesEnd; } }
132    #endregion
133
134    [StorableConstructor]
135    private SymbolicClassificationProblem(bool deserializing) : base(deserializing) { }
136    private SymbolicClassificationProblem(SymbolicClassificationProblem original, Cloner cloner)
137      : base(original, cloner) {
138      RegisterParameterEvents();
139
140      UpdateEstimationLimits();
141      ParameterizeEvaluator();
142      ParameterizeSolutionCreator();
143      ParameterizeGrammar();
144      ParameterizeOperators();
145      ParameterizeAnalyzers();
146    }
147    public SymbolicClassificationProblem()
148      : base() {
149      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to evaluate the symbolic expression tree."));
150      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>(FunctionTreeGrammarParameterName, "The grammar that should be used for symbolic regression models."));
151      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionLengthParameterName, "Maximal length of the symbolic expression."));
152      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionDepthParameterName, "Maximal depth of the symbolic expression."));
153      Parameters.Add(new ValueParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated value that can be returned by the symbolic regression model."));
154      Parameters.Add(new ValueParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated value that can be returned by the symbolic regression model."));
155      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionDefiningBranchensParameterName, "Maximal number of automatically defined functions."));
156      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionArgumentsParameterName, "Maximal number of arguments of automatically defined functions."));
157
158      SolutionCreator = new ProbabilisticTreeCreator();
159      Evaluator = new SymbolicClassifacitionMeanSquaredErrorEvaluator();
160      ParameterizeSolutionCreator();
161      Maximization = new BoolValue(false);
162      FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(new FullFunctionalExpressionGrammar());
163      SymbolicExpressionTreeInterpreter = new SimpleArithmeticExpressionInterpreter();
164      MaxExpressionLength = new IntValue(100);
165      MaxExpressionDepth = new IntValue(10);
166      MaxFunctionDefiningBranches = new IntValue(0);
167      MaxFunctionArguments = new IntValue(0);
168
169      InitializeOperators();
170      RegisterParameterEvents();
171
172      UpdateEstimationLimits();
173      ParameterizeEvaluator();
174      ParameterizeSolutionCreator();
175      ParameterizeGrammar();
176      ParameterizeOperators();
177      ParameterizeAnalyzers();
178    }
179
180    public override IDeepCloneable Clone(Cloner cloner) {
181      return new SymbolicClassificationProblem(this, cloner);
182    }
183
184    private void RegisterParameterEvents() {
185      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
186      FunctionTreeGrammarParameter.ValueChanged += new EventHandler(FunctionTreeGrammarParameter_ValueChanged);
187
188      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
189      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
190      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
191      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
192    }
193
194    protected override void OnEvaluatorChanged() {
195      ParameterizeEvaluator();
196      ParameterizeAnalyzers();
197      base.OnEvaluatorChanged();
198    }
199
200    protected override void OnSolutionCreatorChanged() {
201      ParameterizeSolutionCreator();
202      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
203      base.OnSolutionCreatorChanged();
204    }
205    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, System.EventArgs e) {
206      ParameterizeEvaluator();
207      ParameterizeOperators();
208      ParameterizeAnalyzers();
209    }
210
211    protected override void OnClassificationProblemDataChanged() {
212      ParameterizeAnalyzers();
213      ParameterizeGrammar();
214      ParameterizeEvaluator();
215      UpdateEstimationLimits();
216      base.OnClassificationProblemDataChanged();
217    }
218
219    private void FunctionTreeGrammarParameter_ValueChanged(object sender, System.EventArgs e) {
220      if (!(FunctionTreeGrammar is GlobalSymbolicExpressionGrammar))
221        FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(FunctionTreeGrammar);
222      OnGrammarChanged();
223    }
224    private void OnGrammarChanged() {
225      ParameterizeGrammar();
226    }
227
228    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
229      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
230      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
231      OnArchitectureParameterChanged();
232    }
233    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
234      OnArchitectureParameterChanged();
235    }
236    private void OnArchitectureParameterChanged() {
237      ParameterizeGrammar();
238    }
239
240    private void InitializeOperators() {
241      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
242      Operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
243      Operators.Add(new SymbolicRegressionVariableFrequencyAnalyzer());
244      Operators.Add(new ValidationBestSymbolicClassificationSolutionAnalyzer());
245    }
246
247    #region operator parameterization
248    private void UpdateEstimationLimits() {
249      if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value &&
250        ClassificationProblemData.Dataset.VariableNames.Contains(ClassificationProblemData.TargetVariable.Value)) {
251        var targetValues = ClassificationProblemData.Dataset.GetVariableValues(ClassificationProblemData.TargetVariable.Value, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
252        var mean = targetValues.Average();
253        var range = targetValues.Max() - targetValues.Min();
254        UpperEstimationLimit = new DoubleValue(mean + PunishmentFactor.Value * range);
255        LowerEstimationLimit = new DoubleValue(mean - PunishmentFactor.Value * range);
256      }
257    }
258
259    private void ParameterizeEvaluator() {
260      if (Evaluator != null) {
261        Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
262        Evaluator.RegressionProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
263        Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
264        Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
265      }
266    }
267
268    private void ParameterizeGrammar() {
269      List<LaggedVariable> laggedSymbols = FunctionTreeGrammar.Symbols.OfType<LaggedVariable>().ToList();
270      foreach (Symbol symbol in laggedSymbols)
271        FunctionTreeGrammar.RemoveSymbol(symbol);
272      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
273        varSymbol.VariableNames = ClassificationProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value);
274      }
275      var globalGrammar = FunctionTreeGrammar as GlobalSymbolicExpressionGrammar;
276      if (globalGrammar != null) {
277        globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
278        globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
279      }
280    }
281
282    private void ParameterizeSolutionCreator() {
283      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
284      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
285      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
286      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
287      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
288    }
289
290    private void ParameterizeOperators() {
291      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
292        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
293        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
294        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
295      }
296      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
297        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
298        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
299      }
300      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
301        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
302      }
303      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
304        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
305        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
306      }
307    }
308
309    private void ParameterizeAnalyzers() {
310      foreach (ISymbolicRegressionAnalyzer analyzer in Operators.OfType<ISymbolicRegressionAnalyzer>()) {
311        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
312        var bestValidationSolutionAnalyzer = analyzer as ValidationBestSymbolicClassificationSolutionAnalyzer;
313        if (bestValidationSolutionAnalyzer != null) {
314          bestValidationSolutionAnalyzer.ClassificationProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
315          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
316          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
317          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
318          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
319          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
320          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
321        }
322        var varFreqAnalyzer = analyzer as SymbolicRegressionVariableFrequencyAnalyzer;
323        if (varFreqAnalyzer != null) {
324          varFreqAnalyzer.ProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
325        }
326      }
327    }
328    #endregion
329  }
330}
Note: See TracBrowser for help on using the repository browser.