diff options
Diffstat (limited to 'src/Plotter/Plot.cs')
-rw-r--r-- | src/Plotter/Plot.cs | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/Plotter/Plot.cs b/src/Plotter/Plot.cs new file mode 100644 index 0000000..6732fe1 --- /dev/null +++ b/src/Plotter/Plot.cs @@ -0,0 +1,135 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Windows.Forms; +using TSP; + +namespace Plotter +{ + public partial class PlotWindow : Form + { + // List<TourPlot[]> Plots; + List<TourPlot[]> Plots = new(); + int CurrentGraph { get; set; } = 0; + bool AutoSave = false; + + public PlotWindow() + { + InitializeComponent(); + + var folderBrowserDialog = new FolderBrowserDialog(); + + // Show the FolderBrowserDialog. + DialogResult result = folderBrowserDialog.ShowDialog(); + + foreach (string graphFile in Directory.GetFiles("graphs/", "*.txt")) + { + Plots.Add(GetTourSet(graphFile)); + if (result == DialogResult.OK) + SaveSet(folderBrowserDialog.SelectedPath, Plots.Last()); + } + + NextGraph.Enabled = (Plots.Count > 1); + PrevGraph.Enabled = false; + + RenderWindow(); + } + + void RenderWindow() + { + Text = $"TSP: Graph {CurrentGraph + 1}"; + GraphLabel.Text = $"Graph {CurrentGraph + 1}"; + foreach (var plot in Plots[CurrentGraph]) + plot.Render(); + } + + TourPlot[] GetTourSet(string graphFile) + { + string graphName = Path.GetFileNameWithoutExtension(graphFile); + string coordsFile = $"graphs/{graphName}.csv"; + + TourPlot bruteForce = new(graphName, "bruteforce", BruteForcePlot, BruteForceWeight, BruteForceTime); + TourPlot approximate = new(graphName, "approximation", EstimationPlot, EstimatedWeight, EstimatedTime); + TourPlot mst = new(graphName, "mst", MSTPlot, MSTWeight, MSTTime); + + Stopwatch stopWatch = new Stopwatch(); + stopWatch.Start(); + bruteForce.Tour = TSPSolver.Solve(graphFile, coordsFile, 0); + stopWatch.Stop(); + bruteForce.Runtime = stopWatch.Elapsed; + + + stopWatch.Reset(); + stopWatch.Start(); + approximate.Tour = TSPSolver.Approximate(graphFile, coordsFile, 0); + stopWatch.Stop(); + approximate.Runtime = stopWatch.Elapsed; + + + stopWatch.Reset(); + stopWatch.Start(); + mst.Tour = TSPSolver.MST(graphFile, coordsFile, 0); + stopWatch.Stop(); + mst.Runtime = stopWatch.Elapsed; + + return new[] { bruteForce, approximate, mst }; + } + + private void SaveSet(string folderName, TourPlot[] plots) + { + string header = $"Graph,Type,Runtime,Weight," + + String.Join(',', Enumerable.Repeat("City", 13).Zip(Enumerable.Range(1, 13), (l, n) => $"{l}{n}")); + + if (!File.Exists(Path.Join(folderName, "data.csv"))) + File.WriteAllText(Path.Join(folderName, "data.csv"), $"{header}\n"); + + plots[0].Save(folderName); + plots[1].Save(folderName); + plots[2].Save(folderName); + } + + private void Canvas_SizeChanged(object sender, EventArgs e) + { + RenderWindow(); + } + + private void NextGraph_Click(object sender, EventArgs e) + { + CurrentGraph++; + RenderWindow(); + PrevGraph.Enabled = true; + NextGraph.Enabled = (CurrentGraph < Plots.Count - 1); + } + + private void PrevGraph_Click(object sender, EventArgs e) + { + CurrentGraph--; + RenderWindow(); + NextGraph.Enabled = true; + PrevGraph.Enabled = (CurrentGraph > 0); + } + + private void SaveGraph_Click(object sender, EventArgs e) + { + var folderBrowserDialog = new FolderBrowserDialog(); + + // Show the FolderBrowserDialog. + DialogResult result = folderBrowserDialog.ShowDialog(); + if (result == DialogResult.OK) + SaveSet(folderBrowserDialog.SelectedPath, Plots[CurrentGraph]); + } + + private void SaveAll_Click(object sender, EventArgs e) + { + var folderBrowserDialog = new FolderBrowserDialog(); + + // Show the FolderBrowserDialog. + DialogResult result = folderBrowserDialog.ShowDialog(); + if (result == DialogResult.OK) + for (int i = 0; i < Plots.Count; i++) + SaveSet(folderBrowserDialog.SelectedPath, Plots[i]); + } + } +} |