#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Pipes;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using HeuristicLab.Data;
using HeuristicLab.Data.Views;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
namespace HeuristicLab.Encodings.PermutationEncoding.Views {
[View("Permutation History View")]
[Content(typeof(Permutation), IsDefaultView = false)]
public partial class PermutationHistoryView : AsynchronousContentView {
public new Permutation Content {
get { return (Permutation)base.Content; }
set { base.Content = value; }
}
private List contentHistory;
public PermutationHistoryView() {
InitializeComponent();
}
#region Register Content Events
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
}
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
}
#endregion
protected override void OnContentChanged() {
base.OnContentChanged();
if (Content == null) {
contentHistory = null;
trackBar.Maximum = 0;
permutationView.Content = null;
} else {
contentHistory =
Content.GetHistory().Select(a => new Permutation(Content.PermutationType, (IntArray) a)).Reverse().ToList();
trackBar.Maximum = contentHistory.Count-1;
trackBar.Value = trackBar.Maximum;
permutationView.Content = Content;
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
}
#region Event Handlers
#endregion
private void trackBar_Scroll(object sender, System.EventArgs e) {
permutationView.Content = contentHistory[trackBar.Value];
}
private class Edge {
public int Start, End, Offset;
public Edge(int start, int end, int offset) {
Start = start;
End = end;
Offset = offset;
}
}
private void tabControl_Selected(object sender, TabControlEventArgs e) {
if (e.TabPage == graphTabPage) {
if (chart1.Series.Count == 0) {
progressBar1.Maximum = 100;
progressBar1.Value = 0;
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.ProgressChanged += (s, args) => {
if (args.ProgressPercentage >= 0) {
progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.Value = Math.Min(args.ProgressPercentage, progressBar1.Maximum);
} else {
progressBar1.Style = ProgressBarStyle.Marquee;
}
};
var edgePositions = Enumerable.Range(0, 8).Select(i => new List()).ToList();
worker.DoWork += (s, args) => {
var edgeCount = new Dictionary, int>();
var progress = 0;
foreach (var permutation in contentHistory) {
int last = permutation[0];
worker.ReportProgress(100 * progress++ / contentHistory.Count);
if (worker.CancellationPending) {
args.Cancel = true;
return;
}
for (int i = 1; i < permutation.Length; i++) {
var key = Tuple.Create(last, permutation[i]);
int count = 0;
edgeCount.TryGetValue(key, out count);
edgeCount[key] = count + 1;
last = permutation[i];
}
}
worker.ReportProgress(-1);
var topEdges = edgeCount
.OrderByDescending(kvp => kvp.Value)
.Take(edgePositions.Count)
.Select((kvp, i) => new {i, kvp.Key, kvp.Value})
.ToDictionary(x => x.Key, x => new {count = x.Value, index = x.i});
worker.ReportProgress(0);
int n = 0;
foreach (var permutation in contentHistory) {
n++;
int last = permutation[0];
worker.ReportProgress(100 * progress++ / contentHistory.Count);
if (worker.CancellationPending) {
args.Cancel = true;
return;
}
for (int i = 1; i < permutation.Length; i++) {
var key = Tuple.Create(last, permutation[i]);
if (topEdges.ContainsKey(key)) {
edgePositions[topEdges[key].index].Add(i);
int count = 0;
edgeCount.TryGetValue(key, out count);
edgeCount[key] = count + 1;
}
last = permutation[i];
}
foreach (List list in edgePositions) {
while (list.Count < n) {
if (list.Count > 0)
list.Add(list[list.Count-1]);
else
list.Add(0);
}
}
}
};
worker.RunWorkerCompleted += (s, args) => {
chart1.Series.Clear();
foreach (var list in edgePositions) {
var series = new Series();
series.ChartType = SeriesChartType.FastLine;
foreach (var n in list) {
series.Points.Add(n);
}
chart1.Series.Add(series);
}
};
worker.RunWorkerAsync();
}
}
}
}
}