[8598] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8598] | 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;
|
---|
[8877] | 25 | using System.Globalization;
|
---|
| 26 | using System.IO;
|
---|
[8598] | 27 | using System.Linq;
|
---|
[8877] | 28 | using System.Text;
|
---|
[8598] | 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[8877] | 30 | using HeuristicLab.Random;
|
---|
[8598] | 31 |
|
---|
| 32 | namespace HeuristicLab.Problems.Instances.DataAnalysis {
|
---|
[8877] | 33 | public abstract class DataAnalysisInstanceProvider<TData, ImportType> : ProblemInstanceProvider<TData>
|
---|
| 34 | where TData : class, IDataAnalysisProblemData
|
---|
| 35 | where ImportType : DataAnalysisImportType {
|
---|
[8598] | 36 |
|
---|
[8878] | 37 |
|
---|
[8877] | 38 | public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) {
|
---|
| 39 | TableFileParser csvFileParser = new TableFileParser();
|
---|
[9608] | 40 | csvFileParser.Parse(path, csvFormat.NumberFormatInfo, csvFormat.DateTimeFormatInfo, csvFormat.Separator, csvFormat.VariableNamesAvailable);
|
---|
[8877] | 41 | return ImportData(path, type, csvFileParser);
|
---|
| 42 | }
|
---|
[8598] | 43 |
|
---|
[8878] | 44 | protected virtual TData ImportData(string path, ImportType type, TableFileParser csvFileParser) {
|
---|
| 45 | throw new NotSupportedException();
|
---|
| 46 | }
|
---|
[8877] | 47 |
|
---|
[8598] | 48 | protected List<IList> Shuffle(List<IList> values) {
|
---|
| 49 | int count = values.First().Count;
|
---|
[8877] | 50 | int[] indices = Enumerable.Range(0, count).Shuffle(new FastRandom()).ToArray();
|
---|
[9217] | 51 | List<IList> shuffled = new List<IList>(values.Count);
|
---|
[8598] | 52 | for (int col = 0; col < values.Count; col++) {
|
---|
| 53 |
|
---|
| 54 | if (values[col] is List<double>)
|
---|
[9217] | 55 | shuffled.Add(new List<double>());
|
---|
[8598] | 56 | else if (values[col] is List<DateTime>)
|
---|
[9217] | 57 | shuffled.Add(new List<DateTime>());
|
---|
[8598] | 58 | else if (values[col] is List<string>)
|
---|
[9217] | 59 | shuffled.Add(new List<string>());
|
---|
[8598] | 60 | else
|
---|
| 61 | throw new InvalidOperationException();
|
---|
| 62 |
|
---|
| 63 | for (int i = 0; i < count; i++) {
|
---|
[9217] | 64 | shuffled[col].Add(values[col][indices[i]]);
|
---|
[8598] | 65 | }
|
---|
| 66 | }
|
---|
[9217] | 67 | return shuffled;
|
---|
[8598] | 68 | }
|
---|
| 69 |
|
---|
[8877] | 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);
|
---|
[8598] | 78 | }
|
---|
[8877] | 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 | }
|
---|
[8598] | 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|