Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 10673

Last change on this file since 10673 was 10673, checked in by pfleck, 10 years ago
  • Preprocessing Plugin now uses ISymbolicDataAnalysisProblem instead of IDataAnalysisProblem
  • Added TransformationCollection to ISymbolicDataAnalysisProblem as hidden parameter
File size: 21.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
56    private const string TransformationsParameterName = "Transformations";
57
58    private const string ProblemDataParameterDescription = "";
59    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
60    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
61    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.";
62    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
63    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
64    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
65    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
66    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
67    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
68    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
69    private const string TransformationsParameterDescrioption = "The transformations which were applied on the input variables.";
70    #endregion
71
72    #region parameter properties
73    IParameter IDataAnalysisProblem.ProblemDataParameter {
74      get { return ProblemDataParameter; }
75    }
76    public IValueParameter<T> ProblemDataParameter {
77      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
78    }
79    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
80      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
81    }
82    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
83      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
84    }
85    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
86      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
87    }
88    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
89      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
90    }
91    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
92      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
93    }
94    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
95      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
96    }
97    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
98      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
99    }
100    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
101      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
102    }
103    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
104      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
105    }
106    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
107      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
108    }
109    public IFixedValueParameter<TransformationCollection> TransformationsParameter {
110      get { return (IFixedValueParameter<TransformationCollection>)Parameters[TransformationsParameterName]; }
111    }
112    #endregion
113
114    #region properties
115    public string Filename { get; set; }
116    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
117
118    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
119      get { return ProblemData; }
120    }
121    public T ProblemData {
122      get { return ProblemDataParameter.Value; }
123      set { ProblemDataParameter.Value = value; }
124    }
125
126    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
127      get { return SymbolicExpressionTreeGrammarParameter.Value; }
128      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
129    }
130    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
131      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
132      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
133    }
134
135    public IntValue MaximumSymbolicExpressionTreeDepth {
136      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
137    }
138    public IntValue MaximumSymbolicExpressionTreeLength {
139      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
140    }
141    public IntValue MaximumFunctionDefinitions {
142      get { return MaximumFunctionDefinitionsParameter.Value; }
143    }
144    public IntValue MaximumFunctionArguments {
145      get { return MaximumFunctionArgumentsParameter.Value; }
146    }
147    public PercentValue RelativeNumberOfEvaluatedSamples {
148      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
149    }
150
151    public IntRange FitnessCalculationPartition {
152      get { return FitnessCalculationPartitionParameter.Value; }
153    }
154    public IntRange ValidationPartition {
155      get { return ValidationPartitionParameter.Value; }
156    }
157    public BoolValue ApplyLinearScaling {
158      get { return ApplyLinearScalingParameter.Value; }
159    }
160    #endregion
161
162    [StorableConstructor]
163    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
167        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
168        ApplyLinearScalingParameter.Hidden = true;
169
170        //it is assumed that for all symbolic regression algorithms linear scaling was set to true
171        //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator
172        if (GetType().Name.Contains("SymbolicRegression"))
173          ApplyLinearScaling.Value = true;
174      }
175
176      if (!Parameters.ContainsKey(TransformationsParameterName)) {
177        Parameters.Add(new FixedValueParameter<TransformationCollection>(TransformationsParameterName, TransformationsParameterDescrioption, new TransformationCollection()));
178        TransformationsParameter.Hidden = true;
179      }
180
181      RegisterEventHandlers();
182    }
183    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
184      : base(original, cloner) {
185      RegisterEventHandlers();
186    }
187
188    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
189      : base(evaluator, solutionCreator) {
190      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
191      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
192      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
193      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
194      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
195      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
196      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
197      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
198      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
199      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
200      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
201      Parameters.Add(new FixedValueParameter<TransformationCollection>(TransformationsParameterName, TransformationsParameterDescrioption, new TransformationCollection()));
202
203      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
204      MaximumFunctionArgumentsParameter.Hidden = true;
205      MaximumFunctionDefinitionsParameter.Hidden = true;
206      ApplyLinearScalingParameter.Hidden = true;
207      TransformationsParameter.Hidden = true;
208
209      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
210      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
211
212      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
213      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
214
215      InitializeOperators();
216
217      UpdateGrammar();
218      RegisterEventHandlers();
219    }
220
221    protected virtual void UpdateGrammar() {
222      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
223      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
224      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
225        if (!varSymbol.Fixed) {
226          varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);
227          varSymbol.VariableNames = ProblemData.AllowedInputVariables;
228        }
229      }
230      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
231        if (!varSymbol.Fixed) {
232          varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);
233          varSymbol.VariableNames = ProblemData.AllowedInputVariables;
234        }
235      }
236    }
237
238    private void InitializeOperators() {
239      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
240      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
241      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
242      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
243      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
244      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
245      ParameterizeOperators();
246    }
247
248    #region events
249    private void RegisterEventHandlers() {
250      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
251      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
252
253      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
254
255      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
256      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
257      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
258    }
259
260    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
261      ValidationPartition.Start = 0;
262      ValidationPartition.End = 0;
263      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
264      OnProblemDataChanged();
265    }
266
267    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
268      UpdateGrammar();
269    }
270
271    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
272      UpdateGrammar();
273    }
274
275    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
276      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
277        MaximumSymbolicExpressionTreeDepth.Value = 3;
278    }
279
280    protected override void OnSolutionCreatorChanged() {
281      base.OnSolutionCreatorChanged();
282      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
283      ParameterizeOperators();
284    }
285
286    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
287      ParameterizeOperators();
288    }
289
290    protected override void OnEvaluatorChanged() {
291      base.OnEvaluatorChanged();
292      ParameterizeOperators();
293    }
294
295    public event EventHandler ProblemDataChanged;
296    protected virtual void OnProblemDataChanged() {
297      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
298      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
299
300      UpdateGrammar();
301      ParameterizeOperators();
302
303      var handler = ProblemDataChanged;
304      if (handler != null) handler(this, EventArgs.Empty);
305
306      OnReset();
307    }
308    #endregion
309
310    protected virtual void ParameterizeOperators() {
311      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
312
313      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
314        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
315      }
316      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
317        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
318        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
319      }
320      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
321        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
322        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
323      }
324      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
325        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
326        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
327        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
328        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
329        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
330      }
331      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
332        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
333        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
334      }
335      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
336        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
337      }
338      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
339        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
340      }
341      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
342        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
343      }
344      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
345        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
346      }
347      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
348        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
349      }
350      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
351        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
352        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
353      }
354      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
355        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
356      }
357      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
358        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
359        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
360        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
361        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
362        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
363      }
364    }
365
366    #region Import & Export
367    public virtual void Load(T data) {
368      Name = data.Name;
369      Description = data.Description;
370      ProblemData = data;
371    }
372
373    public virtual T Export() {
374      return ProblemData;
375    }
376    #endregion
377  }
378}
Note: See TracBrowser for help on using the repository browser.