Free cookie consent management tool by TermsFeed Policy Generator

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

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

updated classification branch (ticket #939)

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