Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5624 was 5624, checked in by gkronber, 14 years ago

#1418 renamed interface for interpreter, worked on solutions and models and implemented SVM regression.

File size: 15.2 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//TODO configure grammar with ADF usage
38
39namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
40  [StorableClass]
41  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, ISymbolicDataAnalysisProblem, IStorableContent
42    where T : class,IDataAnalysisProblemData
43    where U : class, ISymbolicDataAnalysisEvaluator<T>
44    where V : class, ISymbolicDataAnalysisSolutionCreator {
45    #region parameter names & descriptions
46    private const string ProblemDataParameterName = "ProblemData";
47    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
48    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
49    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
50    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
51    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
52    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
53    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
54    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
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 LowerEstimationLimitParameterDescription = "The lower limit for the estimated value that can be returned by the symbolic data analysis model.";
60    private const string UpperEstimationLimitParameterDescription = "The upper limit for the estimated value that can be returned by the symbolic data analysis model.";
61    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.";
62    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
63    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
64    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
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<DoubleValue> LowerEstimationLimitParameter {
81      get { return (IFixedValueParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
82    }
83    public IFixedValueParameter<DoubleValue> UpperEstimationLimitParameter {
84      get { return (IFixedValueParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
85    }
86    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
87      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
88    }
89    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
90      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
91    }
92    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
93      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
94    }
95    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
96      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
97    }
98    #endregion
99
100    #region properties
101    public string Filename { get; set; }
102    public override Image ItemImage { get { return VSImageLibrary.Type; } }
103
104    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
105      get { return ProblemData; }
106    }
107    public T ProblemData {
108      get { return ProblemDataParameter.Value; }
109      set { ProblemDataParameter.Value = value; }
110    }
111
112    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
113      get { return SymbolicExpressionTreeGrammarParameter.Value; }
114      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
115    }
116    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
117      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
118      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
119    }
120
121    public DoubleValue LowerEstimationLimit {
122      get { return LowerEstimationLimitParameter.Value; }
123    }
124    public DoubleValue UpperEstimationLimit {
125      get { return UpperEstimationLimitParameter.Value; }
126    }
127
128    public IntValue MaximumSymbolicExpressionTreeDepth {
129      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
130    }
131    public IntValue MaximumSymbolicExpressionTreeLength {
132      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
133    }
134    public IntValue MaximumFunctionDefinitions {
135      get { return MaximumFunctionDefinitionsParameter.Value; }
136    }
137    public IntValue MaximumFunctionArguments {
138      get { return MaximumFunctionArgumentsParameter.Value; }
139    }
140    #endregion
141
142    [StorableConstructor]
143    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
144    [StorableHook(HookType.AfterDeserialization)]
145    private void AfterDeserialization() {
146      RegisterEventHandlers();
147    }
148    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
149      : base(original, cloner) {
150      RegisterEventHandlers();
151    }
152
153    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
154      : base(evaluator, solutionCreator) {
155      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
156      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
157      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
158      Parameters.Add(new FixedValueParameter<DoubleValue>(LowerEstimationLimitParameterName, LowerEstimationLimitParameterDescription, new DoubleValue()));
159      Parameters.Add(new FixedValueParameter<DoubleValue>(UpperEstimationLimitParameterName, UpperEstimationLimitParameterDescription, new DoubleValue()));
160      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription, new IntValue()));
161      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription, new IntValue()));
162      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription, new IntValue()));
163      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription, new IntValue()));
164
165      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
166      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
167
168      UpdateGrammar();
169      UpdateEstimationLimits();
170      RegisterEventHandlers();
171      InitializeOperators();
172    }
173
174    protected abstract void UpdateEstimationLimits();
175
176    private void InitializeOperators() {
177      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
178      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
179      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
180      ParameterizeOperators();
181    }
182
183    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
184      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
185
186      OnProblemDataChanged();
187    }
188    private void RegisterEventHandlers() {
189      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
190      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
191
192      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
193      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
194      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
195    }
196
197    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
198      UpdateGrammar();
199    }
200    protected virtual void UpdateGrammar() {
201      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
202        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
203      }
204      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
205        varSymbol.VariableNames = ProblemData.AllowedInputVariables;
206      }
207    }
208
209    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
210      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
211        MaximumSymbolicExpressionTreeDepth.Value = 3;
212    }
213
214    protected override void OnSolutionCreatorChanged() {
215      base.OnSolutionCreatorChanged();
216      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
217      ParameterizeOperators();
218    }
219    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
220      ParameterizeOperators();
221    }
222
223    protected override void OnEvaluatorChanged() {
224      base.OnEvaluatorChanged();
225    }
226
227    public event EventHandler ProblemDataChanged;
228    protected virtual void OnProblemDataChanged() {
229      UpdateGrammar();
230      UpdateEstimationLimits();
231      var handler = ProblemDataChanged;
232      if (handler != null) handler(this, EventArgs.Empty);
233
234      OnReset();
235    }
236
237    private void ParameterizeOperators() {
238      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
239
240      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
241        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
242      }
243      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
244        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameter.Name;
245        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
246      }
247      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
248        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameter.Name;
249        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameter.Name;
250      }
251      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
252        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
253        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
254        op.SamplesStartParameter.Value.Value = ProblemData.TrainingPartitionStart.Value;
255        op.SamplesEndParameter.Value.Value = ProblemData.TrainingPartitionEnd.Value;
256      }
257      foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedEvaluator<T>>()) {
258        op.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
259        op.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
260      }
261      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
262        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
263        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
264      }
265      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
266        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
267      }
268      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
269        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
270      }
271    }
272
273    public abstract void ImportProblemDataFromFile(string fileName);
274  }
275}
Note: See TracBrowser for help on using the repository browser.