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 Plots; List 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, BruteForcePlot, BruteForceWeight, BruteForceTime); TourPlot approximate = new(graphName, EstimationPlot, EstimatedWeight, EstimatedTime); TourPlot mst = new(graphName, 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); // TODO create estimation function 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) { if (!System.IO.Directory.Exists(Path.Join(folderName, plots[0].GraphName))) System.IO.Directory.CreateDirectory(Path.Join(folderName, plots[0].GraphName)); plots[0].Save(Path.Join(folderName, plots[0].GraphName, $"bruteforce")); plots[1].Save(Path.Join(folderName, plots[0].GraphName, $"approximate")); plots[2].Save(Path.Join(folderName, plots[0].GraphName, $"mst")); } 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]); } } }