[1836] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.DataAnalysis;
|
---|
[2210] | 26 | using HeuristicLab.GP.Interfaces;
|
---|
[1836] | 27 |
|
---|
| 28 | namespace HeuristicLab.GP.StructureIdentification {
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Base class for tree evaluators
|
---|
| 31 | /// </summary>
|
---|
| 32 | public abstract class TreeEvaluatorBase : ItemBase, ITreeEvaluator {
|
---|
| 33 | protected const double EPSILON = 1.0e-7;
|
---|
[2034] | 34 | protected double maxValue;
|
---|
| 35 | protected double minValue;
|
---|
[1836] | 36 |
|
---|
| 37 | protected class Instr {
|
---|
| 38 | public double d_arg0;
|
---|
| 39 | public short i_arg0;
|
---|
| 40 | public short i_arg1;
|
---|
| 41 | public byte arity;
|
---|
| 42 | public byte symbol;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | protected Instr[] codeArr;
|
---|
| 46 | protected int PC;
|
---|
| 47 | protected Dataset dataset;
|
---|
| 48 | protected int sampleIndex;
|
---|
| 49 |
|
---|
[2034] | 50 | public void PrepareForEvaluation(Dataset dataset, int targetVariable, int start, int end, double punishmentFactor, IFunctionTree functionTree) {
|
---|
[1836] | 51 | this.dataset = dataset;
|
---|
[2034] | 52 | // calculate upper and lower bounds for the estimated value (mean +/- punishmentFactor * range)
|
---|
| 53 | double mean = dataset.GetMean(targetVariable, start, end);
|
---|
| 54 | double range = dataset.GetRange(targetVariable, start, end);
|
---|
| 55 | maxValue = mean + punishmentFactor * range;
|
---|
| 56 | minValue = mean - punishmentFactor * range;
|
---|
[1836] | 57 |
|
---|
[2210] | 58 | codeArr = new Instr[functionTree.GetSize()];
|
---|
[1891] | 59 | int i = 0;
|
---|
[2210] | 60 | foreach (IFunctionTree tree in IteratePrefix(functionTree)) {
|
---|
| 61 | codeArr[i++] = TranslateToInstr(tree);
|
---|
[1891] | 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[2210] | 65 | private IEnumerable<IFunctionTree> IteratePrefix(IFunctionTree functionTree) {
|
---|
| 66 | List<IFunctionTree> prefixForm = new List<IFunctionTree>();
|
---|
| 67 | prefixForm.Add(functionTree);
|
---|
| 68 | foreach (IFunctionTree subTree in functionTree.SubTrees) {
|
---|
| 69 | prefixForm.AddRange(IteratePrefix(subTree));
|
---|
| 70 | }
|
---|
| 71 | return prefixForm;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private Instr TranslateToInstr(IFunctionTree tree) {
|
---|
[1836] | 75 | Instr instr = new Instr();
|
---|
[2210] | 76 | instr.arity = (byte)tree.SubTrees.Count;
|
---|
| 77 | instr.symbol = EvaluatorSymbolTable.MapFunction(tree.Function);
|
---|
[1836] | 78 | switch (instr.symbol) {
|
---|
| 79 | case EvaluatorSymbolTable.DIFFERENTIAL:
|
---|
| 80 | case EvaluatorSymbolTable.VARIABLE: {
|
---|
[2210] | 81 | VariableFunctionTree varTree = (VariableFunctionTree)tree;
|
---|
| 82 | instr.i_arg0 = (short)dataset.GetVariableIndex(varTree.VariableName);
|
---|
| 83 | instr.d_arg0 = varTree.Weight;
|
---|
| 84 | instr.i_arg1 = (short)varTree.SampleOffset;
|
---|
[1836] | 85 | break;
|
---|
| 86 | }
|
---|
| 87 | case EvaluatorSymbolTable.CONSTANT: {
|
---|
[2210] | 88 | ConstantFunctionTree constTree = (ConstantFunctionTree)tree;
|
---|
| 89 | instr.d_arg0 = constTree.Value;
|
---|
[1836] | 90 | break;
|
---|
| 91 | }
|
---|
| 92 | case EvaluatorSymbolTable.UNKNOWN: {
|
---|
[2210] | 93 | throw new NotSupportedException("Unknown function symbol: " + instr.symbol);
|
---|
[1836] | 94 | }
|
---|
| 95 | }
|
---|
| 96 | return instr;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[1891] | 99 | public double Evaluate(int sampleIndex) {
|
---|
[1836] | 100 | PC = 0;
|
---|
| 101 | this.sampleIndex = sampleIndex;
|
---|
| 102 |
|
---|
| 103 | double estimated = EvaluateBakedCode();
|
---|
[2034] | 104 | if (double.IsNaN(estimated) || double.IsInfinity(estimated)) estimated = maxValue;
|
---|
| 105 | else if (estimated < minValue) estimated = minValue;
|
---|
| 106 | else if (estimated > maxValue) estimated = maxValue;
|
---|
[1836] | 107 | return estimated;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | // skips a whole branch
|
---|
| 111 | protected void SkipBakedCode() {
|
---|
| 112 | int i = 1;
|
---|
| 113 | while (i > 0) {
|
---|
| 114 | i += codeArr[PC++].arity;
|
---|
| 115 | i--;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | protected abstract double EvaluateBakedCode();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|