Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.GrammaticalEvolution/3.4/SymbolicRegression/GESymbolicDataAnalysisProblem.cs @ 16045

Last change on this file since 16045 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 17.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
29using HeuristicLab.Common.Resources;
30using HeuristicLab.Core;
31using HeuristicLab.Data;
32using HeuristicLab.Encodings.IntegerVectorEncoding;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34using HeuristicLab.Optimization;
35using HeuristicLab.Parameters;
36using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
45  public abstract class GESymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>,
46                                                                 IGESymbolicDataAnalysisProblem, IStorableContent,
47                                                                 IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
48    where T : class, IDataAnalysisProblemData
49    where U : class, IGESymbolicDataAnalysisEvaluator<T>
50    where V : class, IIntegerVectorCreator {
51
52    #region parameter names & descriptions
53    private const string ProblemDataParameterName = "ProblemData";
54    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
55    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
56    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
57    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
58    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
59    private const string ValidationPartitionParameterName = "ValidationPartition";
60    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
61    private const string BoundsParameterName = "Bounds";
62    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
63    private const string ProblemDataParameterDescription = "";
64    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
65    private const string SymbolicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
66    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
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    private const string BoundsParameterDescription = "The integer number range in which the single genomes of a genotype are created.";
72    private const string GenotypeToPhenotypeMapperParameterDescription = "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).";
73    #endregion
74
75    #region parameter properties
76    IParameter IDataAnalysisProblem.ProblemDataParameter {
77      get { return ProblemDataParameter; }
78    }
79    public IValueParameter<T> ProblemDataParameter {
80      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
81    }
82    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
83      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
84    }
85    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
86      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
87    }
88    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
89      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
90    }
91    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
92      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
93    }
94    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
95      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
96    }
97    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
98      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
99    }
100    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
101      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
102    }
103    public IValueParameter<IntMatrix> BoundsParameter {
104      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
105    }
106    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
107      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
108    }
109    #endregion
110
111    #region properties
112    public string Filename { get; set; }
113    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
114
115    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
116      get { return ProblemData; }
117    }
118    public T ProblemData {
119      get { return ProblemDataParameter.Value; }
120      set { ProblemDataParameter.Value = value; }
121    }
122
123    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
124      get { return SymbolicExpressionTreeGrammarParameter.Value; }
125      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
126    }
127    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
128      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
129      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
130    }
131
132    public IntValue MaximumSymbolicExpressionTreeLength {
133      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
134    }
135
136    public PercentValue RelativeNumberOfEvaluatedSamples {
137      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
138    }
139
140    public IntRange FitnessCalculationPartition {
141      get { return FitnessCalculationPartitionParameter.Value; }
142    }
143    public IntRange ValidationPartition {
144      get { return ValidationPartitionParameter.Value; }
145    }
146    public BoolValue ApplyLinearScaling {
147      get { return ApplyLinearScalingParameter.Value; }
148    }
149    #endregion
150
151    [StorableConstructor]
152    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
153    [StorableHook(HookType.AfterDeserialization)]
154    private void AfterDeserialization() {
155      RegisterEventHandlers();
156    }
157    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
158      : base(original, cloner) {
159      RegisterEventHandlers();
160    }
161
162    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
163      : base(evaluator, solutionCreator) {
164      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
165      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
166      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymbolicExpressionTreeInterpreterParameterDescription));
167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
169      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
170      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
171      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
172      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
173      Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
174      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
175
176      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
177      ApplyLinearScalingParameter.Hidden = true;
178
179      if (problemData.AllowedInputVariables.Any(name => !problemData.Dataset.VariableHasType<double>(name))) throw new NotSupportedException("Categorical variables are not supported");
180      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
181      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
182
183      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
184      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
185
186      InitializeOperators();
187
188      UpdateGrammar();
189      RegisterEventHandlers();
190    }
191
192    private void DeregisterGrammarHandler() {
193      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
194    }
195    private void RegisterGrammarHandler() {
196      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
197    }
198
199    private void UpdateGrammar() {
200      DeregisterGrammarHandler();
201      // create a new grammar instance with the correct allowed input variables
202      SymbolicExpressionTreeGrammarParameter.Value =
203        new GESymbolicExpressionGrammar(ProblemData.AllowedInputVariables, ProblemData.AllowedInputVariables.Count() * 3);
204      RegisterGrammarHandler();
205    }
206
207    private void InitializeOperators() {
208      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
209      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
210      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
211      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
212      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
213      ParameterizeOperators();
214    }
215
216    #region events
217    private void RegisterEventHandlers() {
218      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
219      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
220
221      RegisterGrammarHandler();
222    }
223
224    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
225      ValidationPartition.Start = 0;
226      ValidationPartition.End = 0;
227      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
228      OnProblemDataChanged();
229    }
230
231    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
232      UpdateGrammar();
233    }
234
235    protected override void OnEvaluatorChanged() {
236      base.OnEvaluatorChanged();
237      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
238      ParameterizeOperators();
239    }
240
241    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
242      ParameterizeOperators();
243    }
244
245    public event EventHandler ProblemDataChanged;
246    protected virtual void OnProblemDataChanged() {
247      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
248      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
249
250      UpdateGrammar();
251      ParameterizeOperators();
252
253      var handler = ProblemDataChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255
256      OnReset();
257    }
258    #endregion
259
260    protected virtual void ParameterizeOperators() {
261      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
262
263      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
264        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
265      }
266      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
267        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
268        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
269        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
270        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
271        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
272        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
273        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
274        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
275      }
276      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
277        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
278        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
279      }
280      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
281        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
282      }
283      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
284        op.BoundsParameter.ActualName = BoundsParameter.Name;
285        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
286      }
287      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
288        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
289      }
290      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
291        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
292      }
293      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
294        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
295      }
296      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
297        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
298      }
299      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
300        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
301        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
302      }
303      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
304        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
305      }
306    }
307
308    #region Import & Export
309    public virtual void Load(T data) {
310      Name = data.Name;
311      Description = data.Description;
312      ProblemData = data;
313    }
314
315    public virtual T Export() {
316      return ProblemData;
317    }
318    #endregion
319  }
320}
Note: See TracBrowser for help on using the repository browser.