Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 9678

Last change on this file since 9678 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

File size: 18.5 KB
RevLine 
[5577]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5577]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;
[5618]24using System.Linq;
[5577]25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
[5618]29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[5577]30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5618]33using HeuristicLab.PluginInfrastructure;
[8085]34using HeuristicLab.Problems.Instances;
[5577]35
36namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
37  [StorableClass]
[8085]38  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
39    IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
[6978]40    where T : class, IDataAnalysisProblemData
[5580]41    where U : class, ISymbolicDataAnalysisEvaluator<T>
42    where V : class, ISymbolicDataAnalysisSolutionCreator {
[5770]43
[5577]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";
[5759]52    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
[5733]53    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
[5775]54    private const string ValidationPartitionParameterName = "ValidationPartition";
[5577]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.";
[5759]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.";
[5857]65    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
[5577]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    }
[5624]78    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
79      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
[5577]80    }
[5618]81    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
82      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
[5577]83    }
[5618]84    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
85      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
[5577]86    }
[5618]87    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
88      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
[5577]89    }
[5618]90    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
91      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
[5577]92    }
[5759]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    }
[5883]99    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
[5775]100      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
[5759]101    }
[5577]102    #endregion
103
104    #region properties
105    public string Filename { get; set; }
[7201]106    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
[5577]107
108    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
109      get { return ProblemData; }
110    }
111    public T ProblemData {
112      get { return ProblemDataParameter.Value; }
[5618]113      set { ProblemDataParameter.Value = value; }
[5577]114    }
115
116    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
117      get { return SymbolicExpressionTreeGrammarParameter.Value; }
[5618]118      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
[5577]119    }
[5624]120    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
[5577]121      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
[5618]122      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
[5577]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 {
[5618]135      get { return MaximumFunctionArgumentsParameter.Value; }
[5577]136    }
[5759]137    public PercentValue RelativeNumberOfEvaluatedSamples {
138      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
139    }
140
141    public IntRange FitnessCalculationPartition {
142      get { return FitnessCalculationPartitionParameter.Value; }
143    }
[5775]144    public IntRange ValidationPartition {
[5883]145      get { return ValidationPartitionParameter.Value; }
[5759]146    }
[5577]147    #endregion
148
149    [StorableConstructor]
150    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
[5618]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    }
[5577]159
[5618]160    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
161      : base(evaluator, solutionCreator) {
162      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
[5577]163      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
[5624]164      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
[5847]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));
[5759]171      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
[5618]172
[5854]173      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
174      MaximumFunctionArgumentsParameter.Hidden = true;
175      MaximumFunctionDefinitionsParameter.Hidden = true;
176
[5618]177      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
178      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
179
[5770]180      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
181      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
182
[5722]183      InitializeOperators();
184
[5618]185      UpdateGrammar();
186      RegisterEventHandlers();
[5577]187    }
188
[5685]189    protected virtual void UpdateGrammar() {
[5726]190      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
191      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
[5685]192      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
[6803]193        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
[5685]194      }
195      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
[6803]196        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
[5685]197      }
198    }
199
[5618]200    private void InitializeOperators() {
201      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
[7506]202      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
[5618]203      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
[5685]204      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
[5618]205      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
[6978]206      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
[5618]207      ParameterizeOperators();
208    }
209
[5685]210    #region events
[5618]211    private void RegisterEventHandlers() {
212      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
213      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
214
[5841]215      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
216
[5618]217      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
218      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
219      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
220    }
221
[5685]222    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
[5887]223      ValidationPartition.Start = 0;
224      ValidationPartition.End = 0;
[5685]225      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
226      OnProblemDataChanged();
227    }
228
[5841]229    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
230      UpdateGrammar();
231    }
232
[5618]233    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
234      UpdateGrammar();
235    }
236
237    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
238      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
239        MaximumSymbolicExpressionTreeDepth.Value = 3;
240    }
241
242    protected override void OnSolutionCreatorChanged() {
243      base.OnSolutionCreatorChanged();
244      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
245      ParameterizeOperators();
246    }
[5685]247
[5618]248    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
249      ParameterizeOperators();
250    }
251
252    protected override void OnEvaluatorChanged() {
253      base.OnEvaluatorChanged();
[5685]254      ParameterizeOperators();
[5618]255    }
256
[5577]257    public event EventHandler ProblemDataChanged;
258    protected virtual void OnProblemDataChanged() {
[5770]259      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
260      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
261
[5618]262      UpdateGrammar();
[5685]263      ParameterizeOperators();
264
[5577]265      var handler = ProblemDataChanged;
266      if (handler != null) handler(this, EventArgs.Empty);
[5618]267
268      OnReset();
[5577]269    }
[5685]270    #endregion
[5618]271
[5685]272    protected virtual void ParameterizeOperators() {
[7506]273      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
[5618]274
275      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
[5685]276        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
[5618]277      }
278      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
[5685]279        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
280        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
[5618]281      }
282      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
[5685]283        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
284        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
[5618]285      }
286      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
[5685]287        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
[5618]288        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5759]289        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
290        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[5618]291      }
292      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
293        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
294        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
295      }
296      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
297        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
298      }
299      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
300        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
301      }
[6135]302      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
303        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
304      }
[5759]305      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
306        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[5883]307        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
[5759]308      }
[5685]309      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
310        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
311      }
[7506]312      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
313        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameterName;
314        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
315        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
316        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
317        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
318      }
[5618]319    }
[5623]320
[8085]321    #region Import & Export
322    public void Load(T data) {
323      Name = data.Name;
324      Description = data.Description;
325      ProblemData = data;
326      OnReset();
327    }
328
329    public T Export() {
330      return ProblemData;
331    }
332    #endregion
[5577]333  }
334}
Note: See TracBrowser for help on using the repository browser.