#region License Information /* HeuristicLab * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Data; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using HeuristicLab.Problems.DataAnalysis; using HeuristicLab.Problems.Instances.Types; namespace HeuristicLab.Problems.Instances.DataAnalysis.SegmentOptimization { public class SegmentOptimizationInstanceProvider : ProblemInstanceProvider { public override string Name { get { return "Simple Generated"; } } public override string Description { get { return "Simple Generated"; } } public override string ReferencePublication => ""; public override Uri WebLink => null; public override IEnumerable GetDataDescriptors() { return new[] { new SOPDataDescriptor("[1:100]: i sum", "", new SOPData { Values = Enumerable.Range(1, 100).Select(i => (double)i).ToArray(), Lower = 30, Upper = 50, Aggregation = "sum" }), new SOPDataDescriptor("[1:1000]: i sum", "", new SOPData { Values = Enumerable.Range(1, 1000).Select(i => (double)i).ToArray(), Lower = 350, Upper = 500, Aggregation = "sum" }), new SOPDataDescriptor("[1:100]: i^2 sum", "", new SOPData { Values = Enumerable.Range(1, 100).Select(i => (double)i * i).ToArray(), Lower = 30, Upper = 50, Aggregation = "sum" }), new SOPDataDescriptor("[1:1000]: i^2 sum", "", new SOPData { Values = Enumerable.Range(1, 1000).Select(i => (double)i * i).ToArray(), Lower = 350, Upper = 500, Aggregation = "sum" }), }; } public override SOPData LoadData(IDataDescriptor id) { var descriptor = (SOPDataDescriptor)id; return descriptor.Data; } } public class SegmentOptimizationFileInstanceProvider : ProblemInstanceProvider { public override string Name { get { return "SOP File"; } } public override string Description { get { return "SOP File"; } } public override string ReferencePublication => ""; public override Uri WebLink => null; protected virtual string FileName => "SOPData"; protected virtual string ZipEntryName => "GeneratedVectors.csv"; public override IEnumerable GetDataDescriptors() { return new[] { new SOPDataDescriptor("v1", "", "v1", 0, 20, 60, "mean"), new SOPDataDescriptor("v2", "", "v2", 0, 20, 60, "mean"), new SOPDataDescriptor("v3", "", "v3", 0, 20, 60, "mean"), new SOPDataDescriptor("v4", "", "v4", 0, 20, 40, "mean"), new SOPDataDescriptor("v5", "", "v5", 0, 60, 80, "mean"), new SOPDataDescriptor("v6", "", "v6", 0, 40, 60, "mean"), }; } public override SOPData LoadData(IDataDescriptor id) { var descriptor = (SOPDataDescriptor)id; var instanceArchiveName = GetResourceName(FileName + @"\.zip"); var parser = new TableFileParser(); var options = new TableFileFormatOptions { ColumnSeparator = ';', VectorSeparator = ',' }; using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) { var entry = instancesZipFile.GetEntry(ZipEntryName); using (var stream = entry.Open()) { parser.Parse(stream, options, columnNamesInFirstLine: true); var dataTable = new Dataset(parser.VariableNames, parser.Values); var instance = LoadInstance(dataTable, descriptor); instance.Name = id.Name; instance.Description = id.Description; return instance; } } } private SOPData LoadInstance(IDataset dataset, SOPDataDescriptor descriptor) { var data = dataset.GetDoubleVectorValue(descriptor.VariableName, descriptor.Row); return new SOPData { Values = data.ToArray(), Lower = descriptor.Lower, Upper = descriptor.Upper, Aggregation = descriptor.Aggregation }; } protected virtual string GetResourceName(string fileName) { return Assembly.GetExecutingAssembly().GetManifestResourceNames() .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success); } protected virtual string GetInstanceDescription() { return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast().First().Version + "."; } } public class SegmentOptimizationLargeFileInstanceProvider : SegmentOptimizationFileInstanceProvider { public override string Name { get { return "SOP Large File"; } } public override string Description { get { return "SOP Large File"; } } protected override string ZipEntryName => "GeneratedVectorsLarge.csv"; public override IEnumerable GetDataDescriptors() { return base.GetDataDescriptors().Select(id => { var descriptor = (SOPDataDescriptor)id; descriptor.Lower *= 10; descriptor.Upper *= 10; return descriptor; }); } } }