Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 18239

Last change on this file since 18239 was 18229, checked in by pfleck, 2 years ago

#3040 Added mutation for optimizing aggregation window that uses a nested optimizer.

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