Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 14232

Last change on this file since 14232 was 14232, checked in by gkronber, 8 years ago

created a feature branch for #2650 (support for categorical variables in symb reg) with a first set of changes

work in progress...

File size: 21.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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
121    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
122      get { return SymbolicExpressionTreeGrammarParameter.Value; }
123      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
124    }
125    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
126      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
127      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
128    }
129
130    public IntValue MaximumSymbolicExpressionTreeDepth {
131      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
132    }
133    public IntValue MaximumSymbolicExpressionTreeLength {
134      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
135    }
136    public IntValue MaximumFunctionDefinitions {
137      get { return MaximumFunctionDefinitionsParameter.Value; }
138    }
139    public IntValue MaximumFunctionArguments {
140      get { return MaximumFunctionArgumentsParameter.Value; }
141    }
142    public PercentValue RelativeNumberOfEvaluatedSamples {
143      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
144    }
145
146    public IntRange FitnessCalculationPartition {
147      get { return FitnessCalculationPartitionParameter.Value; }
148    }
149    public IntRange ValidationPartition {
150      get { return ValidationPartitionParameter.Value; }
151    }
152    public BoolValue ApplyLinearScaling {
153      get { return ApplyLinearScalingParameter.Value; }
154    }
155    #endregion
156
157    [StorableConstructor]
158    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
159    [StorableHook(HookType.AfterDeserialization)]
160    private void AfterDeserialization() {
161      if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
162        Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
163        ApplyLinearScalingParameter.Hidden = true;
164
165        //it is assumed that for all symbolic regression algorithms linear scaling was set to true
166        //there is no possibility to determine the previous value of the parameter as it was stored in the evaluator
167        if (GetType().Name.Contains("SymbolicRegression"))
168          ApplyLinearScaling.Value = true;
169      }
170
171      RegisterEventHandlers();
172    }
173    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
174      : base(original, cloner) {
175      RegisterEventHandlers();
176    }
177
178    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
179      : base(evaluator, solutionCreator) {
180      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
181      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
182      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
183      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
184      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
185      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
186      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
187      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
188      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
189      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
190      Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
191
192      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
193      MaximumFunctionArgumentsParameter.Hidden = true;
194      MaximumFunctionDefinitionsParameter.Hidden = true;
195      ApplyLinearScalingParameter.Hidden = true;
196
197      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
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    protected virtual void UpdateGrammar() {
210      var problemData = ProblemData;
211      var ds = problemData.Dataset;
212      var grammar = SymbolicExpressionTreeGrammar;
213      grammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
214      grammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
215      foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
216        if (!varSymbol.Fixed) {
217          varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x));
218          varSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<double>(x));
219        }
220      }
221      foreach (var factorSymbol in grammar.Symbols.OfType<FactorVariable>()) {
222        if (!factorSymbol.Fixed) {
223          factorSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<string>(x));
224          factorSymbol.VariableNames = problemData.AllowedInputVariables.Where(x => ds.VariableHasType<string>(x));
225          factorSymbol.VariableValues = factorSymbol.VariableNames
226            .ToDictionary(varName => varName, varName => ds.GetStringValues(varName).Distinct().ToList());
227        }
228      }
229      foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
230        if (!varSymbol.Fixed) {
231          varSymbol.AllVariableNames = problemData.InputVariables.Select(x => x.Value).Where(x => ds.VariableHasType<double>(x));
232          varSymbol.VariableNames = problemData.AllowedInputVariables;
233        }
234      }
235    }
236
237    private void InitializeOperators() {
238      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
239      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
240      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
241      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
242      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
243      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
244      Operators.Add(new SymbolicExpressionTreeBottomUpSimilarityCalculator());
245      Operators.Add(new SymbolicDataAnalysisBottomUpDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreeBottomUpSimilarityCalculator>().First()));
246      ParameterizeOperators();
247    }
248
249    #region events
250    private void RegisterEventHandlers() {
251      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
252      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
253
254      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
255
256      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
257      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
258      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
259    }
260
261    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
262      ValidationPartition.Start = 0;
263      ValidationPartition.End = 0;
264      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
265      OnProblemDataChanged();
266    }
267
268    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
269      UpdateGrammar();
270    }
271
272    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
273      UpdateGrammar();
274    }
275
276    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
277      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
278        MaximumSymbolicExpressionTreeDepth.Value = 3;
279    }
280
281    protected override void OnSolutionCreatorChanged() {
282      base.OnSolutionCreatorChanged();
283      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
284      ParameterizeOperators();
285    }
286
287    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
288      ParameterizeOperators();
289    }
290
291    protected override void OnEvaluatorChanged() {
292      base.OnEvaluatorChanged();
293      ParameterizeOperators();
294    }
295
296    public event EventHandler ProblemDataChanged;
297    protected virtual void OnProblemDataChanged() {
298      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
299      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
300
301      UpdateGrammar();
302      ParameterizeOperators();
303
304      var handler = ProblemDataChanged;
305      if (handler != null) handler(this, EventArgs.Empty);
306
307      OnReset();
308    }
309    #endregion
310
311    protected virtual void ParameterizeOperators() {
312      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
313
314      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
315        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
316      }
317      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
318        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
319        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
320      }
321      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
322        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
323        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
324      }
325      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
326        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
327        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
328        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
329        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
330        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
331      }
332      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
333        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
334        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
335      }
336      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
337        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
338      }
339      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
340        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
341      }
342      foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
343        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
344      }
345      foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
346        op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
347      }
348      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
349        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
350      }
351      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
352        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
353        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
354      }
355      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
356        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
357      }
358      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
359        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
360        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
361        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
362        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
363        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
364      }
365    }
366
367    #region Import & Export
368    public virtual void Load(T data) {
369      Name = data.Name;
370      Description = data.Description;
371      ProblemData = data;
372    }
373
374    public virtual T Export() {
375      return ProblemData;
376    }
377    #endregion
378  }
379}
Note: See TracBrowser for help on using the repository browser.