Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Sliding Window GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 7952

Last change on this file since 7952 was 7850, checked in by mkommend, 12 years ago

#1837: Added view for sliding window GP and updated analyzer.

File size: 19.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
37  [StorableClass]
38  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
39    IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
40    where T : class, IDataAnalysisProblemData
41    where U : class, ISymbolicDataAnalysisEvaluator<T>
42    where V : class, ISymbolicDataAnalysisSolutionCreator {
43
44    #region parameter names & descriptions
45    private const string ProblemDataParameterName = "ProblemData";
46    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
47    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
48    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
49    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
50    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
51    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
52    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
53    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
54    private const string ValidationPartitionParameterName = "ValidationPartition";
55
56    private const string ProblemDataParameterDescription = "";
57    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
58    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
59    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.";
60    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
61    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
62    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
63    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
64    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
65    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
66    #endregion
67
68    #region parameter properties
69    IParameter IDataAnalysisProblem.ProblemDataParameter {
70      get { return ProblemDataParameter; }
71    }
72    public IValueParameter<T> ProblemDataParameter {
73      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
74    }
75    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
76      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
77    }
78    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
79      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
80    }
81    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
82      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
83    }
84    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
85      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
86    }
87    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
88      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
89    }
90    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
91      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
92    }
93    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
94      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
95    }
96    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
97      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
98    }
99    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
100      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
101    }
102    #endregion
103
104    #region properties
105    public string Filename { get; set; }
106    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
107
108    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
109      get { return ProblemData; }
110    }
111    public T ProblemData {
112      get { return ProblemDataParameter.Value; }
113      set { ProblemDataParameter.Value = value; }
114    }
115
116    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
117      get { return SymbolicExpressionTreeGrammarParameter.Value; }
118      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
119    }
120    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
121      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
122      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
123    }
124
125    public IntValue MaximumSymbolicExpressionTreeDepth {
126      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
127    }
128    public IntValue MaximumSymbolicExpressionTreeLength {
129      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
130    }
131    public IntValue MaximumFunctionDefinitions {
132      get { return MaximumFunctionDefinitionsParameter.Value; }
133    }
134    public IntValue MaximumFunctionArguments {
135      get { return MaximumFunctionArgumentsParameter.Value; }
136    }
137    public PercentValue RelativeNumberOfEvaluatedSamples {
138      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
139    }
140
141    public IntRange FitnessCalculationPartition {
142      get { return FitnessCalculationPartitionParameter.Value; }
143    }
144    public IntRange ValidationPartition {
145      get { return ValidationPartitionParameter.Value; }
146    }
147    #endregion
148
149    [StorableConstructor]
150    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
151    [StorableHook(HookType.AfterDeserialization)]
152    private void AfterDeserialization() {
153      RegisterEventHandlers();
154    }
155    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
156      : base(original, cloner) {
157      RegisterEventHandlers();
158    }
159
160    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
161      : base(evaluator, solutionCreator) {
162      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
163      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
164      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
165      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
166      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
169      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
170      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
171      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
172
173      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
174      MaximumFunctionArgumentsParameter.Hidden = true;
175      MaximumFunctionDefinitionsParameter.Hidden = true;
176
177      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
178      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
179
180      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
181      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
182
183      InitializeOperators();
184
185      UpdateGrammar();
186      RegisterEventHandlers();
187    }
188
189    protected virtual void UpdateGrammar() {
190      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
191      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
192      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
193        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
194      }
195      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
196        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
197      }
198    }
199
200    private void InitializeOperators() {
201      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
202      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
203      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
204      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
205      Operators.Add(new SymbolicDataAnalysisComplexityAnalyzer());
206      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
207      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
208      Operators.Add(new GenerationalSlidingWindowAnalyzer<T>());
209      ParameterizeOperators();
210    }
211
212    #region events
213    private void RegisterEventHandlers() {
214      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
215      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
216
217      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
218
219      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
220      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
221      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
222    }
223
224    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
225      ValidationPartition.Start = 0;
226      ValidationPartition.End = 0;
227      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
228      OnProblemDataChanged();
229    }
230
231    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
232      UpdateGrammar();
233    }
234
235    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
236      UpdateGrammar();
237    }
238
239    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
240      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
241        MaximumSymbolicExpressionTreeDepth.Value = 3;
242    }
243
244    protected override void OnSolutionCreatorChanged() {
245      base.OnSolutionCreatorChanged();
246      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
247      ParameterizeOperators();
248    }
249
250    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
251      ParameterizeOperators();
252    }
253
254    protected override void OnEvaluatorChanged() {
255      base.OnEvaluatorChanged();
256      ParameterizeOperators();
257    }
258
259    public event EventHandler ProblemDataChanged;
260    protected virtual void OnProblemDataChanged() {
261      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
262      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
263
264      UpdateGrammar();
265      ParameterizeOperators();
266
267      var handler = ProblemDataChanged;
268      if (handler != null) handler(this, EventArgs.Empty);
269
270      OnReset();
271    }
272    #endregion
273
274    protected virtual void ParameterizeOperators() {
275      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
276
277      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
278        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
279      }
280      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
281        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
282        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
283      }
284      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
285        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
286        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
287      }
288      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
289        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
290        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
291        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
292        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
293      }
294      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
295        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
296        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
297      }
298      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
299        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
300      }
301      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
302        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
303      }
304      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
305        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
306      }
307      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
308        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
309        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
310      }
311      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
312        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
313      }
314      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
315        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameterName;
316      }
317      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
318        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
319        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
320        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
321        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
322      }
323
324      foreach (var op in operators.OfType<SlidingWindowAnalyzer<T>>()) {
325        op.FitnessCalculationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
326        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
327
328        var start = ProblemData.TrainingPartition.Start;
329        var end = ProblemData.TrainingPartition.End;
330        var size = (end - start) / 5;
331        op.SlidingWindowSize.Value = size;
332        op.SlidingWindowStepWidth.Value = size / 2;
333      }
334    }
335
336    #region Import & Export
337    public void Load(T data) {
338      Name = data.Name;
339      Description = data.Description;
340      ProblemData = data;
341      OnReset();
342    }
343
344    public T Export() {
345      return ProblemData;
346    }
347    #endregion
348  }
349}
Note: See TracBrowser for help on using the repository browser.