1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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;
|
---|
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) {
|
---|
39 | if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException();
|
---|
40 | ISymbolicExpressionTree p1 = null;
|
---|
41 | if (coefficients.Length > 0) {
|
---|
42 | p1 = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
|
---|
43 | if (factorCoefficients.Length == 0)
|
---|
44 | return p1;
|
---|
45 | }
|
---|
46 | if (factorCoefficients.Length > 0) {
|
---|
47 | var p2 = CreateTree(factors, factorCoefficients);
|
---|
48 | if (p1 == null) return p2;
|
---|
49 |
|
---|
50 | // combine
|
---|
51 | ISymbolicExpressionTreeNode add = p1.Root.GetSubtree(0).GetSubtree(0);
|
---|
52 | foreach (var binFactorNode in p2.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
|
---|
53 | add.AddSubtree(binFactorNode);
|
---|
54 | return p1;
|
---|
55 | }
|
---|
56 | throw new ArgumentException();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static ISymbolicExpressionTree CreateTree(string[] variableNames, int[] lags, double[] coefficients,
|
---|
60 | double @const = 0) {
|
---|
61 | if (variableNames.Length == 0 ||
|
---|
62 | variableNames.Length != coefficients.Length ||
|
---|
63 | variableNames.Length != lags.Length)
|
---|
64 | throw new ArgumentException("The length of the variable names, lags, and coefficients vectors must match");
|
---|
65 |
|
---|
66 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
67 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
68 | tree.Root.AddSubtree(startNode);
|
---|
69 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
70 | startNode.AddSubtree(addition);
|
---|
71 |
|
---|
72 | for (int i = 0; i < variableNames.Length; i++) {
|
---|
73 | if (lags[i] == 0) {
|
---|
74 | VariableTreeNode vNode = (VariableTreeNode)new Variable().CreateTreeNode();
|
---|
75 | vNode.VariableName = variableNames[i];
|
---|
76 | vNode.Weight = coefficients[i];
|
---|
77 | addition.AddSubtree(vNode);
|
---|
78 | } else {
|
---|
79 | LaggedVariableTreeNode vNode = (LaggedVariableTreeNode)new LaggedVariable().CreateTreeNode();
|
---|
80 | vNode.VariableName = variableNames[i];
|
---|
81 | vNode.Weight = coefficients[i];
|
---|
82 | vNode.Lag = lags[i];
|
---|
83 | addition.AddSubtree(vNode);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (!@const.IsAlmost(0.0)) {
|
---|
88 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
89 | cNode.Value = @const;
|
---|
90 | addition.AddSubtree(cNode);
|
---|
91 | }
|
---|
92 | return tree;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static ISymbolicExpressionTree CreateTree(IEnumerable<KeyValuePair<string, IEnumerable<string>>> factors,
|
---|
96 | double[] factorCoefficients,
|
---|
97 | double @const = 0) {
|
---|
98 |
|
---|
99 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
100 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
101 | tree.Root.AddSubtree(startNode);
|
---|
102 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
103 | startNode.AddSubtree(addition);
|
---|
104 |
|
---|
105 | int i = 0;
|
---|
106 | foreach (var factor in factors) {
|
---|
107 | var varName = factor.Key;
|
---|
108 | foreach (var factorValue in factor.Value) {
|
---|
109 | var node = (BinaryFactorVariableTreeNode)new BinaryFactorVariable().CreateTreeNode();
|
---|
110 | node.VariableValue = factorValue;
|
---|
111 | node.VariableName = varName;
|
---|
112 | node.Weight = factorCoefficients[i];
|
---|
113 | addition.AddSubtree(node);
|
---|
114 | i++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (!@const.IsAlmost(0.0)) {
|
---|
119 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
120 | cNode.Value = @const;
|
---|
121 | addition.AddSubtree(cNode);
|
---|
122 | }
|
---|
123 | return tree;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|