[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 System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 29 | public static class LinearModelToTreeConverter {
|
---|
| 30 | public static ISymbolicExpressionTree CreateTree(string[] variableNames, double[] coefficients,
|
---|
| 31 | double @const = 0) {
|
---|
| 32 | return CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public static ISymbolicExpressionTree CreateTree(
|
---|
| 36 | IEnumerable<KeyValuePair<string, IEnumerable<string>>> factors, double[] factorCoefficients,
|
---|
| 37 | string[] variableNames, double[] coefficients,
|
---|
| 38 | double @const = 0) {
|
---|
[16085] | 39 |
|
---|
[14843] | 40 | if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException();
|
---|
[16085] | 41 |
|
---|
| 42 | // Create tree for double variables
|
---|
| 43 | ISymbolicExpressionTree tree = null;
|
---|
[14843] | 44 | if (coefficients.Length > 0) {
|
---|
[16085] | 45 | tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
|
---|
| 46 | if (factorCoefficients.Length == 0) return tree;
|
---|
[14843] | 47 | }
|
---|
[16085] | 48 |
|
---|
| 49 | // Create tree for string variables
|
---|
| 50 | ISymbolicExpressionTree factorTree = null;
|
---|
[14843] | 51 | if (factorCoefficients.Length > 0) {
|
---|
[16085] | 52 | factorTree = CreateTree(factors, factorCoefficients, @const);
|
---|
| 53 | if (tree == null) return factorTree;
|
---|
| 54 | }
|
---|
[14843] | 55 |
|
---|
[16085] | 56 | // Combine both trees
|
---|
| 57 | ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0);
|
---|
| 58 | foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
|
---|
| 59 | add.InsertSubtree(add.SubtreeCount - 1, binFactorNode);
|
---|
| 60 | return tree;
|
---|
| 61 |
|
---|
[14843] | 62 | throw new ArgumentException();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public static ISymbolicExpressionTree CreateTree(string[] variableNames, int[] lags, double[] coefficients,
|
---|
| 66 | double @const = 0) {
|
---|
| 67 | if (variableNames.Length == 0 ||
|
---|
| 68 | variableNames.Length != coefficients.Length ||
|
---|
| 69 | variableNames.Length != lags.Length)
|
---|
| 70 | throw new ArgumentException("The length of the variable names, lags, and coefficients vectors must match");
|
---|
| 71 |
|
---|
| 72 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
| 73 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
| 74 | tree.Root.AddSubtree(startNode);
|
---|
| 75 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
| 76 | startNode.AddSubtree(addition);
|
---|
| 77 |
|
---|
| 78 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
| 79 | if (lags[i] == 0) {
|
---|
| 80 | VariableTreeNode vNode = (VariableTreeNode)new Variable().CreateTreeNode();
|
---|
| 81 | vNode.VariableName = variableNames[i];
|
---|
| 82 | vNode.Weight = coefficients[i];
|
---|
| 83 | addition.AddSubtree(vNode);
|
---|
| 84 | } else {
|
---|
| 85 | LaggedVariableTreeNode vNode = (LaggedVariableTreeNode)new LaggedVariable().CreateTreeNode();
|
---|
| 86 | vNode.VariableName = variableNames[i];
|
---|
| 87 | vNode.Weight = coefficients[i];
|
---|
| 88 | vNode.Lag = lags[i];
|
---|
| 89 | addition.AddSubtree(vNode);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if (!@const.IsAlmost(0.0)) {
|
---|
| 94 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
| 95 | cNode.Value = @const;
|
---|
| 96 | addition.AddSubtree(cNode);
|
---|
| 97 | }
|
---|
| 98 | return tree;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | public static ISymbolicExpressionTree CreateTree(IEnumerable<KeyValuePair<string, IEnumerable<string>>> factors,
|
---|
| 102 | double[] factorCoefficients,
|
---|
| 103 | double @const = 0) {
|
---|
| 104 |
|
---|
| 105 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
| 106 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
| 107 | tree.Root.AddSubtree(startNode);
|
---|
| 108 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
| 109 | startNode.AddSubtree(addition);
|
---|
| 110 |
|
---|
| 111 | int i = 0;
|
---|
| 112 | foreach (var factor in factors) {
|
---|
| 113 | var varName = factor.Key;
|
---|
| 114 | foreach (var factorValue in factor.Value) {
|
---|
| 115 | var node = (BinaryFactorVariableTreeNode)new BinaryFactorVariable().CreateTreeNode();
|
---|
| 116 | node.VariableValue = factorValue;
|
---|
| 117 | node.VariableName = varName;
|
---|
| 118 | node.Weight = factorCoefficients[i];
|
---|
| 119 | addition.AddSubtree(node);
|
---|
| 120 | i++;
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | if (!@const.IsAlmost(0.0)) {
|
---|
| 125 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
| 126 | cNode.Value = @const;
|
---|
| 127 | addition.AddSubtree(cNode);
|
---|
| 128 | }
|
---|
| 129 | return tree;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|