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