Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418: Fixed grammars to allow ADF's.

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