Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveProblem.cs @ 15529

Last change on this file since 15529 was 14711, checked in by gkronber, 8 years ago

#2520

  • renamed StorableClass -> StorableType
  • changed persistence to use GUIDs instead of type names
File size: 7.6 KB
RevLine 
[13368]1#region License Information
[5618]2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5618]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
21using System.Linq;
22using HeuristicLab.Common;
23using HeuristicLab.Core;
[5623]24using HeuristicLab.Data;
[12103]25using HeuristicLab.Optimization;
[5716]26using HeuristicLab.Parameters;
[5618]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
[12504]30  [Item("Symbolic Classification Problem (multi-objective)", "Represents a multi objective symbolic classfication problem.")]
[14711]31  [StorableType("FB1DE179-6E65-4330-A539-88C98AD87EDD")]
[12504]32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 130)]
[5733]33  public class SymbolicClassificationMultiObjectiveProblem : SymbolicDataAnalysisMultiObjectiveProblem<IClassificationProblemData, ISymbolicClassificationMultiObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem {
[5618]34    private const double PunishmentFactor = 10;
[5685]35    private const int InitialMaximumTreeDepth = 8;
36    private const int InitialMaximumTreeLength = 25;
[5770]37    private const string EstimationLimitsParameterName = "EstimationLimits";
38    private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model.";
[8594]39    private const string ModelCreatorParameterName = "ModelCreator";
[5618]40
[8594]41
[5685]42    #region parameter properties
[5770]43    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
44      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
[5685]45    }
[8594]46    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
47      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
48    }
[5685]49    #endregion
50    #region properties
[5770]51    public DoubleLimit EstimationLimits {
52      get { return EstimationLimitsParameter.Value; }
[5685]53    }
[8594]54    public ISymbolicClassificationModelCreator ModelCreator {
55      get { return ModelCreatorParameter.Value; }
56    }
[5685]57    #endregion
[5618]58    [StorableConstructor]
59    protected SymbolicClassificationMultiObjectiveProblem(bool deserializing) : base(deserializing) { }
[8175]60    protected SymbolicClassificationMultiObjectiveProblem(SymbolicClassificationMultiObjectiveProblem original, Cloner cloner)
61      : base(original, cloner) {
62      RegisterEventHandlers();
63    }
[5618]64    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationMultiObjectiveProblem(this, cloner); }
65
66    public SymbolicClassificationMultiObjectiveProblem()
67      : base(new ClassificationProblemData(), new SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
[5847]68      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
[8594]69      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
[5685]70
[8664]71      ApplyLinearScalingParameter.Value.Value = false;
[5854]72      EstimationLimitsParameter.Hidden = true;
73
[5623]74      Maximization = new BoolArray(new bool[] { false, false });
[5685]75      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
76      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
77
[6803]78
[8175]79      RegisterEventHandlers();
[6803]80      ConfigureGrammarSymbols();
[5685]81      InitializeOperators();
[5716]82      UpdateEstimationLimits();
[5618]83    }
84
[8175]85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
[8883]87      // BackwardsCompatibility3.4
88      #region Backwards compatible code, remove with 3.5
[8594]89      if (!Parameters.ContainsKey(ModelCreatorParameterName))
90        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
[8883]91      #endregion
[8175]92      RegisterEventHandlers();
93    }
94
95    private void RegisterEventHandlers() {
96      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
[8594]97      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
[8175]98    }
99
[6803]100    private void ConfigureGrammarSymbols() {
101      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
102      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
103    }
104
[5685]105    private void InitializeOperators() {
106      Operators.Add(new SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer());
107      Operators.Add(new SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer());
[12103]108      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
109      Operators.Add(new SymbolicClassificationPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()));
[5685]110      ParameterizeOperators();
111    }
112
113    private void UpdateEstimationLimits() {
[8139]114      if (ProblemData.TrainingIndices.Any()) {
115        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
[5618]116        var mean = targetValues.Average();
117        var range = targetValues.Max() - targetValues.Min();
[5770]118        EstimationLimits.Upper = mean + PunishmentFactor * range;
119        EstimationLimits.Lower = mean - PunishmentFactor * range;
[6754]120      } else {
121        EstimationLimits.Upper = double.MaxValue;
122        EstimationLimits.Lower = double.MinValue;
[5618]123      }
124    }
[5623]125
[5685]126    protected override void OnProblemDataChanged() {
127      base.OnProblemDataChanged();
128      UpdateEstimationLimits();
129    }
130
[8594]131    protected override void ParameterizeOperators() {
[5685]132      base.ParameterizeOperators();
[5770]133      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
134        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
[8594]135        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
136          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
137        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
138          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
[5685]139      }
[12103]140
141      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
142        op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
143        op.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
144
145        if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
146          var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
147          phenotypicSimilarityCalculator.ProblemData = ProblemData;
148          phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
149        }
150      }
[5685]151    }
[5618]152  }
153}
Note: See TracBrowser for help on using the repository browser.