1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.DataAnalysis;
|
---|
28 | using HeuristicLab.GP;
|
---|
29 | using HeuristicLab.GP.StructureIdentification;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.LinearRegression {
|
---|
32 | public class LinearRegressionOperator : OperatorBase {
|
---|
33 | private static double constant = 1.0;
|
---|
34 |
|
---|
35 | public LinearRegressionOperator() {
|
---|
36 | AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
39 | AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
|
---|
40 | AddVariableInfo(new VariableInfo("AllowedFeatures", "List of indexes of allowed features", typeof(ItemList<IntData>), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IFunctionTree), VariableKind.Out | VariableKind.New));
|
---|
42 | AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
|
---|
43 | AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New | VariableKind.Out));
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override IOperation Apply(IScope scope) {
|
---|
47 | int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
|
---|
48 | Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
|
---|
49 | int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
|
---|
50 | int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
|
---|
51 | ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);
|
---|
52 | List<int> allowedRows = CalculateAllowedRows(dataset, allowedFeatures, targetVariable, start, end);
|
---|
53 |
|
---|
54 | List<IntData> disallowedFeatures = new List<IntData>();
|
---|
55 | foreach (IntData allowedFeature in allowedFeatures) {
|
---|
56 | if (IsAlmost(dataset.GetMinimum(allowedFeature.Data, start, end), 0.0) &&
|
---|
57 | IsAlmost(dataset.GetMaximum(allowedFeature.Data, start, end), 0.0))
|
---|
58 | disallowedFeatures.Add(allowedFeature);
|
---|
59 | }
|
---|
60 | foreach (IntData disallowedFeature in disallowedFeatures)
|
---|
61 | allowedFeatures.Remove(disallowedFeature);
|
---|
62 |
|
---|
63 | double[,] inputMatrix = PrepareInputMatrix(dataset, allowedFeatures, allowedRows);
|
---|
64 | double[] targetVector = PrepareTargetVector(dataset, targetVariable, allowedRows);
|
---|
65 | double[] coefficients = CalculateCoefficients(inputMatrix, targetVector);
|
---|
66 | IFunctionTree tree = CreateModel(coefficients, allowedFeatures);
|
---|
67 |
|
---|
68 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), tree));
|
---|
69 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(tree.Size)));
|
---|
70 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(tree.Height)));
|
---|
71 | return null;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private bool IsAlmost(double x, double y) {
|
---|
75 | return Math.Abs(x - y) < 1.0E-12;
|
---|
76 | }
|
---|
77 |
|
---|
78 | private IFunctionTree CreateModel(double[] coefficients, ItemList<IntData> allowedFeatures) {
|
---|
79 | IFunctionTree root = new Addition().GetTreeNode();
|
---|
80 | IFunctionTree actNode = root;
|
---|
81 |
|
---|
82 | Queue<IFunctionTree> nodes = new Queue<IFunctionTree>();
|
---|
83 | GP.StructureIdentification.Variable v;
|
---|
84 | for (int i = 0; i < coefficients.Length - 1; i++) {
|
---|
85 | v = new GP.StructureIdentification.Variable();
|
---|
86 | v.GetVariable(GP.StructureIdentification.Variable.INDEX).Value = new ConstrainedIntData(allowedFeatures[i].Data);
|
---|
87 | v.GetVariable(GP.StructureIdentification.Variable.WEIGHT).Value = new ConstrainedDoubleData(coefficients[i]);
|
---|
88 | v.GetVariable(GP.StructureIdentification.Variable.OFFSET).Value = new ConstrainedIntData(0);
|
---|
89 | nodes.Enqueue(v.GetTreeNode());
|
---|
90 | }
|
---|
91 | GP.StructureIdentification.Constant c = new Constant();
|
---|
92 | c.GetVariable(GP.StructureIdentification.Constant.VALUE).Value = new ConstrainedDoubleData(coefficients[coefficients.Length - 1] * 1.0);
|
---|
93 | nodes.Enqueue(c.GetTreeNode());
|
---|
94 |
|
---|
95 | IFunctionTree newTree;
|
---|
96 | while (nodes.Count != 1) {
|
---|
97 | newTree = new Addition().GetTreeNode();
|
---|
98 | newTree.AddSubTree(nodes.Dequeue());
|
---|
99 | newTree.AddSubTree(nodes.Dequeue());
|
---|
100 | nodes.Enqueue(newTree);
|
---|
101 | }
|
---|
102 |
|
---|
103 | return nodes.Dequeue();
|
---|
104 | }
|
---|
105 |
|
---|
106 | private double[] CalculateCoefficients(double[,] inputMatrix, double[] targetVector) {
|
---|
107 | double[] weights = new double[targetVector.Length];
|
---|
108 | double[] coefficients = new double[inputMatrix.GetLength(1)];
|
---|
109 | for(int i=0;i<weights.Length;i++) weights[i] = 1.0;
|
---|
110 | // call external ALGLIB solver
|
---|
111 | leastsquares.buildgeneralleastsquares(ref targetVector, ref weights, ref inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), ref coefficients);
|
---|
112 |
|
---|
113 | return coefficients;
|
---|
114 | }
|
---|
115 |
|
---|
116 | //returns list of valid row indexes (rows without NaN values)
|
---|
117 | private List<int> CalculateAllowedRows(Dataset dataset, ItemList<IntData> allowedFeatures, int targetVariable, int start, int end) {
|
---|
118 | List<int> allowedRows = new List<int>();
|
---|
119 | bool add;
|
---|
120 | for (int row = start; row < end; row++) {
|
---|
121 | add = true;
|
---|
122 | for (int col = 0; col < allowedFeatures.Count && add == true; col++) {
|
---|
123 | if (double.IsNaN(dataset.GetValue(row, allowedFeatures[col].Data)) ||
|
---|
124 | double.IsNaN(dataset.GetValue(row, targetVariable)))
|
---|
125 | add = false;
|
---|
126 | }
|
---|
127 | if (add)
|
---|
128 | allowedRows.Add(row);
|
---|
129 | add = true;
|
---|
130 | }
|
---|
131 | return allowedRows;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private double[,] PrepareInputMatrix(Dataset dataset, ItemList<IntData> allowedFeatures, List<int> allowedRows) {
|
---|
135 | int rowCount = allowedRows.Count;
|
---|
136 | double[,] matrix = new double[rowCount, allowedFeatures.Count + 1];
|
---|
137 | for (int col = 0; col < allowedFeatures.Count; col++) {
|
---|
138 | for (int row = 0; row < allowedRows.Count; row++)
|
---|
139 | matrix[row, col] = dataset.GetValue(allowedRows[row], allowedFeatures[col].Data);
|
---|
140 | }
|
---|
141 | //add constant 1.0 in last column
|
---|
142 | for (int i = 0; i < rowCount; i++)
|
---|
143 | matrix[i, allowedFeatures.Count] = constant;
|
---|
144 | return matrix;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private double[] PrepareTargetVector(Dataset dataset, int targetVariable, List<int> allowedRows) {
|
---|
148 | int rowCount = allowedRows.Count;
|
---|
149 | double[] targetVector = new double[rowCount];
|
---|
150 | double[] samples = dataset.Samples;
|
---|
151 | for (int row = 0; row < rowCount; row++) {
|
---|
152 | targetVector[row] = dataset.GetValue(allowedRows[row], targetVariable);
|
---|
153 | }
|
---|
154 | return targetVector;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|