Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisInstanceProvider.cs @ 8695

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

#1942: csv files for data analysis problems can be shuffled when imported

File size: 2.6 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> : ProblemInstanceProvider<TData>
30    where TData : class, IDataAnalysisProblemData {
31
32    // has to be implemented, if CanImportData is true
33    public virtual TData ImportData(string path, DataAnalysisImportType type) {
34      throw new NotSupportedException();
35    }
36
37    protected List<IList> Shuffle(List<IList> values) {
38      int count = values.First().Count;
39      int[] indices = GetRandomIndices(count);
40      List<IList> shuffeledValues = new List<IList>(values.Count);
41      for (int col = 0; col < values.Count; col++) {
42
43        if (values[col] is List<double>)
44          shuffeledValues.Add(new List<double>());
45        else if (values[col] is List<DateTime>)
46          shuffeledValues.Add(new List<DateTime>());
47        else if (values[col] is List<string>)
48          shuffeledValues.Add(new List<string>());
49        else
50          throw new InvalidOperationException();
51
52        for (int i = 0; i < count; i++) {
53          shuffeledValues[col].Add(values[col][indices[i]]);
54        }
55      }
56      return shuffeledValues;
57    }
58
59    //Fisher–Yates shuffle
60    private int[] GetRandomIndices(int amount) {
61      int[] randomIndices = Enumerable.Range(0, amount).ToArray();
62      System.Random rand = new System.Random();
63      int n = amount;
64      while (n > 1) {
65        n--;
66        int k = rand.Next(n + 1);
67        int value = randomIndices[k];
68        randomIndices[k] = randomIndices[n];
69        randomIndices[n] = value;
70      }
71      return randomIndices;
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.