Free cookie consent management tool by TermsFeed Policy Generator

Changeset 396 for trunk/sources


Ignore:
Timestamp:
07/28/08 18:46:02 (16 years ago)
Author:
gkronber
Message:

fixed ticket #205 by creating the function-specific evaluator in the evaluation operators.

Location:
trunk/sources
Files:
1 added
13 edited

Legend:

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

    r363 r396  
    4848  class BakedFunctionTree : ItemBase, IFunctionTree {
    4949    private List<LightWeightFunction> linearRepresentation;
     50    internal List<LightWeightFunction> LinearRepresentation {
     51      get {
     52        FlattenVariables();
     53        FlattenTrees();
     54        return linearRepresentation;
     55      }
     56    }
    5057    private bool treesExpanded = false;
    5158    private List<IFunctionTree> subTrees;
    5259    private bool variablesExpanded = false;
    5360    private List<IVariable> variables;
     61    private BakedTreeEvaluator evaluator;
    5462
    5563    public BakedFunctionTree() {
     
    262270    }
    263271
    264     public void PrepareEvaluation(Dataset dataset) {
    265       FlattenVariables();
    266       FlattenTrees();
    267       BakedTreeEvaluator.ResetEvaluator(dataset, linearRepresentation);
    268     }
    269 
    270     public double Evaluate(int sampleIndex) {
    271       return BakedTreeEvaluator.Evaluate(sampleIndex);
    272     }
    273 
     272    public IEvaluator CreateEvaluator(Dataset dataset) {
     273      return new BakedTreeEvaluator(dataset);
     274    }
    274275
    275276    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
  • trunk/sources/HeuristicLab.Functions/BakedTreeEvaluator.cs

    r365 r396  
    2929
    3030namespace HeuristicLab.Functions {
    31   internal static class BakedTreeEvaluator {
     31  internal class BakedTreeEvaluator : IEvaluator {
    3232    private const int MAX_TREE_SIZE = 4096;
    3333
     
    4040    }
    4141
    42     private static Instr[] codeArr;
    43     private static int PC;
    44     private static Dataset dataset;
    45     private static int sampleIndex;
    46 
    47 
    48     static BakedTreeEvaluator() {
     42    private Instr[] codeArr;
     43    private int PC;
     44    private Dataset dataset;
     45    private int sampleIndex;
     46
     47
     48    public BakedTreeEvaluator(Dataset dataset) {
     49      this.dataset = dataset;
    4950      codeArr = new Instr[MAX_TREE_SIZE];
    5051      for(int i = 0; i < MAX_TREE_SIZE; i++) {
     
    5354    }
    5455
    55     public static void ResetEvaluator(Dataset dataset, List<LightWeightFunction> linearRepresentation) {
     56    public void ResetEvaluator(IFunctionTree functionTree) {
     57      List<LightWeightFunction> linearRepresentation = ((BakedFunctionTree)functionTree).LinearRepresentation;
    5658      int i = 0;
    57       BakedTreeEvaluator.dataset = dataset;
    5859      foreach(LightWeightFunction f in linearRepresentation) {
    5960        TranslateToInstr(f, codeArr[i++]);
     
    6162    }
    6263
    63     private static Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
     64    private Instr TranslateToInstr(LightWeightFunction f, Instr instr) {
    6465      instr.arity = f.arity;
    6566      instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
     
    7980    }
    8081
    81     internal static double Evaluate(int sampleIndex) {
     82    public double Evaluate(int sampleIndex) {
    8283      PC = 0;
    83       BakedTreeEvaluator.sampleIndex = sampleIndex;
     84      this.sampleIndex = sampleIndex;
    8485      return EvaluateBakedCode();
    8586    }
    8687
    87     private static double EvaluateBakedCode() {
     88    private double EvaluateBakedCode() {
    8889      Instr currInstr = codeArr[PC++];
    8990      switch(currInstr.symbol) {
  • trunk/sources/HeuristicLab.Functions/HeuristicLab.Functions.csproj

    r365 r396  
    5858    <Compile Include="Differential.cs" />
    5959    <Compile Include="GreaterThan.cs" />
     60    <Compile Include="IEvaluator.cs" />
    6061    <Compile Include="IfThenElse.cs">
    6162      <SubType>Code</SubType>
  • trunk/sources/HeuristicLab.Functions/IFunctionTree.cs

    r363 r396  
    4040    void RemoveSubTree(int index);
    4141
    42     void PrepareEvaluation(Dataset dataset);
    43     double Evaluate(int sampleIndex);
     42    IEvaluator CreateEvaluator(Dataset dataset);
    4443  }
    4544}
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs

    r367 r396  
    4949      double originalDeviationTotalSumOfSquares = 0.0;
    5050      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    51       functionTree.PrepareEvaluation(dataset);
    5251      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    53         double estimated = functionTree.Evaluate(sample);
     52        double estimated = evaluator.Evaluate(sample);
    5453        double original = dataset.GetValue(sample, targetVariable);
    5554        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/EarlyStoppingMeanSquaredErrorEvaluator.cs

    r367 r396  
    6060      double errorsSquaredSum = 0;
    6161      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    62       functionTree.PrepareEvaluation(dataset);
    6362      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    64         double estimated = functionTree.Evaluate(sample);
     63        double estimated = evaluator.Evaluate(sample);
    6564        double original = dataset.GetValue(sample, targetVariable);
    6665        if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/GPEvaluatorBase.cs

    r363 r396  
    3535    protected int treeSize;
    3636    protected double totalEvaluatedNodes;
     37    protected IEvaluator evaluator;
    3738
    3839    public GPEvaluatorBase()
     
    5657      this.treeSize = scope.GetVariableValue<IntData>("TreeSize", false).Data;
    5758      this.totalEvaluatedNodes = scope.GetVariableValue<DoubleData>("TotalEvaluatedNodes", true).Data;
     59      if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset);
     60      evaluator.ResetEvaluator(functionTree);
    5861      double result = Evaluate(scope, functionTree, targetVariable, dataset);
    5962
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MCCEvaluator.cs

    r367 r396  
    5858      double negative = 0;
    5959      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    60       functionTree.PrepareEvaluation(dataset);
    6160      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    62         double est = functionTree.Evaluate(sample);
     61        double est = evaluator.Evaluate(sample);
    6362        double orig = dataset.GetValue(sample, targetVariable);
    6463        if(double.IsNaN(est) || double.IsInfinity(est)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanAbsolutePercentageErrorEvaluator.cs

    r395 r396  
    4747      int trainingEnd = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
    4848      double errorsSum = 0.0;
    49       functionTree.PrepareEvaluation(dataset);
    5049      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    51         double estimated = functionTree.Evaluate(sample);
     50        double estimated = evaluator.Evaluate(sample);
    5251        double original = dataset.GetValue(sample, targetVariable);
    5352        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/MeanSquaredErrorEvaluator.cs

    r367 r396  
    5959        }
    6060      }
    61 
    62       functionTree.PrepareEvaluation(dataset);
    6361      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    64         double estimated = functionTree.Evaluate(sample);
     62        double estimated = evaluator.Evaluate(sample);
    6563        double original = dataset.GetValue(sample, targetVariable);
    6664        if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/SimpleEvaluator.cs

    r363 r396  
    3434    protected int treeSize;
    3535    protected double totalEvaluatedNodes;
     36    private IEvaluator evaluator;
    3637
    3738    public SimpleEvaluator()
     
    6667      }
    6768      values.Clear();
    68       functionTree.PrepareEvaluation(dataset);
     69      if(evaluator == null) evaluator = functionTree.CreateEvaluator(dataset);
     70      evaluator.ResetEvaluator(functionTree);
    6971      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    7072        ItemList row = new ItemList();
    71         row.Add(new DoubleData(functionTree.Evaluate(sample)));
     73        row.Add(new DoubleData(evaluator.Evaluate(sample)));
    7274        row.Add(new DoubleData(dataset.GetValue(sample, targetVariable)));
    7375        values.Add(row);
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/TheilInequalityCoefficientEvaluator.cs

    r395 r396  
    5151      double estimatedSquaredSum = 0.0;
    5252      double originalSquaredSum = 0.0;
    53       functionTree.PrepareEvaluation(dataset);
    5453      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    5554        double prevValue = 0.0;
    5655        if(difference) prevValue = dataset.GetValue(sample - 1, targetVariable);
    57         double estimatedChange = functionTree.Evaluate(sample) - prevValue;
     56        double estimatedChange = evaluator.Evaluate(sample) - prevValue;
    5857        double originalChange = dataset.GetValue(sample, targetVariable) - prevValue;
    5958        if(!double.IsNaN(originalChange) && !double.IsInfinity(originalChange)) {
  • trunk/sources/HeuristicLab.StructureIdentification/Evaluation/VarianceAccountedForEvaluator.cs

    r367 r396  
    5959      double[] originalTargetVariableValues = new double[trainingEnd-trainingStart];
    6060      double targetMean = dataset.GetMean(targetVariable, trainingStart, trainingEnd);
    61       functionTree.PrepareEvaluation(dataset);
    6261      for(int sample = trainingStart; sample < trainingEnd; sample++) {
    63         double estimated = functionTree.Evaluate(sample);
     62        double estimated = evaluator.Evaluate(sample);
    6463        double original = dataset.GetValue(sample, targetVariable);
    6564        if(!double.IsNaN(original) && !double.IsInfinity(original)) {
Note: See TracChangeset for help on using the changeset viewer.