Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.GrammaticalEvolution/3.3/Symbolic/GESymbolicDataAnalysisProblem.cs @ 17578

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

#2582 created branch for Hive Web Job Manager

File size: 17.0 KB
RevLine 
[10072]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[10072]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/>.
[10968]19 *
20 * Author: Sabine Winkler
[10072]21 */
[10968]22
[10072]23#endregion
24
25using System;
26using System.Linq;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
[10073]30using HeuristicLab.Encodings.IntegerVectorEncoding;
[10072]31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
[10073]36using HeuristicLab.Problems.DataAnalysis;
37using HeuristicLab.Problems.DataAnalysis.Symbolic;
38using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
[10072]39using HeuristicLab.Problems.Instances;
40
[10073]41namespace HeuristicLab.Problems.GrammaticalEvolution {
[10072]42  [StorableClass]
[10075]43  public abstract class GESymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>,
[10226]44                                                                 IGESymbolicDataAnalysisProblem, IStorableContent,
[10075]45                                                                 IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
[10072]46    where T : class, IDataAnalysisProblemData
[10073]47    where U : class, IGESymbolicDataAnalysisEvaluator<T>
48    where V : class, IIntegerVectorCreator {
[10072]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";
[10073]59    private const string BoundsParameterName = "Bounds";
60    private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
[10072]61    private const string ProblemDataParameterDescription = "";
62    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
[10974]63    private const string SymbolicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
[10072]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.";
[10073]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).";
[10072]71    #endregion
72
73    #region parameter properties
[13656]74    IParameter IDataAnalysisProblem.ProblemDataParameter
75    {
[10072]76      get { return ProblemDataParameter; }
77    }
[13656]78    public IValueParameter<T> ProblemDataParameter
79    {
[10072]80      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
81    }
[13656]82    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter
83    {
[10974]84      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
[10072]85    }
[13656]86    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter
87    {
[10072]88      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
89    }
[13656]90    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter
91    {
[10072]92      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
93    }
[13656]94    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter
95    {
[10072]96      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
97    }
[13656]98    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter
99    {
[10072]100      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
101    }
[13656]102    public IFixedValueParameter<IntRange> ValidationPartitionParameter
103    {
[10072]104      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
105    }
[13656]106    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter
107    {
[10072]108      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
109    }
[13656]110    public IValueParameter<IntMatrix> BoundsParameter
111    {
[10073]112      get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
113    }
[13656]114    public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter
115    {
[10073]116      get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
117    }
[10072]118    #endregion
119
120    #region properties
121    public string Filename { get; set; }
122
[13656]123
124    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData
125    {
[10072]126      get { return ProblemData; }
127    }
[13656]128    public T ProblemData
129    {
[10072]130      get { return ProblemDataParameter.Value; }
131      set { ProblemDataParameter.Value = value; }
132    }
133
[13656]134    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar
135    {
[10072]136      get { return SymbolicExpressionTreeGrammarParameter.Value; }
137      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
138    }
[13656]139    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter
140    {
[10072]141      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
142      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
143    }
144
[13656]145    public IntValue MaximumSymbolicExpressionTreeLength
146    {
[10072]147      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
148    }
[10276]149
[13656]150    public PercentValue RelativeNumberOfEvaluatedSamples
151    {
[10072]152      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
153    }
154
[13656]155    public IntRange FitnessCalculationPartition
156    {
[10072]157      get { return FitnessCalculationPartitionParameter.Value; }
158    }
[13656]159    public IntRange ValidationPartition
160    {
[10072]161      get { return ValidationPartitionParameter.Value; }
162    }
[13656]163    public BoolValue ApplyLinearScaling
164    {
[10072]165      get { return ApplyLinearScalingParameter.Value; }
166    }
167    #endregion
168
169    [StorableConstructor]
[10073]170    protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
[10072]171    [StorableHook(HookType.AfterDeserialization)]
172    private void AfterDeserialization() {
173      RegisterEventHandlers();
174    }
[10073]175    protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
[10072]176      : base(original, cloner) {
177      RegisterEventHandlers();
178    }
179
[10073]180    protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
[10072]181      : base(evaluator, solutionCreator) {
182      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
[10974]183      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
184      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymbolicExpressionTreeInterpreterParameterDescription));
[10072]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)));
[10073]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()));
[10072]193
194      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
195      ApplyLinearScalingParameter.Hidden = true;
196
[10268]197      SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
[10072]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
[10268]209    private void DeregisterGrammarHandler() {
210      SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
211    }
212    private void RegisterGrammarHandler() {
213      SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
214    }
215
[10974]216    private void UpdateGrammar() {
[10268]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();
[10072]222    }
223
224    private void InitializeOperators() {
[10974]225      Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
[10072]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
[10268]238      RegisterGrammarHandler();
[10072]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
[10073]252    protected override void OnEvaluatorChanged() {
253      base.OnEvaluatorChanged();
254      Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
[10072]255      ParameterizeOperators();
256    }
257
[10073]258    private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
[10072]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      }
[10073]283      foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
[10072]284        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
[10073]285        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]286        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
287        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
288        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
[10075]289        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
290        op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
291        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
[10072]292      }
[10073]293      foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
[10075]294        op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
295        op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
[10072]296      }
[10073]297      foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
298        op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
[10072]299      }
[10073]300      foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
301        op.BoundsParameter.ActualName = BoundsParameter.Name;
302        op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
303      }
[10072]304      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
[10073]305        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]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>()) {
[10073]314        op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
[10072]315      }
[10073]316      foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
[10072]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.