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