Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file was 17695, checked in by abeham, 4 years ago

#2521:

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