diff options
Diffstat (limited to 'src/CS340.Plotter')
-rw-r--r-- | src/CS340.Plotter/Plot.Designer.cs | 81 | ||||
-rw-r--r-- | src/CS340.Plotter/Plot.cs | 59 | ||||
-rw-r--r-- | src/CS340.Plotter/Program.cs | 28 | ||||
-rw-r--r-- | src/CS340.Plotter/TourPlot.cs | 79 | ||||
-rw-r--r-- | src/CS340.Plotter/graphs/coordinates.csv | 15 | ||||
-rw-r--r-- | src/CS340.Plotter/graphs/graph1.csv | 13 | ||||
-rw-r--r-- | src/CS340.Plotter/graphs/graph1.txt (renamed from src/CS340.Plotter/graphs/test.txt) | 0 |
7 files changed, 206 insertions, 69 deletions
diff --git a/src/CS340.Plotter/Plot.Designer.cs b/src/CS340.Plotter/Plot.Designer.cs index 562d725..3b8b8d2 100644 --- a/src/CS340.Plotter/Plot.Designer.cs +++ b/src/CS340.Plotter/Plot.Designer.cs @@ -29,38 +29,87 @@ namespace Plotter /// </summary> private void InitializeComponent() { - this.Canvas = new System.Windows.Forms.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.Canvas)).BeginInit(); + this.BruteForcePlot = new System.Windows.Forms.PictureBox(); + this.EstimationPlot = new System.Windows.Forms.PictureBox(); + this.MSTPlot = new System.Windows.Forms.PictureBox(); + this.LayoutPanel = new System.Windows.Forms.TableLayoutPanel(); + ((System.ComponentModel.ISupportInitialize)(this.BruteForcePlot)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.EstimationPlot)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.MSTPlot)).BeginInit(); + this.LayoutPanel.SuspendLayout(); this.SuspendLayout(); // - // Canvas + // BruteForcePlot // - this.Canvas.Dock = System.Windows.Forms.DockStyle.Fill; - this.Canvas.Location = new System.Drawing.Point(0, 0); - this.Canvas.Name = "Canvas"; - this.Canvas.Size = new System.Drawing.Size(604, 368); - this.Canvas.TabIndex = 0; - this.Canvas.TabStop = false; - this.Canvas.SizeChanged += new System.EventHandler(this.Canvas_SizeChanged); - this.Canvas.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Canvas_MouseDown); + this.BruteForcePlot.Dock = System.Windows.Forms.DockStyle.Fill; + this.BruteForcePlot.Location = new System.Drawing.Point(3, 3); + this.BruteForcePlot.Name = "BruteForcePlot"; + this.BruteForcePlot.Size = new System.Drawing.Size(307, 404); + this.BruteForcePlot.TabIndex = 0; + this.BruteForcePlot.TabStop = false; + this.BruteForcePlot.SizeChanged += new System.EventHandler(this.Canvas_SizeChanged); + // + // EstimationPlot + // + this.EstimationPlot.Dock = System.Windows.Forms.DockStyle.Fill; + this.EstimationPlot.Location = new System.Drawing.Point(316, 3); + this.EstimationPlot.Name = "EstimationPlot"; + this.EstimationPlot.Size = new System.Drawing.Size(307, 404); + this.EstimationPlot.TabIndex = 1; + this.EstimationPlot.TabStop = false; + // + // MSTPlot + // + this.MSTPlot.Dock = System.Windows.Forms.DockStyle.Fill; + this.MSTPlot.Location = new System.Drawing.Point(629, 3); + this.MSTPlot.Name = "MSTPlot"; + this.MSTPlot.Size = new System.Drawing.Size(309, 404); + this.MSTPlot.TabIndex = 2; + this.MSTPlot.TabStop = false; + // + // LayoutPanel + // + this.LayoutPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.LayoutPanel.ColumnCount = 3; + this.LayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); + this.LayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); + this.LayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F)); + this.LayoutPanel.Controls.Add(this.EstimationPlot, 1, 0); + this.LayoutPanel.Controls.Add(this.MSTPlot, 2, 0); + this.LayoutPanel.Controls.Add(this.BruteForcePlot, 0, 0); + this.LayoutPanel.Location = new System.Drawing.Point(12, 12); + this.LayoutPanel.Name = "LayoutPanel"; + this.LayoutPanel.RowCount = 1; + this.LayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); + this.LayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F)); + this.LayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F)); + this.LayoutPanel.Size = new System.Drawing.Size(941, 410); + this.LayoutPanel.TabIndex = 4; // // Plot // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(604, 368); - this.Controls.Add(this.Canvas); + this.ClientSize = new System.Drawing.Size(965, 434); + this.Controls.Add(this.LayoutPanel); this.Name = "Plot"; this.Text = "Plot"; - this.Load += new System.EventHandler(this.Plot_Load); - ((System.ComponentModel.ISupportInitialize)(this.Canvas)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.BruteForcePlot)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.EstimationPlot)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.MSTPlot)).EndInit(); + this.LayoutPanel.ResumeLayout(false); this.ResumeLayout(false); } #endregion - private System.Windows.Forms.PictureBox Canvas; + private System.Windows.Forms.PictureBox BruteForcePlot; + private System.Windows.Forms.PictureBox EstimationPlot; + private System.Windows.Forms.PictureBox MSTPlot; + private System.Windows.Forms.TableLayoutPanel LayoutPanel; } } diff --git a/src/CS340.Plotter/Plot.cs b/src/CS340.Plotter/Plot.cs index 0559ece..a962057 100644 --- a/src/CS340.Plotter/Plot.cs +++ b/src/CS340.Plotter/Plot.cs @@ -1,52 +1,57 @@ using System; -using System.Drawing; +using System.Collections.Generic; +using System.IO; using System.Windows.Forms; +using TSP; namespace Plotter { public partial class Plot : Form { - Random rand = new Random(); + // List<TourPlot[]> Plots; + List<TourPlot[]> Plots = new(); + public Plot() { InitializeComponent(); - Render(); + + foreach (string graphFile in Directory.GetFiles("graphs/", "*.txt")) + GetTours(graphFile); + + RenderWindow(debug: true); } - void Render() + + void RenderWindow(bool debug = false) { - using var bmp = new Bitmap(Canvas.Width, Canvas.Height); - using var gfx = Graphics.FromImage(bmp); - using var pen = new Pen(Color.White); - // draw one thousand random white lines on a dark blue background - gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; - gfx.Clear(Color.Navy); - for (int i = 0; i < 1000; i++) + foreach (var plot in Plots) { - var pt1 = new Point(rand.Next(bmp.Width), rand.Next(bmp.Height)); - var pt2 = new Point(rand.Next(bmp.Width), rand.Next(bmp.Height)); - gfx.DrawLine(pen, pt1, pt2); + if (debug) + plot[0].WriteDebug(); + + plot[0].Render(); + plot[1].Render(); + plot[2].Render(); } - - // copy the bitmap to the picturebox (double buffered) - Canvas.Image?.Dispose(); - Canvas.Image = (Bitmap)bmp.Clone(); } + void GetTours(string graphFile) + { + string coordsFile = graphFile.Replace(".txt", ".csv"); + TourPlot bruteForce = new(BruteForcePlot); + TourPlot estimation = new(EstimationPlot); + TourPlot mst = new(MSTPlot); - private void Canvas_SizeChanged(object sender, EventArgs e) - { - Render(); - } + bruteForce.Tour = Solve.BruteForce(graphFile, coordsFile, 0); + estimation.Tour = Solve.BruteForce(graphFile, coordsFile, 0); // TODO create estimation function + mst.Tour = Solve.MST(graphFile, coordsFile, 0); - private void Canvas_MouseDown(object sender, MouseEventArgs e) - { - Render(); + Plots.Add(new[] { bruteForce, estimation, mst }); } - private void Plot_Load(object sender, EventArgs e) + private void Canvas_SizeChanged(object sender, EventArgs e) { - + RenderWindow(); } } } diff --git a/src/CS340.Plotter/Program.cs b/src/CS340.Plotter/Program.cs index 9e9a52a..cfffac2 100644 --- a/src/CS340.Plotter/Program.cs +++ b/src/CS340.Plotter/Program.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using Graph; @@ -22,33 +24,7 @@ namespace Plotter Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Run(); Application.Run(new Plot()); } - - static void Run() - { - foreach (string file in Directory.GetFiles("graphs/")) - { - Tour bruteForce = Solve.BruteForce(file, 0); - Debug.WriteLine(bruteForce); - - // Graph mst = graph.MST(0); - // GraphFile.Print(mst, new ConsoleWriter()); - // Console.WriteLine( - // graph.Vertices.Sum(vertex => - // vertex.Edges.FirstOrDefault(edge => edge.V == vertex.Parent).Weight)); - - // mst.Vertices.ForEach(vertex => - // { - // Console.Write($"{vertex.Id}"); - // vertex.Edges.ForEach(edge => Console.Write($" {edge.V} {edge.Weight.ToString("F1")}")); - // Console.WriteLine(); - // }); - // Console.WriteLine(mst.Vertices - // .Sum(vertex => vertex.Edges - // .FirstOrDefault(edge => edge.V == vertex.Parent).Weight)); - } - } } } diff --git a/src/CS340.Plotter/TourPlot.cs b/src/CS340.Plotter/TourPlot.cs new file mode 100644 index 0000000..11f23ba --- /dev/null +++ b/src/CS340.Plotter/TourPlot.cs @@ -0,0 +1,79 @@ +using System.Diagnostics; +using System.Drawing; +using System.Linq; +using System.Windows.Forms; +using TSP; + +namespace Plotter +{ + public class TourPlot + { + public Tour Tour { get; set; } + public PictureBox Canvas { get; set; } + + public TourPlot(PictureBox canvas) + { + Canvas = canvas; + } + + public void Render() + { + const int Scaler = 125; + using Bitmap bmp = new(Canvas.Width, Canvas.Height); + using Graphics gfx = Graphics.FromImage(bmp); + using Pen pen = new(Color.Black); + using Pen gridPen = new(Color.LightGray); + using Font font = new("Arial", 16); + using Font gridFont = new("Arial", 10); + using SolidBrush brush = new(Color.Black); + + gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; + gfx.Clear(Color.White); + + for (int i = 0; i <= 100; i += 10) + { + Point left = ScaleLocation(new Coordinate(0, i)); + Point right = ScaleLocation(new Coordinate(100, i)); + gfx.DrawLine(gridPen, left, right); + gfx.DrawString(i.ToString(), gridFont, brush, left); + + Point bottom = ScaleLocation(new Coordinate(i, 0)); + Point top = ScaleLocation(new Coordinate(i, 100)); + gfx.DrawLine(gridPen, bottom, top); + gfx.DrawString(i.ToString(), gridFont, brush, bottom); + } + + foreach (City city in Tour.Cities.Where(city => city.Parent != -1)) + { + City parent = Tour[city.Parent]; + + Point cityPoint = ScaleLocation(city.Location); + Point parentPoint = ScaleLocation(parent.Location); + + gfx.DrawString(city.Id.ToString(), font, brush, cityPoint); + gfx.DrawLine(pen, cityPoint, parentPoint); + } + + // copy the bitmap to the picturebox (double buffered) + Canvas.Image?.Dispose(); + Canvas.Image = (Bitmap)bmp.Clone(); + + Point ScaleLocation(Coordinate coordinate) + { + int x = Canvas.Width * (coordinate.X + 10) / Scaler; + int y = Canvas.Height * ((coordinate.Y - 100) * -1 + 15) / Scaler; + + return new Point(x, y); + } + } + + public void WriteDebug() + { + Debug.WriteLine(Tour); + + foreach (City city in Tour.Cities) + Debug.WriteLine(city.Location); + } + + } +} diff --git a/src/CS340.Plotter/graphs/coordinates.csv b/src/CS340.Plotter/graphs/coordinates.csv new file mode 100644 index 0000000..f99e185 --- /dev/null +++ b/src/CS340.Plotter/graphs/coordinates.csv @@ -0,0 +1,15 @@ +Graph 1,,Graph 2,,Graph 3,,Graph 4,,Graph 5,,Graph 6,,Graph 7,,Graph 8,,Graph 9,,Graph 10, +x,y,x,y,x,y,x,y,x,y,x,y,x,y,x,y,x,y,x,y +20,72,92,35,45,38,68,35,34,23,52,24,80,19,86,87,42,18,76,4 +97,89,90,73,36,59,75,20,4,0,23,81,5,13,88,21,67,75,91,57 +44,12,1,42,68,7,23,3,2,20,54,64,69,0,97,72,66,14,95,93 +76,91,31,38,82,99,70,52,29,93,80,70,87,84,82,48,81,1,45,72 +91,61,80,66,20,89,37,72,5,13,69,58,28,37,69,13,21,94,33,14 +23,18,13,2,56,90,6,89,72,57,71,6,51,11,34,4,20,79,72,18 +25,79,82,80,46,65,19,65,42,12,63,15,49,6,54,52,6,20,80,42 +67,53,2,86,67,37,46,60,52,39,35,85,74,20,58,64,93,81,40,86 +72,17,48,25,4,28,48,83,92,43,36,87,68,59,96,51,83,1,20,2 +62,53,17,82,66,57,35,54,74,31,84,21,3,82,7,46,13,6,54,20 +95,98,91,20,93,80,96,53,3,82,70,8,92,14,64,56,21,15,51,97 +8,87,26,48,28,29,14,7,3,31,30,39,27,24,21,29,83,37,65,86 +94,34,87,48,72,60,13,64,98,58,49,98,81,8,21,56,34,79,90,19 diff --git a/src/CS340.Plotter/graphs/graph1.csv b/src/CS340.Plotter/graphs/graph1.csv new file mode 100644 index 0000000..17891b7 --- /dev/null +++ b/src/CS340.Plotter/graphs/graph1.csv @@ -0,0 +1,13 @@ +20,72 +97,89 +44,12 +76,91 +91,61 +23,18 +25,79 +67,53 +72,17 +62,53 +95,98 +8,87 +94,34 diff --git a/src/CS340.Plotter/graphs/test.txt b/src/CS340.Plotter/graphs/graph1.txt index 113fb4f..113fb4f 100644 --- a/src/CS340.Plotter/graphs/test.txt +++ b/src/CS340.Plotter/graphs/graph1.txt |