Free cookie consent management tool by TermsFeed Policy Generator

Changeset 523 for trunk/sources


Ignore:
Timestamp:
08/20/08 08:24:10 (16 years ago)
Author:
gkronber
Message:

minor speed tuning of BakedTreeEvaluator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs

    r483 r523  
    6868      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    6969      switch(instr.symbol) {
    70         case EvaluatorSymbolTable.DIFFERENTIAL: 
     70        case EvaluatorSymbolTable.DIFFERENTIAL:
    7171        case EvaluatorSymbolTable.VARIABLE: {
    7272            instr.i_arg0 = (int)f.data[0]; // var
     
    8787      this.sampleIndex = sampleIndex;
    8888      return EvaluateBakedCode();
     89    }
     90
     91    // skips a whole branch
     92    private void SkipBakedCode() {
     93      int i = 1;
     94      while(i > 0) {
     95        i+=codeArr[PC++].arity;
     96        i--;
     97      }
    8998    }
    9099
     
    179188          }
    180189        case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
    181             double result = 1.0;
    182             // have to evaluate all sub-trees, skipping would probably not lead to a big gain because
    183             // we have to iterate over the linear structure anyway
    184             for(int i = 0; i < currInstr.arity; i++) {
    185               double x = EvaluateBakedCode();
    186               Debug.Assert(x == 0.0 || x == 1.0);
    187               result *= x;
     190            double result = EvaluateBakedCode();
     191            for(int i = 1; i < currInstr.arity; i++) {
     192              if(result == 0.0) SkipBakedCode();
     193              else {
     194                result = EvaluateBakedCode();
     195              }
     196              Debug.Assert(result == 0.0 || result == 1.0);
    188197            }
    189198            return result;
     
    203212            double condition = EvaluateBakedCode();
    204213            Debug.Assert(condition == 0.0 || condition == 1.0);
    205             double x = EvaluateBakedCode();
    206             double y = EvaluateBakedCode();
    207             return condition * y - (condition - 1) * x;
     214            double result;
     215            if(condition == 0.0) {
     216              result = EvaluateBakedCode(); SkipBakedCode();
     217            } else {
     218              SkipBakedCode(); result = EvaluateBakedCode();
     219            }
     220            return result;
    208221          }
    209222        case EvaluatorSymbolTable.LT: {
     
    219232          }
    220233        case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
    221             double result = 0.0; // default is false
    222             for(int i = 0; i < currInstr.arity; i++) {
    223               double x = EvaluateBakedCode();
    224               Debug.Assert(x == 0.0 || x == 1.0);
    225               result += x;
    226             }
    227             return Math.Sign(result);
     234            double result = EvaluateBakedCode();
     235            for(int i = 1; i < currInstr.arity; i++) {
     236              if(result > 0.0) SkipBakedCode();
     237              else {
     238                result = EvaluateBakedCode();
     239                Debug.Assert(result == 0.0 || result == 1.0);
     240              }
     241            }
     242            return result;
    228243          }
    229244        case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1
     
    232247            return Math.Abs(x - y);
    233248          }
     249        case EvaluatorSymbolTable.UNKNOWN:
    234250        default: {
    235251            throw new NotImplementedException();
Note: See TracChangeset for help on using the changeset viewer.