source: branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/SegmentOptimization/SegmentOptimizationInstanceProvider.cs @ 18097

Last change on this file since 18097 was 18097, checked in by pfleck, 8 months ago

#3040 Added SOP instances from csv file with vectors.

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Generic;
24using System.Data;
25using System.IO.Compression;
26using System.Linq;
27using System.Reflection;
28using System.Text.RegularExpressions;
29using HeuristicLab.Problems.DataAnalysis;
30using HeuristicLab.Problems.Instances.Types;
31
32namespace HeuristicLab.Problems.Instances.DataAnalysis.SegmentOptimization {
33  public class SegmentOptimizationInstanceProvider : ProblemInstanceProvider<SOPData> {
34
35    public override string Name {
36      get { return "Simple Generated"; }
37    }
38
39    public override string Description {
40      get { return "Simple Generated"; }
41    }
42
43    public override string ReferencePublication => "";
44    public override Uri WebLink => null;
45
46    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
47      return new[] {
48        new SOPDataDescriptor("[1:100]: i sum", "", new SOPData {
49          Values = Enumerable.Range(1, 100).Select(i => (double)i).ToArray(),
50          Lower = 30, Upper = 50, Aggregation = "sum"
51        }),
52        new SOPDataDescriptor("[1:1000]: i sum", "", new SOPData {
53          Values = Enumerable.Range(1, 1000).Select(i => (double)i).ToArray(),
54          Lower = 350, Upper = 500, Aggregation = "sum"
55        }),
56        new SOPDataDescriptor("[1:100]: i^2 sum", "", new SOPData {
57          Values = Enumerable.Range(1, 100).Select(i => (double)i * i).ToArray(),
58          Lower = 30, Upper = 50, Aggregation = "sum"
59        }),
60        new SOPDataDescriptor("[1:1000]: i^2 sum", "", new SOPData {
61          Values = Enumerable.Range(1, 1000).Select(i => (double)i * i).ToArray(),
62          Lower = 350, Upper = 500, Aggregation = "sum"
63        }),
64      };
65    }
66
67    public override SOPData LoadData(IDataDescriptor id) {
68      var descriptor = (SOPDataDescriptor)id;
69      return descriptor.Data;
70    }
71  }
72
73  public class SegmentOptimizationFileInstanceProvider : ProblemInstanceProvider<SOPData> {
74    public override string Name {
75      get { return "SOP File"; }
76    }
77
78    public override string Description {
79      get { return "SOP File"; }
80    }
81
82    public override string ReferencePublication => "";
83    public override Uri WebLink => null;
84
85    protected virtual string FileName => "SOPData";
86    protected virtual string ZipEntryName => "GeneratedVectors.csv";
87
88    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
89      return new[] {
90        new SOPDataDescriptor("v1", "", "v1", 0, 20, 60, "mean"),
91        new SOPDataDescriptor("v2", "", "v2", 0, 20, 60, "mean"),
92        new SOPDataDescriptor("v3", "", "v3", 0, 20, 60, "mean"),
93        new SOPDataDescriptor("v4", "", "v4", 0, 20, 40, "mean"),
94        new SOPDataDescriptor("v5", "", "v5", 0, 60, 80, "mean"),
95        new SOPDataDescriptor("v6", "", "v6", 0, 40, 60, "mean"),
96      };
97    }
98
99    public override SOPData LoadData(IDataDescriptor id) {
100      var descriptor = (SOPDataDescriptor)id;
101      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
102
103      var parser = new TableFileParser();
104      var options = new TableFileFormatOptions {
105        ColumnSeparator = ';',
106        VectorSeparator = ','
107      };
108
109      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
110        var entry = instancesZipFile.GetEntry(ZipEntryName);
111        using (var stream = entry.Open()) {
112          parser.Parse(stream, options, columnNamesInFirstLine: true);
113
114          var dataTable = new Dataset(parser.VariableNames, parser.Values);
115          var instance = LoadInstance(dataTable, descriptor);
116
117          instance.Name = id.Name;
118          instance.Description = id.Description;
119
120          return instance;
121        }
122      }
123    }
124
125    private SOPData LoadInstance(IDataset dataset, SOPDataDescriptor descriptor) {
126      var data = dataset.GetDoubleVectorValue(descriptor.VariableName, descriptor.Row);
127
128      return new SOPData {
129        Values = data.ToArray(),
130        Lower = descriptor.Lower,
131        Upper = descriptor.Upper,
132        Aggregation = descriptor.Aggregation
133      };
134    }
135
136    protected virtual string GetResourceName(string fileName) {
137      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
138        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
139    }
140
141    protected virtual string GetInstanceDescription() {
142      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
143    }
144  }
145
146  public class SegmentOptimizationLargeFileInstanceProvider : SegmentOptimizationFileInstanceProvider {
147    public override string Name {
148      get { return "SOP Large File"; }
149    }
150
151    public override string Description {
152      get { return "SOP Large File"; }
153    }
154
155    protected override string ZipEntryName => "GeneratedVectorsLarge.csv";
156
157    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
158      return base.GetDataDescriptors().Select(id => {
159        var descriptor = (SOPDataDescriptor)id;
160        descriptor.Lower *= 10;
161        descriptor.Upper *= 10;
162        return descriptor;
163      });
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.