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, 5 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
RevLine 
[5577]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5577]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;
[16692]23using System.Collections.Generic;
[5577]24using System.Drawing;
[5618]25using System.Linq;
[17457]26using HEAL.Attic;
[5577]27using HeuristicLab.Common;
28using HeuristicLab.Common.Resources;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
[5618]31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5577]32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
[5618]34using HeuristicLab.PluginInfrastructure;
[7823]35using HeuristicLab.Problems.Instances;
[5577]36
37namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[16723]38  [StorableType("59935E69-C4A5-480E-8FFB-D9669DE9BFD4")]
[17695]39  public abstract class SymbolicDataAnalysisProblem<T, U> : HeuristicOptimizationProblem<U>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
[7823]40    IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
[6978]41    where T : class, IDataAnalysisProblemData
[17695]42    where U : class, ISymbolicDataAnalysisEvaluator<T> {
[5770]43
[5577]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";
[5759]52    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
[5733]53    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
[5775]54    private const string ValidationPartitionParameterName = "ValidationPartition";
[8664]55    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
[5577]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.";
[5759]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.";
[5857]66    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
[8664]67    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
[5577]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    }
[5624]77    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
78      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
[5577]79    }
[5618]80    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
81      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
[5577]82    }
[5618]83    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
84      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
[5577]85    }
[5618]86    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
87      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
[5577]88    }
[5618]89    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
90      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
[5577]91    }
[5759]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    }
[5883]98    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
[5775]99      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
[5759]100    }
[8664]101    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
102      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
103    }
[5577]104    #endregion
105
106    #region properties
107    public string Filename { get; set; }
[7201]108    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
[5577]109
110    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
111      get { return ProblemData; }
112    }
113    public T ProblemData {
114      get { return ProblemDataParameter.Value; }
[5618]115      set { ProblemDataParameter.Value = value; }
[5577]116    }
117
118    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
119      get { return SymbolicExpressionTreeGrammarParameter.Value; }
[5618]120      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
[5577]121    }
[5624]122    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
[5577]123      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
[5618]124      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
[5577]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 {
[5618]137      get { return MaximumFunctionArgumentsParameter.Value; }
[5577]138    }
[5759]139    public PercentValue RelativeNumberOfEvaluatedSamples {
140      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
141    }
142
143    public IntRange FitnessCalculationPartition {
144      get { return FitnessCalculationPartitionParameter.Value; }
145    }
[5775]146    public IntRange ValidationPartition {
[5883]147      get { return ValidationPartitionParameter.Value; }
[5759]148    }
[8664]149    public BoolValue ApplyLinearScaling {
150      get { return ApplyLinearScalingParameter.Value; }
151    }
[5577]152    #endregion
153
154    [StorableConstructor]
[16723]155    protected SymbolicDataAnalysisProblem(StorableConstructorFlag _) : base(_) { }
[5618]156    [StorableHook(HookType.AfterDeserialization)]
157    private void AfterDeserialization() {
[8664]158      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
159        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
160        ApplyLinearScalingParameter.Hidden = true;
[8666]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;
[8664]166      }
167
[5618]168      RegisterEventHandlers();
169    }
[17695]170    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U> original, Cloner cloner)
[5618]171      : base(original, cloner) {
172      RegisterEventHandlers();
173    }
[5577]174
[17695]175    protected SymbolicDataAnalysisProblem(T problemData, U evaluator)
176      : base(evaluator) {
[5618]177      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
[5577]178      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
[5624]179      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
[5847]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));
[5759]186      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
[8664]187      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
[5618]188
[5854]189      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
190      MaximumFunctionArgumentsParameter.Hidden = true;
191      MaximumFunctionDefinitionsParameter.Hidden = true;
[8664]192      ApplyLinearScalingParameter.Hidden = true;
[5854]193
[5618]194      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
[9830]195      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
[5618]196
[5770]197      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
198      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
199
[5722]200      InitializeOperators();
201
[5618]202      UpdateGrammar();
203      RegisterEventHandlers();
[5577]204    }
205
[5685]206    protected virtual void UpdateGrammar() {
[16692]207      var problemData = ProblemData;
208      var grammar = SymbolicExpressionTreeGrammar;
[17457]209
[16692]210      grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
211      grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
[17457]212
213      grammar.ConfigureVariableSymbols(problemData);
[5685]214    }
215
[5618]216    private void InitializeOperators() {
[16692]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);
[5618]234      ParameterizeOperators();
235    }
236
[5685]237    #region events
[5618]238    private void RegisterEventHandlers() {
239      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
240      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
241
[5841]242      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
243
[5618]244      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
245      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
246      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
247    }
248
[5685]249    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
[5887]250      ValidationPartition.Start = 0;
251      ValidationPartition.End = 0;
[5685]252      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
253      OnProblemDataChanged();
254    }
255
[5841]256    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
257      UpdateGrammar();
258    }
259
[5618]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();
[5685]271      ParameterizeOperators();
[5618]272    }
273
[5577]274    public event EventHandler ProblemDataChanged;
275    protected virtual void OnProblemDataChanged() {
[5770]276      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
277      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
278
[5618]279      UpdateGrammar();
[5685]280      ParameterizeOperators();
281
[5577]282      var handler = ProblemDataChanged;
283      if (handler != null) handler(this, EventArgs.Empty);
[5618]284
285      OnReset();
[5577]286    }
[5685]287    #endregion
[5618]288
[5685]289    protected virtual void ParameterizeOperators() {
[7506]290      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
[5618]291
292      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
[8664]293        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
[5618]294      }
295      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
[8664]296        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
297        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
[5618]298      }
299      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
[8664]300        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
301        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
[5618]302      }
303      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
[5685]304        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
[17695]305        //op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5759]306        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
307        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[8664]308        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
[5618]309      }
310      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
[17695]311        //op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
312        //op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5618]313      }
314      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
[17695]315        //op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5618]316      }
317      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
[17695]318        //op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5618]319      }
[8664]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      }
[6135]326      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
[17695]327        //op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[6135]328      }
[5759]329      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
330        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[5883]331        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
[5759]332      }
[5685]333      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
[8664]334        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
[5685]335      }
[7506]336      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
[8664]337        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
[7506]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      }
[5618]343    }
[5623]344
[7823]345    #region Import & Export
[9452]346    public virtual void Load(T data) {
[7823]347      Name = data.Name;
348      Description = data.Description;
349      ProblemData = data;
350    }
351
[9452]352    public virtual T Export() {
[7823]353      return ProblemData;
354    }
355    #endregion
[5577]356  }
357}
Note: See TracBrowser for help on using the repository browser.