[10845] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11594] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10845] | 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 |
|
---|
[10846] | 22 | using System;
|
---|
[10845] | 23 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 24 |
|
---|
| 25 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
[10872] | 26 | public class TransformationToSymbolicTreeMapper : ITransformationMapper<ISymbolicExpressionTreeNode> {
|
---|
[10846] | 27 | private ITransformation transformation;
|
---|
| 28 | private string column;
|
---|
| 29 |
|
---|
[10845] | 30 | #region ITransformationMapper<ISymbolicExpressionTree> Members
|
---|
| 31 |
|
---|
[10872] | 32 | public ISymbolicExpressionTreeNode GenerateModel(ITransformation transformation) {
|
---|
[10846] | 33 | InitComponents(transformation);
|
---|
[10845] | 34 |
|
---|
| 35 | if (transformation is LinearTransformation) {
|
---|
[10846] | 36 | return GenerateModelForLinearTransformation();
|
---|
[10845] | 37 | } else if (transformation is ExponentialTransformation) {
|
---|
[10848] | 38 | return GenerateModelForExponentialTransformation();
|
---|
[10845] | 39 | } else if (transformation is LogarithmicTransformation) {
|
---|
[10853] | 40 | return GenerateModelForLogarithmicTransformation();
|
---|
[10845] | 41 | } else if (transformation is PowerTransformation) {
|
---|
[10853] | 42 | return GenerateModelForPowerTransformation();
|
---|
[10845] | 43 | } else if (transformation is ReciprocalTransformation) {
|
---|
[10853] | 44 | return GenerateModelForReciprocalTransformation();
|
---|
[10845] | 45 | } else if (transformation is ShiftStandardDistributionTransformation) {
|
---|
[10853] | 46 | return GenerateModelForShiftStandardDistributionTransformation();
|
---|
[10940] | 47 | } else if (transformation is CopyColumnTransformation) {
|
---|
[10943] | 48 | return GenerateTreeNodeForCopyColumnTransformation();
|
---|
[10845] | 49 | }
|
---|
[10846] | 50 | throw new NotImplementedException();
|
---|
[10845] | 51 | }
|
---|
| 52 |
|
---|
[10872] | 53 | public ISymbolicExpressionTreeNode GenerateInverseModel(ITransformation transformation) {
|
---|
[10846] | 54 | InitComponents(transformation);
|
---|
[10845] | 55 |
|
---|
| 56 | if (transformation is LinearTransformation) {
|
---|
[10848] | 57 | return GenerateInverseModelForLinearTransformation();
|
---|
[10845] | 58 | } else if (transformation is ExponentialTransformation) {
|
---|
[10848] | 59 | return GenerateInverseModelForExponentialTransformation();
|
---|
[10845] | 60 | } else if (transformation is LogarithmicTransformation) {
|
---|
[10853] | 61 | return GenerateInverseModelForLogarithmicTransformation();
|
---|
[10845] | 62 | } else if (transformation is PowerTransformation) {
|
---|
[10853] | 63 | return GenerateInverseModelForPowerTransformation();
|
---|
[10845] | 64 | } else if (transformation is ReciprocalTransformation) {
|
---|
[10853] | 65 | return GenerateInverseModelForReciprocalTransformation();
|
---|
[10845] | 66 | } else if (transformation is ShiftStandardDistributionTransformation) {
|
---|
[10866] | 67 | GenerateInverseModelForShiftStandardDistributionTransformation();
|
---|
[10943] | 68 | } else if (transformation is CopyColumnTransformation) {
|
---|
| 69 | return GenerateTreeNodeForCopyColumnTransformation();
|
---|
[10845] | 70 | }
|
---|
| 71 |
|
---|
[10846] | 72 | throw new NotImplementedException();
|
---|
[10845] | 73 | }
|
---|
| 74 |
|
---|
| 75 | #endregion
|
---|
| 76 |
|
---|
[10853] | 77 | // helper
|
---|
| 78 |
|
---|
[10872] | 79 | private ISymbolicExpressionTreeNode GenerateModelForLinearTransformation() {
|
---|
[10846] | 80 | var linearTransformation = (LinearTransformation)transformation;
|
---|
| 81 | var kValue = linearTransformation.Multiplier;
|
---|
| 82 | var dValue = linearTransformation.Addend;
|
---|
| 83 |
|
---|
| 84 | // k * x
|
---|
| 85 | var multiplicationNode = new Multiplication().CreateTreeNode();
|
---|
[10866] | 86 | var kNode = CreateConstantTreeNode("k", kValue);
|
---|
| 87 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
[10846] | 88 | multiplicationNode.AddSubtree(kNode);
|
---|
| 89 | multiplicationNode.AddSubtree(xNode);
|
---|
| 90 |
|
---|
| 91 | // ( k * x ) + d
|
---|
| 92 | var additionNode = new Addition().CreateTreeNode();
|
---|
[10866] | 93 | var dNode = CreateConstantTreeNode("d", dValue);
|
---|
[10846] | 94 | additionNode.AddSubtree(multiplicationNode);
|
---|
| 95 | additionNode.AddSubtree(dNode);
|
---|
| 96 |
|
---|
[10872] | 97 | return additionNode;
|
---|
[10846] | 98 | }
|
---|
| 99 |
|
---|
[10872] | 100 | private ISymbolicExpressionTreeNode GenerateInverseModelForLinearTransformation() {
|
---|
[10848] | 101 | var linearTransformation = (LinearTransformation)transformation;
|
---|
| 102 | var kValue = linearTransformation.Multiplier;
|
---|
| 103 | var dValue = linearTransformation.Addend;
|
---|
| 104 |
|
---|
| 105 | // x - d
|
---|
| 106 | var substractionNode = new Subtraction().CreateTreeNode();
|
---|
[10866] | 107 | var dNode = CreateConstantTreeNode("d", dValue);
|
---|
| 108 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
[10848] | 109 | substractionNode.AddSubtree(xNode);
|
---|
| 110 | substractionNode.AddSubtree(dNode);
|
---|
| 111 |
|
---|
| 112 | // ( x - d ) / k
|
---|
| 113 | var divisionNode = new Division().CreateTreeNode();
|
---|
[10866] | 114 | var kNode = CreateConstantTreeNode("k", kValue);
|
---|
[10848] | 115 | divisionNode.AddSubtree(substractionNode);
|
---|
| 116 | divisionNode.AddSubtree(kNode);
|
---|
| 117 |
|
---|
[10872] | 118 | return divisionNode;
|
---|
[10848] | 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
[10872] | 122 | private ISymbolicExpressionTreeNode GenerateModelForExponentialTransformation() {
|
---|
[10848] | 123 | var exponentialTransformation = (ExponentialTransformation)transformation;
|
---|
| 124 | var bValue = exponentialTransformation.Base;
|
---|
| 125 |
|
---|
[10853] | 126 | return GenTreePow_b_x(bValue);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[10872] | 129 | private ISymbolicExpressionTreeNode GenerateInverseModelForExponentialTransformation() {
|
---|
[10853] | 130 | var exponentialTransformation = (ExponentialTransformation)transformation;
|
---|
| 131 | var bValue = exponentialTransformation.Base;
|
---|
| 132 |
|
---|
| 133 | return GenTreeLog_x_b(bValue);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[10872] | 137 | private ISymbolicExpressionTreeNode GenerateModelForLogarithmicTransformation() {
|
---|
[10853] | 138 | var logarithmicTransformation = (LogarithmicTransformation)transformation;
|
---|
| 139 | var bValue = logarithmicTransformation.Base;
|
---|
| 140 |
|
---|
| 141 | return GenTreeLog_x_b(bValue);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[10872] | 144 | private ISymbolicExpressionTreeNode GenerateInverseModelForLogarithmicTransformation() {
|
---|
[10853] | 145 | var logarithmicTransformation = (LogarithmicTransformation)transformation;
|
---|
| 146 | var bValue = logarithmicTransformation.Base;
|
---|
| 147 |
|
---|
| 148 | return GenTreePow_b_x(bValue);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 |
|
---|
[10872] | 152 | private ISymbolicExpressionTreeNode GenerateModelForPowerTransformation() {
|
---|
[10853] | 153 | var powerTransformation = (PowerTransformation)transformation;
|
---|
| 154 | var expValue = powerTransformation.Exponent;
|
---|
| 155 |
|
---|
| 156 | // x ^ exp
|
---|
[10848] | 157 | var powerNode = new Power().CreateTreeNode();
|
---|
[10866] | 158 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
| 159 | var expNode = CreateConstantTreeNode("exp", expValue);
|
---|
[10848] | 160 | powerNode.AddSubtree(xNode);
|
---|
[10853] | 161 | powerNode.AddSubtree(expNode);
|
---|
[10848] | 162 |
|
---|
[10872] | 163 | return powerNode;
|
---|
[10848] | 164 | }
|
---|
| 165 |
|
---|
[10872] | 166 | private ISymbolicExpressionTreeNode GenerateInverseModelForPowerTransformation() {
|
---|
[10853] | 167 | var powerTransformation = (PowerTransformation)transformation;
|
---|
| 168 | var expValue = powerTransformation.Exponent;
|
---|
[10848] | 169 |
|
---|
[10853] | 170 | // rt(x, b)
|
---|
| 171 | var rootNode = new Root().CreateTreeNode();
|
---|
[10866] | 172 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
| 173 | var bNode = CreateConstantTreeNode("b", expValue);
|
---|
[10853] | 174 | rootNode.AddSubtree(xNode);
|
---|
| 175 | rootNode.AddSubtree(bNode);
|
---|
| 176 |
|
---|
[10872] | 177 | return rootNode;
|
---|
[10853] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
[10872] | 181 | private ISymbolicExpressionTreeNode GenerateModelForReciprocalTransformation() {
|
---|
[10853] | 182 | return GenTreeDiv_1_x();
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[10872] | 185 | private ISymbolicExpressionTreeNode GenerateInverseModelForReciprocalTransformation() {
|
---|
[10853] | 186 | return GenTreeDiv_1_x();
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[10872] | 189 |
|
---|
| 190 | private ISymbolicExpressionTreeNode GenerateModelForShiftStandardDistributionTransformation() {
|
---|
[10853] | 191 | var shiftStandardDistributionTransformation = (ShiftStandardDistributionTransformation)transformation;
|
---|
| 192 | var m_orgValue = shiftStandardDistributionTransformation.OriginalMean;
|
---|
| 193 | var s_orgValue = shiftStandardDistributionTransformation.OriginalStandardDeviation;
|
---|
| 194 | var m_tarValue = shiftStandardDistributionTransformation.Mean;
|
---|
| 195 | var s_tarValue = shiftStandardDistributionTransformation.StandardDeviation;
|
---|
| 196 |
|
---|
[10866] | 197 | return GenTreeShiftStdDist(column, m_orgValue, s_orgValue, m_tarValue, s_tarValue);
|
---|
| 198 | }
|
---|
[10853] | 199 |
|
---|
[10872] | 200 | private ISymbolicExpressionTreeNode GenerateInverseModelForShiftStandardDistributionTransformation() {
|
---|
[10866] | 201 | var shiftStandardDistributionTransformation = (ShiftStandardDistributionTransformation)transformation;
|
---|
| 202 | var m_orgValue = shiftStandardDistributionTransformation.OriginalMean;
|
---|
| 203 | var s_orgValue = shiftStandardDistributionTransformation.OriginalStandardDeviation;
|
---|
| 204 | var m_tarValue = shiftStandardDistributionTransformation.Mean;
|
---|
| 205 | var s_tarValue = shiftStandardDistributionTransformation.StandardDeviation;
|
---|
[10853] | 206 |
|
---|
[10866] | 207 | return GenTreeShiftStdDist(column, m_tarValue, s_tarValue, m_orgValue, s_orgValue);
|
---|
[10853] | 208 | }
|
---|
| 209 |
|
---|
[10943] | 210 | private ISymbolicExpressionTreeNode GenerateTreeNodeForCopyColumnTransformation() {
|
---|
| 211 | var copyColumnTransformation = (CopyColumnTransformation)transformation;
|
---|
| 212 | var copiedColumnName = copyColumnTransformation.CopiedColumnName;
|
---|
| 213 |
|
---|
| 214 | return CreateVariableTreeNode(copiedColumnName, copiedColumnName + "(original)");
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[10853] | 217 | // helper's helper:
|
---|
| 218 |
|
---|
[10872] | 219 | private ISymbolicExpressionTreeNode GenTreeLog_x_b(double b) {
|
---|
[10848] | 220 | // log(x, b)
|
---|
| 221 | var logNode = new Logarithm().CreateTreeNode();
|
---|
[10866] | 222 | var bNode = CreateConstantTreeNode("b", b);
|
---|
| 223 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
[10848] | 224 | logNode.AddSubtree(xNode);
|
---|
| 225 | logNode.AddSubtree(bNode);
|
---|
| 226 |
|
---|
[10872] | 227 | return logNode;
|
---|
[10848] | 228 | }
|
---|
| 229 |
|
---|
[10872] | 230 | private ISymbolicExpressionTreeNode GenTreePow_b_x(double b) {
|
---|
[10853] | 231 | // b ^ x
|
---|
| 232 | var powerNode = new Power().CreateTreeNode();
|
---|
[10866] | 233 | var bNode = CreateConstantTreeNode("b", b);
|
---|
| 234 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
[10853] | 235 | powerNode.AddSubtree(bNode);
|
---|
| 236 | powerNode.AddSubtree(xNode);
|
---|
| 237 |
|
---|
[10872] | 238 | return powerNode;
|
---|
[10853] | 239 | }
|
---|
| 240 |
|
---|
[10872] | 241 | private ISymbolicExpressionTreeNode GenTreeDiv_1_x() {
|
---|
[10853] | 242 | // 1 / x
|
---|
| 243 | var divNode = new Division().CreateTreeNode();
|
---|
[10866] | 244 | var oneNode = CreateConstantTreeNode("1", 1.0);
|
---|
| 245 | var xNode = CreateVariableTreeNode(column, "x");
|
---|
[10853] | 246 | divNode.AddSubtree(oneNode);
|
---|
| 247 | divNode.AddSubtree(xNode);
|
---|
| 248 |
|
---|
[10872] | 249 | return divNode;
|
---|
[10853] | 250 | }
|
---|
[10872] | 251 |
|
---|
| 252 | private ISymbolicExpressionTreeNode GenTreeShiftStdDist(string variable, double m_org, double s_org, double m_tar, double s_tar) {
|
---|
[10866] | 253 | // x - m_org
|
---|
| 254 | var substractionNode = new Subtraction().CreateTreeNode();
|
---|
| 255 | var xNode = CreateVariableTreeNode(variable, "x");
|
---|
| 256 | var m_orgNode = CreateConstantTreeNode("m_org", m_org);
|
---|
| 257 | substractionNode.AddSubtree(xNode);
|
---|
| 258 | substractionNode.AddSubtree(m_orgNode);
|
---|
[10853] | 259 |
|
---|
[10866] | 260 | // (x - m_org) / s_org
|
---|
| 261 | var divisionNode = new Division().CreateTreeNode();
|
---|
| 262 | var s_orgNode = CreateConstantTreeNode("s_org", s_org);
|
---|
| 263 | divisionNode.AddSubtree(substractionNode);
|
---|
| 264 | divisionNode.AddSubtree(s_orgNode);
|
---|
[10853] | 265 |
|
---|
[10866] | 266 | // ((x - m_org) / s_org ) * s_tar
|
---|
| 267 | var multiplicationNode = new Multiplication().CreateTreeNode();
|
---|
| 268 | var s_tarNode = CreateConstantTreeNode("s_tar", s_tar);
|
---|
| 269 | multiplicationNode.AddSubtree(divisionNode);
|
---|
| 270 | multiplicationNode.AddSubtree(s_tarNode);
|
---|
| 271 |
|
---|
| 272 | // ((x - m_org) / s_org ) * s_tar + m_tar
|
---|
| 273 | var additionNode = new Addition().CreateTreeNode();
|
---|
| 274 | var m_tarNode = CreateConstantTreeNode("m_tar", m_tar);
|
---|
| 275 | additionNode.AddSubtree(multiplicationNode);
|
---|
| 276 | additionNode.AddSubtree(m_tarNode);
|
---|
| 277 |
|
---|
[10872] | 278 | return additionNode;
|
---|
[10866] | 279 | }
|
---|
| 280 |
|
---|
| 281 | private ConstantTreeNode CreateConstantTreeNode(string description, double value) {
|
---|
[10869] | 282 | return new ConstantTreeNode(new Constant()) { Value = value };
|
---|
[10866] | 283 | }
|
---|
[10872] | 284 |
|
---|
[10878] | 285 | private VariableTreeNode CreateVariableTreeNode(string name, string description) {
|
---|
| 286 | return new VariableTreeNode(new Variable(name, description)) { VariableName = name, Weight = 1.0 };
|
---|
[10866] | 287 | }
|
---|
| 288 |
|
---|
[10846] | 289 | private void InitComponents(ITransformation transformation) {
|
---|
| 290 | this.transformation = transformation;
|
---|
| 291 | column = transformation.Column;
|
---|
| 292 | }
|
---|
[10845] | 293 | }
|
---|
| 294 | }
|
---|