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

📄 mygaview.cpp

📁 一个遗传算法的程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
           [-280,280],Mina=176.703918567\r\n\
  case 38: Fitness=NeedleInHaystack(px); [-5.12,5.12]\r\n\
  case 39: Fitness=MyRob1(px);\r\n\
  case 40: Fitness=ShekelFoxholes(px); \r\n\
           [-65.536,65.536]\r\n\
  case 41: Fitness=LinFun(px);  (0,1)\r\n\
  default: Fitness=Example1(x);  [-1,2]\r\n\
  ");

void CMyGAView::OnFunSelect() 
{
	// TODO: Add your command handler code here
	CFunSelDlg dlg;
	dlg.m_iFunSelx=gGAParam.iFunSel;
	dlg.m_iVarNum=gGAParam.iVarNum;
	dlg.m_dLower=gGAParam.dLower;
	dlg.m_dUpper=gGAParam.dUpper;
	dlg.m_dOptima=gGAParam.dOptima;
	dlg.m_dAccuracy=gGAParam.dAccuracy;
	dlg.m_strDescription=gstrFunList1+gstrFunList2;
	dlg.m_lMonteCarloNum=gGAParam.lMonteCarloNum;
	dlg.m_bSave=gGAParam.bMonteCarloSave;
	if(dlg.DoModal()==IDOK)
	{
		gGAParam.iFunSel=dlg.m_iFunSelx;
		gGAParam.iVarNum=dlg.m_iVarNum;
		gGAParam.dLower=dlg.m_dLower;
		gGAParam.dUpper=dlg.m_dUpper;
		gGAParam.dOptima=dlg.m_dOptima;
		gGAParam.dAccuracy=dlg.m_dAccuracy;
		m_bParaChanged=TRUE;
		gGAParam.lMonteCarloNum=dlg.m_lMonteCarloNum;
		gGAParam.bMonteCarloSave=dlg.m_bSave;
	}

}
	
void CMyGAView::OnUpdateCalStart(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW==0||gGAParam.iSSW==2);
}

void CMyGAView::OnUpdateCalPause(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW==1);
	
}

long CMyGAView::OnGAFinished(WPARAM wParam,LPARAM lParam)
{
	CString str,strall,strtime;
	GARESULT *pgaResult=(GARESULT *)lParam;
	CTime t;
	t=CTime::GetCurrentTime();
	strtime=t.Format("%Y.%m.%d,  %H:%M:%S ");
	str.Format(_T("%s 计算结束,共计算%d次,平均函数总调用次数:%10.1lf,平均极值点数%5.2f"),
		strtime,(int)wParam,pgaResult->MeanFunCall,pgaResult->AverageOptimaNum);
	m_pStatus->SetPaneText(0,str);
	Invalidate(TRUE);
	str.Format("\r\n%s  计算结束\r\n",strtime);
	strall+=str;
	str.Format("共计算%d次, 平均极值点数 %10.4lf\r\n",(int)wParam,pgaResult->AverageOptimaNum);
	strall+=str;
	str.Format("统计结果: 最大         最小         平均            标准差\r\n");
	strall+=str;
	str.Format("适应值:%14.8lf,%14.8lf, %14.8lf, %12.8le\r\n",
		pgaResult->MaxFitness,pgaResult->MinFitness,pgaResult->MeanFitness,
			pgaResult->StdFitness);
	strall+=str;
	str.Format("进化代数:%12.1lf,%12.1lf, %12.5lf, %12.8le\r\n",
		pgaResult->MaxGeneration,pgaResult->MinGeneration,pgaResult->MeanGeneration,
			pgaResult->StdGeneration);
	strall+=str;
	str.Format("函数调用次数:%12.1lf,%12.1lf, %12.5lf, %12.8le\r\n",
		pgaResult->MaxFunCall,pgaResult->MinFunCall,pgaResult->MeanFunCall,
			pgaResult->StdFunCall);
	strall+=str;
	str.Format("计算时间(s):%12.1lf,%12.1lf, %12.5lf, %12.8le\r\n",
		pgaResult->MaxTime,pgaResult->MinTime,pgaResult->MeanTime,
			pgaResult->StdTime);
	strall+=str;
	str.Format("Any=%d \r\n",pgaResult->any);
	strall+=str;
	m_strSetup+=strall;
	if(gGAParam.iSaveType&&m_pFile)
	{
/*		CIndividual *pInd=&CPopulation::OptimArray[0];
		for(int i=0;i<CPopulation::OptimaNum;i++)
		{
			str.Format("[极值点%d] %12.8lf  ",i+1,pInd->Fitness);
			m_strResult+=str;
			for(int j=0;j<CIndividual::VarNum;j++)
			{
				str.Format("x%d=%12.8lf  ",j+1,pInd->Gene[j]);
				m_strResult+=str;
			}
			pInd++;
			if(gGAParam.iSaveType&&m_pFile)
				fprintf(m_pFile,"%s\n",m_strResult);
			m_strResult.Empty();
		}*/
		fprintf(m_pFile,"%s\n-----------------------------------------------------",strall);
		fclose(m_pFile);
	}
	gGAParam.iSSW=0;
	gGAParam.bResultNotProcessed=FALSE;
	return 0L;
}

