Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1787 for trunk/sources


Ignore:
Timestamp:
05/13/09 15:45:32 (15 years ago)
Author:
gkronber
Message:

simplified code of BakedTreeEvaluator: removed constant expression folding and special cases for DIV and SUB with only one operand. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BakedTreeEvaluator.cs

    r1786 r1787  
    4545      public byte arity;
    4646      public byte symbol;
    47       public ushort exprLength;
    4847      public IFunction function;
    4948    }
     
    6968        codeArr[i++] = TranslateToInstr(f);
    7069      }
    71       exprIndex = 0;
    72       ushort exprLength;
    73       bool constExpr;
    74       PatchExpressionLengthsAndConstants(0, out constExpr, out exprLength);
    75     }
    76 
    77     ushort exprIndex;
    78     private void PatchExpressionLengthsAndConstants(ushort index, out bool constExpr, out ushort exprLength) {
    79       exprLength = 1;
    80       if (codeArr[index].arity == 0) {
    81         // when no children then it's a constant expression only if the terminal is a constant
    82         constExpr = codeArr[index].symbol == EvaluatorSymbolTable.CONSTANT;
    83       } else {
    84         constExpr = true; // when there are children it's a constant expression if all children are constant;
    85       }
    86       for (int i = 0; i < codeArr[index].arity; i++) {
    87         exprIndex++;
    88         ushort branchLength;
    89         bool branchConstExpr;
    90         PatchExpressionLengthsAndConstants(exprIndex, out branchConstExpr, out branchLength);
    91         exprLength += branchLength;
    92         constExpr &= branchConstExpr;
    93       }
    94 
    95       if (constExpr) {
    96         PC = index;
    97         codeArr[index].d_arg0 = EvaluateBakedCode();
    98         codeArr[index].symbol = EvaluatorSymbolTable.CONSTANT;
    99       }
    100       codeArr[index].exprLength = exprLength;
    10170    }
    10271
     
    11180            instr.d_arg0 = f.data[1]; // weight
    11281            instr.i_arg1 = (short)f.data[2]; // sample-offset
    113             instr.exprLength = 1;
    11482            break;
    11583          }
    11684        case EvaluatorSymbolTable.CONSTANT: {
    11785            instr.d_arg0 = f.data[0]; // value
    118             instr.exprLength = 1;
    11986            break;
    12087          }
    12188        case EvaluatorSymbolTable.UNKNOWN: {
    12289            instr.function = f.functionType;
    123             instr.exprLength = 1;
    12490            break;
    12591          }
     
    145111    // skips a whole branch
    146112    private void SkipBakedCode() {
    147       PC += codeArr[PC].exprLength;
     113      int i = 1;
     114      while (i > 0) {
     115        i += codeArr[PC++].arity;
     116        i--;
     117      }
    148118    }
    149119
     
    157127          }
    158128        case EvaluatorSymbolTable.CONSTANT: {
    159             PC += currInstr.exprLength - 1;
    160129            return currInstr.d_arg0;
    161130          }
     
    185154          }
    186155        case EvaluatorSymbolTable.SUBTRACTION: {
    187             if (currInstr.arity == 1) {
    188               return -EvaluateBakedCode();
    189             } else {
    190               double result = EvaluateBakedCode();
    191               for (int i = 1; i < currInstr.arity; i++) {
    192                 result -= EvaluateBakedCode();
    193               }
    194               return result;
    195             }
     156            double result = EvaluateBakedCode();
     157            for (int i = 1; i < currInstr.arity; i++) {
     158              result -= EvaluateBakedCode();
     159            }
     160            return result;
    196161          }
    197162        case EvaluatorSymbolTable.DIVISION: {
    198163            double result;
    199             if (currInstr.arity == 1) {
    200               result = 1.0 / EvaluateBakedCode();
    201             } else {
    202               result = EvaluateBakedCode();
    203               for (int i = 1; i < currInstr.arity; i++) {
    204                 result /= EvaluateBakedCode();
    205               }
     164            result = EvaluateBakedCode();
     165            for (int i = 1; i < currInstr.arity; i++) {
     166              result /= EvaluateBakedCode();
    206167            }
    207168            if (double.IsInfinity(result)) return 0.0;
Note: See TracChangeset for help on using the changeset viewer.