source: branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 6968

Last change on this file since 6968 was 6968, checked in by sforsten, 11 years ago

#1669: First version which can automatically generate data for some problems from http://www.vanillamodeling.com/

File size: 17.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Common.Resources;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35
36namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
37  [StorableClass]
38  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent
39    where T : class,IDataAnalysisProblemData
40    where U : class, ISymbolicDataAnalysisEvaluator<T>
41    where V : class, ISymbolicDataAnalysisSolutionCreator {
42
43    #region parameter names & descriptions
44    private const string ProblemDataParameterName = "ProblemData";
45    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
46    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
47    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
48    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
49    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
50    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
51    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
52    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
53    private const string ValidationPartitionParameterName = "ValidationPartition";
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    #endregion
66
67    #region parameter properties
68    IParameter IDataAnalysisProblem.ProblemDataParameter {
69      get { return ProblemDataParameter; }
70    }
71    public IValueParameter<T> ProblemDataParameter {
72      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
73    }
74    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
75      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
76    }
77    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
78      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
79    }
80    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
81      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
82    }
83    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
84      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
85    }
86    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
87      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
88    }
89    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
90      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
91    }
92    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
93      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
94    }
95    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
96      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
97    }
98    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
99      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
100    }
101    #endregion
102
103    #region properties
104    public string Filename { get; set; }
105    public override Image ItemImage { get { return VSImageLibrary.Type; } }
106
107    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
108      get { return ProblemData; }
109    }
110    public T ProblemData {
111      get { return ProblemDataParameter.Value; }
112      set { ProblemDataParameter.Value = value; }
113    }
114
115    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
116      get { return SymbolicExpressionTreeGrammarParameter.Value; }
117      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
118    }
119    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
120      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
121      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
122    }
123
124    public IntValue MaximumSymbolicExpressionTreeDepth {
125      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
126    }
127    public IntValue MaximumSymbolicExpressionTreeLength {
128      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
129    }
130    public IntValue MaximumFunctionDefinitions {
131      get { return MaximumFunctionDefinitionsParameter.Value; }
132    }
133    public IntValue MaximumFunctionArguments {
134      get { return MaximumFunctionArgumentsParameter.Value; }
135    }
136    public PercentValue RelativeNumberOfEvaluatedSamples {
137      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
138    }
139
140    public IntRange FitnessCalculationPartition {
141      get { return FitnessCalculationPartitionParameter.Value; }
142    }
143    public IntRange ValidationPartition {
144      get { return ValidationPartitionParameter.Value; }
145    }
146    #endregion
147
148    [StorableConstructor]
149    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
150    [StorableHook(HookType.AfterDeserialization)]
151    private void AfterDeserialization() {
152      RegisterEventHandlers();
153    }
154    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
155      : base(original, cloner) {
156      RegisterEventHandlers();
157    }
158
159    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
160      : base(evaluator, solutionCreator) {
161      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
162      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
163      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
164      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
165      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
166      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
169      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
170      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
171
172      SolutionCreatorParameter.Hidden = true;
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.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
203      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
204      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
205      ParameterizeOperators();
206    }
207
208    #region events
209    private void RegisterEventHandlers() {
210      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
211      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
212
213      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
214
215      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
216      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
217      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
218    }
219
220    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
221      ValidationPartition.Start = 0;
222      ValidationPartition.End = 0;
223      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
224      OnProblemDataChanged();
225    }
226
227    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
228      UpdateGrammar();
229    }
230
231    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
232      UpdateGrammar();
233    }
234
235    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
236      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
237        MaximumSymbolicExpressionTreeDepth.Value = 3;
238    }
239
240    protected override void OnSolutionCreatorChanged() {
241      base.OnSolutionCreatorChanged();
242      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
243      ParameterizeOperators();
244    }
245
246    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
247      ParameterizeOperators();
248    }
249
250    protected override void OnEvaluatorChanged() {
251      base.OnEvaluatorChanged();
252      ParameterizeOperators();
253    }
254
255    public event EventHandler ProblemDataChanged;
256    protected virtual void OnProblemDataChanged() {
257      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
258      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
259
260      UpdateGrammar();
261      ParameterizeOperators();
262
263      var handler = ProblemDataChanged;
264      if (handler != null) handler(this, EventArgs.Empty);
265
266      OnReset();
267    }
268    #endregion
269
270    protected virtual void ParameterizeOperators() {
271      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
272
273      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
274        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
275      }
276      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
277        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
278        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
279      }
280      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
281        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
282        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
283      }
284      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
285        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
286        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
287        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
288        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
289      }
290      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
291        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
292        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
293      }
294      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
295        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
296      }
297      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
298        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
299      }
300      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
301        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
302      }
303      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
304        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
305        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
306      }
307      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
308        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
309      }
310    }
311
312    public abstract void ImportProblemDataFromFile(string fileName);
313
314    public abstract void CreateProblemDataFromBenchmark(IDataAnalysisBenchmarkProblemDataGenerator benchmarkGenerator);
315
316    public abstract IEnumerable<IDataAnalysisBenchmarkProblemDataGenerator> GetBenchmarkProblemDataGenerators();
317  }
318}
Note: See TracBrowser for help on using the repository browser.