Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.GP.StructureIdentification/3.3/LinearScalingPredictorBuilder.cs @ 5955

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

Moved linear scaling functionality out of tree evaluator into a separate operator. #823 (Implement tree evaluator with linear scaling to improve convergence in symbolic regression.)

File size: 5.0 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 System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.GP.Interfaces;
29using HeuristicLab.Modeling;
30using HeuristicLab.DataAnalysis;
31using HeuristicLab.Common;
32namespace HeuristicLab.GP.StructureIdentification {
33  public class LinearScalingPredictorBuilder : OperatorBase {
34    public LinearScalingPredictorBuilder()
35      : base() {
36      AddVariableInfo(new VariableInfo("FunctionTree", "The function tree", typeof(IGeneticProgrammingModel), VariableKind.In));
37      AddVariableInfo(new VariableInfo("Beta", "Beta parameter for linear scaling as calculated by LinearScaler", typeof(DoubleData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("Alpha", "Alpha parameter for linear scaling as calculated by LinearScaler", typeof(DoubleData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("UpperEstimationLimit", "Upper limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("LowerEstimationLimit", "Lower limit for estimated value (optional)", typeof(DoubleData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Predictor", "The predictor combines the function tree and the evaluator and can be used to generate estimated values", typeof(IPredictor), VariableKind.New));
42    }
43
44    public override string Description {
45      get { return "Extracts the function tree scales the output of the tree and combines the scaled tree with a HL3TreeEvaluator to a predictor for the model analyzer."; }
46    }
47
48    public override IOperation Apply(IScope scope) {
49      IGeneticProgrammingModel model = GetVariableValue<IGeneticProgrammingModel>("FunctionTree", scope, true);
50      //double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data;
51      //Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
52      //int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
53      //int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
54      //string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
55      double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data;
56      double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data;
57      DoubleData lowerLimit = GetVariableValue<DoubleData>("LowerEstimationLimit", scope, true, false);
58      DoubleData upperLimit = GetVariableValue<DoubleData>("UpperEstimationLimit", scope, true, false);
59      IPredictor predictor;
60      if (lowerLimit == null || upperLimit == null)
61        predictor = CreatePredictor(model, beta, alpha, double.NegativeInfinity, double.PositiveInfinity);
62      else
63        predictor = CreatePredictor(model, beta, alpha, lowerLimit.Data, upperLimit.Data);
64      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("Predictor"), predictor));
65      return null;
66    }
67
68    public static IPredictor CreatePredictor(IGeneticProgrammingModel model, double beta, double alpha, double lowerLimit, double upperLimit) {
69
70      var evaluator = new HL3TreeEvaluator();
71      evaluator.LowerEvaluationLimit = lowerLimit;
72      evaluator.UpperEvaluationLimit = upperLimit;
73      var resultModel = new GeneticProgrammingModel(MakeSum(MakeProduct(model.FunctionTree, beta), alpha));
74      return new Predictor(evaluator, resultModel, lowerLimit, upperLimit);
75    }
76
77    private static IFunctionTree MakeSum(IFunctionTree tree, double x) {
78      if (x.IsAlmost(0.0)) return tree;
79      var sum = (new Addition()).GetTreeNode();
80      sum.AddSubTree(tree);
81      sum.AddSubTree(MakeConstant(x));
82      return sum;
83    }
84
85    private static IFunctionTree MakeProduct(IFunctionTree tree, double a) {
86      if (a.IsAlmost(1.0)) return tree;
87      var prod = (new Multiplication()).GetTreeNode();
88      prod.AddSubTree(tree);
89      prod.AddSubTree(MakeConstant(a));
90      return prod;
91    }
92
93    private static IFunctionTree MakeConstant(double x) {
94      var constX = (ConstantFunctionTree)(new Constant()).GetTreeNode();
95      constX.Value = x;
96      return constX;
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.