Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 13820

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

#2582 created branch for Hive Web Job Manager

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