[5618] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16641] | 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
|
---|
| 21 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
[12103] | 25 | using HeuristicLab.Optimization;
|
---|
[5716] | 26 | using HeuristicLab.Parameters;
|
---|
[16628] | 27 | using HEAL.Attic;
|
---|
[5618] | 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
[12504] | 30 | [Item("Symbolic Regression Problem (single-objective)", "Represents a single objective symbolic regression problem.")]
|
---|
[16641] | 31 | [StorableType("7DDCF683-96FC-4F70-BF4F-FE3A0B0DE6E0")]
|
---|
[12504] | 32 | [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 100)]
|
---|
[5759] | 33 | public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem {
|
---|
[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 limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
[5716] | 39 |
|
---|
[5685] | 40 | #region parameter properties
|
---|
[5770] | 41 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 42 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
[5685] | 43 | }
|
---|
| 44 | #endregion
|
---|
| 45 | #region properties
|
---|
[5770] | 46 | public DoubleLimit EstimationLimits {
|
---|
| 47 | get { return EstimationLimitsParameter.Value; }
|
---|
[5685] | 48 | }
|
---|
| 49 | #endregion
|
---|
[5618] | 50 | [StorableConstructor]
|
---|
[16628] | 51 | protected SymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[8175] | 52 | protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
| 54 | RegisterEventHandlers();
|
---|
| 55 | }
|
---|
[5618] | 56 | public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
|
---|
| 57 |
|
---|
| 58 | public SymbolicRegressionSingleObjectiveProblem()
|
---|
| 59 | : base(new RegressionProblemData(), new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
|
---|
[5847] | 60 | Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
|
---|
[5685] | 61 |
|
---|
[5854] | 62 | EstimationLimitsParameter.Hidden = true;
|
---|
| 63 |
|
---|
[8664] | 64 |
|
---|
| 65 | ApplyLinearScalingParameter.Value.Value = true;
|
---|
[5618] | 66 | Maximization.Value = true;
|
---|
[5685] | 67 | MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
|
---|
| 68 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
| 69 |
|
---|
[8175] | 70 | RegisterEventHandlers();
|
---|
[6803] | 71 | ConfigureGrammarSymbols();
|
---|
[5685] | 72 | InitializeOperators();
|
---|
[5716] | 73 | UpdateEstimationLimits();
|
---|
[5618] | 74 | }
|
---|
| 75 |
|
---|
[8130] | 76 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 77 | private void AfterDeserialization() {
|
---|
[8175] | 78 | RegisterEventHandlers();
|
---|
[8130] | 79 | // compatibility
|
---|
| 80 | bool changed = false;
|
---|
| 81 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
|
---|
| 82 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
| 83 | changed = true;
|
---|
| 84 | }
|
---|
| 85 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
|
---|
| 86 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
| 87 | changed = true;
|
---|
| 88 | }
|
---|
[10596] | 89 | if (!Operators.OfType<SymbolicRegressionSolutionsAnalyzer>().Any()) {
|
---|
| 90 | Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
|
---|
| 91 | changed = true;
|
---|
| 92 | }
|
---|
[8130] | 93 | if (changed) {
|
---|
| 94 | ParameterizeOperators();
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[8175] | 98 | private void RegisterEventHandlers() {
|
---|
| 99 | SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[6803] | 102 | private void ConfigureGrammarSymbols() {
|
---|
| 103 | var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
|
---|
| 104 | if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[5685] | 107 | private void InitializeOperators() {
|
---|
| 108 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
| 109 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
[5747] | 110 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
[7726] | 111 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
[7734] | 112 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
[10596] | 113 | Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
|
---|
[12103] | 114 | Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
|
---|
[16592] | 115 | Operators.Add(new SymbolicRegressionConstraintAnalyzer());
|
---|
[12103] | 116 | Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
|
---|
[5685] | 117 | ParameterizeOperators();
|
---|
| 118 | }
|
---|
[5716] | 119 |
|
---|
[5685] | 120 | private void UpdateEstimationLimits() {
|
---|
[8139] | 121 | if (ProblemData.TrainingIndices.Any()) {
|
---|
| 122 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
[5618] | 123 | var mean = targetValues.Average();
|
---|
| 124 | var range = targetValues.Max() - targetValues.Min();
|
---|
[5770] | 125 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
| 126 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
[6754] | 127 | } else {
|
---|
| 128 | EstimationLimits.Upper = double.MaxValue;
|
---|
| 129 | EstimationLimits.Lower = double.MinValue;
|
---|
[5618] | 130 | }
|
---|
| 131 | }
|
---|
[5623] | 132 |
|
---|
[5685] | 133 | protected override void OnProblemDataChanged() {
|
---|
| 134 | base.OnProblemDataChanged();
|
---|
| 135 | UpdateEstimationLimits();
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | protected override void ParameterizeOperators() {
|
---|
| 139 | base.ParameterizeOperators();
|
---|
[5770] | 140 | if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
|
---|
| 141 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
| 142 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
|
---|
| 143 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
| 144 | }
|
---|
[5685] | 145 | }
|
---|
[12103] | 146 |
|
---|
| 147 | foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
|
---|
| 148 | op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
| 149 | op.QualityVariableName = Evaluator.QualityParameter.ActualName;
|
---|
| 150 |
|
---|
| 151 | if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
|
---|
| 152 | var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
|
---|
| 153 | phenotypicSimilarityCalculator.ProblemData = ProblemData;
|
---|
| 154 | phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
[5685] | 157 | }
|
---|
[5618] | 158 | }
|
---|
| 159 | }
|
---|