Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeDiversityAnalyzers/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 12064

Last change on this file since 12064 was 12064, checked in by mkommend, 9 years ago

#2326: Fixed namespaces, cloning and serialization in diversity analyzer.

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