diff options
author | Toby Vincent <tobyv13@gmail.com> | 2021-04-26 17:07:54 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2021-04-26 17:07:54 -0500 |
commit | c975cfd6552690d8baba6021822ec7bdf3daced9 (patch) | |
tree | f02e09eabb7ad82a883a0904d37865b69c6791b8 | |
parent | 8b8abe2ddd84e975ac16e48220c613687e613eb9 (diff) |
finalized for report
-rw-r--r-- | src/CS340.Plotter/Plot.cs | 23 | ||||
-rw-r--r-- | src/CS340.Plotter/TourPlot.cs | 11 | ||||
-rw-r--r-- | src/CS340.TSP/TSPSolver.cs | 4 | ||||
-rw-r--r-- | src/CS340.TSP/Tour.cs | 3 |
4 files changed, 21 insertions, 20 deletions
diff --git a/src/CS340.Plotter/Plot.cs b/src/CS340.Plotter/Plot.cs index 187e5a5..6732fe1 100644 --- a/src/CS340.Plotter/Plot.cs +++ b/src/CS340.Plotter/Plot.cs @@ -26,7 +26,7 @@ namespace Plotter foreach (string graphFile in Directory.GetFiles("graphs/", "*.txt")) { - Plots.Add(GetTourSet(graphFile)); + Plots.Add(GetTourSet(graphFile)); if (result == DialogResult.OK) SaveSet(folderBrowserDialog.SelectedPath, Plots.Last()); } @@ -50,9 +50,9 @@ namespace Plotter 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); + 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(); @@ -63,7 +63,7 @@ namespace Plotter stopWatch.Reset(); stopWatch.Start(); - approximate.Tour = TSPSolver.Approximate(graphFile, coordsFile, 0); // TODO create estimation function + approximate.Tour = TSPSolver.Approximate(graphFile, coordsFile, 0); stopWatch.Stop(); approximate.Runtime = stopWatch.Elapsed; @@ -79,12 +79,15 @@ namespace Plotter 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)); + string header = $"Graph,Type,Runtime,Weight," + + String.Join(',', Enumerable.Repeat("City", 13).Zip(Enumerable.Range(1, 13), (l, n) => $"{l}{n}")); - 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")); + 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) diff --git a/src/CS340.Plotter/TourPlot.cs b/src/CS340.Plotter/TourPlot.cs index 5b3aa37..751fd2e 100644 --- a/src/CS340.Plotter/TourPlot.cs +++ b/src/CS340.Plotter/TourPlot.cs @@ -10,15 +10,17 @@ namespace Plotter public class TourPlot { public string GraphName { get; set; } + public string Type { 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(string graphName, PictureBox canvas, Label weightLabel, Label runtimeLabel) + public TourPlot(string graphName, string type, PictureBox canvas, Label weightLabel, Label runtimeLabel) { GraphName = graphName; + Type = type; Canvas = canvas; WeightLabel = weightLabel; RuntimeLabel = runtimeLabel; @@ -29,8 +31,8 @@ namespace Plotter public void Save(string filename, int width, int height) { - Draw(300, 300).Save($"{filename}.png"); - File.WriteAllText($"{filename}.txt", $"{this}"); + Draw(300, 300).Save(Path.Join(filename, $"{GraphName}_{Type}.png")); + File.AppendAllText(Path.Join(filename, $"data.csv"), $"{this}\n"); } public void Render() @@ -107,7 +109,6 @@ namespace Plotter } - public override string ToString() => $"{Tour}\n" + - $"runtime: {Runtime}"; + public override string ToString() => $"{GraphName},{Type},{Runtime.TotalSeconds},{Tour}"; } } diff --git a/src/CS340.TSP/TSPSolver.cs b/src/CS340.TSP/TSPSolver.cs index 24e28c2..84d3945 100644 --- a/src/CS340.TSP/TSPSolver.cs +++ b/src/CS340.TSP/TSPSolver.cs @@ -118,9 +118,7 @@ namespace TSP { BestTour = visited; BestWeight = weight; - Debug.WriteLine("*** Found new best tour ***"); - Debug.WriteLine(String.Join(" -> ", visited)); - Debug.WriteLine(weight); + Debug.WriteLine($"New best tour: {String.Join(" -> ", visited)} = {weight}"); } } } diff --git a/src/CS340.TSP/Tour.cs b/src/CS340.TSP/Tour.cs index a4ef10f..5f530be 100644 --- a/src/CS340.TSP/Tour.cs +++ b/src/CS340.TSP/Tour.cs @@ -36,8 +36,7 @@ namespace TSP Cities.AddRange(cities.Select(city => new City(city, coordinates[city.Id]))); public override string ToString() => - $"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" + - $"distance: {Weight}"; + $"{Weight},{String.Join(",", Cities.Select(city => city.Id))}"; } } |