diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CS340.Plotter/Plot.cs | 38 | ||||
-rw-r--r-- | src/CS340.Plotter/TourPlot.cs | 44 | ||||
-rw-r--r-- | src/CS340.TSP/City.cs | 10 | ||||
-rw-r--r-- | src/CS340.TSP/TSPSolver.cs | 11 |
4 files changed, 62 insertions, 41 deletions
diff --git a/src/CS340.Plotter/Plot.cs b/src/CS340.Plotter/Plot.cs index 89894e4..e07cdc0 100644 --- a/src/CS340.Plotter/Plot.cs +++ b/src/CS340.Plotter/Plot.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Windows.Forms; using TSP; @@ -12,13 +13,25 @@ namespace Plotter // 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(); + if (result == DialogResult.OK) + AutoSave = true; + foreach (string graphFile in Directory.GetFiles("graphs/", "*.txt")) + { Plots.Add(GetTourSet(graphFile)); + if (AutoSave) + SaveSet(folderBrowserDialog.SelectedPath, Plots.Last()); + } NextGraph.Enabled = (Plots.Count > 1); PrevGraph.Enabled = false; @@ -36,12 +49,12 @@ namespace Plotter TourPlot[] GetTourSet(string graphFile) { - string coordsFile = graphFile.Replace(".txt", ".csv"); + string graphName = Path.GetFileNameWithoutExtension(graphFile); + string coordsFile = $"graphs/{graphName}.csv"; - - TourPlot bruteForce = new(BruteForcePlot, BruteForceWeight, BruteForceTime); - TourPlot approximate = new(EstimationPlot, EstimatedWeight, EstimatedTime); - TourPlot mst = new(MSTPlot, MSTWeight, MSTTime); + 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(); @@ -66,11 +79,14 @@ namespace Plotter return new[] { bruteForce, approximate, mst }; } - private void SaveSet(string folderName, int graphNum) + private void SaveSet(string folderName, TourPlot[] plots) { - Plots[graphNum][0].Save(Path.Join(folderName, $"graph{graphNum + 1}_trueshortest.png")); - Plots[graphNum][1].Save(Path.Join(folderName, $"graph{graphNum + 1}_approximate.png")); - Plots[graphNum][2].Save(Path.Join(folderName, $"graph{graphNum + 1}_mst.png")); + 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.png")); + plots[1].Save(Path.Join(folderName, plots[0].GraphName, $"approximate.png")); + plots[2].Save(Path.Join(folderName, plots[0].GraphName, $"mst.png")); } private void Canvas_SizeChanged(object sender, EventArgs e) @@ -101,7 +117,7 @@ namespace Plotter // Show the FolderBrowserDialog. DialogResult result = folderBrowserDialog.ShowDialog(); if (result == DialogResult.OK) - SaveSet(folderBrowserDialog.SelectedPath, CurrentGraph); + SaveSet(folderBrowserDialog.SelectedPath, Plots[CurrentGraph]); } private void SaveAll_Click(object sender, EventArgs e) @@ -112,7 +128,7 @@ namespace Plotter DialogResult result = folderBrowserDialog.ShowDialog(); if (result == DialogResult.OK) for (int i = 0; i < Plots.Count; i++) - SaveSet(folderBrowserDialog.SelectedPath, i); + SaveSet(folderBrowserDialog.SelectedPath, Plots[i]); } } } diff --git a/src/CS340.Plotter/TourPlot.cs b/src/CS340.Plotter/TourPlot.cs index 30d0f2a..09cd514 100644 --- a/src/CS340.Plotter/TourPlot.cs +++ b/src/CS340.Plotter/TourPlot.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; using System.Drawing; using System.Linq; using System.Windows.Forms; @@ -9,25 +8,48 @@ namespace Plotter { public class TourPlot { + public string GraphName { get; set; } public Tour Tour { get; set; } public PictureBox Canvas { get; set; } public Label WeightLabel { get; set; } public Label RuntimeLabel { get; set; } public TimeSpan RunTime { get; set; } - public TourPlot(PictureBox canvas, Label weightLabel, Label runtimeLabel) + public TourPlot(string graphName, PictureBox canvas, Label weightLabel, Label runtimeLabel) { + GraphName = graphName; Canvas = canvas; WeightLabel = weightLabel; RuntimeLabel = runtimeLabel; } + public void Save(string filename) => + Save(filename, Canvas.Width, Canvas.Height); + + public void Save(string filename, int width, int height) => + Draw(300, 300).Save(filename); + public void Render() { + Draw(Canvas.Width, Canvas.Height); + + // copy the bitmap to the picturebox (double buffered) + Canvas.Image?.Dispose(); + Canvas.Image = Draw(Canvas.Width, Canvas.Height); + + // set runtime and weight labels + RuntimeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", + RunTime.Hours, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds / 10); + + WeightLabel.Text = Tour.Weight.ToString("F3"); + } + + public Bitmap Draw(int width, int height) + { const int Scaler = 25; const int PlotSize = 100; - using Bitmap bmp = new(Canvas.Width, Canvas.Height); + using Bitmap bmp = new(width, height); using Graphics gfx = Graphics.FromImage(bmp); using Pen pen = new(Color.Black); using Pen gridPen = new(Color.LightGray); @@ -68,27 +90,17 @@ namespace Plotter gfx.DrawLine(pen, cityPoint, parentPoint); } - // copy the bitmap to the picturebox (double buffered) - Canvas.Image?.Dispose(); - Canvas.Image = (Bitmap)bmp.Clone(); - - // set runtime and weight labels - RuntimeLabel.Text = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", - RunTime.Hours, RunTime.Minutes, RunTime.Seconds, RunTime.Milliseconds / 10); - - WeightLabel.Text = Tour.Weight.ToString("F3"); + return (Bitmap)bmp.Clone(); // helper function to unify the scaling of images Point ScaleLocation(Coordinate coordinate) { - int x = Canvas.Width * (coordinate.X + Scaler / 2) / (Scaler + PlotSize); - int y = Canvas.Height * ((coordinate.Y - PlotSize) * -1 + Scaler / 2) / (Scaler + PlotSize); + int x = width * (coordinate.X + Scaler / 2) / (Scaler + PlotSize); + int y = height * ((coordinate.Y - PlotSize) * -1 + Scaler / 2) / (Scaler + PlotSize); return new Point(x, y); } } - public void Save(string filename) => - Canvas.Image.Save(filename); } } diff --git a/src/CS340.TSP/City.cs b/src/CS340.TSP/City.cs index a64439b..460b5cd 100644 --- a/src/CS340.TSP/City.cs +++ b/src/CS340.TSP/City.cs @@ -22,14 +22,8 @@ namespace TSP public City(int id) => Id = id; - public City(int id, List<Road> edges) : this(id) - { - foreach (Road edge in edges) - { - Road newEdge = new Road(edge.U, edge.V, edge.Weight); - Edges.Add(newEdge); - } - } + public City(int id, List<Road> edges) : this(id) => + Edges = edges; public City(IVertex city) : this(city.Id, city.Edges) => Parent = city.Parent; diff --git a/src/CS340.TSP/TSPSolver.cs b/src/CS340.TSP/TSPSolver.cs index 567c7de..159286a 100644 --- a/src/CS340.TSP/TSPSolver.cs +++ b/src/CS340.TSP/TSPSolver.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics; using System.Linq; using Extensions; @@ -74,9 +73,9 @@ namespace TSP static void BruteForce(City cityInput, Tour unvisitedInput, Tour visitedInput) { // create local copy of Tour variables - Tour unvisited = new Tour(unvisitedInput.Cities); - Tour visited = new Tour(visitedInput.Cities); - City city = new City(cityInput); + Tour unvisited = new(unvisitedInput.Cities); + Tour visited = new(visitedInput.Cities); + City city = new(cityInput); // remove current City from tour int removed = unvisited.Cities.RemoveAll(c => c.Id == city.Id); @@ -92,8 +91,8 @@ namespace TSP // add current City to visited visited.Cities.Add(city); - // loop through each city. Each level of recursion has a one less city in currTour.Cities - // leaving the base case + // loop through each city. Each level of recursion has a one less city in unvisited.Cities + // so each loop is (n - 1)! down to n == 0 as the base case foreach (City neighbor in unvisited.Cities) BruteForce(neighbor, unvisited, visited); |