Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/08 11:21:04 (16 years ago)
Author:
gkronber
Message:

fixed #328 by restructuring evaluation operators to remove state in evaluation operators.

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/AveragePercentageChangeEvaluator.cs

    r686 r702  
    3030namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3131  public class AvergePercentageChangeEvaluator : GPEvaluatorBase {
    32     protected DoubleData apc;
    33     private bool differential;
    3432    public override string Description {
    3533      get {
     
    4442    }
    4543
    46     public override IOperation Apply(IScope scope) {
    47       differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
    48       apc = GetVariableValue<DoubleData>("APC", scope, false, false);
     44    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     45      bool differential = GetVariableValue<BoolData>("Differential", scope, true).Data;
     46      DoubleData apc = GetVariableValue<DoubleData>("APC", scope, false, false);
    4947      if(apc == null) {
    5048        apc = new DoubleData();
     
    5250      }
    5351
    54       return base.Apply(scope);
    55     }
    56 
    57     public override void Evaluate(int start, int end) {
    5852      double percentageSum = 0;
    5953      for(int sample = start; sample < end; sample++) {
     
    6256        double estimatedPercentageChange;
    6357        if(differential) {
    64           prevOriginal = GetOriginalValue(sample - 1);
    65           originalPercentageChange = (GetOriginalValue(sample) - prevOriginal) / prevOriginal;
    66           estimatedPercentageChange = (GetEstimatedValue(sample) - prevOriginal) / prevOriginal;
    67           SetOriginalValue(sample, estimatedPercentageChange*prevOriginal + prevOriginal);
     58          prevOriginal = dataset.GetValue(targetVariable,sample - 1);
     59          originalPercentageChange = (dataset.GetValue(targetVariable,sample) - prevOriginal) / prevOriginal;
     60          estimatedPercentageChange = (evaluator.Evaluate(sample) - prevOriginal) / prevOriginal;
     61          if(updateTargetValues) {
     62            dataset.SetValue(targetVariable, sample, estimatedPercentageChange * prevOriginal + prevOriginal);
     63          }
    6864        } else {
    69           originalPercentageChange = GetOriginalValue(sample);
    70           estimatedPercentageChange = GetEstimatedValue(sample);
    71           SetOriginalValue(sample, estimatedPercentageChange);
     65          originalPercentageChange = dataset.GetValue(targetVariable,sample);
     66          estimatedPercentageChange = evaluator.Evaluate(sample);
     67          if(updateTargetValues) {
     68            dataset.SetValue(targetVariable, sample, estimatedPercentageChange);
     69          }
    7270        }
    7371        if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/ProfitEvaluator.cs

    r686 r702  
    3030namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3131  public class ProfitEvaluator : GPEvaluatorBase {
    32     protected DoubleData profit;
    33     private int exchangeRateVarIndex;
    34     private double transactionCost;
    3532    public override string Description {
    3633      get {
     
    4643    }
    4744
    48     public override IOperation Apply(IScope scope) {
    49       exchangeRateVarIndex = GetVariableValue<IntData>("ExchangeRate", scope, true).Data;
    50       transactionCost = GetVariableValue<DoubleData>("TransactionCost", scope, true).Data;
    51       profit = GetVariableValue<DoubleData>("Profit", scope, false, false);
     45    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     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);
    5249      if(profit == null) {
    5350        profit = new DoubleData();
     
    5552      }
    5653
    57       return base.Apply(scope);
    58     }
    59 
    60     public override void Evaluate(int start, int end) {
    6154      double cA = 1.0; // start with a capital of one entity of A
    6255      double cB = 0;
     
    6457      for(int sample = start; sample < end; sample++) {
    6558        exchangeRate = dataset.GetValue(sample, exchangeRateVarIndex);
    66         double originalPercentageChange = GetOriginalValue(sample);
    67         double estimatedPercentageChange = GetEstimatedValue(sample);
    68         SetOriginalValue(sample, estimatedPercentageChange);
     59        double originalPercentageChange = dataset.GetValue(targetVariable, sample);
     60        double estimatedPercentageChange = evaluator.Evaluate(sample);
     61        if(updateTargetValues) {
     62          dataset.SetValue(targetVariable, sample, estimatedPercentageChange);
     63        }
    6964        if(!double.IsNaN(originalPercentageChange) && !double.IsInfinity(originalPercentageChange)) {
    7065          if(estimatedPercentageChange > 0) {
    7166            // prediction is the rate of B/A will increase (= get more B for one A) => exchange all B to A
    72             cA += (cB / exchangeRate) * (1-transactionCost);
     67            cA += (cB / exchangeRate) * (1 - transactionCost);
    7368            cB = 0;
    7469          } else if(estimatedPercentageChange < 0) {
    7570            // prediction is the rate of B/A will drop (= get more A for one B) => exchange all A to B
    76             cB += (cA * exchangeRate) * (1-transactionCost);
     71            cB += (cA * exchangeRate) * (1 - transactionCost);
    7772            cA = 0;
    7873          }
     
    8681      // at the end we must exchange all B back to A
    8782      // (because we want to buy the local beer not import one from B-country)
    88       profit.Data = cA + ((cB / exchangeRate) * (1-transactionCost));
     83      profit.Data = cA + ((cB / exchangeRate) * (1 - transactionCost));
    8984      profit.Data -= 1.0; // substract the start capital to determine actual profit
    9085    }
  • trunk/sources/HeuristicLab.GP.StructureIdentification.TimeSeries/TheilInequalityCoefficientEvaluator.cs

    r695 r702  
    3131namespace HeuristicLab.GP.StructureIdentification.TimeSeries {
    3232  public class TheilInequalityCoefficientEvaluator : GPEvaluatorBase {
    33     private DoubleData theilInequaliy;
    34     private DoubleData uBias;
    35     private DoubleData uVariance;
    36     private DoubleData uCovariance;
    37 
    3833    public override string Description {
    3934      get {
     
    5954    }
    6055
    61     public override IOperation Apply(IScope scope) {
    62       theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);
     56    public override void Evaluate(IScope scope, BakedTreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) {
     57    #region create result variables
     58      DoubleData theilInequaliy = GetVariableValue<DoubleData>("TheilInequalityCoefficient", scope, false, false);
    6359      if(theilInequaliy == null) {
    6460        theilInequaliy = new DoubleData();
    6561        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficient"), theilInequaliy));
    6662      }
    67       uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);
     63      DoubleData uBias = GetVariableValue<DoubleData>("TheilInequalityCoefficientBias", scope, false, false);
    6864      if(uBias == null) {
    6965        uBias = new DoubleData();
    7066        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientBias"), uBias));
    7167      }
    72       uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);
     68      DoubleData uVariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientVariance", scope, false, false);
    7369      if(uVariance == null) {
    7470        uVariance = new DoubleData();
    7571        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientVariance"), uVariance));
    7672      }
    77       uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);
     73      DoubleData uCovariance = GetVariableValue<DoubleData>("TheilInequalityCoefficientCovariance", scope, false, false);
    7874      if(uCovariance == null) {
    7975        uCovariance = new DoubleData();
    8076        scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TheilInequalityCoefficientCovariance"), uCovariance));
    8177      }
    82       return base.Apply(scope);
    83     }
     78      #endregion
    8479
    85     public override void Evaluate(int start, int end) {
    8680      double errorsSquaredSum = 0.0;
    8781      double originalSquaredSum = 0.0;
    88       double[] estimatedChanges = new double[end-start];
    89       double[] originalChanges = new double[end-start];
     82      double[] estimatedChanges = new double[end - start];
     83      double[] originalChanges = new double[end - start];
    9084      int nSamples = 0;
    9185      for(int sample = start; sample < end; sample++) {
    92         double prevValue = GetOriginalValue(sample - 1);
    93         double estimatedChange = GetEstimatedValue(sample) - prevValue;
    94         double originalChange = GetOriginalValue(sample) - prevValue;
    95         SetOriginalValue(sample, estimatedChange + prevValue);
     86        double prevValue = dataset.GetValue(targetVariable, sample - 1);
     87        double estimatedChange = evaluator.Evaluate(sample) - prevValue;
     88        double originalChange = dataset.GetValue(targetVariable, sample) - prevValue;
     89        if(updateTargetValues) {
     90          dataset.SetValue(targetVariable, sample, estimatedChange + prevValue);
     91        }
    9692        if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
    9793          double error = estimatedChange - originalChange;
     
    118114
    119115      // all parts add up to one so I don't have to calculate the correlation coefficient for the covariance propotion
    120       uCovariance.Data = 1.0 - uBias.Data - uVariance.Data; 
     116      uCovariance.Data = 1.0 - uBias.Data - uVariance.Data;
    121117    }
    122118  }
Note: See TracChangeset for help on using the changeset viewer.