Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Views/3.4/MenuItems/ChangeDataOfOptimizersMenuItem.cs @ 18079

Last change on this file since 18079 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.2 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Common;
29using HeuristicLab.MainForm;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimizer;
32using MenuItem = HeuristicLab.MainForm.WindowsForms.MenuItem;
33
34namespace HeuristicLab.Problems.DataAnalysis.Views {
35  internal sealed class ChangeDataOfOptimizersMenuItem : MenuItem, IOptimizerUserInterfaceItemProvider {
36    public override string Name {
37      get { return "Change problem data"; }
38    }
39    public override IEnumerable<string> Structure {
40      get { return new string[] { "&Edit", "&Data Analysis" }; }
41    }
42    public override int Position {
43      get { return 5400; }
44    }
45    public override string ToolTipText {
46      get { return "This command changes the problem data of all optimizers in the active view."; }
47    }
48
49    protected override void OnToolStripItemSet(EventArgs e) {
50      ToolStripItem.Enabled = false;
51    }
52
53    protected override void OnActiveViewChanged(object sender, EventArgs e) {
54      IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView;
55      if (activeView == null) {
56        ToolStripItem.Enabled = false;
57        return;
58      }
59
60      var optimizer = activeView.Content as IOptimizer;
61      if (optimizer == null) {
62        ToolStripItem.Enabled = false;
63        return;
64      }
65
66      ToolStripItem.Enabled = true;
67    }
68
69    public override void Execute() {
70      using (OpenFileDialog openFileDialog = new OpenFileDialog()) {
71        openFileDialog.Title = "Open Problem file";
72        openFileDialog.FileName = "Item";
73        openFileDialog.Multiselect = false;
74        openFileDialog.DefaultExt = "hl";
75        openFileDialog.Filter = "HeuristicLab Files|*.hl|All Files|*.*";
76
77        if (openFileDialog.ShowDialog() != DialogResult.OK) return;
78
79        var content = ContentManager.Load(openFileDialog.FileName);
80        var problem = content as IDataAnalysisProblem;
81        IDataAnalysisProblemData problemData = null;
82        if (problem != null) {
83          problemData = problem.ProblemData;
84        } else {
85          problemData = content as IDataAnalysisProblemData;
86        }
87        if (problemData == null) throw new ArgumentException("The specified file does not contain a HeuristicLab problem.");
88
89        var activeView = (IContentView)MainFormManager.MainForm.ActiveView;
90        var optimizer = (IOptimizer)activeView.Content;
91        var newOptimizer = (IOptimizer)optimizer.Clone();
92
93        var algorithm = newOptimizer as IAlgorithm;
94        if (algorithm != null) {
95          ChangeProblemData(algorithm, problemData);
96        }
97        foreach (var alg in newOptimizer.NestedOptimizers.OfType<IAlgorithm>()) {
98          ChangeProblemData(alg, (IDataAnalysisProblemData)problemData.Clone());
99          if (problem != null) alg.Name += " " + problem.Name;
100        }
101
102        MainFormManager.MainForm.ShowContent(newOptimizer);
103      }
104
105    }
106
107    private void ChangeProblemData(IAlgorithm algorithm, IDataAnalysisProblemData problemData) {
108      if (algorithm == null) throw new ArgumentNullException("algorithm");
109      if (problemData == null) throw new ArgumentNullException("problemData");
110
111      var problem = algorithm.Problem as IDataAnalysisProblem;
112      if (problem != null) problem.ProblemDataParameter.ActualValue = problemData;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.