Free cookie consent management tool by TermsFeed Policy Generator

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

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

this is the remaining part of changeset r2327.
Applied changes in modeling plugins that are necessary for the new model analyzer (#722)

  • predictor has properties for the lower and upper limit of the predicted value
  • added views for predictors that show the limits (also added a new view for GeneticProgrammingModel that shows the size and height of the model)
  • Reintroduced TreeEvaluatorInjectors that read a PunishmentFactor and calculate the lower and upper limits for estimated values (limits are set in the tree evaluators)
  • Added operators to create Predictors. Changed modeling algorithms to use the predictors for the calculation of final model qualities and variable impacts (to be compatible with the new model analyzer the predictors use a very large PunishmentFactor)
  • replaced all private implementations of double.IsAlmost and use HL.Commons instead (see #733 r2324)
  • Implemented operator SolutionExtractor and moved BestSolutionStorer from HL.Logging to HL.Modeling (fixes #734)
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    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 IteratePrefix(functionTree)) {
67        codeArr[i++] = TranslateToInstr(tree);
68      }
69    }
70
71    private IEnumerable<IFunctionTree> IteratePrefix(IFunctionTree functionTree) {
72      List<IFunctionTree> prefixForm = new List<IFunctionTree>();
73      prefixForm.Add(functionTree);
74      foreach (IFunctionTree subTree in functionTree.SubTrees) {
75        prefixForm.AddRange(IteratePrefix(subTree));
76      }
77      return prefixForm;
78    }
79
80    private Instr TranslateToInstr(IFunctionTree tree) {
81      Instr instr = new Instr();
82      instr.arity = (byte)tree.SubTrees.Count;
83      instr.symbol = EvaluatorSymbolTable.MapFunction(tree.Function);
84      switch (instr.symbol) {
85        case EvaluatorSymbolTable.DIFFERENTIAL:
86        case EvaluatorSymbolTable.VARIABLE: {
87            VariableFunctionTree varTree = (VariableFunctionTree)tree;
88            instr.i_arg0 = (short)dataset.GetVariableIndex(varTree.VariableName);
89            instr.d_arg0 = varTree.Weight;
90            instr.i_arg1 = (short)varTree.SampleOffset;
91            break;
92          }
93        case EvaluatorSymbolTable.CONSTANT: {
94            ConstantFunctionTree constTree = (ConstantFunctionTree)tree;
95            instr.d_arg0 = constTree.Value;
96            break;
97          }
98        case EvaluatorSymbolTable.UNKNOWN: {
99            throw new NotSupportedException("Unknown function symbol: " + instr.symbol);
100          }
101      }
102      return instr;
103    }
104
105    public double Evaluate(int sampleIndex) {
106      PC = 0;
107      this.sampleIndex = sampleIndex;
108
109      double estimated = Math.Min(Math.Max(EvaluateBakedCode(), LowerEvaluationLimit), UpperEvaluationLimit);
110      if (double.IsNaN(estimated)) estimated = UpperEvaluationLimit;
111      return estimated;
112    }
113
114    // skips a whole branch
115    protected void SkipBakedCode() {
116      int i = 1;
117      while (i > 0) {
118        i += codeArr[PC++].arity;
119        i--;
120      }
121    }
122
123    protected abstract double EvaluateBakedCode();
124
125    public override object Clone(IDictionary<Guid, object> clonedObjects) {
126      TreeEvaluatorBase clone = (TreeEvaluatorBase)base.Clone(clonedObjects);
127      clone.UpperEvaluationLimit = UpperEvaluationLimit;
128      clone.LowerEvaluationLimit = LowerEvaluationLimit;
129      return clone;
130    }
131
132    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
133      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
134      XmlAttribute maxValueAttribute = document.CreateAttribute("UpperEvaluationLimit");
135      XmlAttribute minValueAttribute = document.CreateAttribute("LowerEvaluationLimit");
136      maxValueAttribute.Value = XmlConvert.ToString(UpperEvaluationLimit);
137      minValueAttribute.Value = XmlConvert.ToString(LowerEvaluationLimit);
138      node.Attributes.Append(minValueAttribute);
139      node.Attributes.Append(maxValueAttribute);
140      return node;
141    }
142
143    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
144      base.Populate(node, restoredObjects);
145      LowerEvaluationLimit = XmlConvert.ToDouble(node.Attributes["LowerEvaluationLimit"].Value);
146      UpperEvaluationLimit = XmlConvert.ToDouble(node.Attributes["UpperEvaluationLimit"].Value);
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.