Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs @ 17120

Last change on this file since 17120 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

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