Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2109 updated license headers and the solution file (for VS2012)

File size: 17.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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
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> MaximumSymbolicExpressionTreeLengthParameter {
93      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
94    }
95    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
96      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
97    }
98    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
99      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
100    }
101    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
102      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
103    }
104    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
105      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
106    }
107    public IValueParameter<IntMatrix> BoundsParameter {
108      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
109    }
110    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
111      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
112    }
113    #endregion
114
115    #region properties
116    public string Filename { get; set; }
117    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
118
119    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
120      get { return ProblemData; }
121    }
122    public T ProblemData {
123      get { return ProblemDataParameter.Value; }
124      set { ProblemDataParameter.Value = value; }
125    }
126
127    public GESymbolicExpressionGrammar SymbolicExpressionTreeGrammar {
128      get { return SymbolicExpressionTreeGrammarParameter.Value; }
129      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
130    }
131    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
132      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
133      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
134    }
135
136    public IntValue MaximumSymbolicExpressionTreeLength {
137      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
138    }
139
140    public PercentValue RelativeNumberOfEvaluatedSamples {
141      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
142    }
143
144    public IntRange FitnessCalculationPartition {
145      get { return FitnessCalculationPartitionParameter.Value; }
146    }
147    public IntRange ValidationPartition {
148      get { return ValidationPartitionParameter.Value; }
149    }
150    public BoolValue ApplyLinearScaling {
151      get { return ApplyLinearScalingParameter.Value; }
152    }
153    #endregion
154
155    [StorableConstructor]
156    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
157    [StorableHook(HookType.AfterDeserialization)]
158    private void AfterDeserialization() {
159      RegisterEventHandlers();
160    }
161    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
162      : base(original, cloner) {
163      RegisterEventHandlers();
164    }
165
166    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
167      : base(evaluator, solutionCreator) {
168      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
169      Parameters.Add(new ValueParameter<GESymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
170      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
171      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
172      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
173      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
174      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
175      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
176      IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
177      Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
178      Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
179
180      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
181      ApplyLinearScalingParameter.Hidden = true;
182
183      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
184      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
185
186      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
187      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
188
189      InitializeOperators();
190
191      UpdateGrammar();
192      RegisterEventHandlers();
193    }
194
195    private void DeregisterGrammarHandler() {
196      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
197    }
198    private void RegisterGrammarHandler() {
199      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
200    }
201
202    protected virtual void UpdateGrammar() {
203      DeregisterGrammarHandler();
204      // create a new grammar instance with the correct allowed input variables
205      SymbolicExpressionTreeGrammarParameter.Value =
206        new GESymbolicExpressionGrammar(ProblemData.AllowedInputVariables, ProblemData.AllowedInputVariables.Count() * 3);
207      RegisterGrammarHandler();
208    }
209
210    private void InitializeOperators() {
211      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>().OfType<IOperator>());
212      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
213      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
214      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
215      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
216      ParameterizeOperators();
217    }
218
219    #region events
220    private void RegisterEventHandlers() {
221      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
222      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
223
224      RegisterGrammarHandler();
225    }
226
227    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
228      ValidationPartition.Start = 0;
229      ValidationPartition.End = 0;
230      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
231      OnProblemDataChanged();
232    }
233
234    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
235      UpdateGrammar();
236    }
237
238    protected override void OnEvaluatorChanged() {
239      base.OnEvaluatorChanged();
240      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
241      ParameterizeOperators();
242    }
243
244    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
245      ParameterizeOperators();
246    }
247
248    public event EventHandler ProblemDataChanged;
249    protected virtual void OnProblemDataChanged() {
250      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
251      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
252
253      UpdateGrammar();
254      ParameterizeOperators();
255
256      var handler = ProblemDataChanged;
257      if (handler != null) handler(this, EventArgs.Empty);
258
259      OnReset();
260    }
261    #endregion
262
263    protected virtual void ParameterizeOperators() {
264      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
265
266      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
267        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
268      }
269      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
270        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
271        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
272        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
273        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
274        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
275        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
276        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
277        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
278      }
279      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
280        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
281        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
282      }
283      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
284        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
285      }
286      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
287        op.BoundsParameter.ActualName = BoundsParameter.Name;
288        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
289      }
290      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
291        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
292      }
293      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
294        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
295      }
296      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
297        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
298      }
299      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
300        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
301      }
302      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
303        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
304        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
305      }
306      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
307        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
308      }
309    }
310
311    #region Import & Export
312    public virtual void Load(T data) {
313      Name = data.Name;
314      Description = data.Description;
315      ProblemData = data;
316    }
317
318    public virtual T Export() {
319      return ProblemData;
320    }
321    #endregion
322  }
323}
Note: See TracBrowser for help on using the repository browser.