Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 5685

Last change on this file since 5685 was 5685, checked in by gkronber, 13 years ago

#1418 Implemented validation best solution analyzers for symbolic classification and regression, added analyzers to symbolic data analysis problem classes and changed details of parameter wiring in problem classes.

File size: 14.6 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.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35//TODO configure training start / end
36//TODO configure analyzer validation
37//TODO configure grammar with ADF usage
38
39namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
40  [StorableClass]
41  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, ISymbolicDataAnalysisProblem, IStorableContent
42    where T : class,IDataAnalysisProblemData
43    where U : class, ISymbolicDataAnalysisEvaluator<T>
44    where V : class, ISymbolicDataAnalysisSolutionCreator {
45    #region parameter names & descriptions
46    private const string ProblemDataParameterName = "ProblemData";
47    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
48    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
49    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
50    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
51    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
52    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
53
54    private const string ProblemDataParameterDescription = "";
55    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
56    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
57    private const string MaximumSymbolicExpressionTreeDepthParameterDescription = "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.";
58    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
59    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
60    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
61    #endregion
62
63    #region parameter properties
64    IParameter IDataAnalysisProblem.ProblemDataParameter {
65      get { return ProblemDataParameter; }
66    }
67    public IValueParameter<T> ProblemDataParameter {
68      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
69    }
70    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
71      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
72    }
73    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
74      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
75    }
76    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
77      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
78    }
79    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
80      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
81    }
82    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
83      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
84    }
85    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
86      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
87    }
88    #endregion
89
90    #region properties
91    public string Filename { get; set; }
92    public override Image ItemImage { get { return VSImageLibrary.Type; } }
93
94    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
95      get { return ProblemData; }
96    }
97    public T ProblemData {
98      get { return ProblemDataParameter.Value; }
99      set { ProblemDataParameter.Value = value; }
100    }
101
102    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
103      get { return SymbolicExpressionTreeGrammarParameter.Value; }
104      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
105    }
106    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
107      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
108      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
109    }
110
111    public IntValue MaximumSymbolicExpressionTreeDepth {
112      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
113    }
114    public IntValue MaximumSymbolicExpressionTreeLength {
115      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
116    }
117    public IntValue MaximumFunctionDefinitions {
118      get { return MaximumFunctionDefinitionsParameter.Value; }
119    }
120    public IntValue MaximumFunctionArguments {
121      get { return MaximumFunctionArgumentsParameter.Value; }
122    }
123    #endregion
124
125    [StorableConstructor]
126    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
127    [StorableHook(HookType.AfterDeserialization)]
128    private void AfterDeserialization() {
129      RegisterEventHandlers();
130    }
131    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
132      : base(original, cloner) {
133      RegisterEventHandlers();
134    }
135
136    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
137      : base(evaluator, solutionCreator) {
138      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
139      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
140      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
141      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription, new IntValue()));
142      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription, new IntValue()));
143      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription, new IntValue()));
144      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription, new IntValue()));
145
146      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
147      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
148
149      UpdateGrammar();
150      UpdateDatasetPartitions();
151      RegisterEventHandlers();
152      InitializeOperators();
153    }
154
155    protected virtual void UpdateDatasetPartitions() {
156      Evaluator.SamplesStartParameter.Value.Value = ProblemData.TrainingPartitionStart.Value;
157      Evaluator.SamplesEndParameter.Value.Value = ProblemData.TrainingPartitionEnd.Value;
158
159      foreach (var analyzer in Operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
160        analyzer.ValidationSamplesStartParameter.Value = new IntValue(ProblemData.TrainingPartitionStart.Value);
161        analyzer.ValidationSamplesEndParameter.Value = new IntValue(ProblemData.TrainingPartitionEnd.Value);
162      }
163    }
164
165    protected virtual void UpdateGrammar() {
166      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
167        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
168      }
169      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
170        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
171      }
172    }
173
174    private void InitializeOperators() {
175      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
176      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
177      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
178      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
179      ParameterizeOperators();
180    }
181
182    #region events
183    private void RegisterEventHandlers() {
184      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
185      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
186
187      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
188      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
189      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
190    }
191
192    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
193      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
194      OnProblemDataChanged();
195    }
196
197    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
198      UpdateGrammar();
199    }
200
201    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
202      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
203        MaximumSymbolicExpressionTreeDepth.Value = 3;
204    }
205
206    protected override void OnSolutionCreatorChanged() {
207      base.OnSolutionCreatorChanged();
208      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
209      ParameterizeOperators();
210    }
211
212    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
213      ParameterizeOperators();
214    }
215
216    protected override void OnEvaluatorChanged() {
217      base.OnEvaluatorChanged();
218      ParameterizeOperators();
219    }
220
221    public event EventHandler ProblemDataChanged;
222    protected virtual void OnProblemDataChanged() {
223      UpdateGrammar();
224      UpdateDatasetPartitions();
225      ParameterizeOperators();
226
227      var handler = ProblemDataChanged;
228      if (handler != null) handler(this, EventArgs.Empty);
229
230      OnReset();
231    }
232    #endregion
233
234    protected virtual void ParameterizeOperators() {
235      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
236
237      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
238        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
239      }
240      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
241        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
242        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
243      }
244      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
245        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
246        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
247      }
248      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
249        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
250        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
251        op.SamplesStartParameter.Value.Value = ProblemData.TrainingPartitionStart.Value;
252        op.SamplesEndParameter.Value.Value = ProblemData.TrainingPartitionEnd.Value;
253      }
254      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
255        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
256        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
257      }
258      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
259        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
260      }
261      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
262        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
263      }
264      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
265        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
266      }
267      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
268        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
269      }
270    }
271
272    public abstract void ImportProblemDataFromFile(string fileName);
273  }
274}
Note: See TracBrowser for help on using the repository browser.