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.IO;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Problems.Instances;
|
---|
29 | using HeuristicLab.Problems.Instances.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.DataAnalysis.Trading {
|
---|
32 | public class CsvProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
|
---|
33 | public override string Name {
|
---|
34 | get { return "CSV File"; }
|
---|
35 | }
|
---|
36 | public override string Description {
|
---|
37 | get {
|
---|
38 | return "Comma separated values file importer";
|
---|
39 | }
|
---|
40 | }
|
---|
41 | public override Uri WebLink {
|
---|
42 | get { return new Uri("http://dev.heuristiclab.com/trac/hl/core/wiki/UsersFAQ#DataAnalysisImportFileFormat"); }
|
---|
43 | }
|
---|
44 | public override string ReferencePublication {
|
---|
45 | get { return ""; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
49 | return new List<IDataDescriptor>();
|
---|
50 | }
|
---|
51 | public override IProblemData LoadData(IDataDescriptor descriptor) {
|
---|
52 | throw new NotImplementedException();
|
---|
53 | }
|
---|
54 |
|
---|
55 | public override bool CanImportData {
|
---|
56 | get { return true; }
|
---|
57 | }
|
---|
58 | public override IProblemData ImportData(string path) {
|
---|
59 | TableFileParser csvFileParser = new TableFileParser();
|
---|
60 | csvFileParser.Parse(path, csvFileParser.AreColumnNamesInFirstLine(path));
|
---|
61 |
|
---|
62 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
63 | string targetVar = (from v in dataset.DoubleVariables
|
---|
64 | where dataset.GetReadOnlyDoubleValues(v).Min() <= 0
|
---|
65 | where dataset.GetReadOnlyDoubleValues(v).Max() >= 0
|
---|
66 | select v).LastOrDefault();
|
---|
67 | if (targetVar == null) throw new ArgumentException("The target variable must contain changes (deltas) of the asset price over time.");
|
---|
68 |
|
---|
69 | // turn off input variables that are constant in the training partition
|
---|
70 | var allowedInputVars = new List<string>();
|
---|
71 | var trainingIndizes = Enumerable.Range(0, (csvFileParser.Rows * 2) / 3);
|
---|
72 | if (trainingIndizes.Count() >= 2) {
|
---|
73 | foreach (var variableName in dataset.DoubleVariables) {
|
---|
74 | if (dataset.GetDoubleValues(variableName, trainingIndizes).Range() > 0)
|
---|
75 | allowedInputVars.Add(variableName);
|
---|
76 | }
|
---|
77 | } else {
|
---|
78 | allowedInputVars.AddRange(dataset.DoubleVariables);
|
---|
79 | }
|
---|
80 |
|
---|
81 | IProblemData problemData = new ProblemData(dataset, allowedInputVars, targetVar);
|
---|
82 |
|
---|
83 | var trainingPartEnd = trainingIndizes.Last();
|
---|
84 | problemData.TrainingPartition.Start = trainingIndizes.First();
|
---|
85 | problemData.TrainingPartition.End = trainingPartEnd;
|
---|
86 | problemData.TestPartition.Start = trainingPartEnd;
|
---|
87 | problemData.TestPartition.End = csvFileParser.Rows;
|
---|
88 |
|
---|
89 | problemData.Name = Path.GetFileName(path);
|
---|
90 |
|
---|
91 | return problemData;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|