Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4689 was 4082, checked in by gkronber, 14 years ago

Added plugin for evolutionary feature selection. #1097

File size: 6.7 KB
RevLine 
[3839]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;
[4068]26using HeuristicLab.Data;
[3839]27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
[4068]29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
[3839]31using HeuristicLab.Parameters;
[4068]32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
[3839]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";
[3848]44    private const string SamplesStartParameterName = "SamplesStart";
45    private const string SamplesEndParameterName = "SamplesEnd";
[3839]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."));
[4068]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."));
[3839]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    }
[3848]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    }
[3839]90    #endregion
91
92
93    public override IOperation Apply() {
[4082]94      double rmsError, cvRmsError;
95      SymbolicExpressionTree = CreateSymbolicExpressionTree(DataAnalysisProblemData.Dataset, DataAnalysisProblemData.TargetVariable.Value, DataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value), SamplesStart.Value, SamplesEnd.Value, out rmsError, out cvRmsError);
[3846]96      return base.Apply();
[3839]97    }
98
[4082]99    public static SymbolicExpressionTree CreateSymbolicExpressionTree(Dataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, int start, int end, out double rmsError, out double cvRmsError) {
[3848]100      double[,] inputMatrix = LinearRegressionUtil.PrepareInputMatrix(dataset, targetVariable, allowedInputVariables, start, end);
[3839]101
102      alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel();
103      alglib.linreg.lrreport ar = new alglib.linreg.lrreport();
104      int nRows = inputMatrix.GetLength(0);
[3848]105      int nFeatures = inputMatrix.GetLength(1) - 1;
106      double[] coefficients = new double[nFeatures + 1]; //last coefficient is for the constant
[3839]107
108      int retVal = 1;
109      alglib.linreg.lrbuild(ref inputMatrix, nRows, nFeatures, ref retVal, ref lm, ref ar);
110      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");
[4082]111      rmsError = ar.rmserror;
112      cvRmsError = ar.cvrmserror;
[3839]113
[3848]114      for (int i = 0; i < nFeatures + 1; i++)
[3839]115        coefficients[i] = lm.w[i + 4];
116
117      SymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
[3848]118      SymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
119      tree.Root.AddSubTree(startNode);
[3839]120      SymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
[3848]121      startNode.AddSubTree(addition);
[3839]122
123      int col = 0;
[3848]124      foreach (string column in allowedInputVariables) {
[3839]125        VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable().CreateTreeNode();
126        vNode.VariableName = column;
127        vNode.Weight = coefficients[col];
128        addition.AddSubTree(vNode);
129        col++;
130      }
131
132      ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
133      cNode.Value = coefficients[coefficients.Length - 1];
[3848]134      addition.AddSubTree(cNode);
[3839]135
136      return tree;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.