Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9262 was 9262, checked in by sluengo, 11 years ago
File size: 3.5 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;
14
15namespace HeuristicLab.Problems.TradeRules
16{
17    [Item("Trade Rules Evaluator", "Calculates the profit")]
18    [StorableClass]
19    public class EvaluatorTradeRules : TradeRulesSingleObjectiveEvaluator
20    {
21        private static int initialTraining;
22        private static int initialTest;
23        public override bool Maximization { get { return true; } }
24
25        [StorableConstructor]
26        protected EvaluatorTradeRules(bool deserializing) : base(deserializing) { }
27        protected EvaluatorTradeRules(EvaluatorTradeRules original, Cloner cloner)
28            : base(original, cloner)
29        {
30        }
31        public override IDeepCloneable Clone(Cloner cloner)
32        {
33            return new EvaluatorTradeRules(this, cloner);
34        }
35
36        public EvaluatorTradeRules() : base() { }
37
38        public override IOperation Apply()
39        {
40            var solution = SymbolicExpressionTreeParameter.ActualValue;
41            IEnumerable<int> rows = GenerateRowsToEvaluate();
42            initialTraining = initialTrainingValue();
43            initialTest = initialTestValue();
44            double quality = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);
45            QualityParameter.ActualValue = new DoubleValue(quality);
46            ITradeRulesExpresionTree interpreter = (ITradeRulesExpresionTree)SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
47            interpreter.clearVariables();
48            return base.Apply();
49        }
50
51        public static double Calculate(ITradeRulesExpresionTree interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows)
52        {
53            interpreter.setInitialTraining(initialTraining);
54            interpreter.setInitialTest(initialTest);
55            IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
56           
57            double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, problemData.TrainingIndices);
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
72            return r2;
73        }
74    }
75}
Note: See TracBrowser for help on using the repository browser.