Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs @ 7810

Last change on this file since 7810 was 7810, checked in by sforsten, 12 years ago

#1784:

  • removed obsolete import & export methods from RegressionProblem and ClassificationProblem, because they are implemented in the base classes
  • removed unnecessary references in Problems.QuadraticAssignment.Views
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;
[5577]34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [StorableClass]
[5883]37  public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent
[6978]38    where T : class, IDataAnalysisProblemData
[5580]39    where U : class, ISymbolicDataAnalysisEvaluator<T>
40    where V : class, ISymbolicDataAnalysisSolutionCreator {
[5770]41
[5577]42    #region parameter names & descriptions
43    private const string ProblemDataParameterName = "ProblemData";
44    private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
45    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
46    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
47    private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
48    private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
49    private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
[5759]50    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
[5733]51    private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
[5775]52    private const string ValidationPartitionParameterName = "ValidationPartition";
[5577]53
54    private const string ProblemDataParameterDescription = "";
55    private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
56    private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
57    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.";
58    private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
59    private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
60    private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
[5759]61    private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
62    private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
[5857]63    private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
[5577]64    #endregion
65
66    #region parameter properties
67    IParameter IDataAnalysisProblem.ProblemDataParameter {
68      get { return ProblemDataParameter; }
69    }
70    public IValueParameter<T> ProblemDataParameter {
71      get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
72    }
73    public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
74      get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
75    }
[5624]76    public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
77      get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
[5577]78    }
[5618]79    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
80      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
[5577]81    }
[5618]82    public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
83      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
[5577]84    }
[5618]85    public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
86      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
[5577]87    }
[5618]88    public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
89      get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
[5577]90    }
[5759]91    public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
92      get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
93    }
94    public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
95      get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
96    }
[5883]97    public IFixedValueParameter<IntRange> ValidationPartitionParameter {
[5775]98      get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
[5759]99    }
[5577]100    #endregion
101
102    #region properties
103    public string Filename { get; set; }
[7201]104    public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
[5577]105
106    IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
107      get { return ProblemData; }
108    }
109    public T ProblemData {
110      get { return ProblemDataParameter.Value; }
[5618]111      set { ProblemDataParameter.Value = value; }
[5577]112    }
113
114    public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
115      get { return SymbolicExpressionTreeGrammarParameter.Value; }
[5618]116      set { SymbolicExpressionTreeGrammarParameter.Value = value; }
[5577]117    }
[5624]118    public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
[5577]119      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
[5618]120      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
[5577]121    }
122
123    public IntValue MaximumSymbolicExpressionTreeDepth {
124      get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
125    }
126    public IntValue MaximumSymbolicExpressionTreeLength {
127      get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
128    }
129    public IntValue MaximumFunctionDefinitions {
130      get { return MaximumFunctionDefinitionsParameter.Value; }
131    }
132    public IntValue MaximumFunctionArguments {
[5618]133      get { return MaximumFunctionArgumentsParameter.Value; }
[5577]134    }
[5759]135    public PercentValue RelativeNumberOfEvaluatedSamples {
136      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
137    }
138
139    public IntRange FitnessCalculationPartition {
140      get { return FitnessCalculationPartitionParameter.Value; }
141    }
[5775]142    public IntRange ValidationPartition {
[5883]143      get { return ValidationPartitionParameter.Value; }
[5759]144    }
[5577]145    #endregion
146
147    [StorableConstructor]
148    protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
[5618]149    [StorableHook(HookType.AfterDeserialization)]
150    private void AfterDeserialization() {
151      RegisterEventHandlers();
152    }
153    protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
154      : base(original, cloner) {
155      RegisterEventHandlers();
156    }
[5577]157
[5618]158    protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
159      : base(evaluator, solutionCreator) {
160      Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
[5577]161      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
[5624]162      Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
[5847]163      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
164      Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
165      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
166      Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
167      Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
168      Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
[5759]169      Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
[5618]170
[5854]171      SymbolicExpressionTreeInterpreterParameter.Hidden = true;
172      MaximumFunctionArgumentsParameter.Hidden = true;
173      MaximumFunctionDefinitionsParameter.Hidden = true;
174
[5618]175      SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
176      SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
177
[5770]178      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
179      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
180
[5722]181      InitializeOperators();
182
[5618]183      UpdateGrammar();
184      RegisterEventHandlers();
[5577]185    }
186
[5685]187    protected virtual void UpdateGrammar() {
[5726]188      SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
189      SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
[5685]190      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
[6803]191        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
[5685]192      }
193      foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
[6803]194        if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
[5685]195      }
196    }
197
[5618]198    private void InitializeOperators() {
199      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
[7506]200      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
[5618]201      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
[5685]202      Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
[7726]203      Operators.Add(new SymbolicDataAnalysisComplexityAnalyzer());
[5618]204      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
[6978]205      Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
[5618]206      ParameterizeOperators();
207    }
208
[5685]209    #region events
[5618]210    private void RegisterEventHandlers() {
211      ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
212      ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
213
[5841]214      SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
215
[5618]216      MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
217      MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
218      MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
219    }
220
[5685]221    private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
[5887]222      ValidationPartition.Start = 0;
223      ValidationPartition.End = 0;
[5685]224      ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
225      OnProblemDataChanged();
226    }
227
[5841]228    private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
229      UpdateGrammar();
230    }
231
[5618]232    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
233      UpdateGrammar();
234    }
235
236    private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
237      if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
238        MaximumSymbolicExpressionTreeDepth.Value = 3;
239    }
240
241    protected override void OnSolutionCreatorChanged() {
242      base.OnSolutionCreatorChanged();
243      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
244      ParameterizeOperators();
245    }
[5685]246
[5618]247    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
248      ParameterizeOperators();
249    }
250
251    protected override void OnEvaluatorChanged() {
252      base.OnEvaluatorChanged();
[5685]253      ParameterizeOperators();
[5618]254    }
255
[5577]256    public event EventHandler ProblemDataChanged;
257    protected virtual void OnProblemDataChanged() {
[5770]258      FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
259      FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
260
[5618]261      UpdateGrammar();
[5685]262      ParameterizeOperators();
263
[5577]264      var handler = ProblemDataChanged;
265      if (handler != null) handler(this, EventArgs.Empty);
[5618]266
267      OnReset();
[5577]268    }
[5685]269    #endregion
[5618]270
[5685]271    protected virtual void ParameterizeOperators() {
[7506]272      var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
[5618]273
274      foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
[5685]275        op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
[5618]276      }
277      foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
[5685]278        op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
279        op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
[5618]280      }
281      foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
[5685]282        op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
283        op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
[5618]284      }
285      foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
[5685]286        op.ProblemDataParameter.ActualName = ProblemDataParameterName;
[5618]287        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
[5759]288        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
289        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[5618]290      }
291      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
292        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
293        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
294      }
295      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
296        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
297      }
298      foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
299        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
300      }
[6135]301      foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
302        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
303      }
[5759]304      foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
305        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
[5883]306        op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
[5759]307      }
[5685]308      foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
309        op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
310      }
[7506]311      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
312        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameterName;
313      }
314      foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>()) {
315        op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
316        op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
317        op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
318        op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
319      }
[5618]320    }
[7770]321
322    #region Import & Export
[7810]323    public void Load(T data) {
[7770]324      Name = data.Name;
325      Description = data.Description;
326      ProblemData = data;
327      OnReset();
328    }
329
[7810]330    public T Export() {
331      return ProblemData;
[7770]332    }
333    #endregion
[5577]334  }
335}
Note: See TracBrowser for help on using the repository browser.