Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs @ 2296

Last change on this file since 2296 was 2226, checked in by gkronber, 15 years ago
  • Added extension method IsAlmost to type double
  • changed evaluation semantics of boolean operations in HL3TreeEvaluator

(#713)

File size: 6.9 KB
Line 
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
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.DataAnalysis;
28using HeuristicLab.Modeling;
29using HeuristicLab.GP;
30using HeuristicLab.GP.StructureIdentification;
31using HeuristicLab.GP.Interfaces;
32
33namespace HeuristicLab.LinearRegression {
34  public class LinearRegressionOperator : OperatorBase {
35    private static double constant = 1.0;
36
37    public LinearRegressionOperator() {
38      AddVariableInfo(new VariableInfo("TargetVariable", "Index of the column of the dataset that holds the target variable", typeof(IntData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
40      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IGeneticProgrammingModel), VariableKind.Out | VariableKind.New));
43    }
44
45    public override IOperation Apply(IScope scope) {
46      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
47      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
48      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
49      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
50      List<int> allowedRows = CalculateAllowedRows(dataset, targetVariable, start, end);
51      List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariable, start, end);
52
53      double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows);
54      double[] targetVector = PrepareTargetVector(dataset, targetVariable, allowedRows);
55      double[] coefficients = CalculateCoefficients(inputMatrix, targetVector);
56      IFunctionTree tree = CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList());
57
58      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), new GeneticProgrammingModel(tree)));
59      return null;
60    }
61
62    private IFunctionTree CreateModel(double[] coefficients, List<string> allowedVariables) {
63      IFunctionTree root = new Addition().GetTreeNode();
64      IFunctionTree actNode = root;
65
66      Queue<IFunctionTree> nodes = new Queue<IFunctionTree>();
67      GP.StructureIdentification.Variable v;
68      for (int i = 0; i < coefficients.Length - 1; i++) {
69        var vNode = (VariableFunctionTree)new GP.StructureIdentification.Variable().GetTreeNode();
70        vNode.VariableName = allowedVariables[i];
71        vNode.Weight = coefficients[i];
72        vNode.SampleOffset = 0;
73        nodes.Enqueue(vNode);
74      }
75      var cNode = (ConstantFunctionTree)new Constant().GetTreeNode();
76
77      cNode.Value = coefficients[coefficients.Length - 1];
78      nodes.Enqueue(cNode);
79
80      IFunctionTree newTree;
81      while (nodes.Count != 1) {
82        newTree = new Addition().GetTreeNode();
83        newTree.AddSubTree(nodes.Dequeue());
84        newTree.AddSubTree(nodes.Dequeue());
85        nodes.Enqueue(newTree);
86      }
87
88      return nodes.Dequeue();
89    }
90
91    private double[] CalculateCoefficients(double[,] inputMatrix, double[] targetVector) {
92      double[] weights = new double[targetVector.Length];
93      double[] coefficients = new double[inputMatrix.GetLength(1)];
94      for (int i = 0; i < weights.Length; i++) weights[i] = 1.0;
95      // call external ALGLIB solver
96      leastsquares.buildgeneralleastsquares(ref targetVector, ref weights, ref inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), ref coefficients);
97
98      return coefficients;
99    }
100
101    //returns list of valid row indexes (rows without NaN values)
102    private List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, int start, int end) {
103      List<int> allowedRows = new List<int>();
104      bool add;
105      for (int row = start; row < end; row++) {
106        add = true;
107        for (int col = 0; col < dataset.Columns && add == true; col++) {
108          if (double.IsNaN(dataset.GetValue(row, col)) ||
109              double.IsNaN(dataset.GetValue(row, targetVariable)))
110            add = false;
111        }
112        if (add)
113          allowedRows.Add(row);
114        add = true;
115      }
116      return allowedRows;
117    }
118
119    //returns list of valid column indexes (columns which contain at least one non-zero value)
120    private List<int> CalculateAllowedColumns(Dataset dataset, int targetVariable, int start, int end) {
121      List<int> allowedColumns = new List<int>();
122      for (int i = 0; i < dataset.Columns; i++) {
123        if (i == targetVariable) continue;
124        if (!dataset.GetMinimum(i, start, end).IsAlmost(0.0) ||
125            !dataset.GetMaximum(i, start, end).IsAlmost(0.0))
126          allowedColumns.Add(i);
127      }
128      return allowedColumns;
129    }
130
131    private double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows) {
132      int rowCount = allowedRows.Count;
133      double[,] matrix = new double[rowCount, allowedColumns.Count + 1];
134      for (int col = 0; col < allowedColumns.Count; col++) {
135        for (int row = 0; row < allowedRows.Count; row++)
136          matrix[row, col] = dataset.GetValue(allowedRows[row], allowedColumns[col]);
137      }
138      //add constant 1.0 in last column
139      for (int i = 0; i < rowCount; i++)
140        matrix[i, allowedColumns.Count] = constant;
141      return matrix;
142    }
143
144    private double[] PrepareTargetVector(Dataset dataset, int targetVariable, List<int> allowedRows) {
145      int rowCount = allowedRows.Count;
146      double[] targetVector = new double[rowCount];
147      double[] samples = dataset.Samples;
148      for (int row = 0; row < rowCount; row++) {
149        targetVector[row] = dataset.GetValue(allowedRows[row], targetVariable);
150      }
151      return targetVector;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.