Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Optimizer/3.3/StartPage.cs @ 17078

Last change on this file since 17078 was 17022, checked in by mkommend, 5 years ago

#2520: Changed samples to attic format and adapted startpage to load the samples with attic without the creation of temporary files.

File size: 8.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 System.IO;
25using System.IO.Compression;
26using System.Linq;
27using System.Reflection;
28using System.Threading;
29using System.Windows.Forms;
30using HEAL.Attic;
31using HeuristicLab.Common;
32using HeuristicLab.Core;
33using HeuristicLab.MainForm;
34
35namespace HeuristicLab.Optimizer {
36  [View("Start Page")]
37  public partial class StartPage : HeuristicLab.MainForm.WindowsForms.View {
38    private const string StandardProblemsGroupName = "Standard Problems";
39    private const string DataAnalysisGroupName = "Data Analysis";
40    private const string ScriptsGroupName = "Scripts";
41    private const string UncategorizedGroupName = "Uncategorized";
42    private const string SampleNamePrefix = "HeuristicLab.Optimizer.Documents.";
43    private const string SampleNameSuffix = ".hl";
44
45    private readonly Dictionary<ListViewGroup, List<string>> groupLookup = new Dictionary<ListViewGroup, List<string>>();
46    private readonly ListViewGroup standardProblemsGroup = new ListViewGroup(StandardProblemsGroupName);
47    private readonly ListViewGroup dataAnalysisGroup = new ListViewGroup(DataAnalysisGroupName);
48    private readonly ListViewGroup scriptsGroup = new ListViewGroup(ScriptsGroupName);
49    private readonly ListViewGroup uncategorizedGroup = new ListViewGroup(UncategorizedGroupName);
50
51    private IProgress progress;
52
53    public StartPage() {
54      InitializeComponent();
55    }
56
57    protected override void OnInitialized(EventArgs e) {
58      base.OnInitialized(e);
59      Assembly assembly = Assembly.GetExecutingAssembly();
60      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
61                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
62      titleLabel.Text = "HeuristicLab Optimizer";
63      if (version != null) titleLabel.Text += " " + version.Version;
64
65      try {
66        using (Stream stream = assembly.GetManifestResourceStream(typeof(StartPage), "Documents.FirstSteps.rtf"))
67          firstStepsRichTextBox.LoadFile(stream, RichTextBoxStreamType.RichText);
68      } catch (Exception) { }
69
70      samplesListView.Enabled = false;
71      samplesListView.Groups.Add(standardProblemsGroup);
72      samplesListView.Groups.Add(dataAnalysisGroup);
73      samplesListView.Groups.Add(scriptsGroup);
74      samplesListView.Groups.Add(uncategorizedGroup);
75      FillGroupLookup();
76
77      showStartPageCheckBox.Checked = Properties.Settings.Default.ShowStartPage;
78
79      ThreadPool.QueueUserWorkItem(new WaitCallback(LoadSamples));
80    }
81
82    protected override void OnClosing(FormClosingEventArgs e) {
83      base.OnClosing(e);
84      if (e.CloseReason == CloseReason.UserClosing) {
85        e.Cancel = true;
86        this.Hide();
87      }
88    }
89
90    private void LoadSamples(object state) {
91      progress = Progress.ShowOnControl(samplesListView, "Loading...");
92      try {
93        var assembly = Assembly.GetExecutingAssembly();
94        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(SampleNameSuffix));
95        int count = samples.Count();
96
97        foreach (var entry in groupLookup) {
98          var group = entry.Key;
99          var sampleList = entry.Value;
100          foreach (var sampleName in sampleList) {
101            string resourceName = SampleNamePrefix + sampleName + SampleNameSuffix;
102            LoadSample(resourceName, assembly, group, count);
103          }
104        }
105
106        var categorizedSamples = groupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
107        var uncategorizedSamples = samples.Except(categorizedSamples);
108
109        foreach (var resourceName in uncategorizedSamples) {
110          LoadSample(resourceName, assembly, uncategorizedGroup, count);
111        }
112
113        OnAllSamplesLoaded();
114      } finally {
115        Progress.HideFromControl(samplesListView);
116      }
117    }
118
119    private void LoadSample(string name, Assembly assembly, ListViewGroup group, int count) {
120      using (var stream = assembly.GetManifestResourceStream(name)) {
121        var serializer = new ProtoBufSerializer();
122        // TODO remove deflateStream with next release of HEAL.Attic
123        using (var deflateStream = new DeflateStream(stream, CompressionMode.Decompress, true)) {
124          var item = (NamedItem)serializer.Deserialize(deflateStream, false);
125          OnSampleLoaded(item, group, 1.0 / count);
126        }
127      }
128    }
129
130    private void FillGroupLookup() {
131      var standardProblems = new List<string> { "ALPSGA_TSP", "ES_Griewank", "OSES_Griewank", "GA_Grouping", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
132                "IslandGA_TSP", "LS_Knapsack", "PSO_Rastrigin", "RAPGA_JSSP",
133                "SA_Rastrigin", "SGP_SantaFe", "GP_Multiplexer", "SGP_Robocode", "SS_VRP", "TS_TSP", "TS_VRP", "VNS_OP", "VNS_TSP", "GA_BPP"
134        };
135      groupLookup[standardProblemsGroup] = standardProblems;
136      var dataAnalysisProblems = new List<string> { "ALPSGP_SymReg", "SGP_SymbClass", "SGP_SymbReg", "OSGP_SymReg", "OSGP_TimeSeries", "GE_SymbReg", "GPR" };
137      groupLookup[dataAnalysisGroup] = dataAnalysisProblems;
138      var scripts = new List<string> { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script",
139                                       "GridSearch_RF_Classification_Script", "GridSearch_RF_Regression_Script",
140                                       "GridSearch_SVM_Classification_Script", "GridSearch_SVM_Regression_Script" };
141      groupLookup[scriptsGroup] = scripts;
142    }
143
144    private void OnSampleLoaded(INamedItem sample, ListViewGroup group, double progress) {
145      if (InvokeRequired)
146        Invoke(new Action<INamedItem, ListViewGroup, double>(OnSampleLoaded), sample, group, progress);
147      else {
148        ListViewItem item = new ListViewItem(new string[] { sample.Name, sample.Description }, group);
149        item.ToolTipText = sample.ItemName + ": " + sample.ItemDescription;
150        samplesListView.SmallImageList.Images.Add(sample.ItemImage);
151        item.ImageIndex = samplesListView.SmallImageList.Images.Count - 1;
152        item.Tag = sample;
153        samplesListView.Items.Add(item);
154        this.progress.ProgressValue += progress;
155      }
156    }
157    private void OnAllSamplesLoaded() {
158      if (InvokeRequired)
159        Invoke(new Action(OnAllSamplesLoaded));
160      else {
161        samplesListView.Enabled = samplesListView.Items.Count > 0;
162        if (samplesListView.Items.Count > 0) {
163          for (int i = 0; i < samplesListView.Columns.Count; i++)
164            samplesListView.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
165        }
166      }
167    }
168
169    private void firstStepsRichTextBox_LinkClicked(object sender, LinkClickedEventArgs e) {
170      System.Diagnostics.Process.Start(e.LinkText);
171    }
172
173    private void samplesListView_DoubleClick(object sender, EventArgs e) {
174      if (samplesListView.SelectedItems.Count == 1) {
175        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
176        try {
177          mainForm.SetWaitCursor();
178          mainForm.ShowContent((IContent)((IItem)samplesListView.SelectedItems[0].Tag).Clone());
179        } finally {
180          mainForm.ResetWaitCursor();
181        }
182
183      }
184    }
185    private void samplesListView_ItemDrag(object sender, ItemDragEventArgs e) {
186      ListViewItem listViewItem = (ListViewItem)e.Item;
187      IItem item = (IItem)listViewItem.Tag;
188      DataObject data = new DataObject();
189      data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, item);
190      DragDropEffects result = DoDragDrop(data, DragDropEffects.Copy);
191    }
192
193    private void showStartPageCheckBox_CheckedChanged(object sender, EventArgs e) {
194      Properties.Settings.Default.ShowStartPage = showStartPageCheckBox.Checked;
195      Properties.Settings.Default.Save();
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.