Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysisCSVImport/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisInstanceProvider.cs @ 8842

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

#1942:

  • add combo boxes to DataAnalysisImportTypeDialog to select csv settings
  • get branch ready
File size: 3.2 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;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Problems.DataAnalysis;
27
28namespace HeuristicLab.Problems.Instances.DataAnalysis {
29  public abstract class DataAnalysisInstanceProvider<TData, ImportType> : ProblemInstanceProvider<TData>
30    where TData : class, IDataAnalysisProblemData
31    where ImportType : DataAnalysisImportType {
32
33    // has to be implemented, if CanImportData is true
34    public TData ImportData(string path, ImportType type) {
35      TableFileParser csvFileParser = new TableFileParser();
36      csvFileParser.Parse(path);
37      return ImportData(path, type, csvFileParser);
38    }
39    public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) {
40      TableFileParser csvFileParser = new TableFileParser();
41      csvFileParser.Parse(path, csvFormat.NumberFormatInfo, csvFormat.DateTimeFormatInfo, csvFormat.Separator);
42      return ImportData(path, type, csvFileParser);
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 = GetRandomIndices(count);
51      List<IList> shuffeledValues = new List<IList>(values.Count);
52      for (int col = 0; col < values.Count; col++) {
53
54        if (values[col] is List<double>)
55          shuffeledValues.Add(new List<double>());
56        else if (values[col] is List<DateTime>)
57          shuffeledValues.Add(new List<DateTime>());
58        else if (values[col] is List<string>)
59          shuffeledValues.Add(new List<string>());
60        else
61          throw new InvalidOperationException();
62
63        for (int i = 0; i < count; i++) {
64          shuffeledValues[col].Add(values[col][indices[i]]);
65        }
66      }
67      return shuffeledValues;
68    }
69
70    //Fisher–Yates shuffle
71    private int[] GetRandomIndices(int amount) {
72      int[] randomIndices = Enumerable.Range(0, amount).ToArray();
73      System.Random rand = new System.Random();
74      int n = amount;
75      while (n > 1) {
76        n--;
77        int k = rand.Next(n + 1);
78        int value = randomIndices[k];
79        randomIndices[k] = randomIndices[n];
80        randomIndices[n] = value;
81      }
82      return randomIndices;
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.