Free cookie consent management tool by TermsFeed Policy Generator

source: branches/sluengo/HeuristicLab.Problems.TradeRules/Evaluator/EvaluatorTradeRules.cs @ 17514

Last change on this file since 17514 was 9499, checked in by sluengo, 12 years ago
File size: 3.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
12using HeuristicLab.Problems.DataAnalysis.Symbolic;
13using HeuristicLab.Problems.DataAnalysis;
14using HeuristicLab.Problems.TradeRules.Evaluator;
15
16namespace HeuristicLab.Problems.TradeRules
17{
18    [Item("Trade Rules Evaluator", "Calculates the profit")]
19    [StorableClass]
20    public class EvaluatorTradeRules : TradeRulesSingleObjectiveEvaluator
21    {
22        private static int initialTraining;
23        private static int initialTest;
24        public override bool Maximization { get { return true; } }
25
26        [StorableConstructor]
27        protected EvaluatorTradeRules(bool deserializing) : base(deserializing) { }
28        protected EvaluatorTradeRules(EvaluatorTradeRules original, Cloner cloner)
29            : base(original, cloner)
30        {
31        }
32        public override IDeepCloneable Clone(Cloner cloner)
33        {
34            return new EvaluatorTradeRules(this, cloner);
35        }
36
37        public EvaluatorTradeRules() : base() { }
38
39        public override IOperation Apply()
40        {
41            var solution = SymbolicExpressionTreeParameter.ActualValue;
42            IEnumerable<int> rows = GenerateRowsToEvaluate();
43            initialTraining = initialTrainingValue();
44            initialTest = initialTestValue();
45            double quality = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
46            QualityParameter.ActualValue = new DoubleValue(quality);
47            return base.Apply();
48        }
49
50        public static double Calculate(ITradeRulesExpresionTree interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows)
51        {
52            interpreter.setInitialTraining(initialTraining);
53            interpreter.setInitialTest(rows.ToArray()[0]);
54            IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
55            interpreter.clearVariables();
56            double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, rows);
57           
58
59            return tradingCash;
60        }
61
62        public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows)
63        {
64            SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
65            EstimationLimitsParameter.ExecutionContext = context;
66
67            double r2 = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
68            SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
69            EstimationLimitsParameter.ExecutionContext = null;
70         
71            return r2;
72        }
73    }
74}
Note: See TracBrowser for help on using the repository browser.