Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10884 was 10276, checked in by sawinkle, 11 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
RevLine 
[10072]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;
[10073]29using HeuristicLab.Encodings.IntegerVectorEncoding;
[10072]30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
[10073]35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
[10072]38using HeuristicLab.Problems.Instances;
39
[10073]40namespace HeuristicLab.Problems.GrammaticalEvolution {
[10072]41  [StorableClass]
[10075]42  public abstract class GESymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>,
[10226]43                                                                 IGESymbolicDataAnalysisProblem, IStorableContent,
[10075]44                                                                 IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
[10072]45    where T : class, IDataAnalysisProblemData
[10073]46    where U : class, IGESymbolicDataAnalysisEvaluator<T>
47    where V : class, IIntegerVectorCreator {
[10072]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";
[10073]58    private const string BoundsParameterName = "Bounds";
59    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
[10072]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.";
[10073]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).";
[10072]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    }
[10268]83    public IValueParameter<GESymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
84      get { return (IValueParameter<GESymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
[10072]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    }
[10073]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    }
[10072]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
[10268]124    public GESymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
[10072]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    }
[10276]136
[10072]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]
[10073]153    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
[10072]154    [StorableHook(HookType.AfterDeserialization)]
155    private void AfterDeserialization() {
156      RegisterEventHandlers();
157    }
[10073]158    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
[10072]159      : base(original, cloner) {
160      RegisterEventHandlers();
161    }
162
[10073]163    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
[10072]164      : base(evaluator, solutionCreator) {
165      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
[10268]166      Parameters.Add(new ValueParameter<GESymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
[10072]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)));
[10073]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()));
[10072]176
177      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
178      ApplyLinearScalingParameter.Hidden = true;
179
[10268]180      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
[10072]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
[10268]192    private void DeregisterGrammarHandler() {
193      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
194    }
195    private void RegisterGrammarHandler() {
196      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
197    }
198
[10072]199    protected virtual void UpdateGrammar() {
[10268]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();
[10072]205    }
206
207    private void InitializeOperators() {
[10073]208      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
[10072]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
[10268]221      RegisterGrammarHandler();
[10072]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
[10073]235    protected override void OnEvaluatorChanged() {
236      base.OnEvaluatorChanged();
237      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
[10072]238      ParameterizeOperators();
239    }
240
[10073]241    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
[10072]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      }
[10073]266      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
[10072]267        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
[10073]268        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]269        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
270        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
271        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
[10075]272        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
273        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
274        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
[10072]275      }
[10073]276      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
[10075]277        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
278        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
[10072]279      }
[10073]280      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
281        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
[10072]282      }
[10073]283      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
284        op.BoundsParameter.ActualName = BoundsParameter.Name;
285        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
286      }
[10072]287      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
[10073]288        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]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>()) {
[10073]297        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]298      }
[10073]299      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
[10072]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.