Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/TreeEvaluatorBase.cs @ 17712

Last change on this file since 17712 was 2578, checked in by gkronber, 15 years ago

Implemented #824 (Refactor: ITreeEvaluator interface to provide a method that evaluates a tree on a range of samples.)

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Core;
25using HeuristicLab.DataAnalysis;
26using HeuristicLab.GP.Interfaces;
27using System.Xml;
28
29namespace HeuristicLab.GP.StructureIdentification {
30  /// <summary>
31  /// Base class for tree evaluators
32  /// </summary>
33  public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
34    protected const double EPSILON = 1.0e-7;
35
36    protected class Instr {
37      public double d_arg0;
38      public short i_arg0;
39      public short i_arg1;
40      public byte arity;
41      public byte symbol;
42    }
43
44    protected Instr[] codeArr;
45    protected int PC;
46    protected Dataset dataset;
47    protected int sampleIndex;
48
49    public double UpperEvaluationLimit { get; set; }
50    public double LowerEvaluationLimit { get; set; }
51
52    public TreeEvaluatorBase() // for persistence
53      : this(double.MinValue, double.MaxValue) {
54    }
55
56    public TreeEvaluatorBase(double minEstimatedValue, double maxEstimatedValue)
57      : base() {
58      UpperEvaluationLimit = maxEstimatedValue;
59      LowerEvaluationLimit = minEstimatedValue;
60    }
61
62    [Obsolete]
63    public virtual void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree) {
64      this.dataset = dataset;
65      codeArr = new Instr[functionTree.GetSize()];
66      int i = 0;
67      foreach (IFunctionTree tree in FunctionTreeIterator.IteratePrefix(functionTree)) {
68        codeArr[i++] = TranslateToInstr(tree);
69      }
70    }
71
72    private Instr TranslateToInstr(IFunctionTree tree) {
73      Instr instr = new Instr();
74      instr.arity = (byte)tree.SubTrees.Count;
75      instr.symbol = EvaluatorSymbolTable.MapFunction(tree.Function);
76      switch (instr.symbol) {
77        case EvaluatorSymbolTable.DIFFERENTIAL:
78        case EvaluatorSymbolTable.VARIABLE: {
79            VariableFunctionTree varTree = (VariableFunctionTree)tree;
80            instr.i_arg0 = (short)dataset.GetVariableIndex(varTree.VariableName);
81            instr.d_arg0 = varTree.Weight;
82            instr.i_arg1 = (short)varTree.SampleOffset;
83            break;
84          }
85        case EvaluatorSymbolTable.CONSTANT: {
86            ConstantFunctionTree constTree = (ConstantFunctionTree)tree;
87            instr.d_arg0 = constTree.Value;
88            break;
89          }
90        case EvaluatorSymbolTable.UNKNOWN: {
91            throw new NotSupportedException("Unknown function symbol: " + instr.symbol);
92          }
93      }
94      return instr;
95    }
96
97    [Obsolete]
98    public virtual double Evaluate(int sampleIndex) {
99      PC = 0;
100      this.sampleIndex = sampleIndex;
101
102      double estimated = Math.Min(Math.Max(EvaluateBakedCode(), LowerEvaluationLimit), UpperEvaluationLimit);
103      if (double.IsNaN(estimated)) estimated = UpperEvaluationLimit;
104      return estimated;
105    }
106
107    public virtual IEnumerable<double> Evaluate(Dataset dataset, IFunctionTree tree, IEnumerable<int> rows) {
108      PrepareForEvaluation(dataset, tree);
109      foreach (int row in rows)
110        yield return Evaluate(row);
111    }
112
113    // skips a whole branch
114    protected void SkipBakedCode() {
115      int i = 1;
116      while (i > 0) {
117        i += codeArr[PC++].arity;
118        i--;
119      }
120    }
121
122    protected abstract double EvaluateBakedCode();
123
124    public override object Clone(IDictionary<Guid, object> clonedObjects) {
125      TreeEvaluatorBase clone = (TreeEvaluatorBase)base.Clone(clonedObjects);
126      clone.UpperEvaluationLimit = UpperEvaluationLimit;
127      clone.LowerEvaluationLimit = LowerEvaluationLimit;
128      return clone;
129    }
130
131    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
132      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
133      XmlAttribute maxValueAttribute = document.CreateAttribute("UpperEvaluationLimit");
134      XmlAttribute minValueAttribute = document.CreateAttribute("LowerEvaluationLimit");
135      maxValueAttribute.Value = XmlConvert.ToString(UpperEvaluationLimit);
136      minValueAttribute.Value = XmlConvert.ToString(LowerEvaluationLimit);
137      node.Attributes.Append(minValueAttribute);
138      node.Attributes.Append(maxValueAttribute);
139      return node;
140    }
141
142    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
143      base.Populate(node, restoredObjects);
144      LowerEvaluationLimit = XmlConvert.ToDouble(node.Attributes["LowerEvaluationLimit"].Value);
145      UpperEvaluationLimit = XmlConvert.ToDouble(node.Attributes["UpperEvaluationLimit"].Value);
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.