1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Persistence;
|
---|
28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.DataPreprocessing {
|
---|
31 | [Item("PreprocessingContext", "PreprocessingContext")]
|
---|
32 | [StorableType("a051828e-2406-4efc-b2db-b47da57fa9cf")]
|
---|
33 | public class PreprocessingContext : NamedItem, IStorableContent {
|
---|
34 |
|
---|
35 | public string Filename { get; set; }
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | public IFilteredPreprocessingData Data { get; private set; }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private IItem Source { get; set; }
|
---|
42 |
|
---|
43 | public event EventHandler Reset;
|
---|
44 |
|
---|
45 | #region Constructors
|
---|
46 | public PreprocessingContext(IDataAnalysisProblemData problemData, IItem source = null)
|
---|
47 | : base("Data Preprocessing") {
|
---|
48 | if (problemData == null) throw new ArgumentNullException("problemData");
|
---|
49 | Import(problemData, source);
|
---|
50 | }
|
---|
51 |
|
---|
52 | [StorableConstructor]
|
---|
53 | protected PreprocessingContext(StorableConstructorFlag deserializing)
|
---|
54 | : base(deserializing) { }
|
---|
55 | protected PreprocessingContext(PreprocessingContext original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | Source = cloner.Clone(original.Source);
|
---|
58 | Data = cloner.Clone(original.Data);
|
---|
59 | }
|
---|
60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
61 | return new PreprocessingContext(this, cloner);
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region Import
|
---|
66 | public void Import(IDataAnalysisProblemData problemData, IItem source = null) {
|
---|
67 | if (problemData == null) throw new ArgumentNullException("problemData");
|
---|
68 | if (source != null && ExtractProblemData(source) != problemData)
|
---|
69 | source = null; // ignore the source if the source's problem data is different
|
---|
70 | Source = source ?? problemData;
|
---|
71 | var namedSource = Source as INamedItem;
|
---|
72 | if (namedSource != null)
|
---|
73 | Name = "Preprocessing " + namedSource.Name;
|
---|
74 | Data = new FilteredPreprocessingData(new TransactionalPreprocessingData(problemData));
|
---|
75 | OnReset();
|
---|
76 | // Reset GUI:
|
---|
77 | // - OnContentChanged for PreprocessingView!
|
---|
78 | // event? task(async import)?
|
---|
79 | }
|
---|
80 | private IDataAnalysisProblemData ExtractProblemData(IItem source) {
|
---|
81 | var algorithm = source as Algorithm;
|
---|
82 | var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : source as IDataAnalysisProblem;
|
---|
83 | var problemData = problem != null ? problem.ProblemData : source as IDataAnalysisProblemData;
|
---|
84 | return problemData;
|
---|
85 | }
|
---|
86 | #endregion
|
---|
87 |
|
---|
88 | #region Export
|
---|
89 | public bool CanExport {
|
---|
90 | get { return Source is IAlgorithm || Source is IDataAnalysisProblem || Source is IDataAnalysisProblemData; }
|
---|
91 | }
|
---|
92 | public IEnumerable<KeyValuePair<string, Func<IItem>>> GetSourceExportOptions() {
|
---|
93 | var algorithm = Source as IAlgorithm;
|
---|
94 | if (algorithm != null)
|
---|
95 | yield return new KeyValuePair<string, Func<IItem>>(
|
---|
96 | algorithm.GetType().GetPrettyName(),
|
---|
97 | () => ExportAlgorithm(algorithm));
|
---|
98 |
|
---|
99 | var problem = algorithm != null ? algorithm.Problem as IDataAnalysisProblem : Source as IDataAnalysisProblem;
|
---|
100 | if (problem != null)
|
---|
101 | yield return new KeyValuePair<string, Func<IItem>>(
|
---|
102 | problem.GetType().GetPrettyName(),
|
---|
103 | () => ExportProblem(problem));
|
---|
104 |
|
---|
105 | var problemData = problem != null ? problem.ProblemData : Source as IDataAnalysisProblemData;
|
---|
106 | if (problemData != null)
|
---|
107 | yield return new KeyValuePair<string, Func<IItem>>(
|
---|
108 | problemData.GetType().GetPrettyName(),
|
---|
109 | () => ExportProblemData(problemData));
|
---|
110 | }
|
---|
111 |
|
---|
112 | private IAlgorithm ExportAlgorithm(IAlgorithm source) {
|
---|
113 | var preprocessedAlgorithm = (IAlgorithm)source.Clone();
|
---|
114 | preprocessedAlgorithm.Name = preprocessedAlgorithm.Name + "(Preprocessed)";
|
---|
115 | preprocessedAlgorithm.Runs.Clear();
|
---|
116 | var problem = (IDataAnalysisProblem)preprocessedAlgorithm.Problem;
|
---|
117 | problem.ProblemDataParameter.ActualValue = CreateNewProblemData();
|
---|
118 | return preprocessedAlgorithm;
|
---|
119 | }
|
---|
120 | private IDataAnalysisProblem ExportProblem(IDataAnalysisProblem source) {
|
---|
121 | var preprocessedProblem = (IDataAnalysisProblem)source.Clone();
|
---|
122 | preprocessedProblem.ProblemDataParameter.ActualValue = CreateNewProblemData();
|
---|
123 | return preprocessedProblem;
|
---|
124 | }
|
---|
125 | private IDataAnalysisProblemData ExportProblemData(IDataAnalysisProblemData source) {
|
---|
126 | return CreateNewProblemData();
|
---|
127 | }
|
---|
128 |
|
---|
129 | public IDataAnalysisProblemData CreateNewProblemData() {
|
---|
130 | var creator = new ProblemDataCreator(this);
|
---|
131 | var oldProblemData = ExtractProblemData(Source);
|
---|
132 | var newProblemData = creator.CreateProblemData(oldProblemData);
|
---|
133 | newProblemData.Name = "Preprocessed " + oldProblemData.Name;
|
---|
134 | return newProblemData;
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 |
|
---|
138 | protected virtual void OnReset() {
|
---|
139 | if (Reset != null)
|
---|
140 | Reset(this, EventArgs.Empty);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|