Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.StructureIdentification/Evaluation/ProfitEvaluator.cs @ 1022

Last change on this file since 1022 was 532, checked in by gkronber, 16 years ago

implemented #265 (GP evaluator for profit simulation with financial forecast models)

File size: 3.9 KB
Line 
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.Operators;
29using HeuristicLab.Functions;
30using HeuristicLab.DataAnalysis;
31
32namespace HeuristicLab.StructureIdentification {
33  public class ProfitEvaluator : GPEvaluatorBase {
34    protected DoubleData profit;
35    private int exchangeRateVarIndex;
36    private double transactionCost;
37    public override string Description {
38      get {
39        return @"TASK";
40      }
41    }
42
43    public ProfitEvaluator()
44      : base() {
45      AddVariableInfo(new VariableInfo("ExchangeRate", "The index of the variable in the dataset for the exchange rate", typeof(IntData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("Profit", "The estimated profit of the model", typeof(DoubleData), VariableKind.New));
47      AddVariableInfo(new VariableInfo("TransactionCost", "Cost of a trade in percent of the transaction volume (0..1)", typeof(DoubleData), VariableKind.In));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
52      transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
53      profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
54      if(profit == null) {
55        profit = new DoubleData();
56        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Profit"), profit));
57      }
58
59      return base.Apply(scope);
60    }
61
62    public override void Evaluate(int start, int end) {
63      double cA = 1.0; // start with a capital of one entity of A
64      double cB = 0;
65      double exchangeRate = double.MaxValue;
66      for(int sample = start; sample < end; sample++) {
67        exchangeRate = dataset.GetValue(sample, exchangeRateVarIndex);
68        double originalPercentageChange = GetOriginalValue(sample);
69        double estimatedPercentageChange = GetEstimatedValue(sample);
70        SetOriginalValue(sample, estimatedPercentageChange);
71        if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
72          if(estimatedPercentageChange > 0) {
73            // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
74            cA += (cB / exchangeRate) * (1-transactionCost);
75            cB = 0;
76          } else if(estimatedPercentageChange < 0) {
77            // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
78            cB += (cA * exchangeRate) * (1-transactionCost);
79            cA = 0;
80          }
81        }
82      }
83
84      if(double.IsNaN(cA) || double.IsInfinity(cA) || double.IsInfinity(cB) || double.IsNaN(cB)) {
85        cA = 0;
86        cB = 0;
87      }
88      // at the end we must exchange all B back to A
89      // (because we want to buy the local beer not import one from B-country)
90      profit.Data = cA + ((cB / exchangeRate) * (1-transactionCost));
91      profit.Data -= 1.0; // substract the start capital to determine actual profit
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.