Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 10285

Last change on this file since 10285 was 10285, checked in by bburlacu, 10 years ago

#1772: Added SymbolicDataAnalysisGenealogyView, updated generic analyzer and operators.

File size: 21.7 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.SymbolicExpressionTreeEncoding;
30using HeuristicLab.EvolutionTracking;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Problems.Instances;
36
37using TGraph = HeuristicLab.EvolutionTracking.IGenealogyGraph<HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>,
38                                                              HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>;
39using TVertex = HeuristicLab.EvolutionTracking.GenealogyGraphNode<HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ISymbolicExpressionTree>;
40
41namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
42  [StorableClass]
43  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
44    IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
45    where T : class, IDataAnalysisProblemData
46    where U : class, ISymbolicDataAnalysisEvaluator<T>
47    where V : class, ISymbolicDataAnalysisSolutionCreator {
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
62    private const string ProblemDataParameterDescription = "";
63    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
64    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
65    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.";
66    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
67    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
68    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
69    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
70    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
71    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
72    private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
73    #endregion
74
75    #region parameter properties
76    IParameter IDataAnalysisProblem.ProblemDataParameter {
77      get { return ProblemDataParameter; }
78    }
79    public IValueParameter<T> ProblemDataParameter {
80      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
81    }
82    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
83      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
84    }
85    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
86      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
87    }
88    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
89      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
90    }
91    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
92      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
93    }
94    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
95      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
96    }
97    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
98      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
99    }
100    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
101      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
102    }
103    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
104      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
105    }
106    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
107      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
108    }
109    public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
110      get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
111    }
112    #endregion
113
114    #region properties
115    public string Filename { get; set; }
116    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
117
118    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
119      get { return ProblemData; }
120    }
121    public T ProblemData {
122      get { return ProblemDataParameter.Value; }
123      set { ProblemDataParameter.Value = value; }
124    }
125
126    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
127      get { return SymbolicExpressionTreeGrammarParameter.Value; }
128      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
129    }
130    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
131      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
132      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
133    }
134
135    public IntValue MaximumSymbolicExpressionTreeDepth {
136      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
137    }
138    public IntValue MaximumSymbolicExpressionTreeLength {
139      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
140    }
141    public IntValue MaximumFunctionDefinitions {
142      get { return MaximumFunctionDefinitionsParameter.Value; }
143    }
144    public IntValue MaximumFunctionArguments {
145      get { return MaximumFunctionArgumentsParameter.Value; }
146    }
147    public PercentValue RelativeNumberOfEvaluatedSamples {
148      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
149    }
150
151    public IntRange FitnessCalculationPartition {
152      get { return FitnessCalculationPartitionParameter.Value; }
153    }
154    public IntRange ValidationPartition {
155      get { return ValidationPartitionParameter.Value; }
156    }
157    public BoolValue ApplyLinearScaling {
158      get { return ApplyLinearScalingParameter.Value; }
159    }
160    #endregion
161
162    [StorableConstructor]
163    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
164    [StorableHook(HookType.AfterDeserialization)]
165    private void AfterDeserialization() {
166      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
167        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
168        ApplyLinearScalingParameter.Hidden = true;
169
170        //it is assumed that for all symbolic regression algorithms linear scaling was set to true
171        //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator
172        if (GetType().Name.Contains("SymbolicRegression"))
173          ApplyLinearScaling.Value = true;
174      }
175
176      RegisterEventHandlers();
177    }
178    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
179      : base(original, cloner) {
180      RegisterEventHandlers();
181    }
182
183    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
184      : base(evaluator, solutionCreator) {
185      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
186      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(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
197      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
198      MaximumFunctionArgumentsParameter.Hidden = true;
199      MaximumFunctionDefinitionsParameter.Hidden = true;
200      ApplyLinearScalingParameter.Hidden = true;
201
202      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
203      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
204
205      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
206      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
207
208      InitializeOperators();
209
210      UpdateGrammar();
211      RegisterEventHandlers();
212    }
213
214    protected virtual void UpdateGrammar() {
215      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
216      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
217      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
218        if (!varSymbol.Fixed) {
219          varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);
220          varSymbol.VariableNames = ProblemData.AllowedInputVariables;
221        }
222      }
223      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
224        if (!varSymbol.Fixed) {
225          varSymbol.AllVariableNames = ProblemData.InputVariables.Select(x => x.Value);
226          varSymbol.VariableNames = ProblemData.AllowedInputVariables;
227        }
228      }
229    }
230
231    private void InitializeOperators() {
232      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
233      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
234      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
235      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
236      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
237      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
238      Operators.Add(new GenealogyAnalyzer<TGraph, TVertex, ISymbolicExpressionTree>());
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      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
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 ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
266      UpdateGrammar();
267    }
268
269    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
270      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
271        MaximumSymbolicExpressionTreeDepth.Value = 3;
272    }
273
274    protected override void OnSolutionCreatorChanged() {
275      base.OnSolutionCreatorChanged();
276      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
277      ParameterizeOperators();
278    }
279
280    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
281      ParameterizeOperators();
282    }
283
284    protected override void OnEvaluatorChanged() {
285      base.OnEvaluatorChanged();
286      ParameterizeOperators();
287    }
288
289    public event EventHandler ProblemDataChanged;
290    protected virtual void OnProblemDataChanged() {
291      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
292      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
293
294      UpdateGrammar();
295      ParameterizeOperators();
296
297      var handler = ProblemDataChanged;
298      if (handler != null) handler(this, EventArgs.Empty);
299
300      OnReset();
301    }
302    #endregion
303
304    protected virtual void ParameterizeOperators() {
305      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
306
307      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
308        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
309      }
310      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
311        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
312        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
313      }
314      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
315        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
316        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
317      }
318      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
319        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
320        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
321        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
322        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
323        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
324      }
325      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
326        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
327        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
328      }
329      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
330        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
331      }
332      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
333        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
334      }
335      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
336        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
337      }
338      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
339        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
340      }
341      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
342        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
343      }
344      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
345        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
346        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
347      }
348      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
349        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
350      }
351      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
352        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
353        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
354        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
355        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
356        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
357      }
358      // add tracking analyzer
359      foreach (var op in operators.OfType<GenealogyAnalyzer<TGraph, TVertex, ISymbolicExpressionTree>>()) {
360        // get crossover parameter names
361        var crossover = operators.OfType<ISymbolicExpressionTreeCrossover>().First();
362        op.CrossoverParentsParameterName = crossover.ParentsParameter.Name;
363        op.CrossoverChildParameterName = crossover.ChildParameter.Name;
364        // get munipulator parameter names
365        var manipulator = operators.OfType<ISymbolicExpressionTreeManipulator>().First();
366        op.ManipulatorChildParameterName = manipulator.SymbolicExpressionTreeParameter.Name;
367      }
368    }
369
370    #region Import & Export
371    public virtual void Load(T data) {
372      Name = data.Name;
373      Description = data.Description;
374      ProblemData = data;
375    }
376
377    public virtual T Export() {
378      return ProblemData;
379    }
380    #endregion
381  }
382}
Note: See TracBrowser for help on using the repository browser.