Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 5759

Last change on this file since 5759 was 5759, checked in by mkommend, 13 years ago

#1418:

  • Worked on IntRange and DoubleRange
  • Updated evaluators, analyzers, problems and problem data to use IntRanges
  • Removed properties to access the value of LookupParameter
  • Corrected files.txt
File size: 16.6 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.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;
34
35//TODO configure training start / end
36//TODO configure analyzer validation
37
38namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
39  [StorableClass]
40  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem, IStorableContent
41    where T : class,IDataAnalysisProblemData
42    where U : class, ISymbolicDataAnalysisEvaluator<T>
43    where V : class, ISymbolicDataAnalysisSolutionCreator {
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 ModelSelectionPartitionParameterName = "ModelSelectionPartition";
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 ModelSelectionPartitionParameterDescription = "The partition of the problem data training partition, that should be used the 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> ModelSelectionPartitionParamater {
100      get { return (IFixedValueParameter<IntRange>)Parameters[ModelSelectionPartitionParameterName]; }
101    }
102    #endregion
103
104    #region properties
105    public string Filename { get; set; }
106    public override Image ItemImage { 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 ModelSelectionPartition {
145      get { return ModelSelectionPartitionParamater.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, new IntValue()));
166      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription, new IntValue()));
167      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription, new IntValue()));
168      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription, new IntValue()));
169      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription, new IntRange()));
170      Parameters.Add(new FixedValueParameter<IntRange>(ModelSelectionPartitionParameterName, ModelSelectionPartitionParameterDescription, new IntRange()));
171      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
172
173      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
174      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
175
176      InitializeOperators();
177
178      UpdateGrammar();
179      RegisterEventHandlers();
180    }
181
182    protected virtual void UpdateGrammar() {
183      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
184      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
185      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
186        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
187      }
188      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
189        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
190      }
191    }
192
193    private void InitializeOperators() {
194      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
195      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
196      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
197      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
198      ParameterizeOperators();
199    }
200
201    #region events
202    private void RegisterEventHandlers() {
203      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
204      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
205
206      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
207      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
208      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
209    }
210
211    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
212      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
213      OnProblemDataChanged();
214    }
215
216    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
217      UpdateGrammar();
218    }
219
220    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
221      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
222        MaximumSymbolicExpressionTreeDepth.Value = 3;
223    }
224
225    protected override void OnSolutionCreatorChanged() {
226      base.OnSolutionCreatorChanged();
227      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
228      ParameterizeOperators();
229    }
230
231    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
232      ParameterizeOperators();
233    }
234
235    protected override void OnEvaluatorChanged() {
236      base.OnEvaluatorChanged();
237      ParameterizeOperators();
238    }
239
240    public event EventHandler ProblemDataChanged;
241    protected virtual void OnProblemDataChanged() {
242      UpdateGrammar();
243      ParameterizeOperators();
244
245      var handler = ProblemDataChanged;
246      if (handler != null) handler(this, EventArgs.Empty);
247
248      OnReset();
249    }
250    #endregion
251
252    protected virtual void ParameterizeOperators() {
253      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
254
255      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
256        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
257      }
258      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
259        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
260        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
261      }
262      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
263        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
264        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
265      }
266      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
267        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
268        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
269        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
270        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
271      }
272      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
273        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
274        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
275      }
276      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
277        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
278      }
279      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
280        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
281      }
282      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
283        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
284        op.ValidationPartitionParameter.ActualName = ModelSelectionPartitionParamater.Name;
285      }
286      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
287        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
288      }
289    }
290
291    public abstract void ImportProblemDataFromFile(string fileName);
292  }
293}
Note: See TracBrowser for help on using the repository browser.