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