void CMyGAView::OnUpdateCalStop(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW==1||gGAParam.iSSW==2);

}
void CMyGAView::OnUpdateCalSetting(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW!=1);
}

void CMyGAView::OnUpdateFunSelect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW==0);
		
}

void CMyGAView::OnUpdateSubpopSet(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(gGAParam.iSSW!=1);
}

long CMyGAView::OnGAResult(WPARAM wParam,LPARAM lParam)
{
	CPopulation *pPopu=g_pPop;//pga->pPopu;
	CString str;
	int i,j;
	int iCount;
	CPoint point;
	if(m_strList.GetCount()>=m_iLine)
	{
		for(i=m_iLine/4;i>0;i--)m_strList.RemoveHead();
		point.x=0;
		point.y=(int)(m_strList.GetCount()/m_iPageLine)*m_iPageLine*m_iLineHeight;
		ScrollToPosition(point);
	}
	CClientDC dc(this);
	OnPrepareDC(&dc);  //!!否则,以下将使用DP坐标
	if(m_bAutoScroll)
	{
		point.x=0;
		point.y=(int)(m_strList.GetCount()/m_iPageLine)*m_iPageLine*m_iLineHeight;
		ScrollToPosition(point);
	}
	m_strResult.Empty();
	lGeneration=(long)wParam;
	m_strResult.Format("--第%d次计算,第%ld代,已找%d到个极值点,%d次无新极值点",
		(int)lParam+1,lGeneration,CPopulation::OptimaNum,CPopulation::NoNewOptima);
	m_strList.AddTail(m_strResult);
	m_pStatus->SetPaneText(0,m_strResult);
	iCount=m_strList.GetCount()-1;
	dc.SetTextColor(RGB(255,0,0));
	dc.TextOut(0,iCount*m_iLineHeight,m_strResult);
	if(CPopulation::StopFlag&&gGAParam.iSaveType&&m_pFile)
				fprintf(m_pFile,"%s\n",m_strResult);
	if(gGAParam.iSaveType>1&&m_pFile)
				fprintf(m_pFile,"%s\n",m_strResult);
	m_strResult.Empty();
	CIndividual *pInd;
	int num1,num2;
	pInd=&CPopulation::OptimArray[0];
	if(m_bAllOptimaFitness)
	{
		num1=CPopulation::OptimaNum;
		num2=CIndividual::VarNum;
		for(i=0;i<num1;i++)
		{
			str.Format("[极值点%d] %12.8lf  ",i+1,pInd->Fitness);
			m_strResult+=str;
			for(j=0;j<num2;j++)
			{
				str.Format("x%d=%12.8lf  ",j+1,pInd->Gene[j]);
				m_strResult+=str;
			}
			pInd++;
			m_strList.AddTail(m_strResult);
			iCount=m_strList.GetCount()-1;
			dc.SetTextColor(m_color[iCount&1]);
		   	dc.TextOut(0,iCount*m_iLineHeight,m_strResult);
				if(CPopulation::StopFlag&&gGAParam.iSaveType&&m_pFile)
				fprintf(m_pFile,"%s\n",m_strResult);
			if(gGAParam.iSaveType>1&&m_pFile)
				fprintf(m_pFile,"%s\n",m_strResult);
			m_strResult.Empty();
		}
	}
	if(m_bAllSubpopFitness&&CPopulation::StopFlag==0)
	{
		num1=gGAParam.iSubpopNum;
		for(i=0;i<num1;i++)
		{
			str.Format("子群%d: %12.8lf  σ=%12.8lf",i+1,g_pPop[i].Ind[0].Fitness,
				g_pPop[i].Sigma);
			m_strResult+=str;
			if(m_bAllSubpopGene){
			num2=CIndividual::VarNum;
			pInd=&g_pPop[i].Ind[0];
			for(j=0;j<num2;j++)
			{
				str.Format("x%d=%12.8lf  ",j+1,pInd->Gene[j]);
				m_strResult+=str;
			}}/**/
		}
		m_strList.AddTail(m_strResult);
		iCount=m_strList.GetCount()-1;
		dc.SetTextColor(m_color[iCount&1]);
		dc.TextOut(0,iCount*m_iLineHeight,m_strResult);
		if(gGAParam.iSaveType>2&&m_pFile)fprintf(m_pFile,"%s",m_strResult);
		m_strResult.Empty();
	}
	if(m_bSubpop1)
	{
		pPopu=&g_pPop[m_iSubpop1];
		num1=CPopulation::PopSize;
		num2=CIndividual::VarNum;
		str.Format("子群%d: Sigma=%15.10lf",m_iSubpop1+1,pPopu->Sigma);
		m_strResult+=str;
		m_strList.AddTail(m_strResult);
		iCount=m_strList.GetCount()-1;
		dc.SetTextColor(m_color[iCount&1]);
		dc.TextOut(0,iCount*m_iLineHeight,m_strResult);
		if(gGAParam.iSaveType>2&&m_pFile)fprintf(m_pFile,"%s",m_strResult);
		m_strResult.Empty();
		for(i=0;i<num1;i++)
		{
			pInd=&pPopu->Ind[i];
			str.Format("个体:%d %12.8lf  ",i+1,pInd->Fitness);
			m_strResult+=str;
			for(j=0;j<num2;j++)
			{
				str.Format("x%d=%12.8lf  ",j+1,pInd->Gene[j]);
				m_strResult+=str;
			}
			m_strList.AddTail(m_strResult);
			iCount=m_strList.GetCount()-1;
			dc.SetTextColor(m_color[iCount&1]);
			dc.TextOut(0,iCount*m_iLineHeight,m_strResult);
			if(gGAParam.iSaveType>2&&m_pFile)fprintf(m_pFile,"%s",m_strResult);
			m_strResult.Empty();
		}
	}
	if(m_bLowSpeed)Sleep(500);
	gGAParam.bResultNotProcessed=FALSE;
	return 0L;
}

