Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveResourcePermissionManagement (trunk integration)/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 8055

Last change on this file since 8055 was 7823, checked in by sforsten, 13 years ago

#1784: merge branch ProblemInstancesRegressionAndClassification into trunk

File size: 18.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
56    private const string ProblemDataParameterDescription = "";
57    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
58    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
59    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.";
60    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
61    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
62    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
63    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
64    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
65    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
66    #endregion
67
68    #region parameter properties
69    IParameter IDataAnalysisProblem.ProblemDataParameter {
70      get { return ProblemDataParameter; }
71    }
72    public IValueParameter<T> ProblemDataParameter {
73      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
74    }
75    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
76      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
77    }
78    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
79      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
80    }
81    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
82      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
83    }
84    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
85      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
86    }
87    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
88      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
89    }
90    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
91      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
92    }
93    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
94      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
95    }
96    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
97      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
98    }
99    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
100      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
101    }
102    #endregion
103
104    #region properties
105    public string Filename { get; set; }
106    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
107
108    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
109      get { return ProblemData; }
110    }
111    public T ProblemData {
112      get { return ProblemDataParameter.Value; }
113      set { ProblemDataParameter.Value = value; }
114    }
115
116    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
117      get { return SymbolicExpressionTreeGrammarParameter.Value; }
118      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
119    }
120    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
121      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
122      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
123    }
124
125    public IntValue MaximumSymbolicExpressionTreeDepth {
126      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
127    }
128    public IntValue MaximumSymbolicExpressionTreeLength {
129      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
130    }
131    public IntValue MaximumFunctionDefinitions {
132      get { return MaximumFunctionDefinitionsParameter.Value; }
133    }
134    public IntValue MaximumFunctionArguments {
135      get { return MaximumFunctionArgumentsParameter.Value; }
136    }
137    public PercentValue RelativeNumberOfEvaluatedSamples {
138      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
139    }
140
141    public IntRange FitnessCalculationPartition {
142      get { return FitnessCalculationPartitionParameter.Value; }
143    }
144    public IntRange ValidationPartition {
145      get { return ValidationPartitionParameter.Value; }
146    }
147    #endregion
148
149    [StorableConstructor]
150    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
151    [StorableHook(HookType.AfterDeserialization)]
152    private void AfterDeserialization() {
153      RegisterEventHandlers();
154    }
155    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
156      : base(original, cloner) {
157      RegisterEventHandlers();
158    }
159
160    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
161      : base(evaluator, solutionCreator) {
162      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
163      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
164      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
165      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
166      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
169      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
170      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
171      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
172
173      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
174      MaximumFunctionArgumentsParameter.Hidden = true;
175      MaximumFunctionDefinitionsParameter.Hidden = true;
176
177      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
178      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
179
180      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
181      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
182
183      InitializeOperators();
184
185      UpdateGrammar();
186      RegisterEventHandlers();
187    }
188
189    protected virtual void UpdateGrammar() {
190      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
191      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
192      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
193        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
194      }
195      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
196        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
197      }
198    }
199
200    private void InitializeOperators() {
201      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
202      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
203      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
204      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
205      Operators.Add(new SymbolicDataAnalysisComplexityAnalyzer());
206      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
207      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
208      ParameterizeOperators();
209    }
210
211    #region events
212    private void RegisterEventHandlers() {
213      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
214      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
215
216      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
217
218      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
219      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
220      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
221    }
222
223    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
224      ValidationPartition.Start = 0;
225      ValidationPartition.End = 0;
226      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
227      OnProblemDataChanged();
228    }
229
230    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
231      UpdateGrammar();
232    }
233
234    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
235      UpdateGrammar();
236    }
237
238    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
239      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
240        MaximumSymbolicExpressionTreeDepth.Value = 3;
241    }
242
243    protected override void OnSolutionCreatorChanged() {
244      base.OnSolutionCreatorChanged();
245      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
246      ParameterizeOperators();
247    }
248
249    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
250      ParameterizeOperators();
251    }
252
253    protected override void OnEvaluatorChanged() {
254      base.OnEvaluatorChanged();
255      ParameterizeOperators();
256    }
257
258    public event EventHandler ProblemDataChanged;
259    protected virtual void OnProblemDataChanged() {
260      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
261      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
262
263      UpdateGrammar();
264      ParameterizeOperators();
265
266      var handler = ProblemDataChanged;
267      if (handler != null) handler(this, EventArgs.Empty);
268
269      OnReset();
270    }
271    #endregion
272
273    protected virtual void ParameterizeOperators() {
274      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
275
276      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
277        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
278      }
279      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
280        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
281        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
282      }
283      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
284        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
285        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
286      }
287      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
288        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
289        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
290        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
291        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
292      }
293      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
294        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
295        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
296      }
297      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
298        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
299      }
300      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
301        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
302      }
303      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
304        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
305      }
306      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
307        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
308        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
309      }
310      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
311        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
312      }
313      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
314        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameterName;
315      }
316      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
317        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
318        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
319        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
320        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
321      }
322    }
323
324    #region Import & Export
325    public void Load(T data) {
326      Name = data.Name;
327      Description = data.Description;
328      ProblemData = data;
329      OnReset();
330    }
331
332    public T Export() {
333      return ProblemData;
334    }
335    #endregion
336  }
337}
Note: See TracBrowser for help on using the repository browser.