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