/////////////////////////////////////////////////////////////////////////////
// CSetupDlg dialog


CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSetupDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetupDlg)
	m_iSaveType = -1;
	m_iRepeatNumber = 0;
	m_NoNewNum = 0;
	m_MaxFunCall = 0;
	m_SubpopRadius = 0.0;
	m_MaxOptimaNum = 0;
	m_SubpopNum = 0;
	m_MuteType = -1;
	m_ReproduceType = -1;
	m_iESType = -1;
	//}}AFX_DATA_INIT
}


void CSetupDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetupDlg)
	DDX_Radio(pDX, IDC_RADIO3, m_iSaveType);
	DDX_Text(pDX, IDC_EDIT8, m_iRepeatNumber);
	DDV_MinMaxInt(pDX, m_iRepeatNumber, 1, 10000000);
	DDX_Text(pDX, IDC_EDIT13, m_NoNewNum);
	DDX_Text(pDX, IDC_EDIT14, m_MaxFunCall);
	DDX_Text(pDX, IDC_EDIT3, m_SubpopRadius);
	DDX_Text(pDX, IDC_EDIT9, m_MaxOptimaNum);
	DDX_Text(pDX, IDC_EDIT1, m_SubpopNum);
	DDX_Radio(pDX, IDC_RADIO1, m_MuteType);
	DDX_Radio(pDX, IDC_RADIO9, m_ReproduceType);
	DDX_Radio(pDX, IDC_RADIO11, m_iESType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
	//{{AFX_MSG_MAP(CSetupDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


BOOL CSetupDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}






