Changeset 318
- Timestamp:
- 06/17/08 18:56:59 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs
r317 r318 30 30 namespace HeuristicLab.Functions { 31 31 internal class BakedTreeEvaluator : StorableBase { 32 private int[] codeArr; 33 private double[] dataArr; 34 private static EvaluatorSymbolTable symbolTable = EvaluatorSymbolTable.SymbolTable; 32 private struct Instr { 33 public double d_arg0; 34 public int i_arg0; 35 public int i_arg1; 36 public int arity; 37 public int symbol; 38 } 39 40 private Instr[] codeArr; 35 41 private int PC; 36 private int DP;37 42 private Dataset dataset; 38 43 private int sampleIndex; … … 43 48 44 49 public BakedTreeEvaluator(List<LightWeightFunction> linearRepresentation) { 45 List<int> code = new List<int>(); 46 List<double> data = new List<double>(); 47 foreach(LightWeightFunction fun in linearRepresentation) { 48 code.Add(fun.arity); 49 code.Add(symbolTable.MapFunction(fun.functionType)); 50 code.Add(fun.data.Count); 51 data.AddRange(fun.data); 50 codeArr = new Instr[linearRepresentation.Count]; 51 int i = 0; 52 foreach(LightWeightFunction f in linearRepresentation) { 53 codeArr[i++] = TranslateToInstr(f); 52 54 } 53 codeArr = code.ToArray(); 54 dataArr = data.ToArray(); 55 } 56 57 private Instr TranslateToInstr(LightWeightFunction f) { 58 Instr instr = new Instr(); 59 instr.arity = f.arity; 60 instr.symbol = EvaluatorSymbolTable.SymbolTable.MapFunction(f.functionType); 61 switch(instr.symbol) { 62 case EvaluatorSymbolTable.VARIABLE: { 63 instr.i_arg0 = (int)f.data[0]; // var 64 instr.d_arg0 = f.data[1]; // weight 65 instr.i_arg1 = (int)f.data[2]; // sample-offset 66 break; 67 } 68 case EvaluatorSymbolTable.CONSTANT: { 69 instr.d_arg0 = f.data[0]; // value 70 break; 71 } 72 } 73 return instr; 55 74 } 56 75 57 76 internal double Evaluate(Dataset dataset, int sampleIndex) { 58 77 PC = 0; 59 DP = 0;60 78 this.sampleIndex = sampleIndex; 61 79 this.dataset = dataset; … … 64 82 65 83 private double EvaluateBakedCode() { 66 int arity = codeArr[PC]; 67 int functionSymbol = codeArr[PC + 1]; 68 int nLocalVariables = codeArr[PC + 2]; 69 PC += 3; 70 switch(functionSymbol) { 84 Instr currInstr = codeArr[PC++]; 85 switch(currInstr.symbol) { 71 86 case EvaluatorSymbolTable.VARIABLE: { 72 int var = (int)dataArr[DP]; 73 double weight = dataArr[DP + 1]; 74 int row = sampleIndex + (int)dataArr[DP + 2]; 75 DP += 3; 87 int row = sampleIndex + currInstr.i_arg1; 76 88 if(row < 0 || row >= dataset.Rows) return double.NaN; 77 else return weight * dataset.GetValue(row, var);89 else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0); 78 90 } 79 91 case EvaluatorSymbolTable.CONSTANT: { 80 return dataArr[DP++];92 return currInstr.d_arg0; 81 93 } 82 94 case EvaluatorSymbolTable.MULTIPLICATION: { 83 95 double result = EvaluateBakedCode(); 84 for(int i = 1; i < arity; i++) {96 for(int i = 1; i < currInstr.arity; i++) { 85 97 result *= EvaluateBakedCode(); 86 98 } … … 89 101 case EvaluatorSymbolTable.ADDITION: { 90 102 double sum = EvaluateBakedCode(); 91 for(int i = 1; i < arity; i++) {103 for(int i = 1; i < currInstr.arity; i++) { 92 104 sum += EvaluateBakedCode(); 93 105 } … … 95 107 } 96 108 case EvaluatorSymbolTable.SUBTRACTION: { 97 if( arity == 1) {109 if(currInstr.arity == 1) { 98 110 return -EvaluateBakedCode(); 99 111 } else { 100 112 double result = EvaluateBakedCode(); 101 for(int i = 1; i < arity; i++) {113 for(int i = 1; i < currInstr.arity; i++) { 102 114 result -= EvaluateBakedCode(); 103 115 } … … 107 119 case EvaluatorSymbolTable.DIVISION: { 108 120 double result; 109 if( arity == 1) {121 if(currInstr.arity == 1) { 110 122 result = 1.0 / EvaluateBakedCode(); 111 123 } else { 112 124 result = EvaluateBakedCode(); 113 for(int i = 1; i < arity; i++) {125 for(int i = 1; i < currInstr.arity; i++) { 114 126 result /= EvaluateBakedCode(); 115 127 } … … 120 132 case EvaluatorSymbolTable.AVERAGE: { 121 133 double sum = EvaluateBakedCode(); 122 for(int i = 1; i < arity; i++) {134 for(int i = 1; i < currInstr.arity; i++) { 123 135 sum += EvaluateBakedCode(); 124 136 } 125 return sum / arity;137 return sum / currInstr.arity; 126 138 } 127 139 case EvaluatorSymbolTable.COSINUS: { … … 157 169 // have to evaluate all sub-trees, skipping would probably not lead to a big gain because 158 170 // we have to iterate over the linear structure anyway 159 for(int i = 0; i < arity; i++) {171 for(int i = 0; i < currInstr.arity; i++) { 160 172 double x = Math.Round(EvaluateBakedCode()); 161 173 if(x == 0 || x == 1.0) result *= x; … … 197 209 case EvaluatorSymbolTable.OR: { 198 210 double result = 0.0; // default is false 199 for(int i = 0; i < arity; i++) {211 for(int i = 0; i < currInstr.arity; i++) { 200 212 double x = Math.Round(EvaluateBakedCode()); 201 213 if(x == 1.0 && result == 0.0) result = 1.0; // found first true (1.0) => set to true … … 214 226 } 215 227 default: { 216 IFunction function = symbolTable.MapSymbol(functionSymbol); 217 double[] args = new double[nLocalVariables + arity]; 218 for(int i = 0; i < nLocalVariables; i++) { 219 args[i] = dataArr[DP++]; 220 } 221 for(int j = 0; j < arity; j++) { 222 args[nLocalVariables + j] = EvaluateBakedCode(); 223 } 224 return function.Apply(dataset, sampleIndex, args); 228 throw new NotImplementedException(); 225 229 } 226 230 } … … 233 237 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 234 238 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 235 node.AppendChild(PersistenceManager.Persist("SymbolTable", symbolTable, document, persistedObjects));239 node.AppendChild(PersistenceManager.Persist("SymbolTable", EvaluatorSymbolTable.SymbolTable, document, persistedObjects)); 236 240 return node; 237 241 }
Note: See TracChangeset
for help on using the changeset viewer.