Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9455 was 9386, 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;
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            return base.Apply();
47        }
48
49        public static double Calculate(ITradeRulesExpresionTree interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable<int> rows)
50        {
51            interpreter.setInitialTraining(initialTraining);
52            interpreter.setInitialTest(rows.ToArray()[0]);
53            IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
54            interpreter.clearVariables();
55            double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, rows);
56           
57            return tradingCash;
58        }
59
60        public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable<int> rows)
61        {
62            SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
63            EstimationLimitsParameter.ExecutionContext = context;
64
65            double r2 = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);
66            SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
67            EstimationLimitsParameter.ExecutionContext = null;
68         
69            return r2;
70        }
71    }
72}
Note: See TracBrowser for help on using the repository browser.