Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 12094

Last change on this file since 12094 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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