[9139] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Parameters;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.TradeRules
|
---|
| 34 | {
|
---|
| 35 | [Item("TradeRules", "Represents a trade rules in a symbolic regression problem.")]
|
---|
| 36 | [StorableClass]
|
---|
| 37 | [Creatable("Problems")]
|
---|
| 38 | public class TradeRulesProblem : TradeRulesAbstractProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem
|
---|
| 39 | {
|
---|
| 40 | private const double PunishmentFactor = 10;
|
---|
| 41 | private const int InitialMaximumTreeDepth = 8;
|
---|
| 42 | private const int InitialMaximumTreeLength = 25;
|
---|
| 43 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
| 44 | private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
|
---|
| 45 |
|
---|
| 46 | #region parameter properties
|
---|
| 47 | public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter
|
---|
| 48 | {
|
---|
| 49 | get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 | #region properties
|
---|
| 53 | public DoubleLimit EstimationLimits
|
---|
| 54 | {
|
---|
| 55 | get { return EstimationLimitsParameter.Value; }
|
---|
| 56 | }
|
---|
| 57 | #endregion
|
---|
| 58 | [StorableConstructor]
|
---|
| 59 | protected TradeRulesProblem(bool deserializing) : base(deserializing) { }
|
---|
| 60 | protected TradeRulesProblem(TradeRulesProblem original, Cloner cloner)
|
---|
| 61 | : base(original, cloner)
|
---|
| 62 | {
|
---|
| 63 | RegisterEventHandlers();
|
---|
| 64 | }
|
---|
| 65 | public override IDeepCloneable Clone(Cloner cloner) { return new TradeRulesProblem(this, cloner); }
|
---|
| 66 |
|
---|
| 67 | public TradeRulesProblem()
|
---|
| 68 | : base(new RegressionProblemData(), new EvaluatorTradeRules(), new SymbolicDataAnalysisExpressionTreeCreator())
|
---|
| 69 | {
|
---|
| 70 | Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
|
---|
| 71 |
|
---|
| 72 | EstimationLimitsParameter.Hidden = true;
|
---|
| 73 |
|
---|
| 74 | Maximization.Value = true;
|
---|
| 75 | MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
|
---|
| 76 | MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
|
---|
| 77 |
|
---|
| 78 | RegisterEventHandlers();
|
---|
| 79 | ConfigureGrammarSymbols();
|
---|
| 80 | InitializeOperators();
|
---|
| 81 | UpdateEstimationLimits();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 85 | private void AfterDeserialization()
|
---|
| 86 | {
|
---|
| 87 | RegisterEventHandlers();
|
---|
| 88 | // compatibility
|
---|
| 89 | bool changed = false;
|
---|
| 90 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any())
|
---|
| 91 | {
|
---|
| 92 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
| 93 | changed = true;
|
---|
| 94 | }
|
---|
| 95 | if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any())
|
---|
| 96 | {
|
---|
| 97 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
| 98 | changed = true;
|
---|
| 99 | }
|
---|
| 100 | if (changed)
|
---|
| 101 | {
|
---|
| 102 | ParameterizeOperators();
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | private void RegisterEventHandlers()
|
---|
| 107 | {
|
---|
| 108 | SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | private void ConfigureGrammarSymbols()
|
---|
| 112 | {
|
---|
| 113 | var grammar = SymbolicExpressionTreeGrammar as Grammar;
|
---|
| 114 | if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | private void InitializeOperators()
|
---|
| 118 | {
|
---|
| 119 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
|
---|
| 120 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
|
---|
| 121 | Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
|
---|
| 122 | Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
|
---|
| 123 | Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
|
---|
| 124 |
|
---|
| 125 | ParameterizeOperators();
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private void UpdateEstimationLimits()
|
---|
| 129 | {
|
---|
| 130 | if (ProblemData.TrainingIndices.Any())
|
---|
| 131 | {
|
---|
| 132 | var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
|
---|
| 133 | var mean = targetValues.Average();
|
---|
| 134 | var range = targetValues.Max() - targetValues.Min();
|
---|
| 135 | EstimationLimits.Upper = mean + PunishmentFactor * range;
|
---|
| 136 | EstimationLimits.Lower = mean - PunishmentFactor * range;
|
---|
| 137 | }
|
---|
| 138 | else
|
---|
| 139 | {
|
---|
| 140 | EstimationLimits.Upper = double.MaxValue;
|
---|
| 141 | EstimationLimits.Lower = double.MinValue;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | protected override void OnProblemDataChanged()
|
---|
| 146 | {
|
---|
| 147 | base.OnProblemDataChanged();
|
---|
| 148 | UpdateEstimationLimits();
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | protected override void ParameterizeOperators()
|
---|
| 152 | {
|
---|
| 153 | base.ParameterizeOperators();
|
---|
| 154 | if (Parameters.ContainsKey(EstimationLimitsParameterName))
|
---|
| 155 | {
|
---|
| 156 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
| 157 | foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
|
---|
| 158 | {
|
---|
| 159 | op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|