Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2389-EpsLexicase/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/MultiObjective/SymbolicClassificationMultiObjectiveProblem.cs @ 17837

Last change on this file since 17837 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
21using System.Linq;
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
30  [Item("Symbolic Classification Problem (multi-objective)", "Represents a multi objective symbolic classfication problem.")]
31  [StorableClass]
32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 130)]
33  public class SymbolicClassificationMultiObjectiveProblem : SymbolicDataAnalysisMultiObjectiveProblem<IClassificationProblemData, ISymbolicClassificationMultiObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem {
34    private const double PunishmentFactor = 10;
35    private const int InitialMaximumTreeDepth = 8;
36    private const int InitialMaximumTreeLength = 25;
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.";
39    private const string ModelCreatorParameterName = "ModelCreator";
40
41
42    #region parameter properties
43    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
44      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
45    }
46    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
47      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
48    }
49    #endregion
50    #region properties
51    public DoubleLimit EstimationLimits {
52      get { return EstimationLimitsParameter.Value; }
53    }
54    public ISymbolicClassificationModelCreator ModelCreator {
55      get { return ModelCreatorParameter.Value; }
56    }
57    #endregion
58    [StorableConstructor]
59    protected SymbolicClassificationMultiObjectiveProblem(bool deserializing) : base(deserializing) { }
60    protected SymbolicClassificationMultiObjectiveProblem(SymbolicClassificationMultiObjectiveProblem original, Cloner cloner)
61      : base(original, cloner) {
62      RegisterEventHandlers();
63    }
64    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationMultiObjectiveProblem(this, cloner); }
65
66    public SymbolicClassificationMultiObjectiveProblem()
67      : base(new ClassificationProblemData(), new SymbolicClassificationMultiObjectiveMeanSquaredErrorTreeSizeEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
68      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
69      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
70
71      ApplyLinearScalingParameter.Value.Value = false;
72      EstimationLimitsParameter.Hidden = true;
73
74      Maximization = new BoolArray(new bool[] { false, false });
75      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
76      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
77
78
79      RegisterEventHandlers();
80      ConfigureGrammarSymbols();
81      InitializeOperators();
82      UpdateEstimationLimits();
83    }
84
85    [StorableHook(HookType.AfterDeserialization)]
86    private void AfterDeserialization() {
87      // BackwardsCompatibility3.4
88      #region Backwards compatible code, remove with 3.5
89      if (!Parameters.ContainsKey(ModelCreatorParameterName))
90        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
91      #endregion
92      RegisterEventHandlers();
93    }
94
95    private void RegisterEventHandlers() {
96      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
97      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
98    }
99
100    private void ConfigureGrammarSymbols() {
101      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
102      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
103    }
104
105    private void InitializeOperators() {
106      Operators.Add(new SymbolicClassificationMultiObjectiveTrainingBestSolutionAnalyzer());
107      Operators.Add(new SymbolicClassificationMultiObjectiveValidationBestSolutionAnalyzer());
108      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
109      Operators.Add(new SymbolicClassificationPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()));
110      ParameterizeOperators();
111    }
112
113    private void UpdateEstimationLimits() {
114      if (ProblemData.TrainingIndices.Any()) {
115        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
116        var mean = targetValues.Average();
117        var range = targetValues.Max() - targetValues.Min();
118        EstimationLimits.Upper = mean + PunishmentFactor * range;
119        EstimationLimits.Lower = mean - PunishmentFactor * range;
120      } else {
121        EstimationLimits.Upper = double.MaxValue;
122        EstimationLimits.Lower = double.MinValue;
123      }
124    }
125
126    protected override void OnProblemDataChanged() {
127      base.OnProblemDataChanged();
128      UpdateEstimationLimits();
129    }
130
131    protected override void ParameterizeOperators() {
132      base.ParameterizeOperators();
133      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
134        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
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;
139      }
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      }
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.