1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 |
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 | using HeuristicLab.Data;
|
---|
9 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
12 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
13 | using HeuristicLab.Problems.DataAnalysis;
|
---|
14 |
|
---|
15 | namespace 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 | }
|
---|