Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionProblem.cs @ 7664

Last change on this file since 7664 was 7664, checked in by sforsten, 12 years ago

#1784:

  • added Keijzer, Korns, Vladislavleva und Nguyen regression problem instances
  • changes have been made in the ProblemView. Some parts have been replaced with views from Problems.Instances.Views
File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.Instances;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  [StorableClass]
32  [Item("Regression Problem", "A general regression problem.")]
33  [Creatable("Problems")]
34  public class RegressionProblem : DataAnalysisProblem<IRegressionProblemData>, IRegressionProblem, IStorableContent,
35    IProblemInstanceConsumer<RegressionData>, IProblemInstanceExporter<RegressionData>, IProblemInstanceConsumer {
36    public string Filename { get; set; }
37
38    [StorableConstructor]
39    protected RegressionProblem(bool deserializing) : base(deserializing) { }
40    protected RegressionProblem(RegressionProblem original, Cloner cloner) : base(original, cloner) { }
41    public override IDeepCloneable Clone(Cloner cloner) { return new RegressionProblem(this, cloner); }
42
43    public RegressionProblem()
44      : base() {
45      ProblemData = new RegressionProblemData();
46    }
47
48    public override void ImportProblemDataFromFile(string fileName) {
49      RegressionProblemData problemData = RegressionProblemData.ImportFromFile(fileName);
50      ProblemData = problemData;
51    }
52
53    public void Load(RegressionData data) {
54      Name = data.Name;
55      Description = data.Description;
56      Dataset dataset = new Dataset(data.InputVariables, data.Values);
57      ProblemData = new RegressionProblemData(dataset, data.AllowedInputVariables, data.TargetVariable);
58      ProblemData.TrainingPartition.Start = data.TrainingPartitionStart;
59      ProblemData.TrainingPartition.End = data.TrainingPartitionEnd;
60      ProblemData.TestPartition.Start = data.TestPartitionStart;
61      ProblemData.TestPartition.End = data.TestPartitionEnd;
62      OnReset();
63    }
64
65    public RegressionData Export() {
66      if (!ProblemData.InputVariables.Count.Equals(ProblemData.Dataset.DoubleVariables.Count()))
67        throw new ArgumentException("Not all input variables are double variables! (Export only works with double variables)");
68
69      RegressionData regData = new RegressionData();
70      regData.Name = Name;
71      regData.Description = Description;
72      regData.TargetVariable = ProblemData.TargetVariable;
73      regData.InputVariables = ProblemData.InputVariables.Select(x => x.Value);
74      regData.AllowedInputVariables = ProblemData.AllowedInputVariables;
75      regData.TrainingPartitionStart = ProblemData.TrainingPartition.Start;
76      regData.TrainingPartitionEnd = ProblemData.TrainingPartition.End;
77      regData.TestPartitionStart = ProblemData.TestPartition.Start;
78      regData.TestPartitionEnd = ProblemData.TestPartition.End;
79
80      List<List<double>> data = new List<List<double>>();
81      foreach (var variable in ProblemData.Dataset.DoubleVariables) {
82        data.Add(ProblemData.Dataset.GetDoubleValues(variable).ToList());
83      }
84      regData.Values = Transformation(data);
85
86      return regData;
87    }
88
89    public static double[,] Transformation(List<List<double>> data) {
90      if (!data.All(x => x.Count.Equals(data.First().Count)))
91        throw new ArgumentException("Can't create jagged array.");
92      double[,] values = new double[data.First().Count, data.Count];
93      for (int i = 0; i < values.GetLength(0); i++) {
94        for (int j = 0; j < values.GetLength(1); j++) {
95          values[i, j] = data[j][i];
96        }
97      }
98      return values;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.