Free cookie consent management tool by TermsFeed Policy Generator

source: branches/sluengo/HeuristicLab.Problems.TradeRules/TradeAnalysisProblem.cs @ 10710

Last change on this file since 10710 was 9139, checked in by sluengo, 12 years ago
File size: 20.3 KB
Line 
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
22using System;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Common.Resources;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.Instances;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37
38namespace HeuristicLab.Problems.TradeRules
39{
40    [StorableClass]
41    public abstract class TradeAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>, ISymbolicDataAnalysisProblem, IStorableContent,
42      IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
43        where T : class, IDataAnalysisProblemData
44        where U : class, ISymbolicDataAnalysisEvaluator<T>
45        where V : class, ISymbolicDataAnalysisSolutionCreator
46    {
47
48        #region parameter names & descriptions
49        private const string ProblemDataParameterName = "ProblemData";
50        private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
51        private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
52        private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
53        private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
54        private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
55        private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
56        private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
57        private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
58        private const string ValidationPartitionParameterName = "ValidationPartition";
59
60        private const string ProblemDataParameterDescription = "";
61        private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
62        private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
63        private const string MaximumSymbolicExpressionTreeDepthParameterDescription = "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.";
64        private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
65        private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
66        private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
67        private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
68        private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
69        private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
70        #endregion
71
72        #region parameter properties
73        IParameter IDataAnalysisProblem.ProblemDataParameter
74        {
75            get { return ProblemDataParameter; }
76        }
77        public IValueParameter<T> ProblemDataParameter
78        {
79            get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
80        }
81        public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter
82        {
83            get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
84        }
85        public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter
86        {
87            get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
88        }
89        public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter
90        {
91            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
92        }
93        public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter
94        {
95            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
96        }
97        public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter
98        {
99            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
100        }
101        public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter
102        {
103            get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
104        }
105        public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter
106        {
107            get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
108        }
109        public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter
110        {
111            get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
112        }
113        public IFixedValueParameter<IntRange> ValidationPartitionParameter
114        {
115            get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
116        }
117        #endregion
118
119        #region properties
120        public string Filename { get; set; }
121        public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
122
123        IDataAnalysisProblemData IDataAnalysisProblem.ProblemData
124        {
125            get { return ProblemData; }
126        }
127        public T ProblemData
128        {
129            get { return ProblemDataParameter.Value; }
130            set { ProblemDataParameter.Value = value; }
131        }
132
133        public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar
134        {
135            get { return SymbolicExpressionTreeGrammarParameter.Value; }
136            set { SymbolicExpressionTreeGrammarParameter.Value = value; }
137        }
138        public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter
139        {
140            get { return SymbolicExpressionTreeInterpreterParameter.Value; }
141            set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
142        }
143
144        public IntValue MaximumSymbolicExpressionTreeDepth
145        {
146            get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
147        }
148        public IntValue MaximumSymbolicExpressionTreeLength
149        {
150            get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
151        }
152        public IntValue MaximumFunctionDefinitions
153        {
154            get { return MaximumFunctionDefinitionsParameter.Value; }
155        }
156        public IntValue MaximumFunctionArguments
157        {
158            get { return MaximumFunctionArgumentsParameter.Value; }
159        }
160        public PercentValue RelativeNumberOfEvaluatedSamples
161        {
162            get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
163        }
164
165        public IntRange FitnessCalculationPartition
166        {
167            get { return FitnessCalculationPartitionParameter.Value; }
168        }
169        public IntRange ValidationPartition
170        {
171            get { return ValidationPartitionParameter.Value; }
172        }
173        #endregion
174
175        [StorableConstructor]
176        protected TradeAnalysisProblem(bool deserializing) : base(deserializing) { }
177        [StorableHook(HookType.AfterDeserialization)]
178        private void AfterDeserialization()
179        {
180            RegisterEventHandlers();
181        }
182        protected TradeAnalysisProblem(TradeAnalysisProblem<T, U, V> original, Cloner cloner)
183            : base(original, cloner)
184        {
185            RegisterEventHandlers();
186        }
187
188        protected TradeAnalysisProblem(T problemData, U evaluator, V solutionCreator)
189            : base(evaluator, solutionCreator)
190        {
191            Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
192            Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
193            Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
194            Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription));
195            Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
196            Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription));
197            Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription));
198            Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
199            Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
200            Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
201
202            SymbolicExpressionTreeInterpreterParameter.Hidden = true;
203            MaximumFunctionArgumentsParameter.Hidden = true;
204            MaximumFunctionDefinitionsParameter.Hidden = true;
205
206            SymbolicExpressionTreeGrammar = new Grammar();
207            SymbolicExpressionTreeInterpreter = new Interpreter();
208
209            FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
210            FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
211
212            InitializeOperators();
213
214            UpdateGrammar();
215            RegisterEventHandlers();
216        }
217
218        protected virtual void UpdateGrammar()
219        {
220            SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
221            SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
222            foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>())
223            {
224                if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
225            }
226            foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>())
227            {
228                if (!varSymbol.Fixed) varSymbol.VariableNames = ProblemData.AllowedInputVariables;
229            }
230        }
231
232        private void InitializeOperators()
233        {
234            Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
235            Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicDataAnalysisExpressionCrossover<T>>());
236            Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
237            Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
238            Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
239            Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
240            ParameterizeOperators();
241        }
242
243        #region events
244        private void RegisterEventHandlers()
245        {
246            ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
247            ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
248
249            SymbolicExpressionTreeGrammarParameter.ValueChanged += new EventHandler(SymbolicExpressionTreeGrammarParameter_ValueChanged);
250
251            MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
252            MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
253            MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
254        }
255
256        private void ProblemDataParameter_ValueChanged(object sender, EventArgs e)
257        {
258            ValidationPartition.Start = 0;
259            ValidationPartition.End = 0;
260            ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
261            OnProblemDataChanged();
262        }
263
264        private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e)
265        {
266            UpdateGrammar();
267        }
268
269        private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e)
270        {
271            UpdateGrammar();
272        }
273
274        private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e)
275        {
276            if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
277                MaximumSymbolicExpressionTreeDepth.Value = 3;
278        }
279
280        protected override void OnSolutionCreatorChanged()
281        {
282            base.OnSolutionCreatorChanged();
283            SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
284            ParameterizeOperators();
285        }
286
287        private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e)
288        {
289            ParameterizeOperators();
290        }
291
292        protected override void OnEvaluatorChanged()
293        {
294            base.OnEvaluatorChanged();
295            ParameterizeOperators();
296        }
297
298        public event EventHandler ProblemDataChanged;
299        protected virtual void OnProblemDataChanged()
300        {
301            FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
302            FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
303
304            UpdateGrammar();
305            ParameterizeOperators();
306
307            var handler = ProblemDataChanged;
308            if (handler != null) handler(this, EventArgs.Empty);
309
310            OnReset();
311        }
312        #endregion
313
314        protected virtual void ParameterizeOperators()
315        {
316            var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
317
318            foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>())
319            {
320                op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
321            }
322            foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>())
323            {
324                op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
325                op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
326            }
327            foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>())
328            {
329                op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
330                op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
331            }
332            foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>())
333            {
334                op.ProblemDataParameter.ActualName = ProblemDataParameterName;
335                op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
336                op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
337                op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
338            }
339            foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>())
340            {
341                op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
342                op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
343            }
344            foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>())
345            {
346                op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
347            }
348            foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>())
349            {
350                op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
351            }
352            foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>())
353            {
354                op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
355            }
356            foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>())
357            {
358                op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
359                op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
360            }
361            foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>())
362            {
363                op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
364            }
365            foreach (var op in operators.OfType<ISymbolicDataAnalysisExpressionCrossover<T>>())
366            {
367                op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameterName;
368                op.ProblemDataParameter.ActualName = ProblemDataParameter.Name;
369                op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
370                op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
371                op.EvaluatorParameter.ActualName = EvaluatorParameter.Name;
372            }
373        }
374
375        #region Import & Export
376        public void Load(T data)
377        {
378            Name = data.Name;
379            Description = data.Description;
380            ProblemData = data;
381            OnReset();
382        }
383
384        public T Export()
385        {
386            return ProblemData;
387        }
388        #endregion
389    }
390}
Note: See TracBrowser for help on using the repository browser.