Free cookie consent management tool by TermsFeed Policy Generator

Changeset 318 for trunk/sources


Ignore:
Timestamp:
06/17/08 18:56:59 (16 years ago)
Author:
gkronber
Message:

adapted and simplified evaluation code for ticket #168

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs

    r317 r318  
    3030namespace HeuristicLab.Functions {
    3131  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;
    3541    private int PC;
    36     private int DP;
    3742    private Dataset dataset;
    3843    private int sampleIndex;
     
    4348
    4449    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);
    5254      }
    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;
    5574    }
    5675
    5776    internal double Evaluate(Dataset dataset, int sampleIndex) {
    5877      PC = 0;
    59       DP = 0;
    6078      this.sampleIndex = sampleIndex;
    6179      this.dataset = dataset;
     
    6482
    6583    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) {
    7186        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;
    7688            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);
    7890          }
    7991        case EvaluatorSymbolTable.CONSTANT: {
    80             return dataArr[DP++];
     92            return currInstr.d_arg0;
    8193          }
    8294        case EvaluatorSymbolTable.MULTIPLICATION: {
    8395            double result = EvaluateBakedCode();
    84             for(int i = 1; i < arity; i++) {
     96            for(int i = 1; i < currInstr.arity; i++) {
    8597              result *= EvaluateBakedCode();
    8698            }
     
    89101        case EvaluatorSymbolTable.ADDITION: {
    90102            double sum = EvaluateBakedCode();
    91             for(int i = 1; i < arity; i++) {
     103            for(int i = 1; i < currInstr.arity; i++) {
    92104              sum += EvaluateBakedCode();
    93105            }
     
    95107          }
    96108        case EvaluatorSymbolTable.SUBTRACTION: {
    97             if(arity == 1) {
     109            if(currInstr.arity == 1) {
    98110              return -EvaluateBakedCode();
    99111            } else {
    100112              double result = EvaluateBakedCode();
    101               for(int i = 1; i < arity; i++) {
     113              for(int i = 1; i < currInstr.arity; i++) {
    102114                result -= EvaluateBakedCode();
    103115              }
     
    107119        case EvaluatorSymbolTable.DIVISION: {
    108120            double result;
    109             if(arity == 1) {
     121            if(currInstr.arity == 1) {
    110122              result = 1.0 / EvaluateBakedCode();
    111123            } else {
    112124              result = EvaluateBakedCode();
    113               for(int i = 1; i < arity; i++) {
     125              for(int i = 1; i < currInstr.arity; i++) {
    114126                result /= EvaluateBakedCode();
    115127              }
     
    120132        case EvaluatorSymbolTable.AVERAGE: {
    121133            double sum = EvaluateBakedCode();
    122             for(int i = 1; i < arity; i++) {
     134            for(int i = 1; i < currInstr.arity; i++) {
    123135              sum += EvaluateBakedCode();
    124136            }
    125             return sum / arity;
     137            return sum / currInstr.arity;
    126138          }
    127139        case EvaluatorSymbolTable.COSINUS: {
     
    157169            // have to evaluate all sub-trees, skipping would probably not lead to a big gain because
    158170            // 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++) {
    160172              double x = Math.Round(EvaluateBakedCode());
    161173              if(x == 0 || x == 1.0) result *= x;
     
    197209        case EvaluatorSymbolTable.OR: {
    198210            double result = 0.0; // default is false
    199             for(int i = 0; i < arity; i++) {
     211            for(int i = 0; i < currInstr.arity; i++) {
    200212              double x = Math.Round(EvaluateBakedCode());
    201213              if(x == 1.0 && result == 0.0) result = 1.0; // found first true (1.0) => set to true
     
    214226          }
    215227        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();
    225229          }
    226230      }
     
    233237    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    234238      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));
    236240      return node;
    237241    }
Note: See TracChangeset for help on using the changeset viewer.