📄 form1.cs
字号:
int i,j;//循环控制变量
int temp;//交换基因存放变量
Random ra=new Random(unchecked((int)DateTime.Now.Ticks));
Double p;//概率
//用已选出的前50个基因进行交叉,再产生50个新基因
for(i=50;i<100;i+=2)
{
for(j=0;j<29;j++)//共29道题
{
//产生随机概率
p=ra.NextDouble();
if(p<0.6)//若小于0.6则杂交
{
//检查交叉后基因中是否存在重复题目
//不重复则交叉,否则不交叉
for(int k=0;k<29;k++)
{
if(Gen2[i-50,k]==Gen2[i-49,j])
{
goto LOOP_cross;//跳过交换
}
}//for(k)
//交换基因
temp=Gen2[i-50,j];
Gen2[i-50,j]=Gen2[i-49,j];
Gen2[i-49,j]=temp;
LOOP_cross:;
}//if(p)
}//for(j)
}//for(i)
}//cross()
//变异函数
public void mutation()
{
int i,j;//循环控制
int num;//临时题号
Random ra=new Random(unchecked((int)DateTime.Now.Ticks));
Double p;//概率
for(i=0;i<100;i++)
{
//选择题
for(j=0;j<20;j++)
{
p=ra.NextDouble();
if(p<0.01)//概率小于0.01则变异
{
Lra:
num=ra.Next(1,400);
//检查重复
for(int k=0;k<20;k++)
if(Gen2[i,k]==num)
goto Lra;
Gen2[i,j]=num;
}
}
//填空题
for(j=20;j<25;j++)
{
p=ra.NextDouble();
if(p<0.01)//概率小于0.01则变异
{
Lra2:
num=ra.Next(401,500);
//检查重复
for(int k=25;k<29;k++)
if(Gen2[i,k]==num)
goto Lra2;
Gen2[i,j]=num;
}
}
//解答题
for(j=25;j<29;j++)
{
p=ra.NextDouble();
if(p<0.01)//概率小于0.01则变异
{
Lra3:
num=ra.Next(501,756);
//检查重复
for(int k=0;k<20;k++)
if(Gen2[i,k]==num)
goto Lra3;
Gen2[i,j]=num;
}
}
}//for(i)至此,新一代基因已全部产生
}//mutation()
//保存最优策略函数
public void save_best_policy()
{
int gen_max_fit_num,gen2_min_fit_num,gen2_max_fit_num;//基因号
Double gen_max_fit;//适应度
//int[,] Gen3=new int[100,29];//临时数组
int i,j;
int[] temp=new int[29];
//选出上一代适应度最大值的基因号和基因
gen_max_fit_num=0;
for(i=0;i<100;i++)
{
if(fit(gen_max_fit_num)<fit(i))
{
gen_max_fit_num=i;
}
}
gen_max_fit=fit(gen_max_fit_num);
for(i=0;i<29;i++)
temp[i]=Gen[gen_max_fit_num,i];
//result_listBox.Items.Add(gen_max_fit.ToString());
//下一代变成上一代
for(i=0;i<100;i++)
for(j=0;j<29;j++)
Gen[i,j]=Gen2[i,j];
//选出下一代适应度最小值的基因号,和最大的适应度
gen2_max_fit_num=0;
for(i=0;i<100;i++)
{
if(fit(gen2_max_fit_num)<fit(i))
{
gen2_max_fit_num=i;
}
}
gen2_min_fit_num=0;
for(i=0;i<100;i++)
{
if(fit(gen2_min_fit_num)>=fit(i))
{
gen2_min_fit_num=i;
}
}
//保存
if(gen_max_fit>=fit(gen2_max_fit_num))//error,已都变成新一代了
{
for(i=0;i<29;i++)
Gen[gen2_min_fit_num,i]=temp[i];
}
}//save_best_policy()
//遗传算法函数
public void genetic_arithmetic()
{
int gen=0;//遗传代数
int best_num=0;//最优解基因号
while(gen<200)
{
product_wheel();//生成轮盘表
choose_arithmetic_operators();//选择算子
cross();//交叉
mutation();//变异
save_best_policy();//保存最优策略
gen++;
}
//挑出第200代中最优的输出
for(int m=0;m<100;m++)
{
if(fit(best_num)<fit(m))
{
best_num=m;
}
//result_listBox.Items.Add(fit(m).ToString());
}
for(int i=0;i<29;i++)
{
result_listBox.Items.Add(Gen[best_num,i].ToString());
}
result_textBox.Text=fit(best_num).ToString();
}//genetic_arithmetic()
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.hardlevel_textBox = new System.Windows.Forms.TextBox();
this.result_textBox = new System.Windows.Forms.TextBox();
this.begin_listBox = new System.Windows.Forms.ListBox();
this.result_listBox = new System.Windows.Forms.ListBox();
this.beginfit_listBox = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(136, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 1;
this.button1.Text = "开始组卷";
this.button1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// hardlevel_textBox
//
this.hardlevel_textBox.Location = new System.Drawing.Point(24, 24);
this.hardlevel_textBox.Name = "hardlevel_textBox";
this.hardlevel_textBox.Size = new System.Drawing.Size(104, 21);
this.hardlevel_textBox.TabIndex = 2;
this.hardlevel_textBox.Text = "";
//
// result_textBox
//
this.result_textBox.Location = new System.Drawing.Point(224, 88);
this.result_textBox.Name = "result_textBox";
this.result_textBox.Size = new System.Drawing.Size(184, 21);
this.result_textBox.TabIndex = 4;
this.result_textBox.Text = "";
//
// begin_listBox
//
this.begin_listBox.ItemHeight = 12;
this.begin_listBox.Location = new System.Drawing.Point(24, 88);
this.begin_listBox.Name = "begin_listBox";
this.begin_listBox.Size = new System.Drawing.Size(64, 196);
this.begin_listBox.TabIndex = 6;
//
// result_listBox
//
this.result_listBox.ItemHeight = 12;
this.result_listBox.Location = new System.Drawing.Point(224, 112);
this.result_listBox.Name = "result_listBox";
this.result_listBox.Size = new System.Drawing.Size(184, 172);
this.result_listBox.TabIndex = 7;
//
// beginfit_listBox
//
this.beginfit_listBox.ItemHeight = 12;
this.beginfit_listBox.Location = new System.Drawing.Point(112, 88);
this.beginfit_listBox.Name = "beginfit_listBox";
this.beginfit_listBox.Size = new System.Drawing.Size(104, 196);
this.beginfit_listBox.TabIndex = 8;
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 9;
this.label1.Text = "初始种群";
//
// label2
//
this.label2.Location = new System.Drawing.Point(112, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(104, 16);
this.label2.TabIndex = 10;
this.label2.Text = "初始种群适应度";
//
// label3
//
this.label3.Location = new System.Drawing.Point(232, 64);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(176, 16);
this.label3.TabIndex = 11;
this.label3.Text = "优化后的基因和适应度";
//
// button2
//
this.button2.Location = new System.Drawing.Point(224, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 24);
this.button2.TabIndex = 12;
this.button2.Text = "重新生成";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 310);
this.Controls.Add(this.button2);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.beginfit_listBox);
this.Controls.Add(this.result_listBox);
this.Controls.Add(this.begin_listBox);
this.Controls.Add(this.result_textBox);
this.Controls.Add(this.hardlevel_textBox);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "遗传算法组卷";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
//如不指定难度,则默认为1点难度
if(hardlevel_textBox.Text=="")
{
MessageBox.Show("不输入难度默认为 1");
hardlevel_textBox.Text="1";
}
OpenDb();//连接数据库,取数据
make_begin_recordset();//生成初始种群
genetic_arithmetic();//遗传算法优化
}
private void button2_Click(object sender, System.EventArgs e)
{
//清空列表和文本域
begin_listBox.Items.Clear();
beginfit_listBox.Items.Clear();
result_listBox.Items.Clear();
result_textBox.Clear();
OpenDb();//连接数据库
make_begin_recordset();//生成初始种群
genetic_arithmetic();//遗传算法优化
}//button1_Click
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -