Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/BaseClasses/TreeEvaluatorBase.cs @ 2494

Last change on this file since 2494 was 2364, checked in by mkommend, 15 years ago

added FunctionTreeIterator to HeuristicLab.GP.Interfaces (ticket #749)

File size: 5.1 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    public void PrepareForEvaluation(Dataset dataset, IFunctionTree functionTree) {
63      this.dataset = dataset;
64      codeArr = new Instr[functionTree.GetSize()];
65      int i = 0;
66      foreach (IFunctionTree tree in FunctionTreeIterator.IteratePrefix(functionTree)) {
67        codeArr[i++] = TranslateToInstr(tree);
68      }
69    }
70
71    private Instr TranslateToInstr(IFunctionTree tree) {
72      Instr instr = new Instr();
73      instr.arity = (byte)tree.SubTrees.Count;
74      instr.symbol = EvaluatorSymbolTable.MapFunction(tree.Function);
75      switch (instr.symbol) {
76        case EvaluatorSymbolTable.DIFFERENTIAL:
77        case EvaluatorSymbolTable.VARIABLE: {
78            VariableFunctionTree varTree = (VariableFunctionTree)tree;
79            instr.i_arg0 = (short)dataset.GetVariableIndex(varTree.VariableName);
80            instr.d_arg0 = varTree.Weight;
81            instr.i_arg1 = (short)varTree.SampleOffset;
82            break;
83          }
84        case EvaluatorSymbolTable.CONSTANT: {
85            ConstantFunctionTree constTree = (ConstantFunctionTree)tree;
86            instr.d_arg0 = constTree.Value;
87            break;
88          }
89        case EvaluatorSymbolTable.UNKNOWN: {
90            throw new NotSupportedException("Unknown function symbol: " + instr.symbol);
91          }
92      }
93      return instr;
94    }
95
96    public double Evaluate(int sampleIndex) {
97      PC = 0;
98      this.sampleIndex = sampleIndex;
99
100      double estimated = Math.Min(Math.Max(EvaluateBakedCode(), LowerEvaluationLimit), UpperEvaluationLimit);
101      if (double.IsNaN(estimated)) estimated = UpperEvaluationLimit;
102      return estimated;
103    }
104
105    // skips a whole branch
106    protected void SkipBakedCode() {
107      int i = 1;
108      while (i > 0) {
109        i += codeArr[PC++].arity;
110        i--;
111      }
112    }
113
114    protected abstract double EvaluateBakedCode();
115
116    public override object Clone(IDictionary<Guid, object> clonedObjects) {
117      TreeEvaluatorBase clone = (TreeEvaluatorBase)base.Clone(clonedObjects);
118      clone.UpperEvaluationLimit = UpperEvaluationLimit;
119      clone.LowerEvaluationLimit = LowerEvaluationLimit;
120      return clone;
121    }
122
123    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
124      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
125      XmlAttribute maxValueAttribute = document.CreateAttribute("UpperEvaluationLimit");
126      XmlAttribute minValueAttribute = document.CreateAttribute("LowerEvaluationLimit");
127      maxValueAttribute.Value = XmlConvert.ToString(UpperEvaluationLimit);
128      minValueAttribute.Value = XmlConvert.ToString(LowerEvaluationLimit);
129      node.Attributes.Append(minValueAttribute);
130      node.Attributes.Append(maxValueAttribute);
131      return node;
132    }
133
134    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
135      base.Populate(node, restoredObjects);
136      LowerEvaluationLimit = XmlConvert.ToDouble(node.Attributes["LowerEvaluationLimit"].Value);
137      UpperEvaluationLimit = XmlConvert.ToDouble(node.Attributes["UpperEvaluationLimit"].Value);
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.