[9262] | 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;
|
---|
[9499] | 14 | using HeuristicLab.Problems.TradeRules.Evaluator;
|
---|
[9262] | 15 |
|
---|
| 16 | namespace 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);
|
---|
[9386] | 53 | interpreter.setInitialTest(rows.ToArray()[0]);
|
---|
[9262] | 54 | IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows);
|
---|
[9386] | 55 | interpreter.clearVariables();
|
---|
| 56 | double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, rows);
|
---|
[9262] | 57 |
|
---|
[9499] | 58 |
|
---|
[9262] | 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 | }
|
---|