[3651] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
[4068] | 24 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
[3651] | 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).
|
---|
| 33 | /// </summary>
|
---|
| 34 | [Item("SymbolicRegressionSolutionLinearScaler", "An operator that creates a linearly transformed symbolic regression solution (given alpha and beta).")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public sealed class SymbolicRegressionSolutionLinearScaler : SingleSuccessorOperator {
|
---|
| 37 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 38 | private const string ScaledSymbolicExpressionTreeParameterName = "ScaledSymbolicExpressionTree";
|
---|
| 39 | private const string AlphaParameterName = "Alpha";
|
---|
| 40 | private const string BetaParameterName = "Beta";
|
---|
| 41 |
|
---|
[3710] | 42 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 43 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
[3651] | 44 | }
|
---|
[3710] | 45 | public ILookupParameter<SymbolicExpressionTree> ScaledSymbolicExpressionTreeParameter {
|
---|
| 46 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[ScaledSymbolicExpressionTreeParameterName]; }
|
---|
[3651] | 47 | }
|
---|
[3710] | 48 | public ILookupParameter<DoubleValue> AlphaParameter {
|
---|
| 49 | get { return (ILookupParameter<DoubleValue>)Parameters[AlphaParameterName]; }
|
---|
[3651] | 50 | }
|
---|
[3710] | 51 | public ILookupParameter<DoubleValue> BetaParameter {
|
---|
| 52 | get { return (ILookupParameter<DoubleValue>)Parameters[BetaParameterName]; }
|
---|
[3651] | 53 | }
|
---|
| 54 |
|
---|
| 55 | public SymbolicRegressionSolutionLinearScaler()
|
---|
| 56 | : base() {
|
---|
[3710] | 57 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to transform."));
|
---|
| 58 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(ScaledSymbolicExpressionTreeParameterName, "The resulting symbolic expression trees after transformation."));
|
---|
| 59 | Parameters.Add(new LookupParameter<DoubleValue>(AlphaParameterName, "Alpha parameter for linear transformation."));
|
---|
| 60 | Parameters.Add(new LookupParameter<DoubleValue>(BetaParameterName, "Beta parameter for linear transformation."));
|
---|
[3651] | 61 | }
|
---|
| 62 |
|
---|
| 63 | public override IOperation Apply() {
|
---|
[3710] | 64 | SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 65 | DoubleValue alpha = AlphaParameter.ActualValue;
|
---|
| 66 | DoubleValue beta = BetaParameter.ActualValue;
|
---|
[3801] | 67 | if (alpha != null && beta != null) {
|
---|
[3986] | 68 | ScaledSymbolicExpressionTreeParameter.ActualValue = Scale(tree, alpha.Value, beta.Value);
|
---|
[3801] | 69 | } else {
|
---|
| 70 | // alpha or beta parameter not available => do not scale tree
|
---|
| 71 | ScaledSymbolicExpressionTreeParameter.ActualValue = tree;
|
---|
| 72 | }
|
---|
[3986] | 73 |
|
---|
[3651] | 74 | return base.Apply();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[3986] | 77 | public static SymbolicExpressionTree Scale(SymbolicExpressionTree original, double alpha, double beta) {
|
---|
| 78 | var mainBranch = original.Root.SubTrees[0].SubTrees[0];
|
---|
| 79 | var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
|
---|
| 80 |
|
---|
| 81 | // remove the main branch before cloning to prevent cloning of sub-trees
|
---|
| 82 | original.Root.SubTrees[0].RemoveSubTree(0);
|
---|
| 83 | var scaledTree = (SymbolicExpressionTree)original.Clone();
|
---|
| 84 | // insert main branch into the original tree again
|
---|
| 85 | original.Root.SubTrees[0].InsertSubTree(0, mainBranch);
|
---|
| 86 | // insert the scaled main branch into the cloned tree
|
---|
| 87 | scaledTree.Root.SubTrees[0].InsertSubTree(0, scaledMainBranch);
|
---|
| 88 | return scaledTree;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | private static SymbolicExpressionTreeNode MakeSum(SymbolicExpressionTreeNode treeNode, double alpha) {
|
---|
[3651] | 92 | var node = (new Addition()).CreateTreeNode();
|
---|
| 93 | var alphaConst = MakeConstant(alpha);
|
---|
| 94 | node.AddSubTree(treeNode);
|
---|
| 95 | node.AddSubTree(alphaConst);
|
---|
| 96 | return node;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[3986] | 99 | private static SymbolicExpressionTreeNode MakeProduct(double beta, SymbolicExpressionTreeNode treeNode) {
|
---|
[3651] | 100 | var node = (new Multiplication()).CreateTreeNode();
|
---|
| 101 | var betaConst = MakeConstant(beta);
|
---|
| 102 | node.AddSubTree(treeNode);
|
---|
| 103 | node.AddSubTree(betaConst);
|
---|
| 104 | return node;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[3986] | 107 | private static SymbolicExpressionTreeNode MakeConstant(double c) {
|
---|
[3651] | 108 | var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
|
---|
| 109 | node.Value = c;
|
---|
| 110 | return node;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|