Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs

Last change on this file was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27
28namespace 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
40      if (factorCoefficients.Length == 0 && coefficients.Length == 0 && @const==0) throw new ArgumentException();
41
42      // Combine both trees
43      ISymbolicExpressionTreeNode add = (new Addition()).CreateTreeNode();
44
45      // Create tree for double variables
46      if (coefficients.Length > 0) {
47        var varTree = CreateTree(variableNames, new int[variableNames.Length], coefficients);
48        foreach (var varNode in varTree.IterateNodesPrefix().OfType<VariableTreeNode>())
49          add.AddSubtree(varNode);
50      }
51
52      // Create tree for string variables
53      if (factorCoefficients.Length > 0) {
54        var factorTree = CreateTree(factors, factorCoefficients);
55        foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
56          add.AddSubtree(binFactorNode);
57      }
58
59      if (@const!=0.0) {
60        ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
61        cNode.Value = @const;
62        add.AddSubtree(cNode);
63      }
64
65      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
66      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
67      tree.Root.AddSubtree(startNode);
68      startNode.AddSubtree(add);
69      return tree;
70    }
71
72    public static ISymbolicExpressionTree CreateTree(string[] variableNames, int[] lags, double[] coefficients,
73      double @const = 0) {
74      if (variableNames.Length == 0 ||
75        variableNames.Length != coefficients.Length ||
76        variableNames.Length != lags.Length)
77        throw new ArgumentException("The length of the variable names, lags, and coefficients vectors must match");
78
79      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
80      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
81      tree.Root.AddSubtree(startNode);
82      ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
83      startNode.AddSubtree(addition);
84
85      for (int i = 0; i < variableNames.Length; i++) {
86        if (lags[i] == 0) {
87          VariableTreeNode vNode = (VariableTreeNode)new Variable().CreateTreeNode();
88          vNode.VariableName = variableNames[i];
89          vNode.Weight = coefficients[i];
90          addition.AddSubtree(vNode);
91        } else {
92          LaggedVariableTreeNode vNode = (LaggedVariableTreeNode)new LaggedVariable().CreateTreeNode();
93          vNode.VariableName = variableNames[i];
94          vNode.Weight = coefficients[i];
95          vNode.Lag = lags[i];
96          addition.AddSubtree(vNode);
97        }
98      }
99
100      if (!@const.IsAlmost(0.0)) {
101        ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
102        cNode.Value = @const;
103        addition.AddSubtree(cNode);
104      }
105      return tree;
106    }
107
108    public static ISymbolicExpressionTree CreateTree(IEnumerable<KeyValuePair<string, IEnumerable<string>>> factors,
109      double[] factorCoefficients,
110      double @const = 0) {
111
112      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
113      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
114      tree.Root.AddSubtree(startNode);
115      ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
116      startNode.AddSubtree(addition);
117
118      int i = 0;
119      foreach (var factor in factors) {
120        var varName = factor.Key;
121        foreach (var factorValue in factor.Value) {
122          var node = (BinaryFactorVariableTreeNode)new BinaryFactorVariable().CreateTreeNode();
123          node.VariableValue = factorValue;
124          node.VariableName = varName;
125          node.Weight = factorCoefficients[i];
126          addition.AddSubtree(node);
127          i++;
128        }
129      }
130
131      if (!@const.IsAlmost(0.0)) {
132        ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
133        cNode.Value = @const;
134        addition.AddSubtree(cNode);
135      }
136      return tree;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.