using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; using HeuristicLab.Problems.DataAnalysis.Symbolic; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.TradeRules.Evaluator; namespace HeuristicLab.Problems.TradeRules { [Item("Trade Rules Evaluator", "Calculates the profit")] [StorableClass] public class EvaluatorTradeRules : TradeRulesSingleObjectiveEvaluator { private static int initialTraining; private static int initialTest; public override bool Maximization { get { return true; } } [StorableConstructor] protected EvaluatorTradeRules(bool deserializing) : base(deserializing) { } protected EvaluatorTradeRules(EvaluatorTradeRules original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new EvaluatorTradeRules(this, cloner); } public EvaluatorTradeRules() : base() { } public override IOperation Apply() { var solution = SymbolicExpressionTreeParameter.ActualValue; IEnumerable rows = GenerateRowsToEvaluate(); initialTraining = initialTrainingValue(); initialTest = initialTestValue(); double quality = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows); QualityParameter.ActualValue = new DoubleValue(quality); return base.Apply(); } public static double Calculate(ITradeRulesExpresionTree interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, IRegressionProblemData problemData, IEnumerable rows) { interpreter.setInitialTraining(initialTraining); interpreter.setInitialTest(rows.ToArray()[0]); IEnumerable estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); interpreter.clearVariables(); double tradingCash = OnlineTradeRulesCalculator.Calculate(estimatedValues, problemData, rows); return tradingCash; } public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IRegressionProblemData problemData, IEnumerable rows) { SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context; EstimationLimitsParameter.ExecutionContext = context; double r2 = Calculate((ITradeRulesExpresionTree) SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows); SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null; EstimationLimitsParameter.ExecutionContext = null; return r2; } } }