📄 gadlg.cpp
字号:
m_taskListCtrl.MoveWindow(&rect);
m_taskNum = m_tasksSet[m_selectTS]->m_number;
m_taskListCtrl.UpdateData(FALSE);
m_taskListView.UpdateData(FALSE);
}
void CGADlg::update_agent_list_ctrl()
{
int i;
m_agentListCtrl.DeleteAllItems();
while(m_agentListCtrl.DeleteColumn(0));
m_agentListCtrl.InsertColumn(0, "Agent", LVCFMT_CENTER, 50);
int nAB = m_agentsSet[m_selectAS]->m_abilityNum;
CString str;
for (i=0; i<nAB; i++)
{
str.Format("HvAb%d", i+1);
m_agentListCtrl.InsertColumn(i+1, str, LVCFMT_CENTER, 50);
}
m_agentListCtrl.InsertColumn(++i, "cost", LVCFMT_CENTER, 50);
m_agentListCtrl.InsertColumn(++i, "flag", LVCFMT_CENTER, 50);
int nAgent = m_agentsSet[m_selectAS]->m_number;
for(i=0; i< nAgent; i++)
{
str.Format("%d", i+1);
m_agentListCtrl.InsertItem(i, str);
for (int j=0; j<nAB; j++)
{
str.Format("%d", m_agentsSet[m_selectAS]->m_agentList[i].ability[j]);
m_agentListCtrl.SetItemText(i, j+1, str);
}
str.Format("%d", m_agentsSet[m_selectAS]->m_agentList[i].cost);
m_agentListCtrl.SetItemText(i, ++j, str);
str.Format("%d", m_agentsSet[m_selectAS]->m_agentList[i].flag);
m_agentListCtrl.SetItemText(i, ++j, str);
}
RECT rect;
rect.left = 50;
rect.top = 50;
rect.right = rect.left + 50*(1+nAB+2) + 50;
if(nAgent <= 0) rect.bottom = rect.top + 200 + 50;
else rect.bottom = rect.top + 15*(nAgent+1) + 50;
m_agentListView.MoveWindow(&rect);
rect.left =10;
rect.top = 10;
rect.right = rect.left + 50*(1+nAB+2) ;
if(nAgent <= 0) rect.bottom = rect.top + 200 ;
else rect.bottom = rect.top + 15*(nAgent+1);
m_agentListCtrl.MoveWindow(&rect);
m_agentNum = m_agentsSet[m_selectAS]->m_number;
m_agentListView.UpdateData(FALSE);
m_agentListCtrl.UpdateData(FALSE);
}
void CGADlg::OnButtonAgentView()
{
// TODO: Add your control notification handler code here
m_agentListView.ShowWindow(true);
}
void CGADlg::OnSelendokComboAgent()
{
// TODO: Add your control notification handler code here
CString str;
m_comboAgent.GetLBText(m_comboAgent.GetCurSel(), str);
m_selectAS = m_mapAgentSet.find(str)->second - 1;
this->update_agent_list_ctrl();
UpdateData(FALSE);
}
void CGADlg::init_param_set()
{
m_pramSet.clear();
m_pramSet.push_back(new Param(100, 0.01f, 0.7f, 200, 10)); //默认参数设置
m_mapParamSet["默认随机"] = 1;
char filename[] = "param_set.txt";
FILE *fp = fopen(filename, "r");
if(fp==NULL) {
AfxMessageBox(CString("open ") + filename + "failed");
return;
}
char str[256];
int cnt = 1;
while (true)
{
if(fscanf(fp, "%s", str)==EOF) break;
if(!strcmp(str, "end")) break;
if(!strncmp(str, "paramset", 8))
{
m_pramSet.push_back(new Param);
m_iterPS = m_pramSet.end() -1;
char paramSetID[256];
fgets(paramSetID, 255, fp);
m_comboParam.AddString(paramSetID);
m_mapParamSet[paramSetID] = ++cnt;
}
if(!strncmp(str,"popsize", 7))
{
fscanf(fp, "%d", &(*m_iterPS)->popSize);
}
if (!strncmp(str, "pmutation", 9))
{
fscanf(fp, "%f", &(*m_iterPS)->pMutation);
}
if (!strncmp(str, "pcross", 6))
{
fscanf(fp, "%f", &(*m_iterPS)->pCross);
}
if (!strncmp(str, "gentimes", 8))
{
fscanf(fp, "%d", &(*m_iterPS)->genTimes);
}
if (!strncmp(str, "retrytimes", 10))
{
fscanf(fp, "%d", &(*m_iterPS)->retryTimes);
}
}
fclose(fp);
}
void CGADlg::OnSelendokCOMBOParamSet()
{
// TODO: Add your control notification handler code here
CString str;
m_comboParam.GetLBText(m_comboParam.GetCurSel(), str);
m_selectPS = m_mapParamSet.find(str)->second - 1;
this->update_param_view();
UpdateData(FALSE);
}
void CGADlg::update_param_view()
{
m_popSize = m_pramSet[m_selectPS]->popSize;
m_mutationP = m_pramSet[m_selectPS]->pMutation;
m_crossP = m_pramSet[m_selectPS]->pCross;
m_GenTimes = m_pramSet[m_selectPS]->genTimes;
m_retryTimes = m_pramSet[m_selectPS]->retryTimes;
UpdateData(FALSE);
}
void CGADlg::excute_GA()
{
UpdateData(TRUE); //获取当前输入数据
MSG message;
delete m_testResSet;
m_testResSet = new TestResult_Set(m_retryTimes, m_GenTimes);
int start = clock();
int gen = 0;
individual elitist;
elitist.fitness = 0.0f;
int i;
for(i=0; i<m_agentNum; i++)
elitist.chrom[i] = 0;
delete m_genetic;
m_genetic = new CGenetic(m_popSize, m_agentNum, m_GenTimes,
m_crossP, m_mutationP,
m_agentsSet[m_selectAS], m_tasksSet[m_selectTS],
m_taskNum, 1); //分配一个对象
int k = 0;
int bestK = 0;
float maxFitnessK = 0.0f;
for (k=0; k<m_retryTimes; k++) //执行多次,取平均效果
{
int gen = 0;
int bestGen = 0;
float maxFitnessGen = 0;
srand((unsigned)time(NULL)); //随机种子
//gene_list_flag = 0;
if(1 != 0) //执行条件
{
do {
m_genetic->generation(); //生成下一代
//保存最优解
elitist.fitness = m_genetic->elitist.fitness;
for(i=0; i<m_agentNum; i++)
{
elitist.chrom[i] = m_genetic->elitist.chrom[i];
}
if (elitist.fitness > maxFitnessGen){
maxFitnessGen = elitist.fitness;
bestGen = gen;
}
m_testResSet->res_set[k].res[gen].elitist = elitist;
for(int ntemp=0; ntemp<1; ntemp++) //CPU轮转一次
{
if(::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
//使新一代成父母
memcpy(m_genetic->oldpop, m_genetic->newpop,
sizeof(struct individual) * m_genetic->popsize);
gen ++;
if(gen%200==0) m_genetic->p_mutation+=0.01f; //每200代增加0.01的变异率
}while(gen < m_genetic->gen_number); //以遗传代数作为终止条件
int tmpClk = clock();
m_testResSet->res_set[k].useTime = (float)(tmpClk-start)/CLOCKS_PER_SEC;
m_testResSet->res_set[k].bestIndex = bestGen;
CString stradd;
stradd.Format("---Gen %d, useTime: %f", k+1, m_testResSet->res_set[k].useTime);
this->SetWindowText(m_strTitle+stradd);
}
if (maxFitnessK < maxFitnessGen)
{
maxFitnessK = maxFitnessGen;
bestK = k;
}
for (i=1; i<m_taskNum+1; i++)
{
m_testResSet->res_set[k].cs_flag[i] = m_genetic->cs_flag[i];
}
}
m_testResSet->bestIndex = bestK;
int finish = clock();
m_testResSet->totalUseTime = (float)(finish-start)/CLOCKS_PER_SEC;
CString str;
str.Format("Total use time: %fs", m_testResSet->totalUseTime);
AfxMessageBox(str);
}
void CGADlg::excute_RA()
{
m_rand_start = clock();
CString stradd;
stradd.Format("---RA start...");
this->SetWindowText(m_strTitle+stradd);
srand(time(NULL));
char * arr= new char[m_agentNum];
delete [] m_rand_arr;
m_rand_arr = new char[m_agentNum];
delete [] m_rand_cs_flag;
m_rand_cs_flag = new int[m_taskNum+1];
delete m_genetic;
m_genetic = new CGenetic(m_popSize, m_agentNum, m_GenTimes,
m_crossP, m_mutationP,
m_agentsSet[m_selectAS], m_tasksSet[m_selectTS],
m_taskNum, 1); //分配一个对象
float max = 0.0f;
for (int i=0; i<m_randTimes; i++)
{
for(int j=0; j<m_agentNum; j++)
{
arr[j] = rand()%(m_taskNum+1);
}
float tmpmax = m_genetic->fitness_v(arr);
if (tmpmax > max)
{
max = tmpmax;
memcpy(m_rand_arr, arr, sizeof(m_rand_arr[0])*m_agentNum);
memcpy(m_rand_cs_flag, m_genetic->cs_flag, sizeof(m_rand_cs_flag[0])*(m_taskNum+1));
}
}
m_rand_best_fitness = max;
int finish = clock();
float usetime = (float)(finish-m_rand_start)/CLOCKS_PER_SEC;
stradd.Format("---RA end, useTime: %f", usetime);
this->SetWindowText(m_strTitle+stradd);
CString str;
str.Format("Total use time: %fs", usetime);
AfxMessageBox(str);
delete [] arr;
}
void CGADlg::excute_DFS()
{
CString stradd;
stradd.Format("---DFS start...");
this->SetWindowText(m_strTitle+stradd);
char *arr = new char[m_agentNum];
delete [] m_dfs_arr;
m_dfs_arr = new char[m_agentNum];
delete [] m_dfs_cs_flag;
m_dfs_cs_flag = new int[m_taskNum +1];
delete m_genetic;
m_genetic = new CGenetic(m_popSize, m_agentNum, m_GenTimes,
m_crossP, m_mutationP,
m_agentsSet[m_selectAS], m_tasksSet[m_selectTS],
m_taskNum, 1); //分配一个对象
m_dfs_best_fitness = 0.0f;
m_dfs_start = clock();
DFS(0, arr);
int finish = clock();
float usetime = (float)(finish-m_dfs_start)/CLOCKS_PER_SEC;
stradd.Format("---DFS end, useTime: %f", usetime);
this->SetWindowText(m_strTitle+stradd);
CString str;
str.Format("Total use time: %fs", usetime);
AfxMessageBox(str);
delete [] arr;
}
void CGADlg::viewResult()
{
int i;
CString str;
switch (m_radio1)
{
case 0:
//ADD GA Res
int bestSet;
bestSet = m_testResSet->bestIndex;
int bestIndex;
bestIndex = m_testResSet->res_set[bestSet].bestIndex;
individual elitist;
elitist = m_testResSet->res_set[bestSet].res[bestIndex].elitist;
m_ResultView.m_editResGA = elitist.fitness;
while(m_ResultView.m_listGA.GetCount())
m_ResultView.m_listGA.DeleteString(0);
for (i=1; i<m_taskNum+1; i++)
{
str.Format("TID:%03d,F:%d", i, m_testResSet->res_set[bestSet].cs_flag[i]);
m_ResultView.m_listGA.AddString(str);
}
for (i=0; i<m_agentNum; i++)
{
str.Format("AID%03d,F:%d", i+1, (int)elitist.chrom[i]);
m_ResultView.m_listGA.AddString(str);
}
break;
case 1:
//add RA res
m_ResultView.m_editResRand = m_rand_best_fitness;
while(m_ResultView.m_listRand.GetCount())
m_ResultView.m_listRand.DeleteString(0);
for (i=1; i<m_taskNum+1; i++)
{
str.Format("TID:%03d,F:%d", i, m_rand_cs_flag[i]);
m_ResultView.m_listRand.AddString(str);
}
for (i=0; i<m_agentNum; i++)
{
str.Format("AID:%03d,F:%d", i+1, m_rand_arr[i]);
m_ResultView.m_listRand.AddString(str);
}
break;
case 2:
//add DFS res
m_ResultView.m_editReDFS = m_dfs_best_fitness;
while(m_ResultView.m_listDFS.GetCount())
m_ResultView.m_listDFS.DeleteString(0);
for (i=1; i<m_taskNum+1; i++)
{
str.Format("TID:%03d,F:%d", i, m_dfs_cs_flag[i]);
m_ResultView.m_listDFS.AddString(str);
}
str.Format("AgentID, flag");
m_ResultView.m_listDFS.AddString(str);
for (i=0; i<m_agentNum; i++)
{
str.Format("AID:%03d,F:%d", i+1, m_dfs_arr[i]);
m_ResultView.m_listDFS.AddString(str);
}
break;
default:
break;
}
}
void CGADlg::DFS(int d, char *arr)
{
if(d >= m_agentNum || clock()-m_dfs_start > (int)(m_timeDFS*CLOCKS_PER_SEC))
{
DFS_cpt(arr);
return;
}
for (int i=0; i<m_taskNum+1; i++)
{
arr[d] = i;
DFS(d+1, arr);
}
}
void CGADlg::DFS_cpt(char *arr)
{
float tmpmax = m_genetic->fitness_v(arr);
if(m_dfs_best_fitness < tmpmax)
{
m_dfs_best_fitness = tmpmax;
memcpy(m_dfs_arr, arr, sizeof(arr[0])*m_agentNum);
memcpy(m_dfs_cs_flag, m_genetic->cs_flag,
sizeof(m_genetic->cs_flag[0])*(m_taskNum+1));
}
}
BOOL CGADlg::DestroyWindow()
{
m_agentListCtrl.DestroyWindow();
m_taskListCtrl.DestroyWindow();
m_ResultView.DestroyWindow();
engClose(m_matlabEG);
delete [] m_rand_arr;
delete [] m_dfs_arr;
delete [] m_rand_cs_flag;
delete [] m_dfs_cs_flag;
delete m_testResSet;
delete m_genetic;
int i;
for(i=0; i<m_tasksSet.size(); i++)
{
delete m_tasksSet[i];
}
m_tasksSet.clear();
for(i=0; i<m_agentsSet.size(); i++)
{
delete m_agentsSet[i];
}
m_agentsSet.clear();
// TODO: Add your specialized code here and/or call the base class
return CDialog::DestroyWindow();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -