Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.GrammaticalEvolution/3.4/SymbolicRegression/GESymbolicDataAnalysisProblem.cs @ 15762

Last change on this file since 15762 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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