[9497] | 1 | using System.Collections;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.IO;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Reflection;
|
---|
| 6 | using HeuristicLab.Common;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Optimization;
|
---|
| 9 | using HeuristicLab.Persistence.Core;
|
---|
| 10 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 11 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 12 |
|
---|
| 13 | namespace HeuristicLab.FileShrinker {
|
---|
| 14 | class Program {
|
---|
| 15 | private static void Main(string[] args) {
|
---|
| 16 | string directoryName = ".";
|
---|
| 17 |
|
---|
| 18 | if (args.Length == 1) directoryName = args[0];
|
---|
| 19 | var variableValuesField = typeof(Dataset).GetField("variableValues", BindingFlags.NonPublic | BindingFlags.Instance);
|
---|
| 20 |
|
---|
| 21 | foreach (var fileName in Directory.GetFiles(directoryName, "*.hl")) {
|
---|
| 22 | IStorableContent content;
|
---|
| 23 | try {
|
---|
| 24 | content = XmlParser.Deserialize<IStorableContent>(fileName);
|
---|
| 25 | }
|
---|
| 26 | catch (PersistenceException) {
|
---|
| 27 | continue;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | var datasetCache = new Dictionary<string, List<Dataset>>();
|
---|
| 31 | foreach (var problemData in content.GetObjectGraphObjects().OfType<DataAnalysisProblemData>()) {
|
---|
| 32 | Dataset uniqueData = GetEqualDataset(problemData, datasetCache);
|
---|
| 33 | if (uniqueData == null) continue;
|
---|
| 34 |
|
---|
| 35 | var uniqueValues = (Dictionary<string, IList>)variableValuesField.GetValue(uniqueData);
|
---|
| 36 | variableValuesField.SetValue(problemData.Dataset, new Dictionary<string, IList>(uniqueValues));
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | foreach (var run in content.GetObjectGraphObjects().OfType<IRun>()) {
|
---|
| 40 | var results = (Dictionary<string, IItem>)run.Results;
|
---|
| 41 | if (results.ContainsKey("ProblemData.Dataset"))
|
---|
| 42 | results.Remove("ProblemData.Dataset");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | string directory = Path.GetDirectoryName(fileName);
|
---|
| 46 | string file = Path.GetFileName(fileName);
|
---|
| 47 | XmlGenerator.Serialize(content, directory + Path.DirectorySeparatorChar + "Shrinked " + file, 9);
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | private static Dataset GetEqualDataset(DataAnalysisProblemData problemData, Dictionary<string, List<Dataset>> datasetCache) {
|
---|
| 52 | if (!datasetCache.ContainsKey(problemData.Name)) {
|
---|
| 53 | datasetCache.Add(problemData.Name, new List<Dataset>() { problemData.Dataset });
|
---|
| 54 | return null;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | foreach (var dataset in datasetCache[problemData.Name]) {
|
---|
| 58 | if (EqualDatasets(problemData.Dataset, dataset)) return dataset;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | datasetCache[problemData.Name].Add(problemData.Dataset);
|
---|
| 62 | return null;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | private static bool EqualDatasets(Dataset ds1, Dataset ds2) {
|
---|
| 66 | if (ds1.Rows != ds2.Rows) return false;
|
---|
| 67 | if (!ds1.VariableNames.SequenceEqual(ds2.VariableNames)) return false;
|
---|
| 68 |
|
---|
| 69 | foreach (string variable in ds1.DoubleVariables) {
|
---|
| 70 | var values1 = ds1.GetDoubleValues(variable);
|
---|
| 71 | var values2 = ds2.GetDoubleValues(variable);
|
---|
| 72 | if (!values1.SequenceEqual(values2)) return false;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | return true;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | }
|
---|