Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1836


Ignore:
Timestamp:
05/15/09 15:37:26 (16 years ago)
Author:
gkronber
Message:

implemented persistence of tree evaluators in a common base class for tree evaluators. #615 (Evaluation of HL3 function trees should be equivalent to evaluation in HL2)

Location:
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BakedTreeEvaluator.cs

    r1796 r1836  
    3434  /// Not thread-safe!
    3535  /// </summary>
    36   public class BakedTreeEvaluator : ItemBase, ITreeEvaluator {
    37     private const double EPSILON = 1.0e-7;
    38     private double estimatedValueMax;
    39     private double estimatedValueMin;
     36  public class BakedTreeEvaluator : TreeEvaluatorBase {
    4037
    41     private class Instr {
    42       public double d_arg0;
    43       public short i_arg0;
    44       public short i_arg1;
    45       public byte arity;
    46       public byte symbol;
    47       public IFunction function;
    48     }
    4938
    50     private Instr[] codeArr;
    51     private int PC;
    52     private Dataset dataset;
    53     private int sampleIndex;
    54 
    55     public void ResetEvaluator(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
    56       this.dataset = dataset;
    57       double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, start, end);
    58 
    59       // get the mean of the values of the target variable to determine the max and min bounds of the estimated value
    60       double targetMean = dataset.GetMean(targetVariable, start, end);
    61       estimatedValueMin = targetMean - maximumPunishment;
    62       estimatedValueMax = targetMean + maximumPunishment;
    63 
    64     }
    65 
    66     private Instr TranslateToInstr(LightWeightFunction f) {
    67       Instr instr = new Instr();
    68       instr.arity = f.arity;
    69       instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    70       switch (instr.symbol) {
    71         case EvaluatorSymbolTable.DIFFERENTIAL:
    72         case EvaluatorSymbolTable.VARIABLE: {
    73             instr.i_arg0 = (short)f.data[0]; // var
    74             instr.d_arg0 = f.data[1]; // weight
    75             instr.i_arg1 = (short)f.data[2]; // sample-offset
    76             break;
    77           }
    78         case EvaluatorSymbolTable.CONSTANT: {
    79             instr.d_arg0 = f.data[0]; // value
    80             break;
    81           }
    82         case EvaluatorSymbolTable.UNKNOWN: {
    83             instr.function = f.functionType;
    84             break;
    85           }
    86       }
    87       return instr;
    88     }
    89 
    90     public double Evaluate(IFunctionTree functionTree, int sampleIndex) {
    91       BakedFunctionTree bakedTree = functionTree as BakedFunctionTree;
    92       if (bakedTree == null) throw new ArgumentException("BakedTreeEvaluator can only evaluate BakedFunctionTrees");
    93 
    94       List<LightWeightFunction> linearRepresentation = bakedTree.LinearRepresentation;
    95       codeArr = new Instr[linearRepresentation.Count];
    96       int i = 0;
    97       foreach (LightWeightFunction f in linearRepresentation) {
    98         codeArr[i++] = TranslateToInstr(f);
    99       }
    100 
    101       PC = 0;
    102       this.sampleIndex = sampleIndex;
    103 
    104       double estimated = EvaluateBakedCode();
    105       if (double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    106         estimated = estimatedValueMax;
    107       } else if (estimated > estimatedValueMax) {
    108         estimated = estimatedValueMax;
    109       } else if (estimated < estimatedValueMin) {
    110         estimated = estimatedValueMin;
    111       }
    112       return estimated;
    113     }
    114 
    115     // skips a whole branch
    116     private void SkipBakedCode() {
    117       int i = 1;
    118       while (i > 0) {
    119         i += codeArr[PC++].arity;
    120         i--;
    121       }
    122     }
    123 
    124     private double EvaluateBakedCode() {
     39    protected override double EvaluateBakedCode() {
    12540      Instr currInstr = codeArr[PC++];
    12641      switch (currInstr.symbol) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HL2TreeEvaluator.cs

    r1817 r1836  
    3131namespace HeuristicLab.GP.StructureIdentification {
    3232  /// <summary>
    33   /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node.
     33  /// Evaluates FunctionTrees recursively by interpretation of the function symbols in each node with HL2 semantics.
    3434  /// Not thread-safe!
    3535  /// </summary>
    36   public class HL2TreeEvaluator : ItemBase, ITreeEvaluator {
    37     private const double EPSILON = 1.0e-10;
    38     private double estimatedValueMax;
    39     private double estimatedValueMin;
     36  public class HL2TreeEvaluator : TreeEvaluatorBase {
    4037
    41     private class Instr {
    42       public double d_arg0;
    43       public short i_arg0;
    44       public short i_arg1;
    45       public byte arity;
    46       public byte symbol;
    47       public IFunction function;
    48     }
    49 
    50     private Instr[] codeArr;
    51     private int PC;
    52     private Dataset dataset;
    53     private int sampleIndex;
    54 
    55     public void ResetEvaluator(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
    56       this.dataset = dataset;
    57       double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, start, end);
    58 
    59       // get the mean of the values of the target variable to determine the max and min bounds of the estimated value
    60       double targetMean = dataset.GetMean(targetVariable, start, end);
    61       estimatedValueMin = targetMean - maximumPunishment;
    62       estimatedValueMax = targetMean + maximumPunishment;
    63     }
    64 
    65     private Instr TranslateToInstr(LightWeightFunction f) {
    66       Instr instr = new Instr();
    67       instr.arity = f.arity;
    68       instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
    69       switch (instr.symbol) {
    70         case EvaluatorSymbolTable.DIFFERENTIAL:
    71         case EvaluatorSymbolTable.VARIABLE: {
    72             instr.i_arg0 = (short)f.data[0]; // var
    73             instr.d_arg0 = f.data[1]; // weight
    74             instr.i_arg1 = (short)f.data[2]; // sample-offset
    75             break;
    76           }
    77         case EvaluatorSymbolTable.CONSTANT: {
    78             instr.d_arg0 = f.data[0]; // value
    79             break;
    80           }
    81         case EvaluatorSymbolTable.UNKNOWN: {
    82             instr.function = f.functionType;
    83             break;
    84           }
    85       }
    86       return instr;
    87     }
    88 
    89     public double Evaluate(IFunctionTree functionTree, int sampleIndex) {
    90       BakedFunctionTree bakedTree = functionTree as BakedFunctionTree;
    91       if (bakedTree == null) throw new ArgumentException("HL2Evaluator can only evaluate BakedFunctionTrees");
    92 
    93       List<LightWeightFunction> linearRepresentation = bakedTree.LinearRepresentation;
    94       codeArr = new Instr[linearRepresentation.Count];
    95       int i = 0;
    96       foreach (LightWeightFunction f in linearRepresentation) {
    97         codeArr[i++] = TranslateToInstr(f);
    98       }
    99 
    100       PC = 0;
    101       this.sampleIndex = sampleIndex;
    102 
    103       double estimated = EvaluateBakedCode();
    104       if (double.IsNaN(estimated) || double.IsInfinity(estimated)) {
    105         estimated = estimatedValueMax;
    106       } else if (estimated > estimatedValueMax) {
    107         estimated = estimatedValueMax;
    108       } else if (estimated < estimatedValueMin) {
    109         estimated = estimatedValueMin;
    110       }
    111       return estimated;
    112     }
    113 
    114     // skips a whole branch
    115     private void SkipBakedCode() {
    116       int i = 1;
    117       while (i > 0) {
    118         i += codeArr[PC++].arity;
    119         i--;
    120       }
    121     }
    122 
    123     private double EvaluateBakedCode() {
     38    protected override double EvaluateBakedCode() {
    12439      Instr currInstr = codeArr[PC++];
    12540      switch (currInstr.symbol) {
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj

    r1817 r1836  
    8989    <Compile Include="Constant.cs" />
    9090    <Compile Include="AlgorithmBase.cs" />
     91    <Compile Include="TreeEvaluatorBase.cs" />
    9192    <Compile Include="HL2TreeEvaluator.cs" />
    9293    <Compile Include="HL2TreeEvaluatorInjector.cs" />
Note: See TracChangeset for help on using the changeset viewer.