Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/3.3/ProfitEvaluator.cs @ 2211

Last change on this file since 2211 was 1891, checked in by gkronber, 15 years ago

Fixed #645 (Tree evaluators precompile the model for each evaluation of a row).

File size: 3.9 KB
RevLine 
[645]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.GP.StructureIdentification;
29
[686]30namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
[645]31  public class ProfitEvaluator : GPEvaluatorBase {
32    public override string Description {
33      get {
34        return @"TASK";
35      }
36    }
37
38    public ProfitEvaluator()
39      : base() {
40      AddVariableInfo(new VariableInfo("ExchangeRate", "The index of the variable in the dataset for the exchange rate", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Profit", "The estimated profit of the model", typeof(DoubleData), VariableKind.New));
42      AddVariableInfo(new VariableInfo("TransactionCost", "Cost of a trade in percent of the transaction volume (0..1)", typeof(DoubleData), VariableKind.In));
43    }
44
[1891]45    public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
[702]46      int exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
47      double transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
48      DoubleData profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
[712]49      if (profit == null) {
[645]50        profit = new DoubleData();
51        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Profit"), profit));
52      }
53
54      double cA = 1.0; // start with a capital of one entity of A
55      double cB = 0;
56      double exchangeRate = double.MaxValue;
[712]57      for (int sample = start; sample < end; sample++) {
[645]58        exchangeRate = dataset.GetValue(sample, exchangeRateVarIndex);
[712]59        double originalPercentageChange = dataset.GetValue(sample, targetVariable);
[1891]60        double estimatedPercentageChange = evaluator.Evaluate(sample);
[712]61        if (updateTargetValues) {
62          dataset.SetValue(sample, targetVariable, estimatedPercentageChange);
[702]63        }
[712]64        if (!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
65          if (estimatedPercentageChange > 0) {
[645]66            // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
[702]67            cA += (cB / exchangeRate) * (1 - transactionCost);
[645]68            cB = 0;
[712]69          } else if (estimatedPercentageChange < 0) {
[645]70            // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
[702]71            cB += (cA * exchangeRate) * (1 - transactionCost);
[645]72            cA = 0;
73          }
74        }
75      }
76
[712]77      if (double.IsNaN(cA) || double.IsInfinity(cA) || double.IsInfinity(cB) || double.IsNaN(cB)) {
[645]78        cA = 0;
79        cB = 0;
80      }
81      // at the end we must exchange all B back to A
82      // (because we want to buy the local beer not import one from B-country)
[702]83      profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost));
[645]84      profit.Data -= 1.0; // substract the start capital to determine actual profit
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.