[5618] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
[7750] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[5618] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[5716] | 27 | using HeuristicLab.Parameters;
|
---|
[5618] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[7750] | 29 | using HeuristicLab.Problems.Instances;
|
---|
[5618] | 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
|
---|
| 32 | [Item("Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem.")]
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Creatable("Problems")]
|
---|
[7750] | 35 | public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem,
|
---|
| 36 | IProblemInstanceConsumer<RegressionData>, IProblemInstanceExporter<RegressionData> {
|
---|
[5618] | 37 | private const double PunishmentFactor = 10;
|
---|
[5685] | 38 | private const int InitialMaximumTreeDepth = 8;
|
---|
| 39 | private const int InitialMaximumTreeLength = 25;
|
---|
[5770] | 40 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
| 41 | private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
[5716] | 42 |
|
---|
[5685] | 43 | #region parameter properties
|
---|
[5770] | 44 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 45 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
[5685] | 46 | }
|
---|
| 47 | #endregion
|
---|
| 48 | #region properties
|
---|
[5770] | 49 | public DoubleLimit EstimationLimits {
|
---|
| 50 | get { return EstimationLimitsParameter.Value; }
|
---|
[5685] | 51 | }
|
---|
| 52 | #endregion
|
---|
[5618] | 53 | [StorableConstructor]
|
---|
| 54 | protected SymbolicRegressionSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
|
---|
| 55 | protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) { }
|
---|
| 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 |
|
---|
[5618] | 64 | Maximization.Value = true;
|
---|
[5685] | 65 | MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
|
---|
| 66 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
| 67 |
|
---|
[6803] | 68 | SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
|
---|
| 69 |
|
---|
| 70 | ConfigureGrammarSymbols();
|
---|
[5685] | 71 | InitializeOperators();
|
---|
[5716] | 72 | UpdateEstimationLimits();
|
---|
[5618] | 73 | }
|
---|
| 74 |
|
---|
[6803] | 75 | private void ConfigureGrammarSymbols() {
|
---|
| 76 | var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
|
---|
| 77 | if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[5685] | 80 | private void InitializeOperators() {
|
---|
| 81 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
| 82 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
[5747] | 83 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
[7726] | 84 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
[7734] | 85 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
[7726] | 86 |
|
---|
[5685] | 87 | ParameterizeOperators();
|
---|
| 88 | }
|
---|
[5716] | 89 |
|
---|
[5685] | 90 | private void UpdateEstimationLimits() {
|
---|
[6754] | 91 | if (ProblemData.TrainingIndizes.Any()) {
|
---|
[6740] | 92 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();
|
---|
[5618] | 93 | var mean = targetValues.Average();
|
---|
| 94 | var range = targetValues.Max() - targetValues.Min();
|
---|
[5770] | 95 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
| 96 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
[6754] | 97 | } else {
|
---|
| 98 | EstimationLimits.Upper = double.MaxValue;
|
---|
| 99 | EstimationLimits.Lower = double.MinValue;
|
---|
[5618] | 100 | }
|
---|
| 101 | }
|
---|
[5623] | 102 |
|
---|
[5685] | 103 | protected override void OnProblemDataChanged() {
|
---|
| 104 | base.OnProblemDataChanged();
|
---|
| 105 | UpdateEstimationLimits();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | protected override void ParameterizeOperators() {
|
---|
| 109 | base.ParameterizeOperators();
|
---|
[5770] | 110 | if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
|
---|
| 111 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
| 112 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
|
---|
| 113 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
| 114 | }
|
---|
[5685] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[5623] | 118 | public override void ImportProblemDataFromFile(string fileName) {
|
---|
| 119 | RegressionProblemData problemData = RegressionProblemData.ImportFromFile(fileName);
|
---|
| 120 | ProblemData = problemData;
|
---|
| 121 | }
|
---|
[7750] | 122 |
|
---|
| 123 | public void Load(RegressionData data) {
|
---|
| 124 | Name = data.Name;
|
---|
| 125 | Description = data.Description;
|
---|
| 126 | Dataset dataset = new Dataset(data.InputVariables, data.Values);
|
---|
| 127 | ProblemData = new RegressionProblemData(dataset, data.AllowedInputVariables, data.TargetVariable);
|
---|
| 128 | ProblemData.TrainingPartition.Start = data.TrainingPartitionStart;
|
---|
| 129 | ProblemData.TrainingPartition.End = data.TrainingPartitionEnd;
|
---|
| 130 | ProblemData.TestPartition.Start = data.TestPartitionStart;
|
---|
| 131 | ProblemData.TestPartition.End = data.TestPartitionEnd;
|
---|
| 132 | OnReset();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | public RegressionData Export() {
|
---|
| 136 | if (!ProblemData.InputVariables.Count.Equals(ProblemData.Dataset.DoubleVariables.Count()))
|
---|
| 137 | throw new ArgumentException("Not all input variables are double variables! (Export only works with double variables)");
|
---|
| 138 |
|
---|
| 139 | RegressionData regData = new RegressionData();
|
---|
| 140 | regData.Name = Name;
|
---|
| 141 | regData.Description = Description;
|
---|
| 142 | regData.TargetVariable = ProblemData.TargetVariable;
|
---|
| 143 | regData.InputVariables = ProblemData.InputVariables.Select(x => x.Value).ToArray();
|
---|
| 144 | regData.AllowedInputVariables = ProblemData.AllowedInputVariables.ToArray();
|
---|
| 145 | regData.TrainingPartitionStart = ProblemData.TrainingPartition.Start;
|
---|
| 146 | regData.TrainingPartitionEnd = ProblemData.TrainingPartition.End;
|
---|
| 147 | regData.TestPartitionStart = ProblemData.TestPartition.Start;
|
---|
| 148 | regData.TestPartitionEnd = ProblemData.TestPartition.End;
|
---|
| 149 |
|
---|
| 150 | List<List<double>> data = new List<List<double>>();
|
---|
| 151 | foreach (var variable in ProblemData.Dataset.DoubleVariables) {
|
---|
| 152 | data.Add(ProblemData.Dataset.GetDoubleValues(variable).ToList());
|
---|
| 153 | }
|
---|
| 154 | regData.Values = Transformer.Transformation(data);
|
---|
| 155 |
|
---|
| 156 | return regData;
|
---|
| 157 | }
|
---|
[5618] | 158 | }
|
---|
| 159 | }
|
---|