📄 form1.cs
字号:
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;
namespace TSP
{
public class JSPFrm : System.Windows.Forms.Form
{
public const int GROUP_NUM = 200; // 最大规模
private int count; // 计数变量
Random r; // 随机变量
DateTime startTime,endTime; // 计算时间
private int cityNum; // 城市数目--染色体长度
private PointF[] ps; // 城市坐标
private float [] xn; // 城市横坐标
private float [] yn; // 城市纵坐标
private int [] citySerial; // 城市序号
private int startCityNum; // 开始城市下标
private int [,] genvalue; // 每代中的最优染色体
private double [] minDis; // 每代中的最短距离
private double [,] distance; // 城市间距离
private int [,] gene; // 染色体数目
private int [,] selectgene; // 遗传算子选择时的染色体
private double[] pFun; // 原始适应度函数值
private double[] pRealFun; // 变换后适应度函数值
private double Pc; // 交叉概率
private double Pm; // 变异概率
private int generation; // 当前代
private int genCounter; // 迭代次数
private double bestValue; // 最佳路径长度
private bool isFirstEntered; // 是否第一次进窗体
private bool isShowResult; // 是否显示结果
private int bestGene; // 最佳路径在多少代时出现
private int startInGene; // 起始点在最佳路径中的位置
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txt_pm;
private System.Windows.Forms.TextBox txt_pc;
private System.Windows.Forms.TextBox txt_time;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.GroupBox grp_path;
private System.Windows.Forms.Panel pnl_result;
private System.Windows.Forms.GroupBox grp_oper;
private System.Windows.Forms.Button btnDiedai;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox txtCityNum;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtStartCity;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.TextBox txt_shortDis;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TextBox txtMaxFit;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.RichTextBox txtShortPath;
private System.Windows.Forms.RichTextBox txt_minPathLocation;
private System.Windows.Forms.Button btnClose;
private System.ComponentModel.Container components = null;
public JSPFrm()
{
InitializeComponent();
r = new Random();
isFirstEntered = true;
isShowResult = false;
bestGene = 0 ;
startInGene = 1;
initMenu();
btnStart.Enabled = false;
txtStartCity.Clear();
txt_pc.Clear();
txt_pm.Clear();
txt_time.Clear();
txtStartCity.Focus();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtStartCity = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txt_pm = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txt_time = new System.Windows.Forms.TextBox();
this.txt_pc = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label9 = new System.Windows.Forms.Label();
this.txtShortPath = new System.Windows.Forms.RichTextBox();
this.txtMaxFit = new System.Windows.Forms.TextBox();
this.label8 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txt_shortDis = new System.Windows.Forms.TextBox();
this.txt_minPathLocation = new System.Windows.Forms.RichTextBox();
this.grp_path = new System.Windows.Forms.GroupBox();
this.pnl_result = new System.Windows.Forms.Panel();
this.grp_oper = new System.Windows.Forms.GroupBox();
this.btnDiedai = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.txtCityNum = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.grp_path.SuspendLayout();
this.grp_oper.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtStartCity);
this.groupBox1.Controls.Add(this.label7);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.txt_pm);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.txt_time);
this.groupBox1.Controls.Add(this.txt_pc);
this.groupBox1.Location = new System.Drawing.Point(8, 72);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(192, 144);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "迭代时输入";
//
// txtStartCity
//
this.txtStartCity.Location = new System.Drawing.Point(70, 20);
this.txtStartCity.Name = "txtStartCity";
this.txtStartCity.Size = new System.Drawing.Size(96, 21);
this.txtStartCity.TabIndex = 2;
this.txtStartCity.Text = "1";
//
// label7
//
this.label7.Location = new System.Drawing.Point(13, 27);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(64, 16);
this.label7.TabIndex = 9;
this.label7.Text = "出发城市";
//
// label3
//
this.label3.Location = new System.Drawing.Point(14, 85);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 16);
this.label3.TabIndex = 8;
this.label3.Text = "变异概率";
//
// txt_pm
//
this.txt_pm.Location = new System.Drawing.Point(71, 79);
this.txt_pm.Name = "txt_pm";
this.txt_pm.Size = new System.Drawing.Size(96, 21);
this.txt_pm.TabIndex = 7;
this.txt_pm.Text = "0.01";
//
// label2
//
this.label2.Location = new System.Drawing.Point(14, 55);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 5;
this.label2.Text = "交叉概率";
//
// label1
//
this.label1.Location = new System.Drawing.Point(13, 115);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 3;
this.label1.Text = "迭代次数";
//
// txt_time
//
this.txt_time.Location = new System.Drawing.Point(71, 109);
this.txt_time.Name = "txt_time";
this.txt_time.Size = new System.Drawing.Size(96, 21);
this.txt_time.TabIndex = 10;
this.txt_time.Text = "150";
//
// txt_pc
//
this.txt_pc.Location = new System.Drawing.Point(69, 51);
this.txt_pc.Name = "txt_pc";
this.txt_pc.Size = new System.Drawing.Size(96, 21);
this.txt_pc.TabIndex = 6;
this.txt_pc.Text = "0.6";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label9);
this.groupBox2.Controls.Add(this.txtShortPath);
this.groupBox2.Controls.Add(this.txtMaxFit);
this.groupBox2.Controls.Add(this.label8);
this.groupBox2.Controls.Add(this.label5);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Controls.Add(this.txt_shortDis);
this.groupBox2.Controls.Add(this.txt_minPathLocation);
this.groupBox2.Location = new System.Drawing.Point(8, 262);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(576, 114);
this.groupBox2.TabIndex = 8;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "结果显示";
//
// label9
//
this.label9.Location = new System.Drawing.Point(16, 86);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(56, 15);
this.label9.TabIndex = 21;
this.label9.Text = "路径坐标";
//
// txtShortPath
//
this.txtShortPath.Location = new System.Drawing.Point(87, 46);
this.txtShortPath.Name = "txtShortPath";
this.txtShortPath.ReadOnly = true;
this.txtShortPath.Size = new System.Drawing.Size(480, 25);
this.txtShortPath.TabIndex = 20;
this.txtShortPath.Text = "";
//
// txtMaxFit
//
this.txtMaxFit.Location = new System.Drawing.Point(408, 17);
this.txtMaxFit.Name = "txtMaxFit";
this.txtMaxFit.ReadOnly = true;
this.txtMaxFit.Size = new System.Drawing.Size(160, 21);
this.txtMaxFit.TabIndex = 18;
this.txtMaxFit.Text = "";
//
// label8
//
this.label8.Location = new System.Drawing.Point(329, 20);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(72, 16);
this.label8.TabIndex = 17;
this.label8.Text = "最大适应度";
//
// label5
//
this.label5.Location = new System.Drawing.Point(15, 54);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(67, 16);
this.label5.TabIndex = 2;
this.label5.Text = "最短路径";
//
// label4
//
this.label4.Location = new System.Drawing.Point(15, 22);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(73, 16);
this.label4.TabIndex = 0;
this.label4.Text = "最小距离";
//
// txt_shortDis
//
this.txt_shortDis.Location = new System.Drawing.Point(88, 16);
this.txt_shortDis.Name = "txt_shortDis";
this.txt_shortDis.ReadOnly = true;
this.txt_shortDis.Size = new System.Drawing.Size(192, 21);
this.txt_shortDis.TabIndex = 15;
this.txt_shortDis.Text = "";
//
// txt_minPathLocation
//
this.txt_minPathLocation.Location = new System.Drawing.Point(88, 81);
this.txt_minPathLocation.Name = "txt_minPathLocation";
this.txt_minPathLocation.ReadOnly = true;
this.txt_minPathLocation.Size = new System.Drawing.Size(480, 24);
this.txt_minPathLocation.TabIndex = 16;
this.txt_minPathLocation.Text = "";
//
// grp_path
//
this.grp_path.Controls.Add(this.pnl_result);
this.grp_path.Controls.Add(this.grp_oper);
this.grp_path.Location = new System.Drawing.Point(200, 0);
this.grp_path.Name = "grp_path";
this.grp_path.Size = new System.Drawing.Size(384, 264);
this.grp_path.TabIndex = 9;
this.grp_path.TabStop = false;
this.grp_path.Text = "城市图";
//
// pnl_result
//
this.pnl_result.BackColor = System.Drawing.SystemColors.Window;
this.pnl_result.Location = new System.Drawing.Point(8, 16);
this.pnl_result.Name = "pnl_result";
this.pnl_result.Size = new System.Drawing.Size(368, 240);
this.pnl_result.TabIndex = 3;
//
// grp_oper
//
this.grp_oper.Controls.Add(this.btnDiedai);
this.grp_oper.Location = new System.Drawing.Point(400, 8);
this.grp_oper.Name = "grp_oper";
this.grp_oper.Size = new System.Drawing.Size(160, 272);
this.grp_oper.TabIndex = 7;
this.grp_oper.TabStop = false;
this.grp_oper.Text = "操作";
//
// btnDiedai
//
this.btnDiedai.Location = new System.Drawing.Point(0, 0);
this.btnDiedai.Name = "btnDiedai";
this.btnDiedai.TabIndex = 0;
//
// label6
//
this.label6.Location = new System.Drawing.Point(16, 33);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(55, 16);
this.label6.TabIndex = 10;
this.label6.Text = "城市数目";
//
// txtCityNum
//
this.txtCityNum.Location = new System.Drawing.Point(79, 27);
this.txtCityNum.Name = "txtCityNum";
this.txtCityNum.ReadOnly = true;
this.txtCityNum.Size = new System.Drawing.Size(96, 21);
this.txtCityNum.TabIndex = 11;
this.txtCityNum.Text = "";
//
// groupBox3
//
this.groupBox3.Location = new System.Drawing.Point(8, 8);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(192, 56);
this.groupBox3.TabIndex = 12;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "初始显示";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(8, 224);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -