[10219] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10219] | 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 |
|
---|
[13502] | 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[10219] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[10240] | 26 | using HeuristicLab.Optimization;
|
---|
[13502] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[10221] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[10219] | 29 |
|
---|
| 30 | namespace HeuristicLab.DataPreprocessing {
|
---|
| 31 | [Item("PreprocessingContext", "PreprocessingContext")]
|
---|
[13504] | 32 | [StorableClass]
|
---|
[13502] | 33 | public class PreprocessingContext : NamedItem, IStorableContent {
|
---|
[10383] | 34 |
|
---|
[13502] | 35 | public string Filename { get; set; }
|
---|
[10990] | 36 |
|
---|
[13502] | 37 | public IEnumerable<KeyValuePair<string, Func<IItem>>> ExportPossibilities {
|
---|
| 38 | get {
|
---|
| 39 | var algorithm = Source as IAlgorithm;
|
---|
| 40 | if (algorithm != null)
|
---|
| 41 | yield return new KeyValuePair<string, Func<IItem>>(algorithm.Name, () => ExportAlgorithm(algorithm));
|
---|
| 42 |
|
---|
| 43 | var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
|
---|
| 44 | if (problem != null)
|
---|
| 45 | yield return new KeyValuePair<string, Func<IItem>>(problem.Name, () => ExportProblem(problem));
|
---|
| 46 |
|
---|
| 47 | var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
|
---|
| 48 | if (problemData != null)
|
---|
| 49 | yield return new KeyValuePair<string, Func<IItem>>(problemData.Name, () => ExportProblemData(problemData));
|
---|
| 50 |
|
---|
| 51 | // ToDo: Export CSV
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | public bool CanExport {
|
---|
| 55 | get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [Storable]
|
---|
[10783] | 59 | public IFilteredPreprocessingData Data { get; private set; }
|
---|
[10990] | 60 |
|
---|
[13502] | 61 | [Storable]
|
---|
| 62 | private IItem Source { get; set; }
|
---|
[10614] | 63 |
|
---|
[10990] | 64 |
|
---|
[13502] | 65 | public PreprocessingContext() : this(new RegressionProblemData()) { }
|
---|
[13504] | 66 | public PreprocessingContext(IItem source)
|
---|
| 67 | : base("PreprocessingContext") {
|
---|
[13502] | 68 | Import(source);
|
---|
[10219] | 69 | }
|
---|
| 70 |
|
---|
[13502] | 71 | [StorableConstructor]
|
---|
[13504] | 72 | protected PreprocessingContext(bool deserializing)
|
---|
[13502] | 73 | : base(deserializing) { }
|
---|
[11114] | 74 | protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
|
---|
[10219] | 75 | : base(original, cloner) {
|
---|
[13502] | 76 | Source = cloner.Clone(original.Source);
|
---|
[10219] | 77 | Data = cloner.Clone(original.Data);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 81 | return new PreprocessingContext(this, cloner);
|
---|
| 82 | }
|
---|
[10383] | 83 |
|
---|
[13502] | 84 | #region Import
|
---|
| 85 | public void Import(IItem source) {
|
---|
| 86 | Source = source;
|
---|
| 87 |
|
---|
| 88 | var dataSource = ExtractProblemData(source);
|
---|
| 89 | Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(dataSource));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | private IDataAnalysisProblemData ExtractProblemData(IItem source) {
|
---|
| 93 | var algorithm = source as Algorithm;
|
---|
| 94 | var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
|
---|
| 95 | var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
|
---|
| 96 | return problemData;
|
---|
| 97 | }
|
---|
| 98 | #endregion
|
---|
| 99 |
|
---|
| 100 | #region Export
|
---|
[10990] | 101 | public IItem Export() {
|
---|
[13502] | 102 | var creator = new ProblemDataCreator(this);
|
---|
| 103 | if (Source is IAlgorithm)
|
---|
| 104 | return ExportAlgorithm((IAlgorithm)Source);
|
---|
| 105 | if (Source is IDataAnalysisProblem)
|
---|
| 106 | return ExportProblem((IDataAnalysisProblem)Source);
|
---|
| 107 | if (Source is IDataAnalysisProblemData)
|
---|
| 108 | return ExportProblemData((IDataAnalysisProblemData)Source);
|
---|
| 109 | return null;
|
---|
[10383] | 110 | }
|
---|
[13502] | 111 | private IAlgorithm ExportAlgorithm(IAlgorithm source) {
|
---|
| 112 | var preprocessedAlgorithm = (IAlgorithm)source.Clone();
|
---|
[10990] | 113 | preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
|
---|
[13502] | 114 | preprocessedAlgorithm.Runs.Clear();
|
---|
[10990] | 115 | var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
|
---|
| 116 | SetNewProblemData(problem);
|
---|
| 117 | return preprocessedAlgorithm;
|
---|
[10614] | 118 | }
|
---|
[13502] | 119 | private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
|
---|
| 120 | var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
|
---|
[10990] | 121 | SetNewProblemData(preprocessedProblem);
|
---|
| 122 | return preprocessedProblem;
|
---|
| 123 | }
|
---|
[13502] | 124 | private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
|
---|
| 125 | var creator = new ProblemDataCreator(this);
|
---|
| 126 | return creator.CreateProblemData(source);
|
---|
[10383] | 127 | }
|
---|
[10990] | 128 | private void SetNewProblemData(IDataAnalysisProblem problem) {
|
---|
[13502] | 129 | var data = ExtractProblemData(problem.ProblemData);
|
---|
[10383] | 130 | problem.ProblemDataParameter.ActualValue = data;
|
---|
[10536] | 131 | problem.Name = "Preprocessed " + problem.Name;
|
---|
[10383] | 132 | }
|
---|
[13502] | 133 | #endregion
|
---|
[10219] | 134 | }
|
---|
| 135 | }
|
---|