/////////////////////////////////////////////////////////////////////////////
// CFunSelDlg dialog

CFunSelDlg::CFunSelDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFunSelDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFunSelDlg)
	m_iFunSel = -1;
	m_iVarNum = 0;
	m_dLower = 0.0;
	m_dUpper = 0.0;
	m_dOptima = 0.0;
	m_dAccuracy = 0.0;
	m_strDescription = _T("");
	m_iFunSelx = 0;
	m_lMonteCarloNum = 0;
	m_bSave = FALSE;
	//}}AFX_DATA_INIT
}


void CFunSelDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFunSelDlg)
	DDX_CBIndex(pDX, IDC_COMBO3, m_iFunSel);
	DDX_Text(pDX, IDC_EDIT1, m_iVarNum);
	DDV_MinMaxInt(pDX, m_iVarNum, 1, 1000);
	DDX_Text(pDX, IDC_EDIT2, m_dLower);
	DDX_Text(pDX, IDC_EDIT3, m_dUpper);
	DDX_Text(pDX, IDC_EDIT4, m_dOptima);
	DDX_Text(pDX, IDC_EDIT5, m_dAccuracy);
	DDX_Text(pDX, IDC_EDIT6, m_strDescription);
	DDV_MaxChars(pDX, m_strDescription, 5000);
	DDX_Text(pDX, IDC_EDIT7, m_iFunSelx);
	DDV_MinMaxInt(pDX, m_iFunSelx, 0, 400);
	DDX_Text(pDX, IDC_EDIT8, m_lMonteCarloNum);
	DDV_MinMaxLong(pDX, m_lMonteCarloNum, 0, 1000000000);
	DDX_Check(pDX, IDC_CHECK1, m_bSave);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFunSelDlg, CDialog)
	//{{AFX_MSG_MAP(CFunSelDlg)
	ON_CBN_SELCHANGE(IDC_COMBO3, OnSelchange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFunSelDlg message handlers




void CMyGAView::OnDispSet() 
{
	// TODO: Add your command handler code here
	if(gGAParam.iSSW==1)OnCalPause();
	CDisplayDlg dlg;
	dlg.m_bAllOptimaFitness=m_bAllOptimaFitness;
	dlg.m_bAllOptimaGene=m_bAllOptimaGene;
	dlg.m_bAllSubpopFitness=m_bAllSubpopFitness;
	dlg.m_bAllSubpopGene=m_bAllSubpopGene;
	dlg.m_bAutoScroll=m_bAutoScroll;
	dlg.m_bLowSpeed=m_bLowSpeed;
	dlg.m_lBreakPos=gGAParam.lBreakPos;
	dlg.m_bSubpop1=m_bSubpop1;
	dlg.m_bSubpop2=m_bSubpop2;
	dlg.m_bSubpop3=m_bSubpop3;
	dlg.m_iResultInterval=gGAParam.iResultInterval;
	dlg.m_iSubpop1=m_iSubpop1;
	dlg.m_iSubpop2=m_iSubpop2;
	dlg.m_iSubpop3=m_iSubpop3;
	if(dlg.DoModal()==IDOK)
	{
		m_bAllOptimaFitness=dlg.m_bAllOptimaFitness;
		m_bAllOptimaGene=dlg.m_bAllOptimaGene;
		m_bAllSubpopFitness=dlg.m_bAllSubpopFitness;
		m_bAllSubpopGene=dlg.m_bAllSubpopGene;
		m_bAutoScroll=dlg.m_bAutoScroll;
		m_bLowSpeed=dlg.m_bLowSpeed;
		gGAParam.lBreakPos=dlg.m_lBreakPos;
		m_bSubpop1=dlg.m_bSubpop1;
		m_bSubpop2=dlg.m_bSubpop2;
		m_bSubpop3=dlg.m_bSubpop3;
		gGAParam.iResultInterval=dlg.m_iResultInterval;
		m_iSubpop1=dlg.m_iSubpop1<CPopulation::MaxOptimaNum?dlg.m_iSubpop1:0;

⌨️ 快捷键说明

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