Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Classification/3.3/Symbolic/SymbolicClassificationProblem.cs @ 5304

Last change on this file since 5304 was 5279, checked in by mkommend, 14 years ago

Corrected validation range in ClassificationProblem (ticket #1373).

File size: 18.8 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;
39using HeuristicLab.Problems.DataAnalysis.Classification.Symbolic.Analyzers;
40
41namespace HeuristicLab.Problems.DataAnalysis.Classification {
42  [Item("Classification Problem", "Represents a classfication problem.")]
43  [StorableClass]
44  [Creatable("Problems")]
45  public sealed class SymbolicClassificationProblem : SingleObjectiveClassificationProblem<ISymbolicClassificationEvaluator, ISymbolicExpressionTreeCreator>, IStorableContent {
46    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
47    private const string FunctionTreeGrammarParameterName = "FunctionTreeGrammar";
48    private const string MaxExpressionLengthParameterName = "MaxExpressionLength";
49    private const string MaxExpressionDepthParameterName = "MaxExpressionDepth";
50    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
51    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
52    private const string MaxFunctionDefiningBranchensParameterName = "MaxFunctionDefiningBranches";
53    private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
54
55    #region properties
56    public string Filename { get; set; }
57
58    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
59      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
60      private set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
61    }
62    public IValueParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
63      get { return (IValueParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
64    }
65
66    public ISymbolicExpressionGrammar FunctionTreeGrammar {
67      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
68      private set { FunctionTreeGrammarParameter.Value = value; }
69    }
70    public IValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
71      get { return (IValueParameter<ISymbolicExpressionGrammar>)Parameters[FunctionTreeGrammarParameterName]; }
72    }
73
74    public IntValue MaxExpressionLength {
75      get { return MaxExpressionLengthParameter.Value; }
76      private set { MaxExpressionLengthParameter.Value = value; }
77    }
78    public IValueParameter<IntValue> MaxExpressionLengthParameter {
79      get { return (IValueParameter<IntValue>)Parameters[MaxExpressionLengthParameterName]; }
80    }
81
82    public IntValue MaxExpressionDepth {
83      get { return MaxExpressionDepthParameter.Value; }
84      private set { MaxExpressionDepthParameter.Value = value; }
85    }
86    public ValueParameter<IntValue> MaxExpressionDepthParameter {
87      get { return (ValueParameter<IntValue>)Parameters[MaxExpressionDepthParameterName]; }
88    }
89
90    public DoubleValue UpperEstimationLimit {
91      get { return UpperEstimationLimitParameter.Value; }
92      private set { UpperEstimationLimitParameter.Value = value; }
93    }
94    public IValueParameter<DoubleValue> UpperEstimationLimitParameter {
95      get { return (IValueParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
96    }
97
98    public DoubleValue LowerEstimationLimit {
99      get { return LowerEstimationLimitParameter.Value; }
100      private set { LowerEstimationLimitParameter.Value = value; }
101    }
102    public IValueParameter<DoubleValue> LowerEstimationLimitParameter {
103      get { return (IValueParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
104    }
105
106    public IntValue MaxFunctionDefiningBranches {
107      get { return MaxFunctionDefiningBranchesParameter.Value; }
108      private set { MaxFunctionDefiningBranchesParameter.Value = value; }
109    }
110    public IValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
111      get { return (IValueParameter<IntValue>)Parameters[MaxFunctionDefiningBranchensParameterName]; }
112    }
113
114    public IntValue MaxFunctionArguments {
115      get { return MaxFunctionArgumentsParameter.Value; }
116      private set { MaxFunctionArgumentsParameter.Value = value; }
117    }
118    public IValueParameter<IntValue> MaxFunctionArgumentsParameter {
119      get { return (IValueParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
120    }
121
122    public DoubleValue PunishmentFactor {
123      get { return new DoubleValue(10.0); }
124    }
125    public IntValue TrainingSamplesStart { get { return new IntValue(ClassificationProblemData.TrainingIndizes.First()); } }
126    public IntValue TrainingSamplesEnd {
127      get {
128        int endIndex = (int)(ClassificationProblemData.TrainingIndizes.Count() * (1.0 - ClassificationProblemData.ValidationPercentage.Value) - 1);
129        if (endIndex < 0) endIndex = 0;
130        return new IntValue(ClassificationProblemData.TrainingIndizes.ElementAt(endIndex));
131      }
132    }
133    public IntValue ValidationSamplesStart { get { return TrainingSamplesEnd; } }
134    public IntValue ValidationSamplesEnd { get { return new IntValue(ClassificationProblemData.TrainingIndizes.Last() + 1); } }
135    public IntValue TestSamplesStart { get { return ClassificationProblemData.TestSamplesStart; } }
136    public IntValue TestSamplesEnd { get { return ClassificationProblemData.TestSamplesEnd; } }
137    #endregion
138
139    [StorableConstructor]
140    private SymbolicClassificationProblem(bool deserializing) : base(deserializing) { }
141    private SymbolicClassificationProblem(SymbolicClassificationProblem original, Cloner cloner)
142      : base(original, cloner) {
143      RegisterParameterEvents();
144    }
145
146    public SymbolicClassificationProblem()
147      : base() {
148      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to evaluate the symbolic expression tree."));
149      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>(FunctionTreeGrammarParameterName, "The grammar that should be used for symbolic regression models."));
150      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionLengthParameterName, "Maximal length of the symbolic expression."));
151      Parameters.Add(new ValueParameter<IntValue>(MaxExpressionDepthParameterName, "Maximal depth of the symbolic expression."));
152      Parameters.Add(new ValueParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit for the estimated value that can be returned by the symbolic regression model."));
153      Parameters.Add(new ValueParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit for the estimated value that can be returned by the symbolic regression model."));
154      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionDefiningBranchensParameterName, "Maximal number of automatically defined functions."));
155      Parameters.Add(new ValueParameter<IntValue>(MaxFunctionArgumentsParameterName, "Maximal number of arguments of automatically defined functions."));
156
157      SolutionCreator = new ProbabilisticTreeCreator();
158      Evaluator = new SymbolicClassifacitionMeanSquaredErrorEvaluator();
159      ParameterizeSolutionCreator();
160      Maximization = new BoolValue(false);
161      FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(new FullFunctionalExpressionGrammar());
162      SymbolicExpressionTreeInterpreter = new SimpleArithmeticExpressionInterpreter();
163      MaxExpressionLength = new IntValue(100);
164      MaxExpressionDepth = new IntValue(10);
165      MaxFunctionDefiningBranches = new IntValue(0);
166      MaxFunctionArguments = new IntValue(0);
167
168      InitializeOperators();
169      RegisterParameterEvents();
170
171      UpdateEstimationLimits();
172      ParameterizeEvaluator();
173      ParameterizeSolutionCreator();
174      ParameterizeGrammar();
175      ParameterizeOperators();
176      ParameterizeAnalyzers();
177    }
178
179    public override IDeepCloneable Clone(Cloner cloner) {
180      return new SymbolicClassificationProblem(this, cloner);
181    }
182
183    private void RegisterParameterEvents() {
184      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
185      FunctionTreeGrammarParameter.ValueChanged += new EventHandler(FunctionTreeGrammarParameter_ValueChanged);
186
187      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
188      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
189      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
190      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
191    }
192
193    protected override void OnEvaluatorChanged() {
194      ParameterizeEvaluator();
195      ParameterizeAnalyzers();
196      base.OnEvaluatorChanged();
197    }
198
199    protected override void OnSolutionCreatorChanged() {
200      ParameterizeSolutionCreator();
201      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
202      base.OnSolutionCreatorChanged();
203    }
204    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, System.EventArgs e) {
205      ParameterizeEvaluator();
206      ParameterizeOperators();
207      ParameterizeAnalyzers();
208    }
209
210    protected override void OnClassificationProblemDataChanged() {
211      ParameterizeAnalyzers();
212      ParameterizeGrammar();
213      ParameterizeEvaluator();
214      UpdateEstimationLimits();
215      base.OnClassificationProblemDataChanged();
216    }
217
218    private void FunctionTreeGrammarParameter_ValueChanged(object sender, System.EventArgs e) {
219      if (!(FunctionTreeGrammar is GlobalSymbolicExpressionGrammar))
220        FunctionTreeGrammar = new GlobalSymbolicExpressionGrammar(FunctionTreeGrammar);
221      OnGrammarChanged();
222    }
223    private void OnGrammarChanged() {
224      ParameterizeGrammar();
225    }
226
227    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
228      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
229      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
230      OnArchitectureParameterChanged();
231    }
232    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
233      OnArchitectureParameterChanged();
234    }
235    private void OnArchitectureParameterChanged() {
236      ParameterizeGrammar();
237    }
238
239    private void InitializeOperators() {
240      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
241      Operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
242      Operators.Add(new SymbolicRegressionVariableFrequencyAnalyzer());
243      Operators.Add(new ValidationBestSymbolicClassificationSolutionAnalyzer());
244      Operators.Add(new TrainingBestSymbolicClassificationSolutionAnalyzer());
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 bestTrainingSolutionAnalyzer = analyzer as TrainingBestSymbolicClassificationSolutionAnalyzer;
323        if (bestTrainingSolutionAnalyzer != null) {
324          bestTrainingSolutionAnalyzer.ProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
325          bestTrainingSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
326          bestTrainingSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
327          bestTrainingSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
328          bestTrainingSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
329        }
330        var varFreqAnalyzer = analyzer as SymbolicRegressionVariableFrequencyAnalyzer;
331        if (varFreqAnalyzer != null) {
332          varFreqAnalyzer.ProblemDataParameter.ActualName = ClassificationProblemDataParameter.Name;
333        }
334      }
335    }
336    #endregion
337  }
338}
Note: See TracBrowser for help on using the repository browser.