Free cookie consent management tool by TermsFeed Policy Generator

Changeset 328 for branches


Ignore:
Timestamp:
06/20/08 18:12:20 (16 years ago)
Author:
gkronber
Message:

more work on GP evaluation - work in progress - bug nest

Location:
branches/BottomUpTreeEvaluation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/BottomUpTreeEvaluation/BakedTreeEvaluator.cs

    r327 r328  
    3333    private const int MAX_TREE_DEPTH = 20;
    3434    private class Instr {
     35      public double result;
    3536      public double d_arg0;
    3637      public int i_arg0;
     
    5960      int length;
    6061      for(int i = 0; i < MAX_TREE_DEPTH; i++) nInstr[i] = 0;
    61       TranslateToInstr(0, linearRepresentation, out length);
    62     }
    63 
    64     private static int TranslateToInstr(int pos, List<LightWeightFunction> linearRepresentation, out int branchLength) {
     62      //TranslateToInstr(0, linearRepresentation, out length);
     63      int[] heights = new int[linearRepresentation.Count];
     64      CalcHeights(linearRepresentation, heights, 0, out length);
     65      TranslateToTable(0, linearRepresentation, heights);
     66    }
     67
     68    private static int CalcHeights(List<LightWeightFunction> linearRepresentation, int[] heights, int p, out int branchLength) {
     69      if(linearRepresentation[p].arity == 0) {
     70        heights[p] = 1;
     71        branchLength = 1;
     72        return 1;
     73      }
    6574      int height = 0;
    6675      int length = 1;
    67       LightWeightFunction f = linearRepresentation[pos];
    68       for(int i = 0; i < f.arity; i++) {
     76      for(int i = 0; i < linearRepresentation[p].arity; i++) {
    6977        int curBranchLength;
    70         int curBranchHeight = TranslateToInstr(pos + length, linearRepresentation, out curBranchLength);
    71         if(curBranchHeight > height) height = curBranchHeight;
     78        int curHeight = CalcHeights(linearRepresentation, heights, p + length, out curBranchLength);
     79        if(curHeight > height) {
     80          height = curHeight;
     81        }
    7282        length += curBranchLength;
    7383      }
    74       Instr instr = evaluationTable[nInstr[height], height];
    75       instr.arity = f.arity;
    76       instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    77       switch(instr.symbol) {
    78         case EvaluatorSymbolTable.VARIABLE: {
    79             instr.i_arg0 = (int)f.data[0]; // var
    80             instr.d_arg0 = f.data[1]; // weight
    81             instr.i_arg1 = (int)f.data[2]; // sample-offset
    82             break;
     84      heights[p] = height+1;
     85      branchLength = length;
     86      return height+1;
     87    }
     88
     89    private static int TranslateToTable(int pos, List<LightWeightFunction> list, int[] heights) {
     90      LightWeightFunction f = list[pos];
     91      if(f.arity == 0) {
     92        Instr instr = evaluationTable[nInstr[0], 0];
     93        instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
     94        switch(instr.symbol) {
     95          case EvaluatorSymbolTable.VARIABLE: {
     96              instr.i_arg0 = (int)f.data[0]; // var
     97              instr.d_arg0 = f.data[1]; // weight
     98              instr.i_arg1 = (int)f.data[2]; // sample-offset
     99              break;
     100            }
     101          case EvaluatorSymbolTable.CONSTANT: {
     102              instr.result = f.data[0]; // value
     103              break;
     104            }
     105        }
     106        nInstr[0]++;
     107        return 1;
     108      } else {
     109        int length = 1;
     110        int height = heights[pos];
     111        for(int i = 0; i < f.arity; i++) {
     112          int curBranchHeight = heights[pos + length];
     113          if(curBranchHeight < height - 1) {
     114            for(int j = curBranchHeight; j < height - 1; j++) {
     115              evaluationTable[nInstr[j], j].symbol = EvaluatorSymbolTable.IDENTITY;
     116              nInstr[j]++;
     117            }
    83118          }
    84         case EvaluatorSymbolTable.CONSTANT: {
    85             instr.d_arg0 = f.data[0]; // value
    86             break;
    87           }
    88       }
    89       nInstr[height]++;
    90       branchLength = length;
    91       return height;
    92     }
    93 
    94     //private static Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
     119          int curBranchLength = TranslateToTable(pos + length, list, heights);
     120          length += curBranchLength;
     121        }
     122
     123        Instr cell = evaluationTable[nInstr[height-1], height-1];
     124        nInstr[height-1]++;
     125        cell.arity = f.arity;
     126        cell.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
     127        return length;
     128      }
     129    }
     130
     131
     132    //private static int TranslateToInstr(int pos, List<LightWeightFunction> linearRepresentation, out int branchLength) {
     133    //  int height = 0;
     134    //  int length = 1;
     135    //  LightWeightFunction f = linearRepresentation[pos];
     136    //  for(int i = 0; i < f.arity; i++) {
     137    //    int curBranchLength;
     138    //    int curBranchHeight = TranslateToInstr(pos + length, linearRepresentation, out curBranchLength);
     139    //    if(curBranchHeight > height) height = curBranchHeight;
     140    //    length += curBranchLength;
     141    //  }
     142    //  Instr instr = evaluationTable[nInstr[height], height];
    95143    //  instr.arity = f.arity;
    96144    //  instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
     
    103151    //      }
    104152    //    case EvaluatorSymbolTable.CONSTANT: {
    105     //        instr.d_arg0 = f.data[0]; // value
     153    //        instr.result = f.data[0]; // value
    106154    //        break;
    107155    //      }
    108156    //  }
    109     //  return instr;
     157    //  nInstr[height]++;
     158    //  branchLength = length;
     159    //  return height + 1;
    110160    //}
    111161
     
    118168    private static double EvaluateTable() {
    119169      int terminalP = 0;
    120       for(; terminalP < nInstr[0]; terminalP += 2) {
     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] - 4; terminalP += 4) {
    121181        Instr curInstr0 = evaluationTable[terminalP, 0];
    122182        Instr curInstr1 = evaluationTable[terminalP + 1, 0];
     183        Instr curInstr2 = evaluationTable[terminalP + 2, 0];
     184        Instr curInstr3 = evaluationTable[terminalP + 3, 0];
    123185        if(curInstr0.symbol == EvaluatorSymbolTable.VARIABLE) {
    124186          int row = sampleIndex + curInstr0.i_arg1;
    125           if(row < 0 || row >= dataset.Rows) curInstr0.d_arg0 = double.NaN;
    126           else curInstr0.d_arg0 = curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0);
     187          if(row < 0 || row >= dataset.Rows) curInstr0.result = double.NaN;
     188          else curInstr0.result = curInstr0.d_arg0 * dataset.GetValue(row, curInstr0.i_arg0);
    127189        }
    128190        if(curInstr1.symbol == EvaluatorSymbolTable.VARIABLE) {
    129191          int row = sampleIndex + curInstr1.i_arg1;
    130           if(row < 0 || row >= dataset.Rows) curInstr1.d_arg0 = double.NaN;
    131           else curInstr1.d_arg0 = curInstr1.d_arg0 * dataset.GetValue(row, curInstr1.i_arg0);
     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);
    132204        }
    133205      }
     
    140212          switch(curInstr.symbol) {
    141213            case EvaluatorSymbolTable.MULTIPLICATION: {
    142                 curInstr.d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
     214                curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    143215                for(int i = 1; i < curInstr.arity; i++) {
    144                   curInstr.d_arg0 *= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;
    145                 }
    146                 lastLayerInstrP += curInstr.arity;
     216                  curInstr.result *= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     217                }
    147218                break;
    148219              }
    149220            case EvaluatorSymbolTable.ADDITION: {
    150                 curInstr.d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
     221                curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    151222                for(int i = 1; i < curInstr.arity; i++) {
    152                   curInstr.d_arg0 += evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;
    153                 }
    154                 lastLayerInstrP += curInstr.arity;
     223                  curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     224                }
    155225                break;
    156226              }
    157227            case EvaluatorSymbolTable.SUBTRACTION: {
    158228                if(curInstr.arity == 1) {
    159                   curInstr.d_arg0 = -evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;
     229                  curInstr.result = -evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    160230                } else {
    161                   curInstr.d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
     231                  curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    162232                  for(int i = 1; i < curInstr.arity; i++) {
    163                     curInstr.d_arg0 -= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;
     233                    curInstr.result -= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    164234                  }
    165                   lastLayerInstrP += curInstr.arity;
    166235                }
    167236                break;
     
    169238            case EvaluatorSymbolTable.DIVISION: {
    170239                if(curInstr.arity == 1) {
    171                   curInstr.d_arg0 = 1.0 / evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;
     240                  curInstr.result = 1.0 / evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    172241                } else {
    173                   curInstr.d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
     242                  curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    174243                  for(int i = 1; i < curInstr.arity; i++) {
    175                     curInstr.d_arg0 /= evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;
     244                    curInstr.result /= evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    176245                  }
    177                   lastLayerInstrP += curInstr.arity;
    178                 }
    179                 if(double.IsInfinity(curInstr.d_arg0)) curInstr.d_arg0 = 0.0;
     246                }
     247                if(double.IsInfinity(curInstr.result)) curInstr.result = 0.0;
    180248                break;
    181249              }
    182250            case EvaluatorSymbolTable.AVERAGE: {
    183                 curInstr.d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
     251                curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
    184252                for(int i = 1; i < curInstr.arity; i++) {
    185                   curInstr.d_arg0 += evaluationTable[lastLayerInstrP + i, curLevel - 1].d_arg0;
    186                 }
    187                 lastLayerInstrP += curInstr.arity;
    188                 curInstr.d_arg0 /= curInstr.arity;
     253                  curInstr.result += evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     254                }
     255                curInstr.result /= curInstr.arity;
    189256                break;
    190257              }
    191258            case EvaluatorSymbolTable.COSINUS: {
    192                 curInstr.d_arg0 = Math.Cos(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     259                curInstr.result = Math.Cos(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    193260                break;
    194261              }
    195262            case EvaluatorSymbolTable.SINUS: {
    196                 curInstr.d_arg0 = Math.Sin(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     263                curInstr.result = Math.Sin(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    197264                break;
    198265              }
    199266            case EvaluatorSymbolTable.EXP: {
    200                 curInstr.d_arg0 = Math.Exp(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     267                curInstr.result = Math.Exp(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    201268                break;
    202269              }
    203270            case EvaluatorSymbolTable.LOG: {
    204                 curInstr.d_arg0 = Math.Log(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     271                curInstr.result = Math.Log(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    205272                break;
    206273              }
    207274            case EvaluatorSymbolTable.POWER: {
    208                 double x = evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;
    209                 double p = evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;
    210                 curInstr.d_arg0 = Math.Pow(x, p);
     275                double x = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     276                double p = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     277                curInstr.result = Math.Pow(x, p);
    211278                break;
    212279              }
    213280            case EvaluatorSymbolTable.SIGNUM: {
    214                 double value = evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0;
    215                 if(double.IsNaN(value)) curInstr.d_arg0 = double.NaN;
    216                 else curInstr.d_arg0 = Math.Sign(value);
     281                double value = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     282                if(double.IsNaN(value)) curInstr.result = double.NaN;
     283                else curInstr.result = Math.Sign(value);
    217284                break;
    218285              }
    219286            case EvaluatorSymbolTable.SQRT: {
    220                 curInstr.d_arg0 = Math.Sqrt(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     287                curInstr.result = Math.Sqrt(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    221288                break;
    222289              }
    223290            case EvaluatorSymbolTable.TANGENS: {
    224                 curInstr.d_arg0 = Math.Tan(evaluationTable[lastLayerInstrP++, curLevel - 1].d_arg0);
     291                curInstr.result = Math.Tan(evaluationTable[lastLayerInstrP++, curLevel - 1].result);
    225292                break;
    226293              }
     
    285352            //    return double.NaN;
    286353            //  }
     354            case EvaluatorSymbolTable.IDENTITY: {
     355                curInstr.result = evaluationTable[lastLayerInstrP++, curLevel - 1].result;
     356                break;
     357              }
    287358            default: {
    288359                throw new NotImplementedException();
     
    290361          }
    291362        }
    292         // copy remaining results from previous layer to current layer (identiy function)
    293         int r = 0;
    294         for(; lastLayerInstrP < nInstr[curLevel - 1]; lastLayerInstrP++) {
    295           evaluationTable[nInstr[curLevel] + r, curLevel].d_arg0 = evaluationTable[lastLayerInstrP, curLevel - 1].d_arg0;
    296           r++;
    297         }
    298363        curLevel++;
    299364      }
    300       return evaluationTable[0, curLevel - 1].d_arg0;
     365      return evaluationTable[0, curLevel - 1].result;
    301366    }
    302367  }
  • branches/BottomUpTreeEvaluation/SymbolTable.cs

    r319 r328  
    5252    public const int VARIABLE = 22;
    5353    public const int XOR = 23;
    54     public const int UNKNOWN = 24;
     54    public const int IDENTITY = 24;
     55    public const int UNKNOWN = 25;
    5556
    5657    private static Dictionary<Type, int> staticTypes = new Dictionary<Type,int>();
Note: See TracChangeset for help on using the changeset viewer.