1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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
|
---|
21 | using System.Linq;
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Optimization;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
|
---|
30 | [Item("Symbolic Classification Problem (multi objective)", "Represents a multi objective symbolic classfication problem.")]
|
---|
31 | [StorableClass]
|
---|
32 | [Creatable("Problems")]
|
---|
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 | DiversityResultName = "Phenotypic Diversity"
|
---|
111 | });
|
---|
112 | ParameterizeOperators();
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void UpdateEstimationLimits() {
|
---|
116 | if (ProblemData.TrainingIndices.Any()) {
|
---|
117 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
118 | var mean = targetValues.Average();
|
---|
119 | var range = targetValues.Max() - targetValues.Min();
|
---|
120 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
121 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
122 | } else {
|
---|
123 | EstimationLimits.Upper = double.MaxValue;
|
---|
124 | EstimationLimits.Lower = double.MinValue;
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override void OnProblemDataChanged() {
|
---|
129 | base.OnProblemDataChanged();
|
---|
130 | UpdateEstimationLimits();
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected override void ParameterizeOperators() {
|
---|
134 | base.ParameterizeOperators();
|
---|
135 | if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
|
---|
136 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
137 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
|
---|
138 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
139 | foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
|
---|
140 | op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
|
---|
141 | }
|
---|
142 |
|
---|
143 | foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
144 | op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
145 | op.QualityVariableName = Evaluator.QualitiesParameter.ActualName;
|
---|
146 |
|
---|
147 | if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
|
---|
148 | var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
|
---|
149 | phenotypicSimilarityCalculator.ProblemData = ProblemData;
|
---|
150 | phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|