summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2021-04-22 20:43:24 -0500
committerToby Vincent <tobyv13@gmail.com>2021-04-22 20:43:24 -0500
commit2a5db460a9d9091238f64c48a640aac7cbe40678 (patch)
treea57b990a664e0cf8501660fe2bc5c329fcd16848
parentf77dd59b7cfe5c8237e5410156dd940bc7d7b069 (diff)
implemented graph printing
-rw-r--r--graphs/coordinates.csv13
-rw-r--r--graphs/graph1.csv13
-rw-r--r--graphs/graph10.csv13
-rw-r--r--graphs/graph2.csv13
-rw-r--r--graphs/graph3.csv13
-rw-r--r--graphs/graph4.csv13
-rw-r--r--graphs/graph5.csv13
-rw-r--r--graphs/graph6.csv13
-rw-r--r--graphs/graph7.csv13
-rw-r--r--graphs/graph8.csv13
-rw-r--r--graphs/graph9.csv13
-rw-r--r--src/CS340.Plotter/Plot.Designer.cs81
-rw-r--r--src/CS340.Plotter/Plot.cs59
-rw-r--r--src/CS340.Plotter/Program.cs28
-rw-r--r--src/CS340.Plotter/TourPlot.cs79
-rw-r--r--src/CS340.Plotter/graphs/coordinates.csv15
-rw-r--r--src/CS340.Plotter/graphs/graph1.csv13
-rw-r--r--src/CS340.Plotter/graphs/graph1.txt (renamed from src/CS340.Plotter/graphs/test.txt)0
-rw-r--r--src/CS340.TSP/CS340.TSP.csproj6
-rw-r--r--src/CS340.TSP/City.cs20
-rw-r--r--src/CS340.TSP/Coordinates.cs33
-rw-r--r--src/CS340.TSP/Solve.cs17
-rw-r--r--src/CS340.TSP/Tour.cs11
-rw-r--r--src/CS340.TSP/graphs/test.txt6
24 files changed, 406 insertions, 105 deletions
diff --git a/graphs/coordinates.csv b/graphs/coordinates.csv
new file mode 100644
index 0000000..ef67aeb
--- /dev/null
+++ b/graphs/coordinates.csv
@@ -0,0 +1,13 @@
+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/graphs/graph1.csv b/graphs/graph1.csv
new file mode 100644
index 0000000..17891b7
--- /dev/null
+++ b/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/graphs/graph10.csv b/graphs/graph10.csv
new file mode 100644
index 0000000..b443717
--- /dev/null
+++ b/graphs/graph10.csv
@@ -0,0 +1,13 @@
+76,4
+91,57
+95,93
+45,72
+33,14
+72,18
+80,42
+40,86
+20,2
+54,20
+51,97
+65,86
+90,19
diff --git a/graphs/graph2.csv b/graphs/graph2.csv
new file mode 100644
index 0000000..2610f5c
--- /dev/null
+++ b/graphs/graph2.csv
@@ -0,0 +1,13 @@
+92,35
+90,73
+1,42
+31,38
+80,66
+13,2
+82,80
+2,86
+48,25
+17,82
+91,20
+26,48
+87,48
diff --git a/graphs/graph3.csv b/graphs/graph3.csv
new file mode 100644
index 0000000..863fc99
--- /dev/null
+++ b/graphs/graph3.csv
@@ -0,0 +1,13 @@
+45,38
+36,59
+68,7
+82,99
+20,89
+56,90
+46,65
+67,37
+4,28
+66,57
+93,80
+28,29
+72,60
diff --git a/graphs/graph4.csv b/graphs/graph4.csv
new file mode 100644
index 0000000..8d4b0c8
--- /dev/null
+++ b/graphs/graph4.csv
@@ -0,0 +1,13 @@
+68,35
+75,20
+23,3
+70,52
+37,72
+6,89
+19,65
+46,60
+48,83
+35,54
+96,53
+14,7
+13,64
diff --git a/graphs/graph5.csv b/graphs/graph5.csv
new file mode 100644
index 0000000..58dd108
--- /dev/null
+++ b/graphs/graph5.csv
@@ -0,0 +1,13 @@
+34,23
+4,0
+2,20
+29,93
+5,13
+72,57
+42,12
+52,39
+92,43
+74,31
+3,82
+3,31
+98,58
diff --git a/graphs/graph6.csv b/graphs/graph6.csv
new file mode 100644
index 0000000..442af1a
--- /dev/null
+++ b/graphs/graph6.csv
@@ -0,0 +1,13 @@
+52,24
+23,81
+54,64
+80,70
+69,58
+71,6
+63,15
+35,85
+36,87
+84,21
+70,8
+30,39
+49,98
diff --git a/graphs/graph7.csv b/graphs/graph7.csv
new file mode 100644
index 0000000..2d6f16e
--- /dev/null
+++ b/graphs/graph7.csv
@@ -0,0 +1,13 @@
+80,19
+5,13
+69,0
+87,84
+28,37
+51,11
+49,6
+74,20
+68,59
+3,82
+92,14
+27,24
+81,8
diff --git a/graphs/graph8.csv b/graphs/graph8.csv
new file mode 100644
index 0000000..f0b9474
--- /dev/null
+++ b/graphs/graph8.csv
@@ -0,0 +1,13 @@
+86,87
+88,21
+97,72
+82,48
+69,13
+34,4
+54,52
+58,64
+96,51
+7,46
+64,56
+21,29
+21,56
diff --git a/graphs/graph9.csv b/graphs/graph9.csv
new file mode 100644
index 0000000..46016eb
--- /dev/null
+++ b/graphs/graph9.csv
@@ -0,0 +1,13 @@
+42,18
+67,75
+66,14
+81,1
+21,94
+20,79
+6,20
+93,81
+83,1
+13,6
+21,15
+83,37
+34,79
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
diff --git a/src/CS340.TSP/CS340.TSP.csproj b/src/CS340.TSP/CS340.TSP.csproj
index 08af2ae..dae9f9e 100644
--- a/src/CS340.TSP/CS340.TSP.csproj
+++ b/src/CS340.TSP/CS340.TSP.csproj
@@ -5,12 +5,6 @@
</PropertyGroup>
<ItemGroup>
- <Content Include="graphs\*.*">
- <CopyToOutputDirectory>Always</CopyToOutputDirectory>
- </Content>
- </ItemGroup>
-
- <ItemGroup>
<ProjectReference Include="..\CS340.Graph\CS340.Graph.csproj" />
<ProjectReference Include="..\CS340.Interfaces\CS340.Interfaces.csproj" />
<ProjectReference Include="..\CS340.Extensions\CS340.Extensions.csproj" />
diff --git a/src/CS340.TSP/City.cs b/src/CS340.TSP/City.cs
index 962a864..a64439b 100644
--- a/src/CS340.TSP/City.cs
+++ b/src/CS340.TSP/City.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
-using System.Linq;
using Graph;
using Interfaces;
@@ -16,25 +15,28 @@ namespace TSP
public new double Key { get => this[Parent].Weight; set => Key = value; }
- public Coordinates Location { get; set; }
+ public Coordinate Location { get; set; }
public City() { }
public City(int id) =>
Id = id;
- public City(int id, List<Road> edges) : this(id) =>
- Edges = edges;
-
- public City(IVertex city)
+ public City(int id, List<Road> edges) : this(id)
{
- Id = city.Id;
- Parent = city.Parent;
- foreach (Road edge in city.Edges)
+ foreach (Road edge in edges)
{
Road newEdge = new Road(edge.U, edge.V, edge.Weight);
Edges.Add(newEdge);
}
}
+
+ public City(IVertex city) : this(city.Id, city.Edges) =>
+ Parent = city.Parent;
+
+ public City(IVertex city, Coordinate location) : this(city) =>
+ Location = new Coordinate(location.X, location.Y);
+
+ public City(City city) : this(city, city.Location) { }
}
} \ No newline at end of file
diff --git a/src/CS340.TSP/Coordinates.cs b/src/CS340.TSP/Coordinates.cs
index f377c89..1e74021 100644
--- a/src/CS340.TSP/Coordinates.cs
+++ b/src/CS340.TSP/Coordinates.cs
@@ -1,17 +1,38 @@
-using Graph;
-using Interfaces;
+using System.Collections.Generic;
+using System.IO;
namespace TSP
{
- public struct Coordinates
+ public struct Coordinate
{
- public double X { get; set; }
- public double Y { get; set; }
+ public int X { get; set; }
+ public int Y { get; set; }
- public Coordinates(double x, double y)
+ public Coordinate(int x, int y)
{
X = x;
Y = y;
}
+
+ public Coordinate(string coords)
+ {
+ string[] items = coords.Split(',');
+ X = int.Parse(items[0]);
+ Y = int.Parse(items[1]);
+ }
+
+
+
+ public static List<Coordinate> Parse(string file)
+ {
+ List<Coordinate> coordinates = new List<Coordinate>();
+ foreach (string coords in File.ReadAllLines(file))
+ coordinates.Add(new Coordinate(coords));
+
+ return coordinates;
+ }
+
+
+ public override string ToString() => $"({X}, {Y})";
}
}
diff --git a/src/CS340.TSP/Solve.cs b/src/CS340.TSP/Solve.cs
index dd3d306..8adcd50 100644
--- a/src/CS340.TSP/Solve.cs
+++ b/src/CS340.TSP/Solve.cs
@@ -1,6 +1,6 @@
using System.Diagnostics;
using System.Linq;
-using System.Text.RegularExpressions;
+using Extensions;
using Graph;
using Graph.IO;
@@ -14,11 +14,18 @@ namespace TSP
static Map Map { get; set; }
static Tour BestTour = null;
- public static Tour BruteForce(string file, int init)
+
+ public static Tour MST(string file, string coords, int init)
+ {
+ Map mst = GraphFile.Read(new FileReader(file)).MST(0);
+ return new Tour(mst.Vertices.OrderBy(vertex => vertex.Id).ToList(), Coordinate.Parse(coords));
+ }
+
+ public static Tour BruteForce(string file, string coords, int init)
{
Map = GraphFile.Read(new FileReader(file));
- Tour intialTour = new Tour(Map.Vertices);
- GenerateTour(intialTour[init], new Tour(Map.Vertices), new Tour());
+ Tour intialTour = new Tour(Map.Vertices, Coordinate.Parse(coords));
+ GenerateTour(intialTour[init], intialTour, new Tour());
return BestTour;
}
@@ -39,9 +46,7 @@ namespace TSP
// add current City to visited
if (currVisited.Cities.Count > 0)
- {
currCity.Parent = currVisited.Cities.Last().Id;
- }
currVisited.Cities.Add(currCity);
diff --git a/src/CS340.TSP/Tour.cs b/src/CS340.TSP/Tour.cs
index e6e5d40..d90ab7e 100644
--- a/src/CS340.TSP/Tour.cs
+++ b/src/CS340.TSP/Tour.cs
@@ -19,11 +19,16 @@ namespace TSP
// indexer: get vertex where vertex.Id == index
public City this[int index] { get => Cities.Find(vertex => vertex.Id == index); }
- public Tour(List<Vertex> cities) => Cities.AddRange(cities.Select(city => new City(city)));
- public Tour(List<City> cities) => Cities.AddRange(cities.Select(city => new City(city)));
- public Tour() => Cities = new List<City>();
+ public Tour() =>
+ Cities = new List<City>();
+
public Tour(Tour tour) : this(tour.Cities) { }
+ public Tour(List<City> cities) =>
+ Cities.AddRange(cities.Select(city => new City(city)));
+
+ public Tour(List<Vertex> cities, List<Coordinate> coordinates) =>
+ Cities.AddRange(cities.Zip(coordinates, (city, coord) => new City(city, coord)));
public override string ToString() =>
$"tour: {String.Join(" -> ", Cities.Select(city => city.Id))}\n" +
diff --git a/src/CS340.TSP/graphs/test.txt b/src/CS340.TSP/graphs/test.txt
deleted file mode 100644
index 113fb4f..0000000
--- a/src/CS340.TSP/graphs/test.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-0 1 78.85429601486528 2 64.62197768561404 3 59.135437767890075 4 71.84705978674423 5 54.08326913195984
-1 0 78.85429601486528 2 93.47726996441435 3 21.095023109728988 4 28.635642126552707 5 102.55242561733974
-2 0 64.62197768561404 1 93.47726996441435 3 85.23496934944014 4 67.89698078707183 5 21.840329667841555
-3 0 59.135437767890075 1 21.095023109728988 2 85.23496934944014 4 33.54101966249684 5 90.21086409075129
-4 0 71.84705978674423 1 28.635642126552707 2 67.89698078707183 3 33.54101966249684 5 80.45495634204272
-5 0 54.08326913195984 1 102.55242561733974 2 21.840329667841555 3 90.21086409075129 4 80.45495634204272 \ No newline at end of file