1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 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.Globalization;
|
---|
26 | using System.IO;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
33 | public abstract class DataAnalysisInstanceProvider<TData, ImportType> : ProblemInstanceProvider<TData>
|
---|
34 | where TData : class, IDataAnalysisProblemData
|
---|
35 | where ImportType : DataAnalysisImportType {
|
---|
36 |
|
---|
37 |
|
---|
38 | public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) {
|
---|
39 | TableFileParser csvFileParser = new TableFileParser();
|
---|
40 | csvFileParser.Parse(path, csvFormat.NumberFormatInfo, csvFormat.DateTimeFormatInfo, csvFormat.Separator, csvFormat.VariableNamesAvailable);
|
---|
41 | return ImportData(path, type, csvFileParser);
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected virtual TData ImportData(string path, ImportType type, TableFileParser csvFileParser) {
|
---|
45 | throw new NotSupportedException();
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected List<IList> Shuffle(List<IList> values) {
|
---|
49 | int count = values.First().Count;
|
---|
50 | int[] indices = Enumerable.Range(0, count).Shuffle(new FastRandom()).ToArray();
|
---|
51 | List<IList> shuffled = new List<IList>(values.Count);
|
---|
52 | for (int col = 0; col < values.Count; col++) {
|
---|
53 |
|
---|
54 | if (values[col] is List<double>)
|
---|
55 | shuffled.Add(new List<double>());
|
---|
56 | else if (values[col] is List<DateTime>)
|
---|
57 | shuffled.Add(new List<DateTime>());
|
---|
58 | else if (values[col] is List<string>)
|
---|
59 | shuffled.Add(new List<string>());
|
---|
60 | else
|
---|
61 | throw new InvalidOperationException();
|
---|
62 |
|
---|
63 | for (int i = 0; i < count; i++) {
|
---|
64 | shuffled[col].Add(values[col][indices[i]]);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return shuffled;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override bool CanExportData {
|
---|
71 | get { return true; }
|
---|
72 | }
|
---|
73 | public override void ExportData(TData instance, string path) {
|
---|
74 | var strBuilder = new StringBuilder();
|
---|
75 | var colSep = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
|
---|
76 | foreach (var variable in instance.Dataset.VariableNames) {
|
---|
77 | strBuilder.Append(variable.Replace(colSep, String.Empty) + colSep);
|
---|
78 | }
|
---|
79 | strBuilder.Remove(strBuilder.Length - colSep.Length, colSep.Length);
|
---|
80 | strBuilder.AppendLine();
|
---|
81 |
|
---|
82 | var dataset = instance.Dataset;
|
---|
83 |
|
---|
84 | for (int i = 0; i < dataset.Rows; i++) {
|
---|
85 | for (int j = 0; j < dataset.Columns; j++) {
|
---|
86 | if (j > 0) strBuilder.Append(colSep);
|
---|
87 | strBuilder.Append(dataset.GetValue(i, j));
|
---|
88 | }
|
---|
89 | strBuilder.AppendLine();
|
---|
90 | }
|
---|
91 |
|
---|
92 | using (var writer = new StreamWriter(path)) {
|
---|
93 | writer.Write(strBuilder);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|