Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/LinearRegression/LinearRegressionSolutionCreator.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 6.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
34
35namespace HeuristicLab.Problems.DataAnalysis.Regression.LinearRegression {
36  /// <summary>
37  /// A base class for operators which evaluates OneMax solutions given in BinaryVector encoding.
38  /// </summary>
39  [Item("LinearRegressionSolutionCreator", "Uses linear regression to create a structure tree.")]
40  [StorableClass]
41  public class LinearRegressionSolutionCreator : SingleSuccessorOperator, ISolutionCreator {
42    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
43    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
44    private const string SamplesStartParameterName = "SamplesStart";
45    private const string SamplesEndParameterName = "SamplesEnd";
46
47    public LinearRegressionSolutionCreator() {
48      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The resulting solution encoded as a symbolic expression tree."));
49      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The problem data on which the linear regression should be calculated."));
50      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start of the samples on which the linear regression should be applied."));
51      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end of the samples on which the linear regression should be applied."));
52    }
53    [StorableConstructor]
54    public LinearRegressionSolutionCreator(bool deserializing)
55      : base(deserializing) {
56    }
57
58    #region parameter properties
59    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
60      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
61    }
62    public SymbolicExpressionTree SymbolicExpressionTree {
63      get { return SymbolicExpressionTreeParameter.ActualValue; }
64      set { SymbolicExpressionTreeParameter.ActualValue = value; }
65    }
66
67    public ILookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
68      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
69    }
70    public DataAnalysisProblemData DataAnalysisProblemData {
71      get { return DataAnalysisProblemDataParameter.ActualValue; }
72      set { DataAnalysisProblemDataParameter.ActualValue = value; }
73    }
74
75    public IValueLookupParameter<IntValue> SamplesStartParameter {
76      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
77    }
78    public IntValue SamplesStart {
79      get { return SamplesStartParameter.ActualValue; }
80      set { SamplesStartParameter.ActualValue = value; }
81    }
82
83    public IValueLookupParameter<IntValue> SamplesEndParameter {
84      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
85    }
86    public IntValue SamplesEnd {
87      get { return SamplesEndParameter.ActualValue; }
88      set { SamplesEndParameter.ActualValue = value; }
89    }
90    #endregion
91
92
93    public override IOperation Apply() {
94      SymbolicExpressionTree = CreateSymbolicExpressionTree(DataAnalysisProblemData.Dataset, DataAnalysisProblemData.TargetVariable.Value, DataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value), SamplesStart.Value, SamplesEnd.Value);
95      return base.Apply();
96    }
97
98    public static SymbolicExpressionTree CreateSymbolicExpressionTree(Dataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, int start, int end) {
99      double[,] inputMatrix = LinearRegressionUtil.PrepareInputMatrix(dataset, targetVariable, allowedInputVariables, start, end);
100
101      alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel();
102      alglib.linreg.lrreport ar = new alglib.linreg.lrreport();
103      int nRows = inputMatrix.GetLength(0);
104      int nFeatures = inputMatrix.GetLength(1) - 1;
105      double[] coefficients = new double[nFeatures + 1]; //last coefficient is for the constant
106
107      int retVal = 1;
108      alglib.linreg.lrbuild(ref inputMatrix, nRows, nFeatures, ref retVal, ref lm, ref ar);
109      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");
110
111      for (int i = 0; i < nFeatures + 1; i++)
112        coefficients[i] = lm.w[i + 4];
113
114      SymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
115      SymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
116      tree.Root.AddSubTree(startNode);
117      SymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
118      startNode.AddSubTree(addition);
119
120      int col = 0;
121      foreach (string column in allowedInputVariables) {
122        VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable().CreateTreeNode();
123        vNode.VariableName = column;
124        vNode.Weight = coefficients[col];
125        addition.AddSubTree(vNode);
126        col++;
127      }
128
129      ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
130      cNode.Value = coefficients[coefficients.Length - 1];
131      addition.AddSubTree(cNode);
132
133      return tree;
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.