Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10268 was 10268, checked in by gkronber, 10 years ago

#2109 implemented a grammar especially for GE (the grammar is not configured correctly when used in a classic symbolic regression/classification problem).
To use this grammar in a classical symbolic expression problem, first set the grammar in a GEProblem and load the problem instance (this creates the necessary variable symbols).
After this the configured grammar can be dragged onto the grammar parameter of the classical problem.

File size: 21.5 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 MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
54    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
55    //private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
56    //private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
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
64    private const string ProblemDataParameterDescription = "";
65    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
66    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
67    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.";
68    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
69    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
70    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
71    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
72    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
73    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
74    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
75    private const string BoundsParameterDescription = "The integer number range in which the single genomes of a genotype are created.";
76    private const string GenotypeToPhenotypeMapperParameterDescription = "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).";
77    #endregion
78
79    #region parameter properties
80    IParameter IDataAnalysisProblem.ProblemDataParameter {
81      get { return ProblemDataParameter; }
82    }
83    public IValueParameter<T> ProblemDataParameter {
84      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
85    }
86    public IValueParameter<GESymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter {
87      get { return (IValueParameter<GESymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
88    }
89    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
90      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
91    }
92    //public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
93    //  get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
94    //}
95    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
96      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
97    }
98    //public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
99    //  get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
100    //}
101    //public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
102    //  get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
103    //}
104    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
105      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
106    }
107    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
108      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
109    }
110    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
111      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
112    }
113    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
114      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
115    }
116    public IValueParameter<IntMatrix> BoundsParameter {
117      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
118    }
119    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
120      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
121    }
122    #endregion
123
124    #region properties
125    public string Filename { get; set; }
126    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
127
128    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
129      get { return ProblemData; }
130    }
131    public T ProblemData {
132      get { return ProblemDataParameter.Value; }
133      set { ProblemDataParameter.Value = value; }
134    }
135
136    public GESymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
137      get { return SymbolicExpressionTreeGrammarParameter.Value; }
138      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
139    }
140    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
141      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
142      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
143    }
144
145    //public IntValue MaximumSymbolicExpressionTreeDepth {
146    //  get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
147    //}
148    public IntValue MaximumSymbolicExpressionTreeLength {
149      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
150    }
151    //public IntValue MaximumFunctionDefinitions {
152    //  get { return MaximumFunctionDefinitionsParameter.Value; }
153    //}
154    //public IntValue MaximumFunctionArguments {
155    //  get { return MaximumFunctionArgumentsParameter.Value; }
156    //}
157    public PercentValue RelativeNumberOfEvaluatedSamples {
158      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
159    }
160
161    public IntRange FitnessCalculationPartition {
162      get { return FitnessCalculationPartitionParameter.Value; }
163    }
164    public IntRange ValidationPartition {
165      get { return ValidationPartitionParameter.Value; }
166    }
167    public BoolValue ApplyLinearScaling {
168      get { return ApplyLinearScalingParameter.Value; }
169    }
170    #endregion
171
172    [StorableConstructor]
173    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
174    [StorableHook(HookType.AfterDeserialization)]
175    private void AfterDeserialization() {
176      RegisterEventHandlers();
177    }
178    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
179      : base(original, cloner) {
180      RegisterEventHandlers();
181    }
182
183    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
184      : base(evaluator, solutionCreator) {
185      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
186      Parameters.Add(new ValueParameter<GESymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
187      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
188      //Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
189      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
190      //Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
191      //Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
192      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
193      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
194      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
195      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
196      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
197      Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
198      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
199
200      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
201      //MaximumFunctionArgumentsParameter.Hidden = true;
202      //MaximumFunctionDefinitionsParameter.Hidden = true;
203      ApplyLinearScalingParameter.Hidden = true;
204
205      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
206      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
207
208      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
209      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
210
211      InitializeOperators();
212
213      UpdateGrammar();
214      RegisterEventHandlers();
215    }
216
217    private void DeregisterGrammarHandler() {
218      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
219    }
220    private void RegisterGrammarHandler() {
221      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
222    }
223
224    protected virtual void UpdateGrammar() {
225      DeregisterGrammarHandler();
226      // create a new grammar instance with the correct allowed input variables
227      SymbolicExpressionTreeGrammarParameter.Value =
228        new GESymbolicExpressionGrammar(ProblemData.AllowedInputVariables, ProblemData.AllowedInputVariables.Count() * 3);
229      RegisterGrammarHandler();
230    }
231
232    private void InitializeOperators() {
233      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
234      // Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
235      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
236      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
237      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
238      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
239      ParameterizeOperators();
240    }
241
242    #region events
243    private void RegisterEventHandlers() {
244      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
245      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
246
247      RegisterGrammarHandler();
248
249      //MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
250      //MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
251      //MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
252    }
253
254    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
255      ValidationPartition.Start = 0;
256      ValidationPartition.End = 0;
257      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
258      OnProblemDataChanged();
259    }
260
261    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
262      UpdateGrammar();
263    }
264
265    //private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
266    //  if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
267    //    MaximumSymbolicExpressionTreeDepth.Value = 3;
268    //}
269
270    //protected override void OnSolutionCreatorChanged() {
271    //  base.OnSolutionCreatorChanged();
272    //  ParameterizeOperators();
273    //}
274
275    protected override void OnEvaluatorChanged() {
276      base.OnEvaluatorChanged();
277      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
278      ParameterizeOperators();
279    }
280
281    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
282      ParameterizeOperators();
283    }
284
285    public event EventHandler ProblemDataChanged;
286    protected virtual void OnProblemDataChanged() {
287      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
288      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
289
290      UpdateGrammar();
291      ParameterizeOperators();
292
293      var handler = ProblemDataChanged;
294      if (handler != null) handler(this, EventArgs.Empty);
295
296      OnReset();
297    }
298    #endregion
299
300    protected virtual void ParameterizeOperators() {
301      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
302
303      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
304        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
305      }
306      /*
307      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
308        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
309        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
310      }
311      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
312        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
313        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
314      }
315      */
316      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
317        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
318        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
319        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
320        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
321        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
322        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
323        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
324        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
325      }
326      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
327        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
328        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
329      }
330      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
331        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
332      }
333      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
334        op.BoundsParameter.ActualName = BoundsParameter.Name;
335        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
336      }
337      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
338        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
339      }
340      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
341        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
342      }
343      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
344        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
345      }
346      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
347        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
348      }
349      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
350        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
351        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
352      }
353      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
354        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
355      }
356      /*
357      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
358        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
359        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
360        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
361        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
362        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
363      }
364      */
365    }
366
367    #region Import & Export
368    public virtual void Load(T data) {
369      Name = data.Name;
370      Description = data.Description;
371      ProblemData = data;
372    }
373
374    public virtual T Export() {
375      return ProblemData;
376    }
377    #endregion
378  }
379}
Note: See TracBrowser for help on using the repository browser.