Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs @ 17259

Last change on this file since 17259 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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