⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gfdlg.cpp

📁 用vc++编写的遗传算法程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	
    printstrings=1;
	UpdateData(false);
   
}


void CGFDlg::initialize()  /* 遗传算法初始化 */
{
    /* 键盘输入遗传算法参数 */
     //initdata();
    /* 确定染色体的字节长度 */
    chromsize = (lchrom/(8*sizeof(unsigned)));
    if(lchrom%(8*sizeof(unsigned))) chromsize++;
    /*分配给全局数据结构空间 */
    initmalloc();
    /* 初始化随机数发生器 */
    randomize();
    /* 初始化全局计数变量和一些数值*/
    nmutation = 0;
    ncross = 0;
    bestfit.fitness = 0.0;
    bestfit.generation = 0;
	bestfit_min.fitness=3000;
	bestfit_min.generation=3000;
    /* 初始化种群,并统计计算结果 */
    initpop();
	statistics(oldpop);
    //initreport();
}

//////////////////////////////////////////////////////////

void CGFDlg::OnChange_function() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_pc() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_pm() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_maxgen() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_popsize() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_xmin() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_xmax() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_xl() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_ymin() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_ymax() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_yl() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_zmin() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_zmax() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_zl() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_output1() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnSelchange_output2() 
{
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_xls() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_yls() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChange_zls() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	process dlg1;
	dlg1.Create(IDD_DIALOG2,this);
	dlg1.ShowWindow(SW_SHOW);
    p_member chang[7];
	char a=chanshu[0].c;
	
	struct individual *temp;
    initdata();
	maxruns=1;
	gen=0;
	



	CString load="";
	load=load+"                         本次计算          ";
	m_output2.InsertString(-1,load);
    for(run=1; run<=maxruns; run++)
    {
        initialize();
        for(gen=0; gen<=maxgen; gen++)
        {
			UpdateData(false); 
			CString s="";
			//cout<<"第"<<run<<"/"<<maxruns<<"次运行"<<":当前代为="<<gen<<"共"<<maxgen<<"代"<<endl;
         
            /* 产生新一代 */
            generation();
            /* 计算新一代种群的适应度统计数据 */
            statistics(newpop);
          
		 CString gen_="",ncross_="",nmutation_="",saves_mi="",saves_ma="",saves_avg="",saves_f="",saves_g="";
		 CString b_v_1="",b_v_2="",b_v_3="",b_v_4="",b_v_5="",b_v_6="",b_v_7="";
		 gen_.Format("%s%d",gen_,gen);
		 ncross_.Format("%s%d",ncross_,ncross);
		 nmutation_.Format("%s%d",nmutation_,nmutation);
		 b_v_1.Format("%s%f",b_v_1,bestfit.varible[0]);
		 if(bestfit.varible[1])b_v_2.Format("%s%f",b_v_2,bestfit.varible[1]);
		 if(bestfit.varible[2])b_v_3.Format("%s%f",b_v_3,bestfit.varible[2]);
		 if(bestfit.varible[3])b_v_4.Format("%s%f",b_v_4,bestfit.varible[3]);
		 if(bestfit.varible[4])b_v_5.Format("%s%f",b_v_5,bestfit.varible[4]);
		 if(bestfit.varible[5])b_v_6.Format("%s%f",b_v_6,bestfit.varible[5]);
		 if(bestfit.varible[6])b_v_7.Format("%s%f",b_v_7,bestfit.varible[6]);
	
		 
		 s=s+"第"+gen_+"代统计";
		 m_output2.InsertString(-1,s);
		 s="";
	     s=s+"总交叉操作次数="+ncross_+"    总变异操作数 ="+nmutation_;  
         m_output2.InsertString(-1,s);
		 
		 saves_mi.Format("%s%f",saves_mi,min);
		 saves_ma.Format("%s%f",saves_ma,max);
		 s="";
		 s=s+"最小适应度:"+saves_mi+"  最大适应度:"+saves_ma;
		 m_output2.InsertString(-1,s);
		 saves_avg.Format("%s%f",saves_avg,avg);
		 s="";
		 s=s+"平均适应度:"+saves_avg;
		 m_output2.InsertString(-1,s);
		 saves_f.Format("%s%f",saves_f,bestfit.fitness);
		 saves_g.Format("%s%d",saves_g,bestfit.generation);
		 s="";
         s=s+"迄今发现最佳个体 => 所在代数:"+saves_g+"   适应度:" +saves_f;
         m_output2.InsertString(-1,s);
		 
		 
		 
		 CString l=writechrom((&bestfit)->chrom);
         CString x_len="",y_len="",z_len="",e_len="",f_len="",g_len="",h_len="";
		 for(int i=0;i<chanshu[0].l;i++)
			 x_len=x_len+l.GetAt(i);
		 for(int j=chanshu[0].l;j<(chanshu[0].l+chanshu[1].l);j++)
			 y_len=y_len+l.GetAt(j);
		 for(int v=(chanshu[0].l+chanshu[1].l);v<(chanshu[1].l+chanshu[2].l+chanshu[0].l);v++)
			 z_len=z_len+l.GetAt(v);
         for(int q=(chanshu[1].l+chanshu[2].l+chanshu[0].l);q<(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l);q++)
			 e_len=e_len+l.GetAt(q);
		 for(int w=(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l);w<(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l);w++)
			 f_len=f_len+l.GetAt(w);
		 for(int r=(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l);r<(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l);r++)
			 g_len=g_len+l.GetAt(r);
		 for(int t=(chanshu[0].l+chanshu[1].l+chanshu[2].l+chanshu[3].l+chanshu[4].l+chanshu[5].l);t<l.GetLength();t++)
			 h_len=h_len+l.GetAt(t);
  


		 s="";
		 s=s+"染色体x:"+x_len;
		 m_output2.InsertString(-1,s);
		 if(y_len!=""){
		 s="";
		 s=s+"染色体y:"+y_len;
		 m_output2.InsertString(-1,s);
		 }
		 if(z_len!=""){
		 s="";
		 s=s+"染色体z:"+z_len;
		 m_output2.InsertString(-1,s);
		 }
		 if(e_len!=""){
		 s="";
		 s=s+"染色体e:"+e_len;
		 m_output2.InsertString(-1,s);
		 }
		 if(f_len!=""){
		 s="";
		 s=s+"染色体f:"+f_len;
		 m_output2.InsertString(-1,s);
		 }
		 if(g_len!=""){
		 s="";
		 s=s+"染色体g:"+g_len;
		 m_output2.InsertString(-1,s);
		 }
		 if(h_len!=""){
		 s="";
		 s=s+"染色体h:"+h_len;
		 m_output2.InsertString(-1,s);
		 }
	

         

         s="";
		 s=s+" 对应的变量值:x= "+ b_v_1;
		 m_output2.InsertString(-1,s);
		 if(b_v_2!=" "){
		 s="";
		 s=s+" 对应的变量值:y= "+b_v_2;
		 m_output2.InsertString(-1,s);
		 }
		 if(b_v_3!=""){
		 s="";
		 s=s+" 对应的变量值:z= "+b_v_3;
		 m_output2.InsertString(-1,s);
		 }
		 if(b_v_4!=""){
		 s="";
		 s=s+" 对应的变量值:e= "+b_v_4;
		 m_output2.InsertString(-1,s);
		 }
		 if(b_v_5!=""){
		 s="";
		 s=s+" 对应的变量值:f= "+b_v_5;
		 m_output2.InsertString(-1,s);
		 s="";
		 }
		 if(b_v_6!=""){
		 s=s+" 对应的变量值:g= "+b_v_6;
		 m_output2.InsertString(-1,s);
		 }
		 if(b_v_7!=""){
		 s="";
		 s=s+" 对应的变量值:h= "+b_v_7;
		 m_output2.InsertString(-1,s);
		 }
         m_output2.InsertString(-1,"*******************************************************************************8******************");      
		    
		 temp = oldpop;
         oldpop = newpop;
         newpop = temp;
        }
       
    }
	
     dlg1.ShowWindow(SW_HIDE);
	///CDialog::OnOK();
}

void CGFDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CGFDlg::OnEditchangeCombo_chanshu() 
{
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnChangeEdit_l() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
    UpdateData(true);
	int l=(int)(log10((m_max-m_min)*pow(10,m_l))/log10(2));
	chanshu[m_change.GetCurSel()].l=l;
	CString s="";
	//chanshu[m_change.GetCurSel()].m_min=m_min;
	s.Format("%s%d",s,l);
	
	s="染色体:"+s;
	m_output3.InsertString(-1,s);


	
}

void CGFDlg::OnChangeEdit_min() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
    UpdateData(true);
	CString s="";
	chanshu[m_change.GetCurSel()].m_min=m_min;
	
	s.Format("%s%f",s,m_min);
	s="下限:"+s;
	m_output3.InsertString(-1,s);


	
}

void CGFDlg::OnChangeEdit_max() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString s="";
	chanshu[m_change.GetCurSel()].m_max=m_max;
	s.Format("%s%f",s,m_max);
	s="上限:"+s;
	m_output3.InsertString(-1,s);

	
}

void CGFDlg::OnSelchangeList_change() 
{
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnSelchangeCombo_chanshu() 
{
	// TODO: Add your control notification handler code here
	CString s="";
	m_change.GetLBText(m_change.GetCurSel(),s);
	
	chanshu[m_change.GetCurSel()].c=s.GetAt(0);
	s="";
	s=s+chanshu[m_change.GetCurSel()].c;
	s="已选参数:"+s;
	m_output3.InsertString(-1,s);
}

void CGFDlg::OnSelchangeList_mls() 
{
	// TODO: Add your control notification handler code here
	
}

void CGFDlg::OnButton_help() 
{
	// TODO: Add your control notification handler code here
	help dlg;
	int result=dlg.DoModal();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -