/KillerameiseGui/MainForm.cs
C# | 357 lines | 290 code | 52 blank | 15 comment | 25 complexity | 225cd309a7bccc6417cd67f4a458ab29 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using KillerAnts.Graph;
- using KillerAnts.Ants;
- using KillerAnts.Miscellaneous;
- using System.Threading;
- using KillerAnts.Properties;
- using System.IO;
-
-
- namespace KillerAnts
- {
- public partial class MainForm : Form
- {
- public String tspFileName = Settings.Default.tspFileName;
- private Node[] nodes;
-
- private AntStats globalAntStats;
- private SimulationParams globalSimParams;
-
- private int pgbMaximum = 0;
- private double bestDistance = 0;
- private double bestDistanceGlobal = 0;
- private double averageDistance = 0;
- private double averageDistanceGlobal = 0;
- private DateTime startTime;
- private DateTime stopTime;
-
- private DateTime lastGraphRefresh = DateTime.UtcNow;
- private const int graphRefreshTiming = 1000; // milliseconds
-
- private bool calculationIsRunning = false;
-
- private Thread workerThread;
-
- delegate void setIterationDelegate(string text);
- delegate void setAntDelegate(AntStats stats);
-
- public MainForm()
- {
- InitializeComponent();
-
- tspFileName = File.Exists(Settings.Default.tspFileName) ? Settings.Default.tspFileName : DefaultSettings.Default.tspFileName;
- if (File.Exists(tspFileName))
- {
- nodes = TourIO.NodesFromFile(tspFileName);
- }
- else
- {
- nodes = new Node[0]; // new empty tsp, because the tspFileName is still the same as the default one, it is can't be saved in the editor (save as works)
- }
- RedrawCanvas();
-
- ToolTip forButton = new ToolTip();
- string tooltiptext = "Fortschrittsbalken zeigt die relative Länge des Weges an";
- forButton.SetToolTip(groupBox1, tooltiptext);
- forButton.SetToolTip(groupBox2, tooltiptext);
- forButton.SetToolTip(labAverageDistance, tooltiptext);
- forButton.SetToolTip(labAverageDistanceGlobal, tooltiptext);
- forButton.SetToolTip(labBestDistance, tooltiptext);
- forButton.SetToolTip(labBestDistanceGlobal, tooltiptext);
- forButton.SetToolTip(valAverageDistance, tooltiptext);
- forButton.SetToolTip(valAverageDistanceGlobal, tooltiptext);
- forButton.SetToolTip(valBestDistance, tooltiptext);
- forButton.SetToolTip(valBestDistanceGlobal, tooltiptext);
- forButton.SetToolTip(pgbAverageDistance, tooltiptext);
- forButton.SetToolTip(pgbAverageDistanceGlobal, tooltiptext);
- forButton.SetToolTip(pgbBestDistance, tooltiptext);
- forButton.SetToolTip(pgbBestDistanceGlobal, tooltiptext);
- forButton.SetToolTip(pgbBestDistanceGlobal, tooltiptext);
-
- forButton.SetToolTip(imgWalkingAnt, "I'm walking");
- forButton.AutomaticDelay = 500;
-
- }
-
- private void koordinatenLadenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- toolStripStatusLabel3.Text = "Geladene Datei:";
- dlgOpenTspFile.ShowDialog();
- if (dlgOpenTspFile.FileName != null && dlgOpenTspFile.FileName.Length > 0)
- {
- toolStripStatusLabel4.Text = dlgOpenTspFile.FileName;
- tspFileName = dlgOpenTspFile.FileName;
- nodes = TourIO.NodesFromFile(tspFileName);
- RedrawCanvas();
- }
- }
-
- private void Close(object sender, EventArgs e)
- {
- MainForm.ActiveForm.Close();
- }
-
- private void MainForm_Load(object sender, EventArgs e)
- {
- toolStripStatusLabel1.Visible = false;
- RedrawCanvas();
- }
-
- private void CancelCalculation(object sender, EventArgs e)
- {
- stopTime = DateTime.UtcNow;
- workerThread.Abort();
- SetCalculationMode(false);
- }
-
- public void RedrawCanvas()
- {
- graphCanvas.Clear();
- if (nodes.Length > 1)
- {
- graphCanvas.FitToNodes(nodes);
- graphCanvas.DrawNodes(nodes);
- }
- }
-
-
- private void StartSimulation(object sender, EventArgs e)
- {
- RedrawCanvas();
-
- toolStripStatusLabel1.Visible = true;
- toolStripStatusLabel2.Text = "0,0 ms";
-
- // reset progress bars
- pgbMaximum = 0;
- pgbBestDistance.Value = 0;
- pgbBestDistanceGlobal.Value = 0;
- pgbAverageDistance.Value = 0;
- pgbAverageDistanceGlobal.Value = 0;
- pgbIteration.Value = 0;
- pgbIteration.Maximum = paramPanel.GetSimulationParams().iterations;
- startTime = DateTime.UtcNow;
-
- // clear progress bar labels
- valBestDistance.Text = String.Empty;
- valBestDistanceGlobal.Text = String.Empty;
- valAverageDistance.Text = String.Empty;
- valAverageDistanceGlobal.Text = String.Empty;
-
- valIterationen.Text = "1 / 1";
-
- // solve trivial problems instantly
- switch (nodes.Length)
- {
- case 0:
- case 1:
- break;
-
- case 2:
- int[] wayPoints2 = { 0, 1 };
- graphCanvas.HighlightTour(nodes, wayPoints2);
- break;
-
- case 3:
- int[] wayPoints3 = { 0, 1, 2 };
- graphCanvas.HighlightTour(nodes, wayPoints3);
- break;
-
- default:
- valIterationen.Text = "0 / " + paramPanel.GetSimulationParams().iterations;
- SetCalculationMode(true);
- startTime = DateTime.UtcNow;
- statsTimer.Start();
-
- workerThread = new Thread(this.RunAnts);
- globalSimParams = paramPanel.GetSimulationParams();
- workerThread.Start(globalSimParams);
- break;
- }
- }
-
- private void RunAnts(Object data)
- {
- SimulationParams simParam = (SimulationParams)data;
- AntColony antColony = new AntColony(simParam, nodes);
-
- AntStats stats;
- int maxIterations = paramPanel.GetSimulationParams().iterations;
- for (int i = 1; i <= maxIterations; i++)
- {
- antColony.NextIteration();
- SetIteration(i + " / " + maxIterations);
- stats = antColony.GetStats();
- SetAntStats(stats);
- if (i == 1)
- {
- pgbMaximum = (int)(1.25 * stats.GetAverageDistance());
- }
- bestDistance = stats.GetBestDistance();
- bestDistanceGlobal = stats.GetBestDistanceGlobal();
- averageDistance = stats.GetAverageDistance();
- averageDistanceGlobal = stats.GetAverageDistanceGlobal();
-
- // always draw first and last iteration
- // and if the refresh checkbox is checked als every iterationModulo iteration
- bool draw = (i == maxIterations) || (i == 1) || (((DateTime.UtcNow - lastGraphRefresh).TotalMilliseconds > graphRefreshTiming) && cbRefreshCanvas.Checked);
- if (draw)
- {
- lastGraphRefresh = DateTime.UtcNow;
- graphCanvas.Clear();
- graphCanvas.DrawEdges(nodes, antColony.GetEdges());
- graphCanvas.HighlightTour(nodes, stats.GetBestTourGlobalWayPoints());
- graphCanvas.DrawNodes(nodes);
- }
-
- }
-
- calculationIsRunning = false;
- stopTime = DateTime.UtcNow;
- }
-
- private void SetIteration(string text)
- {
- if (this.valIterationen.InvokeRequired)
- {
- setIterationDelegate iterationDelegate = new setIterationDelegate(SetIteration);
- this.Invoke(iterationDelegate, new object[] { text });
- }
- else
- {
- valIterationen.Text = text;
- pgbIteration.PerformStep();
- }
- }
-
- private void SetAntStats(AntStats stats)
- {
- if (this.valIterationen.InvokeRequired)
- {
- setAntDelegate iterationDelegate = new setAntDelegate(SetAntStats);
- this.Invoke(iterationDelegate, new object[] { stats });
- }
- else
- {
- // save the stats to global
- globalAntStats = stats;
- ergebnisSpeichernToolStripMenuItem.Enabled = true;
- }
- }
-
- private void SetCalculationMode(bool calculating)
- {
- calculationIsRunning = calculating;
- btnStart.Enabled = !calculating;
- btnCancel.Enabled = calculating;
- menuStrip1.Enabled = !calculating;
- imgWalkingAnt.Enabled = calculating;
- ergebnisSpeichernToolStripMenuItem.Enabled = !calculating;
-
- if (calculating)
- {
- paramPanel.Disable();
- }
- else
- {
- paramPanel.Enable();
- }
- }
-
- private void StatsTimerTick(object sender, EventArgs e)
- {
- // refresh time running
- toolStripStatusLabel2.Text = String.Format("{0:0,0}", ((DateTime.UtcNow - startTime).TotalMilliseconds)) + " ms";
-
- // pgbMaximum will be set to 0 after starting a simulation and set to something above the average distance after
- // the first iteration
- if (pgbMaximum > 0)
- {
- RefreshStats();
- }
-
- if (!calculationIsRunning)
- {
- SetCalculationMode(false);
-
- // stop this timer
- ((System.Windows.Forms.Timer)sender).Stop();
- }
- }
-
- private void RefreshStats()
- {
- // if the progress bar maxima changed adjust it for all progress bars
- // this happens after the first iteration
- if (pgbBestDistance.Maximum != pgbMaximum)
- {
- pgbBestDistance.Maximum = pgbMaximum;
- pgbBestDistanceGlobal.Maximum = pgbMaximum;
- pgbAverageDistance.Maximum = pgbMaximum;
- pgbAverageDistanceGlobal.Maximum = pgbMaximum;
- }
-
- // refresh the progress bars, but do not set values above pbgMaximum
- // (which is also the maximum of the progress bars)
- pgbBestDistance.Value = (int)(bestDistance < pgbMaximum ? bestDistance : pgbMaximum);
- pgbBestDistanceGlobal.Value = (int)(bestDistanceGlobal < pgbMaximum ? bestDistanceGlobal : pgbMaximum);
- pgbAverageDistance.Value = (int)(averageDistance < pgbMaximum ? averageDistance : pgbMaximum);
- pgbAverageDistanceGlobal.Value = (int)(averageDistanceGlobal < pgbMaximum ? averageDistanceGlobal : pgbMaximum);
-
- // refresh labels right to the progress bars
- valBestDistance.Text = String.Format("{0:0.00}", bestDistance);
- valBestDistanceGlobal.Text = String.Format("{0:0.00}", bestDistanceGlobal);
- valAverageDistance.Text = String.Format("{0:0.00}", averageDistance);
- valAverageDistanceGlobal.Text = String.Format("{0:0.00}", averageDistanceGlobal);
- }
-
- private void editorToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Editor editor = new Editor(tspFileName, nodes);
- editor.ShowDialog();
- nodes = editor.getNodes();
- tspFileName = editor.GetFileName();
- RedrawCanvas();
- }
-
- private void graphCanvas_Paint(object sender, PaintEventArgs e)
- {
- RedrawCanvas();
- }
-
- private void ergebnisSpeichernToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "Optimale Tour Datei|*.opt.tour";
- saveFileDialog1.Title = "Optimale Tour Datei speichern";
- saveFileDialog1.ShowDialog();
-
- if (saveFileDialog1.FileName != null && saveFileDialog1.FileName.Length > 0)
- {
- Tour bestTour = globalAntStats.GetBestTourGlobal();
- double time = (stopTime - startTime).TotalMilliseconds;
- TourIO.SaveOptimalTour(saveFileDialog1.FileName, globalSimParams, nodes, bestTour, globalAntStats.GetAverageDistanceGlobal(), time);
- }
- }
-
- private void OpenAbout(object sender, EventArgs e)
- {
- AboutForm about = new AboutForm();
- about.ShowDialog();
- }
-
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- Settings.Default.tspFileName = tspFileName;
- Settings.Default.Save();
- }
-
- }
- }