Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 17520

Last change on this file since 17520 was 17520, checked in by mkommend, 4 years ago

#2521: Removed parameter for problemData in IDataAnalysisProblem.

File size: 20.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Drawing;
25using System.Linq;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Problems.Instances;
36
37namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
38  [StorableType("59935E69-C4A5-480E-8FFB-D9669DE9BFD4")]
39  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
40    IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
41    where T : class, IDataAnalysisProblemData
42    where U : class, ISymbolicDataAnalysisEvaluator<T>
43    where V : class, ISymbolicDataAnalysisSolutionCreator {
44
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    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
54    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
55    private const string ValidationPartitionParameterName = "ValidationPartition";
56    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
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    #endregion
70
71    #region parameter properties
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    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
103      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
104    }
105    #endregion
106
107    #region properties
108    public string Filename { get; set; }
109    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
110
111    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
112      get { return ProblemData; }
113    }
114    public T ProblemData {
115      get { return ProblemDataParameter.Value; }
116      set { ProblemDataParameter.Value = value; }
117    }
118
119    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
120      get { return SymbolicExpressionTreeGrammarParameter.Value; }
121      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
122    }
123    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
124      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
125      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
126    }
127
128    public IntValue MaximumSymbolicExpressionTreeDepth {
129      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
130    }
131    public IntValue MaximumSymbolicExpressionTreeLength {
132      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
133    }
134    public IntValue MaximumFunctionDefinitions {
135      get { return MaximumFunctionDefinitionsParameter.Value; }
136    }
137    public IntValue MaximumFunctionArguments {
138      get { return MaximumFunctionArgumentsParameter.Value; }
139    }
140    public PercentValue RelativeNumberOfEvaluatedSamples {
141      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
142    }
143
144    public IntRange FitnessCalculationPartition {
145      get { return FitnessCalculationPartitionParameter.Value; }
146    }
147    public IntRange ValidationPartition {
148      get { return ValidationPartitionParameter.Value; }
149    }
150    public BoolValue ApplyLinearScaling {
151      get { return ApplyLinearScalingParameter.Value; }
152    }
153    #endregion
154
155    [StorableConstructor]
156    protected SymbolicDataAnalysisProblem(StorableConstructorFlag _) : base(_) { }
157    [StorableHook(HookType.AfterDeserialization)]
158    private void AfterDeserialization() {
159      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
160        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
161        ApplyLinearScalingParameter.Hidden = true;
162
163        //it is assumed that for all symbolic regression algorithms linear scaling was set to true
164        //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator
165        if (GetType().Name.Contains("SymbolicRegression"))
166          ApplyLinearScaling.Value = true;
167      }
168
169      RegisterEventHandlers();
170    }
171    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
172      : base(original, cloner) {
173      RegisterEventHandlers();
174    }
175
176    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
177      : base(evaluator, solutionCreator) {
178      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
179      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
180      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
181      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
182      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
183      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
184      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
185      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
186      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
187      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
188      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
189
190      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
191      MaximumFunctionArgumentsParameter.Hidden = true;
192      MaximumFunctionDefinitionsParameter.Hidden = true;
193      ApplyLinearScalingParameter.Hidden = true;
194
195      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
196      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
197
198      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
199      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
200
201      InitializeOperators();
202
203      UpdateGrammar();
204      RegisterEventHandlers();
205    }
206
207    protected virtual void UpdateGrammar() {
208      var problemData = ProblemData;
209      var grammar = SymbolicExpressionTreeGrammar;
210
211      grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
212      grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
213
214      grammar.ConfigureVariableSymbols(problemData);
215    }
216
217    private void InitializeOperators() {
218      var operators = new HashSet<IItem>(new TypeEqualityComparer<IItem>());
219      operators.Add(new SubtreeCrossover());
220      operators.Add(new MultiSymbolicExpressionTreeManipulator());
221
222      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>())
223        operators.Add(op);
224      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>())
225        operators.Add(op);
226
227      operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
228      operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
229      operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
230      operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
231      operators.Add(new SymbolicExpressionTreeBottomUpSimilarityCalculator());
232      operators.Add(new SymbolicDataAnalysisBottomUpDiversityAnalyzer(operators.OfType<SymbolicExpressionTreeBottomUpSimilarityCalculator>().First()));
233
234      Operators.AddRange(operators);
235      ParameterizeOperators();
236    }
237
238    #region events
239    private void RegisterEventHandlers() {
240      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
241      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
242
243      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
244
245      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
246      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
247      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
248    }
249
250    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
251      ValidationPartition.Start = 0;
252      ValidationPartition.End = 0;
253      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
254      OnProblemDataChanged();
255    }
256
257    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
258      UpdateGrammar();
259    }
260
261    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
262      UpdateGrammar();
263    }
264
265    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
266      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
267        MaximumSymbolicExpressionTreeDepth.Value = 3;
268    }
269
270    protected override void OnSolutionCreatorChanged() {
271      base.OnSolutionCreatorChanged();
272      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
273      ParameterizeOperators();
274    }
275
276    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
277      ParameterizeOperators();
278    }
279
280    protected override void OnEvaluatorChanged() {
281      base.OnEvaluatorChanged();
282      ParameterizeOperators();
283    }
284
285    public event EventHandler ProblemDataChanged;
286    protected virtual void OnProblemDataChanged() {
287      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
288      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
289
290      UpdateGrammar();
291      ParameterizeOperators();
292
293      var handler = ProblemDataChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295
296      OnReset();
297    }
298    #endregion
299
300    protected virtual void ParameterizeOperators() {
301      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
302
303      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
304        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
305      }
306      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
307        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
308        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
309      }
310      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
311        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
312        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
313      }
314      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
315        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
316        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
317        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
318        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
319        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
320      }
321      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
322        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
323        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
324      }
325      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
326        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
327      }
328      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
329        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
330      }
331      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
332        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
333      }
334      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
335        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
336      }
337      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
338        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
339      }
340      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
341        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
342        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
343      }
344      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
345        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
346      }
347      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
348        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
349        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
350        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
351        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
352        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
353      }
354    }
355
356    #region Import & Export
357    public virtual void Load(T data) {
358      Name = data.Name;
359      Description = data.Description;
360      ProblemData = data;
361    }
362
363    public virtual T Export() {
364      return ProblemData;
365    }
366    #endregion
367  }
368}
Note: See TracBrowser for help on using the repository browser.