Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.GrammaticalEvolution/3.4/SymbolicRegression/GESymbolicDataAnalysisProblem.cs @ 17695

Last change on this file since 17695 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: 17.0 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 * Author: Sabine Winkler
21 */
22
23#endregion
24
25using System;
26using System.Drawing;
27using System.Linq;
28using HEAL.Attic;
29using HeuristicLab.Common;
30using HeuristicLab.Common.Resources;
31using HeuristicLab.Core;
32using HeuristicLab.Data;
33using HeuristicLab.Encodings.IntegerVectorEncoding;
34using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
35using HeuristicLab.Optimization;
36using HeuristicLab.Parameters;
37using HeuristicLab.PluginInfrastructure;
38using HeuristicLab.Problems.DataAnalysis;
39using HeuristicLab.Problems.DataAnalysis.Symbolic;
40using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
41using HeuristicLab.Problems.Instances;
42
43namespace HeuristicLab.Problems.GrammaticalEvolution {
44  [StorableType("E31AC1E8-590D-4D65-883F-3113544B6C91")]
45  public abstract class GESymbolicDataAnalysisProblem<T, U> : HeuristicOptimizationProblem<U>, IDataAnalysisProblem<T>,
46                                                                 IGESymbolicDataAnalysisProblem, IStorableContent,
47                                                                 IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
48    where T : class, IDataAnalysisProblemData
49    where U : class, IGESymbolicDataAnalysisEvaluator<T> {
50
51    #region parameter names & descriptions
52    private const string ProblemDataParameterName = "ProblemData";
53    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
54    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
55    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
56    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
57    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
58    private const string ValidationPartitionParameterName = "ValidationPartition";
59    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
60    private const string BoundsParameterName = "Bounds";
61    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
62    private const string ProblemDataParameterDescription = "";
63    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
64    private const string SymbolicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
65    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
66    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
67    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
68    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
69    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
70    private const string BoundsParameterDescription = "The integer number range in which the single genomes of a genotype are created.";
71    private const string GenotypeToPhenotypeMapperParameterDescription = "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).";
72    #endregion
73
74    #region parameter properties
75    public IValueParameter<T> ProblemDataParameter {
76      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
77    }
78    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
79      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
80    }
81    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
82      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
83    }
84    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
85      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
86    }
87    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
88      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
89    }
90    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
91      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
92    }
93    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
94      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
95    }
96    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
97      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
98    }
99    public IValueParameter<IntMatrix> BoundsParameter {
100      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
101    }
102    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
103      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
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 MaximumSymbolicExpressionTreeLength {
129      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
130    }
131
132    public PercentValue RelativeNumberOfEvaluatedSamples {
133      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
134    }
135
136    public IntRange FitnessCalculationPartition {
137      get { return FitnessCalculationPartitionParameter.Value; }
138    }
139    public IntRange ValidationPartition {
140      get { return ValidationPartitionParameter.Value; }
141    }
142    public BoolValue ApplyLinearScaling {
143      get { return ApplyLinearScalingParameter.Value; }
144    }
145    #endregion
146
147    [StorableConstructor]
148    protected GESymbolicDataAnalysisProblem(StorableConstructorFlag _) : base(_) { }
149    [StorableHook(HookType.AfterDeserialization)]
150    private void AfterDeserialization() {
151      RegisterEventHandlers();
152    }
153    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U> original, Cloner cloner)
154      : base(original, cloner) {
155      RegisterEventHandlers();
156    }
157
158    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator)
159      : base(evaluator) {
160      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
161      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
162      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymbolicExpressionTreeInterpreterParameterDescription));
163      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
164      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
165      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
166      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
167      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
168      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
169      Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
170      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
171
172      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
173      ApplyLinearScalingParameter.Hidden = true;
174
175      if (problemData.AllowedInputVariables.Any(name => !problemData.Dataset.VariableHasType<double>(name))) throw new NotSupportedException("Categorical variables are not supported");
176      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
177      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
178
179      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
180      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
181
182      InitializeOperators();
183
184      UpdateGrammar();
185      RegisterEventHandlers();
186    }
187
188    private void DeregisterGrammarHandler() {
189      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
190    }
191    private void RegisterGrammarHandler() {
192      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
193    }
194
195    private void UpdateGrammar() {
196      DeregisterGrammarHandler();
197      // create a new grammar instance with the correct allowed input variables
198      SymbolicExpressionTreeGrammarParameter.Value =
199        new GESymbolicExpressionGrammar(ProblemData.AllowedInputVariables, ProblemData.AllowedInputVariables.Count() * 3);
200      RegisterGrammarHandler();
201    }
202
203    private void InitializeOperators() {
204      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
205      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
206      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
207      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
208      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
209      ParameterizeOperators();
210    }
211
212    #region events
213    private void RegisterEventHandlers() {
214      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
215      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
216
217      RegisterGrammarHandler();
218    }
219
220    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
221      ValidationPartition.Start = 0;
222      ValidationPartition.End = 0;
223      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
224      OnProblemDataChanged();
225    }
226
227    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
228      UpdateGrammar();
229    }
230
231    protected override void OnEvaluatorChanged() {
232      base.OnEvaluatorChanged();
233      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
234      ParameterizeOperators();
235    }
236
237    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
238      ParameterizeOperators();
239    }
240
241    public event EventHandler ProblemDataChanged;
242    protected virtual void OnProblemDataChanged() {
243      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
244      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
245
246      UpdateGrammar();
247      ParameterizeOperators();
248
249      var handler = ProblemDataChanged;
250      if (handler != null) handler(this, EventArgs.Empty);
251
252      OnReset();
253    }
254    #endregion
255
256    protected virtual void ParameterizeOperators() {
257      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
258
259      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
260        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
261      }
262      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
263        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
264        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
265        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
266        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
267        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
268        //op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
269        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
270        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
271      }
272      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
273        //op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
274        //op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
275      }
276      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
277        //op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
278      }
279      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
280        op.BoundsParameter.ActualName = BoundsParameter.Name;
281        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
282      }
283      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
284        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
285      }
286      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
287        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
288      }
289      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
290        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
291      }
292      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
293        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
294      }
295      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
296        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
297        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
298      }
299      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
300        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
301      }
302    }
303
304    #region Import & Export
305    public virtual void Load(T data) {
306      Name = data.Name;
307      Description = data.Description;
308      ProblemData = data;
309    }
310
311    public virtual T Export() {
312      return ProblemData;
313    }
314    #endregion
315  }
316}
Note: See TracBrowser for help on using the repository browser.