Changeset 1787
- Timestamp:
- 05/13/09 15:45:32 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BakedTreeEvaluator.cs
r1786 r1787 45 45 public byte arity; 46 46 public byte symbol; 47 public ushort exprLength;48 47 public IFunction function; 49 48 } … … 69 68 codeArr[i++] = TranslateToInstr(f); 70 69 } 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 constant82 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;101 70 } 102 71 … … 111 80 instr.d_arg0 = f.data[1]; // weight 112 81 instr.i_arg1 = (short)f.data[2]; // sample-offset 113 instr.exprLength = 1;114 82 break; 115 83 } 116 84 case EvaluatorSymbolTable.CONSTANT: { 117 85 instr.d_arg0 = f.data[0]; // value 118 instr.exprLength = 1;119 86 break; 120 87 } 121 88 case EvaluatorSymbolTable.UNKNOWN: { 122 89 instr.function = f.functionType; 123 instr.exprLength = 1;124 90 break; 125 91 } … … 145 111 // skips a whole branch 146 112 private void SkipBakedCode() { 147 PC += codeArr[PC].exprLength; 113 int i = 1; 114 while (i > 0) { 115 i += codeArr[PC++].arity; 116 i--; 117 } 148 118 } 149 119 … … 157 127 } 158 128 case EvaluatorSymbolTable.CONSTANT: { 159 PC += currInstr.exprLength - 1;160 129 return currInstr.d_arg0; 161 130 } … … 185 154 } 186 155 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; 196 161 } 197 162 case EvaluatorSymbolTable.DIVISION: { 198 163 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(); 206 167 } 207 168 if (double.IsInfinity(result)) return 0.0;
Note: See TracChangeset
for help on using the changeset viewer.