Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.Encodings.PermutationEncoding.Views/3.3/PermutationHistoryView.cs @ 16147

Last change on this file since 16147 was 14658, checked in by epitzer, 8 years ago

#2727 add tracking (snapshot) analyzer and new visualization for permutations

File size: 6.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.IO.Pipes;
26using System.Linq;
27using System.Windows.Forms;
28using System.Windows.Forms.DataVisualization.Charting;
29using HeuristicLab.Data;
30using HeuristicLab.Data.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33
34namespace HeuristicLab.Encodings.PermutationEncoding.Views {
35
36  [View("Permutation History View")]
37  [Content(typeof(Permutation), IsDefaultView = false)]
38  public partial class PermutationHistoryView : AsynchronousContentView {
39
40    public new Permutation Content {
41      get { return (Permutation)base.Content; }
42      set { base.Content = value; }
43    }
44
45    private List<Permutation> contentHistory;
46
47    public PermutationHistoryView() {
48      InitializeComponent();
49    }
50
51    #region Register Content Events
52    protected override void DeregisterContentEvents() {
53      base.DeregisterContentEvents();
54    }
55    protected override void RegisterContentEvents() {
56      base.RegisterContentEvents();
57    }
58    #endregion
59
60    protected override void OnContentChanged() {
61      base.OnContentChanged();
62      if (Content == null) {
63        contentHistory = null;
64        trackBar.Maximum = 0;
65        permutationView.Content = null;
66      } else {
67        contentHistory =
68          Content.GetHistory().Select(a => new Permutation(Content.PermutationType, (IntArray) a)).Reverse().ToList();
69        trackBar.Maximum = contentHistory.Count-1;
70        trackBar.Value = trackBar.Maximum;
71        permutationView.Content = Content;
72      }
73    }
74
75    protected override void SetEnabledStateOfControls() {
76      base.SetEnabledStateOfControls();
77    }
78
79    #region Event Handlers
80
81    #endregion
82
83    private void trackBar_Scroll(object sender, System.EventArgs e) {
84      permutationView.Content = contentHistory[trackBar.Value];
85    }
86
87    private class Edge {
88      public int Start, End, Offset;
89
90      public Edge(int start, int end, int offset) {
91        Start = start;
92        End = end;
93        Offset = offset;
94      }
95    }
96
97    private void tabControl_Selected(object sender, TabControlEventArgs e) {
98      if (e.TabPage == graphTabPage) {
99        if (chart1.Series.Count == 0) {
100          progressBar1.Maximum = 100;
101          progressBar1.Value = 0;
102          var worker = new BackgroundWorker();
103          worker.WorkerReportsProgress = true;
104          worker.WorkerSupportsCancellation = true;
105          worker.ProgressChanged += (s, args) => {
106            if (args.ProgressPercentage >= 0) {
107              progressBar1.Style = ProgressBarStyle.Continuous;
108              progressBar1.Value = Math.Min(args.ProgressPercentage, progressBar1.Maximum);
109            } else {
110              progressBar1.Style = ProgressBarStyle.Marquee;
111            }
112          };
113          var edgePositions = Enumerable.Range(0, 8).Select(i => new List<int>()).ToList();
114          worker.DoWork += (s, args) => {
115            var edgeCount = new Dictionary<Tuple<int, int>, int>();
116            var progress = 0;
117            foreach (var permutation in contentHistory) {
118              int last = permutation[0];
119              worker.ReportProgress(100 * progress++ / contentHistory.Count);
120              if (worker.CancellationPending) {
121                args.Cancel = true;
122                return;
123              }
124              for (int i = 1; i < permutation.Length; i++) {
125                var key = Tuple.Create(last, permutation[i]);
126                int count = 0;
127                edgeCount.TryGetValue(key, out count);
128                edgeCount[key] = count + 1;
129                last = permutation[i];
130              }
131            }
132            worker.ReportProgress(-1);
133            var topEdges = edgeCount
134              .OrderByDescending(kvp => kvp.Value)
135              .Take(edgePositions.Count)
136              .Select((kvp, i) => new {i, kvp.Key, kvp.Value})
137              .ToDictionary(x => x.Key, x => new {count = x.Value, index = x.i});
138            worker.ReportProgress(0);
139            int n = 0;
140            foreach (var permutation in contentHistory) {
141              n++;
142              int last = permutation[0];
143              worker.ReportProgress(100 * progress++ / contentHistory.Count);
144              if (worker.CancellationPending) {
145                args.Cancel = true;
146                return;
147              }
148              for (int i = 1; i < permutation.Length; i++) {
149                var key = Tuple.Create(last, permutation[i]);
150                if (topEdges.ContainsKey(key)) {
151                  edgePositions[topEdges[key].index].Add(i);
152                  int count = 0;
153                  edgeCount.TryGetValue(key, out count);
154                  edgeCount[key] = count + 1;
155                }
156                last = permutation[i];
157              }
158              foreach (List<int> list in edgePositions) {
159                while (list.Count < n) {
160                  if (list.Count > 0)
161                    list.Add(list[list.Count-1]);
162                  else
163                    list.Add(0);
164                }
165              }
166            }
167          };
168          worker.RunWorkerCompleted += (s, args) => {
169            chart1.Series.Clear();
170            foreach (var list in edgePositions) {
171              var series = new Series();
172              series.ChartType = SeriesChartType.FastLine;
173              foreach (var n in list) {
174                series.Points.Add(n);
175              }
176              chart1.Series.Add(series);
177            }
178          };
179          worker.RunWorkerAsync();
180        }
181      }
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.