Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4695 was 4452, checked in by mkommend, 14 years ago

Marked SymbolicClassificationProblem as StorableContent (ticket #939).

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