1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.Instances.Regression {
|
---|
30 | public abstract class RegressionInstanceProvider : IProblemInstanceProvider<RegressionData> {
|
---|
31 | public RegressionData LoadData(string path) {
|
---|
32 | TableFileParser csvFileParser = new TableFileParser();
|
---|
33 | csvFileParser.Parse(path);
|
---|
34 |
|
---|
35 | RegressionData regData = new RegressionData();
|
---|
36 | int pos = path.LastIndexOf('\\');
|
---|
37 | if (pos < 0)
|
---|
38 | regData.Name = path;
|
---|
39 | else {
|
---|
40 | pos++;
|
---|
41 | regData.Name = path.Substring(pos, path.Length - pos);
|
---|
42 | }
|
---|
43 | regData.InputVariables = new List<string>(csvFileParser.VariableNames);
|
---|
44 | regData.TargetVariable = csvFileParser.VariableNames.Last();
|
---|
45 | regData.AllowedInputVariables = regData.InputVariables.Where(x => !x.Equals(regData.TargetVariable));
|
---|
46 | //convert to multidimensional array
|
---|
47 | List<IList> values = csvFileParser.Values;
|
---|
48 | regData.Values = new double[values.First().Count, values.Count];
|
---|
49 | for (int i = 0; i < values.Count; i++) {
|
---|
50 | for (int j = 0; j < values.First().Count; j++) {
|
---|
51 | regData.Values[j, i] = (double)values[i][j];
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | int trainingPartEnd = csvFileParser.Rows * 2 / 3;
|
---|
56 | regData.TrainingPartitionStart = 0;
|
---|
57 | regData.TrainingPartitionEnd = trainingPartEnd;
|
---|
58 | regData.TestPartitionStart = trainingPartEnd;
|
---|
59 | regData.TestPartitionEnd = csvFileParser.Rows;
|
---|
60 | return regData;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void SaveData(RegressionData instance, string path) {
|
---|
64 | StringBuilder strBuilder = new StringBuilder();
|
---|
65 |
|
---|
66 | foreach (var variable in instance.InputVariables) {
|
---|
67 | strBuilder.Append(variable + ";");
|
---|
68 | }
|
---|
69 | strBuilder.Remove(strBuilder.Length - 1, 1);
|
---|
70 | strBuilder.AppendLine();
|
---|
71 |
|
---|
72 | double[,] values = instance.Values;
|
---|
73 |
|
---|
74 | for (int i = 0; i < values.GetLength(0); i++) {
|
---|
75 | for (int j = 0; j < values.GetLength(1); j++) {
|
---|
76 | strBuilder.Append(values[i, j] + ";");
|
---|
77 | }
|
---|
78 | strBuilder.Remove(strBuilder.Length - 1, 1);
|
---|
79 | strBuilder.AppendLine();
|
---|
80 | }
|
---|
81 |
|
---|
82 | using (StreamWriter writer = new StreamWriter(path)) {
|
---|
83 | writer.Write(strBuilder);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public abstract IEnumerable<IDataDescriptor> GetDataDescriptors();
|
---|
88 | public abstract RegressionData LoadData(IDataDescriptor descriptor);
|
---|
89 |
|
---|
90 | public abstract string Name { get; }
|
---|
91 | public abstract string Description { get; }
|
---|
92 | public abstract Uri WebLink { get; }
|
---|
93 | public abstract string ReferencePublication { get; }
|
---|
94 |
|
---|
95 | public IProblemInstanceConsumer<RegressionData> Consumer { get; set; }
|
---|
96 | }
|
---|
97 | }
|
---|