Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3057_DynamicALPS/TestProblems/oesr-alps-master/HeuristicLab.Algorithms.OESRALPS/Problems/SymbolicRegressionSingleObjectiveSlidingWindowProblem.cs

Last change on this file was 17479, checked in by kyang, 5 years ago

#3057

  1. upload the latest version of ALPS with SMS-EMOA
  2. upload the related dynamic test problems (dynamic, single-objective symbolic regression), written by David Daninel.
File size: 8.0 KB
RevLine 
[17479]1using HEAL.Attic;
2using HeuristicLab.Algorithms.OESRALPS.Analyzers.Regression;
3using HeuristicLab.Algorithms.OESRALPS.Evaluators;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Problems.DataAnalysis;
9using HeuristicLab.Problems.DataAnalysis.Symbolic;
10using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Text;
15using System.Threading.Tasks;
16
17namespace HeuristicLab.Algorithms.OESRALPS.Problems
18{
19    [Item("Symbolic Regression Sliding Window Problem (single-objective)", "Represents a single objective symbolic regression problem.")]
20    [StorableType("7DDCF683-96FC-4F70-BF4F-FE3A0B0DE110")]
21    [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 101)]
22    class SymbolicRegressionSingleObjectiveSlidingWindowProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem
23    {
24        private const double PunishmentFactor = 10;
25        private const int InitialMaximumTreeDepth = 12;
26        private const int InitialMaximumTreeLength = 50;
27        private const string EstimationLimitsParameterName = "EstimationLimits";
28        private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
29
30        #region parameter properties
31        public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
32            get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
33        }
34        #endregion
35        #region properties
36        public DoubleLimit EstimationLimits {
37            get { return EstimationLimitsParameter.Value; }
38        }
39        #endregion
40        [StorableConstructor]
41        protected SymbolicRegressionSingleObjectiveSlidingWindowProblem(StorableConstructorFlag _) : base(_) { }
42        protected SymbolicRegressionSingleObjectiveSlidingWindowProblem(SymbolicRegressionSingleObjectiveSlidingWindowProblem original, Cloner cloner)
43          : base(original, cloner)
44        {
45            RegisterEventHandlers();
46        }
47        public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveSlidingWindowProblem(this, cloner); }
48
49        public SymbolicRegressionSingleObjectiveSlidingWindowProblem()
50          : base(new RegressionProblemData(), new SymbolicRegressionSingleObjectivePearsonRSquaredSlidingWindowEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator())
51        {
52            Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
53
54            EstimationLimitsParameter.Hidden = true;
55
56
57            ApplyLinearScalingParameter.Value.Value = true;
58            Maximization.Value = true;
59            MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
60            MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
61
62            RegisterEventHandlers();
63            ConfigureGrammarSymbols();
64            InitializeOperators();
65            UpdateEstimationLimits();
66        }
67
68        [StorableHook(HookType.AfterDeserialization)]
69        private void AfterDeserialization()
70        {
71            RegisterEventHandlers();
72            // compatibility
73            bool changed = false;
74            if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any())
75            {
76                Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
77                changed = true;
78            }
79            if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any())
80            {
81                Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
82                changed = true;
83            }
84            if (!Operators.OfType<SymbolicRegressionSolutionsAnalyzer>().Any())
85            {
86                Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
87                changed = true;
88            }
89            if (changed)
90            {
91                ParameterizeOperators();
92            }
93        }
94
95        private void RegisterEventHandlers()
96        {
97            SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
98        }
99
100        private void ConfigureGrammarSymbols()
101        {
102            var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
103            if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
104        }
105
106        private void InitializeOperators()
107        {
108            Operators.Add(new SymbolicRegressionSingleObjectiveValidationLayerBestSolutionSlidingWindowAnalyzer());
109            Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
110            Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
111            Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
112            Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
113            Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
114            Operators.Add(new SymbolicRegressionSolutionsAnalyzer());
115            Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
116            Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
117            ParameterizeOperators();
118        }
119
120        private void UpdateEstimationLimits()
121        {
122            if (ProblemData.TrainingIndices.Any())
123            {
124                var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
125                var mean = targetValues.Average();
126                var range = targetValues.Max() - targetValues.Min();
127                EstimationLimits.Upper = mean + PunishmentFactor * range;
128                EstimationLimits.Lower = mean - PunishmentFactor * range;
129            }
130            else
131            {
132                EstimationLimits.Upper = double.MaxValue;
133                EstimationLimits.Lower = double.MinValue;
134            }
135        }
136
137        protected override void OnProblemDataChanged()
138        {
139            base.OnProblemDataChanged();
140            UpdateEstimationLimits();
141        }
142
143        protected override void ParameterizeOperators()
144        {
145            base.ParameterizeOperators();
146            if (Parameters.ContainsKey(EstimationLimitsParameterName))
147            {
148                var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
149                foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
150                {
151                    op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
152                }
153            }
154
155            foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>())
156            {
157                op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
158                op.QualityVariableName = Evaluator.QualityParameter.ActualName;
159
160                if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator)
161                {
162                    var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
163                    phenotypicSimilarityCalculator.ProblemData = ProblemData;
164                    phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
165                }
166            }
167        }
168    }
169}
Note: See TracBrowser for help on using the repository browser.