Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Classification/3.3/Symbolic/SymbolicClassificationProblem.cs @ 5785

Last change on this file since 5785 was 5625, checked in by mkommend, 14 years ago

#1418: Reorganized branch and removed CreateAble-Attribute from outdated classes.

File size: 19.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Classification.Symbolic.Analyzers;
37using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
38using HeuristicLab.Problems.DataAnalysis.Symbolic;
39using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
40
41namespace HeuristicLab.Problems.DataAnalysis.Classification {
42  [Item("Classification Problem", "Represents a classfication problem.")]
43  [StorableClass]
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.TrainingIndizes.First()); } }
125    public IntValue TrainingSamplesEnd {
126      get {
127        int endIndex = (int)(ClassificationProblemData.TrainingIndizes.Count() * (1.0 - ClassificationProblemData.ValidationPercentage.Value) - 1);
128        if (endIndex < 0) endIndex = 0;
129        return new IntValue(ClassificationProblemData.TrainingIndizes.ElementAt(endIndex));
130      }
131    }
132    public IntValue ValidationSamplesStart { get { return TrainingSamplesEnd; } }
133    public IntValue ValidationSamplesEnd { get { return new IntValue(ClassificationProblemData.TrainingIndizes.Last() + 1); } }
134    public IntValue TestSamplesStart { get { return ClassificationProblemData.TestSamplesStart; } }
135    public IntValue TestSamplesEnd { get { return ClassificationProblemData.TestSamplesEnd; } }
136    #endregion
137
138    [StorableConstructor]
139    private SymbolicClassificationProblem(bool deserializing) : base(deserializing) { }
140    private SymbolicClassificationProblem(SymbolicClassificationProblem original, Cloner cloner)
141      : base(original, cloner) {
142      RegisterParameterEvents();
143    }
144
145    public SymbolicClassificationProblem()
146      : base() {
147      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to evaluate the symbolic expression tree."));
148      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>(FunctionTreeGrammarParameterName, "The grammar that should be used for symbolic regression models."));
149      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionLengthParameterName, "Maximal length of the symbolic expression."));
150      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionDepthParameterName, "Maximal depth of the symbolic expression."));
151      Parameters.Add(new ValueParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated value that can be returned by the symbolic regression model."));
152      Parameters.Add(new ValueParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated value that can be returned by the symbolic regression model."));
153      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionDefiningBranchensParameterName, "Maximal number of automatically defined functions."));
154      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionArgumentsParameterName, "Maximal number of arguments of automatically defined functions."));
155
156      SolutionCreator = new ProbabilisticTreeCreator();
157      Evaluator = new SymbolicClassifacitionMeanSquaredErrorEvaluator();
158      ParameterizeSolutionCreator();
159      Maximization = new BoolValue(false);
160      FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(new FullFunctionalExpressionGrammar());
161      SymbolicExpressionTreeInterpreter = new SimpleArithmeticExpressionInterpreter();
162      MaxExpressionLength = new IntValue(100);
163      MaxExpressionDepth = new IntValue(10);
164      MaxFunctionDefiningBranches = new IntValue(0);
165      MaxFunctionArguments = new IntValue(0);
166
167      InitializeOperators();
168      RegisterParameterEvents();
169
170      UpdateEstimationLimits();
171      ParameterizeEvaluator();
172      ParameterizeSolutionCreator();
173      ParameterizeGrammar();
174      ParameterizeOperators();
175      ParameterizeAnalyzers();
176    }
177
178    public override IDeepCloneable Clone(Cloner cloner) {
179      return new SymbolicClassificationProblem(this, cloner);
180    }
181
182    [StorableHook(HookType.AfterDeserialization)]
183    private void AfterDeserialization() {
184      RegisterParameterEvents();
185    }
186
187    private void RegisterParameterEvents() {
188      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
189      FunctionTreeGrammarParameter.ValueChanged += new EventHandler(FunctionTreeGrammarParameter_ValueChanged);
190
191      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
192      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
193      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
194      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
195    }
196
197    protected override void OnEvaluatorChanged() {
198      ParameterizeEvaluator();
199      ParameterizeAnalyzers();
200      ParameterizeProblem();
201      base.OnEvaluatorChanged();
202    }
203
204    protected override void OnSolutionCreatorChanged() {
205      ParameterizeSolutionCreator();
206      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
207      base.OnSolutionCreatorChanged();
208    }
209    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, System.EventArgs e) {
210      ParameterizeEvaluator();
211      ParameterizeOperators();
212      ParameterizeAnalyzers();
213    }
214
215    protected override void OnClassificationProblemDataChanged() {
216      ParameterizeAnalyzers();
217      ParameterizeGrammar();
218      ParameterizeEvaluator();
219      UpdateEstimationLimits();
220      base.OnClassificationProblemDataChanged();
221    }
222
223    private void FunctionTreeGrammarParameter_ValueChanged(object sender, System.EventArgs e) {
224      if (!(FunctionTreeGrammar is GlobalSymbolicExpressionGrammar)) {
225        FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(FunctionTreeGrammar);
226      }
227      OnGrammarChanged();
228    }
229    private void OnGrammarChanged() {
230      ParameterizeGrammar();
231    }
232
233    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
234      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
235      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
236      OnArchitectureParameterChanged();
237    }
238    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
239      OnArchitectureParameterChanged();
240    }
241    private void OnArchitectureParameterChanged() {
242      ParameterizeGrammar();
243    }
244
245    private void InitializeOperators() {
246      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
247      Operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
248      Operators.Add(new SymbolicRegressionVariableFrequencyAnalyzer());
249      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
250      Operators.Add(new ValidationBestSymbolicClassificationSolutionAnalyzer());
251      Operators.Add(new TrainingBestSymbolicClassificationSolutionAnalyzer());
252    }
253
254    #region operator parameterization
255    private void UpdateEstimationLimits() {
256      if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value &&
257        ClassificationProblemData.Dataset.VariableNames.Contains(ClassificationProblemData.TargetVariable.Value)) {
258        var targetValues = ClassificationProblemData.Dataset.GetVariableValues(ClassificationProblemData.TargetVariable.Value, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
259        var mean = targetValues.Average();
260        var range = targetValues.Max() - targetValues.Min();
261        UpperEstimationLimit = new DoubleValue(mean + PunishmentFactor.Value * range);
262        LowerEstimationLimit = new DoubleValue(mean - PunishmentFactor.Value * range);
263      }
264    }
265
266    private void ParameterizeEvaluator() {
267      if (Evaluator != null) {
268        Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
269        Evaluator.RegressionProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
270        Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
271        Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
272      }
273    }
274
275    private void ParameterizeGrammar() {
276      List<LaggedVariable> laggedSymbols = FunctionTreeGrammar.Symbols.OfType<LaggedVariable>().ToList();
277      foreach (Symbol symbol in laggedSymbols)
278        FunctionTreeGrammar.RemoveSymbol(symbol);
279      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
280        varSymbol.VariableNames = ClassificationProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value);
281      }
282      var globalGrammar = FunctionTreeGrammar as GlobalSymbolicExpressionGrammar;
283      if (globalGrammar != null) {
284        globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
285        globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
286      }
287    }
288
289    private void ParameterizeSolutionCreator() {
290      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
291      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
292      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
293      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
294      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
295    }
296
297    private void ParameterizeOperators() {
298      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
299        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
300        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
301        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
302      }
303      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
304        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
305        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
306      }
307      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
308        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
309      }
310      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
311        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
312        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
313      }
314    }
315
316    private void ParameterizeAnalyzers() {
317      foreach (ISymbolicRegressionAnalyzer analyzer in Operators.OfType<ISymbolicRegressionAnalyzer>()) {
318        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
319        var bestValidationSolutionAnalyzer = analyzer as ValidationBestSymbolicClassificationSolutionAnalyzer;
320        if (bestValidationSolutionAnalyzer != null) {
321          bestValidationSolutionAnalyzer.ClassificationProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
322          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
323          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
324          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
325          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
326          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
327          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
328        }
329        var bestTrainingSolutionAnalyzer = analyzer as TrainingBestSymbolicClassificationSolutionAnalyzer;
330        if (bestTrainingSolutionAnalyzer != null) {
331          bestTrainingSolutionAnalyzer.ProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
332          bestTrainingSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
333          bestTrainingSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
334          bestTrainingSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
335          bestTrainingSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
336        }
337        var varFreqAnalyzer = analyzer as SymbolicRegressionVariableFrequencyAnalyzer;
338        if (varFreqAnalyzer != null) {
339          varFreqAnalyzer.ProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
340        }
341      }
342    }
343
344    private void ParameterizeProblem() {
345      if (Maximization != null) {
346        Maximization.Value = Evaluator.Maximization;
347      } else {
348        Maximization = new BoolValue(Evaluator.Maximization);
349      }
350    }
351    #endregion
352  }
353}
Note: See TracBrowser for help on using the repository browser.