Free cookie consent management tool by TermsFeed Policy Generator

Changeset 330


Ignore:
Timestamp:
06/21/08 12:26:58 (16 years ago)
Author:
gkronber
Message:

small improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/BottomUpTreeEvaluation/BakedTreeEvaluator.cs

    r329 r330  
    2727using HeuristicLab.Core;
    2828using System.Xml;
     29using System.Runtime.InteropServices;
    2930
    3031namespace HeuristicLab.Functions {
    3132  internal static class BakedTreeEvaluator {
    32     private const int MAX_TREE_SIZE = 4096;
     33    private const int MAX_TREE_SIZE = 128;
    3334    private const int MAX_TREE_DEPTH = 20;
    3435    private class Instr {
     
    4243
    4344    private static int[] nInstr;
    44     private static Instr[,] evaluationTable;
     45    private static Instr[] variables;
     46    private static int variablesCount;
     47    private static Instr[] evaluationTable;
    4548    private static Dataset dataset;
    4649    private static int sampleIndex;
     
    4851
    4952    static BakedTreeEvaluator() {
    50       evaluationTable = new Instr[MAX_TREE_SIZE, MAX_TREE_DEPTH];
     53      variables = new Instr[100];
     54      evaluationTable = new Instr[MAX_TREE_SIZE* MAX_TREE_DEPTH];
    5155      nInstr = new int[MAX_TREE_DEPTH];
    5256      for(int j = 0; j < MAX_TREE_DEPTH; j++) {
    5357        for(int i = 0; i < MAX_TREE_SIZE; i++) {
    54           evaluationTable[i, j] = new Instr();
     58          evaluationTable[j*MAX_TREE_SIZE+i] = new Instr();
    5559        }
    5660      }
     
    5963    public static void ResetEvaluator(List<LightWeightFunction> linearRepresentation) {
    6064      int length;
     65      variablesCount = 0;
    6166      for(int i = 0; i < MAX_TREE_DEPTH; i++) nInstr[i] = 0;
    6267      //TranslateToInstr(0, linearRepresentation, out length);
     
    8287        length += curBranchLength;
    8388      }
    84       heights[p] = height+1;
     89      heights[p] = height + 1;
    8590      branchLength = length;
    86       return height+1;
     91      return height + 1;
    8792    }
    8893
     
    9095      LightWeightFunction f = list[pos];
    9196      if(f.arity == 0) {
    92         Instr instr = evaluationTable[nInstr[0], 0];
     97        Instr instr = evaluationTable[nInstr[0]];
    9398        instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    9499        switch(instr.symbol) {
     
    97102              instr.d_arg0 = f.data[1]; // weight
    98103              instr.i_arg1 = (int)f.data[2]; // sample-offset
     104              variables[variablesCount++] = instr;
    99105              break;
    100106            }
     
    113119          if(curBranchHeight < height - 1) {
    114120            for(int j = curBranchHeight; j < height - 1; j++) {
    115               evaluationTable[nInstr[j], j].symbol = EvaluatorSymbolTable.IDENTITY;
     121              evaluationTable[nInstr[j]+ j*MAX_TREE_SIZE].symbol = EvaluatorSymbolTable.IDENTITY;
    116122              nInstr[j]++;
    117123            }
     
    121127        }
    122128
    123         Instr cell = evaluationTable[nInstr[height-1], height-1];
    124         nInstr[height-1]++;
     129        Instr cell = evaluationTable[nInstr[height - 1]+MAX_TREE_SIZE*( height - 1)];
     130        nInstr[height - 1]++;
    125131        cell.arity = f.arity;
    126132        cell.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
     
    167173
    168174    private static double EvaluateTable() {
    169       int terminalP = 0;
    170       // process remaining instr first
    171       for(int i = 0; i < nInstr[0] % 4; i++) {
    172         Instr curInstr = evaluationTable[terminalP++, 0];
    173         if(curInstr.symbol == EvaluatorSymbolTable.VARIABLE) {
    174           int row = sampleIndex + curInstr.i_arg1;
    175           if(row < 0 || row >= dataset.Rows) curInstr.result = double.NaN;
    176           else curInstr.result = curInstr.d_arg0 * dataset.GetValue(row, curInstr.i_arg0);
    177         }
    178       }
    179       // unrolled loop
    180       for(; terminalP < nInstr[0] - 3; terminalP += 4) {
    181         Instr curInstr0 = evaluationTable[terminalP, 0];
    182         Instr curInstr1 = evaluationTable[terminalP + 1, 0];
    183         Instr curInstr2 = evaluationTable[terminalP + 2, 0];
    184         Instr curInstr3 = evaluationTable[terminalP + 3, 0];
    185         if(curInstr0.symbol == EvaluatorSymbolTable.VARIABLE) {
    186           int row = sampleIndex + curInstr0.i_arg1;
    187           if(row < 0 || row >= dataset.Rows) curInstr0.result = double.NaN;
    188           else curInstr0.result = curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0);
    189         }
    190         if(curInstr1.symbol == EvaluatorSymbolTable.VARIABLE) {
    191           int row = sampleIndex + curInstr1.i_arg1;
    192           if(row < 0 || row >= dataset.Rows) curInstr1.result = double.NaN;
    193           else curInstr1.result = curInstr1.d_arg0 * dataset.GetValue(row, curInstr1.i_arg0);
    194         }
    195         if(curInstr2.symbol == EvaluatorSymbolTable.VARIABLE) {
    196           int row = sampleIndex + curInstr2.i_arg1;
    197           if(row < 0 || row >= dataset.Rows) curInstr2.result = double.NaN;
    198           else curInstr2.result = curInstr2.d_arg0 * dataset.GetValue(row, curInstr2.i_arg0);
    199         }
    200         if(curInstr3.symbol == EvaluatorSymbolTable.VARIABLE) {
    201           int row = sampleIndex + curInstr3.i_arg1;
    202           if(row < 0 || row >= dataset.Rows) curInstr3.result = double.NaN;
    203           else curInstr3.result = curInstr3.d_arg0 * dataset.GetValue(row, curInstr3.i_arg0);
    204         }
    205       }
    206 
     175      for(int i = 0; i < variablesCount; i++) {
     176        Instr curInstr = variables[i];
     177        int row = sampleIndex + curInstr.i_arg1;
     178        if(row < 0 || row >= dataset.Rows) curInstr.result = double.NaN;
     179        else curInstr.result = curInstr.d_arg0 * dataset.GetValue(row, curInstr.i_arg0);
     180      }
    207181      int curLevel = 1;
     182      int lastLayerInstrP = 0;
     183      int curLayerOffset = MAX_TREE_SIZE;
    208184      while(nInstr[curLevel] > 0) {
    209         int lastLayerInstrP = 0;
    210185        for(int curLayerInstrP = 0; curLayerInstrP < nInstr[curLevel]; curLayerInstrP++) {
    211           Instr curInstr = evaluationTable[curLayerInstrP, curLevel];
     186          Instr curInstr = evaluationTable[curLayerInstrP+curLayerOffset];
    212187          switch(curInstr.symbol) {
    213188            case EvaluatorSymbolTable.MULTIPLICATION: {
    214                 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     189                curInstr.result = evaluationTable[lastLayerInstrP++].result;
    215190                for(int i = 1; i < curInstr.arity; i++) {
    216                   curInstr.result *= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     191                  curInstr.result *= evaluationTable[lastLayerInstrP++].result;
    217192                }
    218193                break;
    219194              }
    220195            case EvaluatorSymbolTable.ADDITION: {
    221                 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     196                curInstr.result = evaluationTable[lastLayerInstrP++].result;
    222197                for(int i = 1; i < curInstr.arity; i++) {
    223                   curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     198                  curInstr.result += evaluationTable[lastLayerInstrP++].result;
    224199                }
    225200                break;
     
    227202            case EvaluatorSymbolTable.SUBTRACTION: {
    228203                if(curInstr.arity == 1) {
    229                   curInstr.result = -evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     204                  curInstr.result = -evaluationTable[lastLayerInstrP++].result;
    230205                } else {
    231                   curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     206                  curInstr.result = evaluationTable[lastLayerInstrP++].result;
    232207                  for(int i = 1; i < curInstr.arity; i++) {
    233                     curInstr.result -= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     208                    curInstr.result -= evaluationTable[lastLayerInstrP++].result;
    234209                  }
    235210                }
     
    238213            case EvaluatorSymbolTable.DIVISION: {
    239214                if(curInstr.arity == 1) {
    240                   curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     215                  curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++].result;
    241216                } else {
    242                   curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     217                  curInstr.result = evaluationTable[lastLayerInstrP++].result;
    243218                  for(int i = 1; i < curInstr.arity; i++) {
    244                     curInstr.result /= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     219                    curInstr.result /= evaluationTable[lastLayerInstrP++].result;
    245220                  }
    246221                }
     
    249224              }
    250225            case EvaluatorSymbolTable.AVERAGE: {
    251                 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     226                curInstr.result = evaluationTable[lastLayerInstrP++].result;
    252227                for(int i = 1; i < curInstr.arity; i++) {
    253                   curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     228                  curInstr.result += evaluationTable[lastLayerInstrP++].result;
    254229                }
    255230                curInstr.result /= curInstr.arity;
     
    257232              }
    258233            case EvaluatorSymbolTable.COSINUS: {
    259                 curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     234                curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++].result);
    260235                break;
    261236              }
    262237            case EvaluatorSymbolTable.SINUS: {
    263                 curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     238                curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++].result);
    264239                break;
    265240              }
    266241            case EvaluatorSymbolTable.EXP: {
    267                 curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     242                curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++].result);
    268243                break;
    269244              }
    270245            case EvaluatorSymbolTable.LOG: {
    271                 curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     246                curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++].result);
    272247                break;
    273248              }
    274249            case EvaluatorSymbolTable.POWER: {
    275                 double x = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    276                 double p = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     250                double x = evaluationTable[lastLayerInstrP++].result;
     251                double p = evaluationTable[lastLayerInstrP++].result;
    277252                curInstr.result = Math.Pow(x, p);
    278253                break;
    279254              }
    280255            case EvaluatorSymbolTable.SIGNUM: {
    281                 double value = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     256                double value = evaluationTable[lastLayerInstrP++].result;
    282257                if(double.IsNaN(value)) curInstr.result = double.NaN;
    283258                else curInstr.result = Math.Sign(value);
     
    285260              }
    286261            case EvaluatorSymbolTable.SQRT: {
    287                 curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     262                curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++].result);
    288263                break;
    289264              }
    290265            case EvaluatorSymbolTable.TANGENS: {
    291                 curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
     266                curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++].result);
    292267                break;
    293268              }
     
    353328            //  }
    354329            case EvaluatorSymbolTable.IDENTITY: {
    355                 curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     330                curInstr.result = evaluationTable[lastLayerInstrP++].result;
    356331                break;
    357332              }
     
    362337        }
    363338        curLevel++;
    364       }
    365       return evaluationTable[0, curLevel - 1].result;
     339        lastLayerInstrP = curLayerOffset;
     340        curLayerOffset += MAX_TREE_SIZE;
     341      }
     342      return evaluationTable[lastLayerInstrP].result;
    366343    }
    367344  }
Note: See TracChangeset for help on using the changeset viewer.