#region License Information
/* HeuristicLab
* Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.GP.Interfaces;
using HeuristicLab.DataAnalysis;
using System.Linq;
namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
public class ProfitEvaluator : GPEvaluatorBase {
public override string Description {
get {
return @"TASK";
}
}
public ProfitEvaluator()
: base() {
AddVariableInfo(new VariableInfo("ExchangeRate", "The index of the variable in the dataset for the exchange rate", typeof(IntData), VariableKind.In));
AddVariableInfo(new VariableInfo("Profit", "The estimated profit of the model", typeof(DoubleData), VariableKind.New));
AddVariableInfo(new VariableInfo("TransactionCost", "Cost of a trade in percent of the transaction volume (0..1)", typeof(DoubleData), VariableKind.In));
}
public override void Evaluate(IScope scope, IFunctionTree tree, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end) {
int exchangeRateVarIndex = GetVariableValue("ExchangeRate", scope, true).Data;
double transactionCost = GetVariableValue("TransactionCost", scope, true).Data;
DoubleData profit = GetVariableValue("Profit", scope, false, false);
if (profit == null) {
profit = new DoubleData();
scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Profit"), profit));
}
double cA = 1.0; // start with a capital of one entity of A
double cB = 0;
double exchangeRate = double.MaxValue;
double[] estimatedValues = evaluator.Evaluate(dataset, tree, Enumerable.Range(start, end - start)).ToArray();
double[] originalValues = dataset.GetVariableValues(targetVariable, start, end);
double[] exchangeRateValues = dataset.GetVariableValues(exchangeRateVarIndex, start, end);
for (int i = 0; i < estimatedValues.Length; i++) {
exchangeRate = exchangeRateValues[i];
double originalPercentageChange = originalValues[i];
double estimatedPercentageChange = estimatedValues[i];
if (!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
if (estimatedPercentageChange > 0) {
// prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
cA += (cB / exchangeRate) * (1 - transactionCost);
cB = 0;
} else if (estimatedPercentageChange < 0) {
// prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
cB += (cA * exchangeRate) * (1 - transactionCost);
cA = 0;
}
}
}
if (double.IsNaN(cA) || double.IsInfinity(cA) || double.IsInfinity(cB) || double.IsNaN(cB)) {
cA = 0;
cB = 0;
}
// at the end we must exchange all B back to A
// (because we want to buy the local beer not import one from B-country)
profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost));
profit.Data -= 1.0; // substract the start capital to determine actual profit
}
}
}