source: branches/GrammaticalEvolution/HeuristicLab.Problems.GrammaticalEvolution/Symbolic/GESymbolicDataAnalysisProblem.cs @ 10276

Last change on this file since 10276 was 10276, checked in by sawinkle, 9 years ago

#2109:
Refactoring:

  • Removed recursive version of mapping in /Mappers/DepthFirstMapper.cs, because only the iterative one is used.
  • Removed unused using statements and unused commented code.
File size: 17.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
38using HeuristicLab.Problems.Instances;
39
40namespace HeuristicLab.Problems.GrammaticalEvolution {
41  [StorableClass]
42  public abstract class GESymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>,
43                                                                 IGESymbolicDataAnalysisProblem, IStorableContent,
44                                                                 IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
45    where T : class, IDataAnalysisProblemData
46    where U : class, IGESymbolicDataAnalysisEvaluator<T>
47    where V : class, IIntegerVectorCreator {
48
49    #region parameter names & descriptions
50    private const string ProblemDataParameterName = "ProblemData";
51    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
52    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
53    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
54    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
55    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
56    private const string ValidationPartitionParameterName = "ValidationPartition";
57    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
58    private const string BoundsParameterName = "Bounds";
59    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
60
61    private const string ProblemDataParameterDescription = "";
62    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
63    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
64    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.";
65    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
66    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
67    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
68    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
69    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
70    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
71    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
72    private const string BoundsParameterDescription = "The integer number range in which the single genomes of a genotype are created.";
73    private const string GenotypeToPhenotypeMapperParameterDescription = "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).";
74    #endregion
75
76    #region parameter properties
77    IParameter IDataAnalysisProblem.ProblemDataParameter {
78      get { return ProblemDataParameter; }
79    }
80    public IValueParameter<T> ProblemDataParameter {
81      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
82    }
83    public IValueParameter<GESymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
84      get { return (IValueParameter<GESymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
85    }
86    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
87      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
88    }
89    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
90      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
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    public IValueParameter<IntMatrix> BoundsParameter {
105      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
106    }
107    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
108      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
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 GESymbolicExpressionGrammar 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 MaximumSymbolicExpressionTreeLength {
134      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
135    }
136
137    public PercentValue RelativeNumberOfEvaluatedSamples {
138      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
139    }
140
141    public IntRange FitnessCalculationPartition {
142      get { return FitnessCalculationPartitionParameter.Value; }
143    }
144    public IntRange ValidationPartition {
145      get { return ValidationPartitionParameter.Value; }
146    }
147    public BoolValue ApplyLinearScaling {
148      get { return ApplyLinearScalingParameter.Value; }
149    }
150    #endregion
151
152    [StorableConstructor]
153    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
154    [StorableHook(HookType.AfterDeserialization)]
155    private void AfterDeserialization() {
156      RegisterEventHandlers();
157    }
158    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
159      : base(original, cloner) {
160      RegisterEventHandlers();
161    }
162
163    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
164      : base(evaluator, solutionCreator) {
165      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
166      Parameters.Add(new ValueParameter<GESymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
167      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
169      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
170      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
171      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
172      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
173      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
174      Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
175      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
176
177      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
178      ApplyLinearScalingParameter.Hidden = true;
179
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    protected virtual 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>().OfType<IOperator>());
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.