using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace Tspform { public partial class Tsp : Form { private double Nc;//current iteration; private int cityn;//City number private double xmax = 0; //the max value of x coordinate of city private double xmin = double.MaxValue; private double ymax = 0; //the max value of y coordinate of city private double ymin = double.MaxValue; private double XY = 20; private string dataPath;//The file path Bitmap bmap;// draw picture Graphics g;//draw picture private double[] x; //save x point of city private double[] y;//save y point of city private int[] bestL; //store the best route public Tsp() { InitializeComponent(); } private void FilePath_Click(object sender, EventArgs e)//begin find the data path and then to set some parameter { labelNc.Text = ""; labelCL.Text = ""; openfile.InitialDirectory = Application.StartupPath + @"\data"; xmax = 0; xmin = float.MaxValue; ymax = 0; ymin = float.MaxValue; Nc = 0; x = null; y = null; File(); } private void File()//open the file { if (openfile.ShowDialog(this) == DialogResult.OK) { dataPath = openfile.FileName; ReadFile(dataPath); Thread thdDraw = new Thread(DrawCity); thdDraw.IsBackground = true; thdDraw.Start(); ACO.Enabled = true; Greedy.Enabled = true; Random.Enabled = true; } else { ACO.Enabled = false;//if the user does not choose the file, he/she can not click the ACO, Greedy,Random Greedy.Enabled = false; Random.Enabled = false; } } private bool ReadFile(string fr)//read the data of file and store it in the array { string stringline; string[] strdata; int i = 0; int c = 0; StreamReader sr = new StreamReader(fr); while ((stringline = sr.ReadLine()) != "EOF") { if (i == 1) { strdata = stringline.Split(' '); x[c] = double.Parse(strdata[1]); y[c] = double.Parse(strdata[2]); if (x[c] > xmax) xmax = x[c]; else if (x[c]xmin) xmin = xmin; if (y[c] > ymax) ymax = y[c]; else if (y[c] < ymax) ymax = ymax; if (y[c] < ymin) ymin = y[c]; else if (y[c] > ymin) ymin = ymin; c++; } if (stringline.Contains("NAME")) { strdata = stringline.Split(':'); labelfilename.Text = strdata[1].Trim();//去掉空格delete the space } if (stringline.Contains("Optimum")) { int L; L = stringline.IndexOf("is") + 2;//查找字串中指定字符 find 'is' in string labellength.Text = stringline.Substring(L).Trim(); } if (stringline.Contains("DIMENSION")) { strdata = stringline.Split(':'); labelCity.Text = strdata[1].Trim(); cityn = int.Parse(strdata[1].Trim()); x = new double[cityn]; y = new double[cityn]; bestL = new int[cityn]; } if (stringline.Contains("NODE_COORD_SECTION")) { i = 1; } } sr.Close(); sr.Dispose(); return true; } private void DrawCity()//invoke DrawPoint { picture.Image = null; bmap = new Bitmap(picture.Width, picture.Height); g = Graphics.FromImage(bmap); DrawPoint(); } private delegate void DrawPointInvoke();//draw city point private void DrawPoint() { if (this.InvokeRequired) { this.Invoke(new DrawPointInvoke(DrawPoint)); } else { int i; float x1, y1; System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black); Brush blackBrush = Brushes.Black; float pw = picture.Width; float ph = picture.Height; double a, b; a = (pw - XY) / (xmax - xmin); b = (ph - XY) / (ymax - ymin); float X = (float)a; float Y = (float)b; for (i = 0; i < cityn; i++) { x1 = (float)(x[i] - xmin) * X; y1 = (float)(y[i] - ymin) * Y; g.FillEllipse(myBrush, new RectangleF(x1, y1, 5, 5)); } picture.Image = (Image)bmap; } } public delegate void drawrouteInvoke(int[] curL);//draw the current route public void DrawRoute(int[] curL) { picture.Image = null;//clean the route last time bmap = new Bitmap(picture.Width, picture.Height); g = Graphics.FromImage(bmap); DrawPoint();//Repaint the City Point ; if (picture.InvokeRequired) { drawrouteInvoke _setInvoke = new drawrouteInvoke(DrawRoute); picture.BeginInvoke(_setInvoke, new object[] { curL }); } else { int i; float x1, y1, x2, y2; float pw = picture.Width; float fh = picture.Height; double a, b; a = (pw - XY) / (xmax - xmin); b = (fh - XY) / (ymax - ymin); float X = (float)a; float Y = (float)b; Pen p = new Pen(Color.Black, 1);//定义了一个 黑色 宽度为2的画笔 for (i = 1; i < cityn; i++) { x1 = (float)(x[curL[i - 1]] - xmin) * X; y1 = (float)(y[curL[i - 1]] - ymin) * Y; x2 = (float)(x[curL[i]] - xmin) * X; y2 = (float)(y[curL[i]] - ymin) * Y; g.DrawLine(p, new PointF(x1, y1), new PointF(x2, y2));//DrawLine } x1 = (float)(x[curL[0]] - xmin) * X; y1 = (float)(y[curL[0]] - ymin) * Y; x2 = (float)(x[curL[cityn - 1]] - xmin) * X; y2 = (float)(y[curL[cityn - 1]] - ymin) * Y; g.DrawLine(p, new PointF(x1, y1), new PointF(x2, y2)); } } public void ShowLength(int iNc, double CulLength)//show the current length and Nc { if (labelNc.InvokeRequired) { Action actionWriteLenth = delegate(double dLength) { labelNc.Text = dLength == 0 ? "" : dLength.ToString("F0"); }; labelNc.BeginInvoke(actionWriteLenth, CulLength); } else { labelNc.Text = CulLength == 0 ? "" : CulLength.ToString("F0"); } if (labelCL.InvokeRequired) { Action actionWriteCount = delegate(int cou) { labelCL.Text = cou.ToString(); }; labelCL.BeginInvoke(actionWriteCount, iNc); } else { labelCL.Text = iNc.ToString(); } } private void ACO_Click(object sender, EventArgs e)//begin ACO thread { labelNc.Text = ""; labelCL.Text = ""; ACOThread(); } public void ACOThread()// { Thread thdACO = new Thread(new ThreadStart(AcoCal)); thdACO.IsBackground = true; thdACO.Start(); } private void AcoCal() { ACO ACO = new ACO(cityn, cityn, this, 7.0, 0.5, 100, 400, x, y, 1, ref bestL); ACO.ResultRoute(); } private void Random_Click(object sender, EventArgs e)//begin random thread { RandomThread(); } public void RandomThread() { Thread thdRANDOM = new Thread(new ThreadStart(RanCal)); thdRANDOM.IsBackground = true; thdRANDOM.Start(); } private void RanCal() { randomAlg randomAlg = new randomAlg(cityn, x, y,Wj.startcityNumber, ref bestL, this); randomAlg.ResultRoute(); } private void Greedy_Click(object sender, EventArgs e)//begin greedy thread { GreedyThread(); } public void GreedyThread() { Thread Greedy = new Thread(new ThreadStart(GreedyCal)); Greedy.IsBackground = true; Greedy.Start(); } private void GreedyCal() { Greedy Greedy = new Greedy(cityn, x, y, Wj.startcityNumber, ref bestL, this); Greedy.ResultRoute(); } private void Tsp_Load(object sender, EventArgs e) { } private void Tsp_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); } private void panel1_Paint(object sender, PaintEventArgs e) { } private void picture_Click(object sender, EventArgs e) { } private void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void labelName_Click(object sender, EventArgs e) { } private void panelRBottom_Paint(object sender, PaintEventArgs e) { } } }