Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18082 was 18082, checked in by pfleck, 3 years ago

#3040: Added the ISymbolicDataAnalysisExpressionManipulator and necessary code analogously to the ISymbolicDataAnalysisExpressionCrossover.

File size: 21.2 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
203      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
204      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
205
206      InitializeOperators();
207
208      UpdateGrammar();
209      RegisterEventHandlers();
210    }
211
212    protected virtual void UpdateGrammar() {
213      var problemData = ProblemData;
214      var grammar = SymbolicExpressionTreeGrammar;
215
216      grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
217      grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
218
219      grammar.ConfigureVariableSymbols(problemData);
220    }
221
222    private void InitializeOperators() {
223      var operators = new HashSet<IItem>(new TypeEqualityComparer<IItem>());
224      operators.Add(new SubtreeCrossover());
225      operators.Add(new MultiSymbolicExpressionTreeManipulator());
226
227      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>())
228        operators.Add(op);
229      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>())
230        operators.Add(op);
231      foreach (var op in ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionManipulator<T>>())
232        operators.Add(op);
233
234      operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
235      operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
236      operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
237      operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
238      operators.Add(new SymbolicExpressionTreeBottomUpSimilarityCalculator());
239      operators.Add(new SymbolicDataAnalysisBottomUpDiversityAnalyzer(operators.OfType<SymbolicExpressionTreeBottomUpSimilarityCalculator>().First()));
240
241      Operators.AddRange(operators);
242      ParameterizeOperators();
243    }
244
245    #region events
246    private void RegisterEventHandlers() {
247      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
248      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
249
250      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
251
252      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
253      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
254      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
255    }
256
257    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
258      ValidationPartition.Start = 0;
259      ValidationPartition.End = 0;
260      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
261      OnProblemDataChanged();
262    }
263
264    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
265      UpdateGrammar();
266    }
267
268    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
269      UpdateGrammar();
270    }
271
272    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
273      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
274        MaximumSymbolicExpressionTreeDepth.Value = 3;
275    }
276
277    protected override void OnSolutionCreatorChanged() {
278      base.OnSolutionCreatorChanged();
279      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
280      ParameterizeOperators();
281    }
282
283    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
284      ParameterizeOperators();
285    }
286
287    protected override void OnEvaluatorChanged() {
288      base.OnEvaluatorChanged();
289      ParameterizeOperators();
290    }
291
292    public event EventHandler ProblemDataChanged;
293    protected virtual void OnProblemDataChanged() {
294      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
295      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
296
297      UpdateGrammar();
298      ParameterizeOperators();
299
300      var handler = ProblemDataChanged;
301      if (handler != null) handler(this, EventArgs.Empty);
302
303      OnReset();
304    }
305    #endregion
306
307    protected virtual void ParameterizeOperators() {
308      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
309
310      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
311        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
312      }
313      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
314        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
315        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
316      }
317      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
318        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
319        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
320      }
321      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
322        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
323        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
324        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
325        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
326        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
327      }
328      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
329        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
330        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
331      }
332      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
333        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
334      }
335      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
336        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
337      }
338      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
339        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
340      }
341      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
342        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
343      }
344      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
345        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
346      }
347      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
348        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
349        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
350      }
351      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
352        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
353      }
354      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
355        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
356        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
357        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
358        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
359        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
360      }
361      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionManipulator<T>>()) {
362        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
363        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
364        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
365        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
366        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
367      }
368    }
369
370    #region Import & Export
371    public virtual void Load(T data) {
372      Name = data.Name;
373      Description = data.Description;
374      ProblemData = data;
375    }
376
377    public virtual T Export() {
378      return ProblemData;
379    }
380    #endregion
381  }
382}
Note: See TracBrowser for help on using the repository browser.