📄 gatspdlg.cpp
字号:
if(pick < pmutation) //变异操作
{
mutation(newpop.pop_chrom[j+1]);
chromCost(newpop.pop_chrom[j+1]); //计算适应度
}
times = j + 2;
j += 2;
if(count > popsize * 2) // 产生足够的中间个体后退出
break;
}
sort(&newpop.pop_chrom[0],&newpop.pop_chrom[0]+times,comp);
for(int k = 0; k < popsize-1; k++) // 产生新一代个体
newpop1.pop_chrom[k+1] = newpop.pop_chrom[k];
popCost(newpop1); //计算新种群的适应度之和
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGATSPDlg dialog
CGATSPDlg::CGATSPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CGATSPDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CGATSPDlg)
m_popsize = 100;
m_cross = 0.85;
m_mutation = 0.1;
m_gen = 100;
m_total_price = 0.0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
pMenu = new CMenu;
// 初始化全部基因,gene保存了全部基因
int i,j;
for(i = 0; i < lchrom; i++)
{
genes[i].name =citys[i];
for(j=0; j<lchrom; j++)
{
genes[i].linkCost[&genes[j]] = dis[i][j];
}
genes[i].num = i;
}
}
void CGATSPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CGATSPDlg)
DDX_Control(pDX, IDC_LIST_RESULT, m_city);
DDX_Text(pDX, IDC_EDIT1, m_popsize);
DDX_Text(pDX, IDC_EDIT2, m_cross);
DDX_Text(pDX, IDC_EDIT3, m_mutation);
DDX_Text(pDX, IDC_EDIT5, m_gen);
DDX_Text(pDX, IDC_EDIT_RESULT, m_total_price);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CGATSPDlg, CDialog)
//{{AFX_MSG_MAP(CGATSPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_ZHIXING, OnButtonZhixing)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_BN_CLICKED(IDC_BUTTON_DRAW, OnButtonDraw)
ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
ON_COMMAND(ID_MENU_SHEZHI, OnMenuShezhi)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGATSPDlg message handlers
BOOL CGATSPDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
/* CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
*/
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
SetWindowText("遗传算法求解TSP问题"); //改变标题栏
//设置主菜单
pMenu->LoadMenu(MAKEINTRESOURCE(IDR_MENU2));
SetMenu(pMenu);
// 列表控件操作
m_ImageList.Create(16,16,ILC_COLOR32,1,1);
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON_CITY));
m_city.SetImageList(&m_ImageList,LVSIL_SMALL);
m_city.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_city.InsertColumn(0,"步数",LVCFMT_LEFT,120,0);
m_city.InsertColumn(1,"城市",LVCFMT_LEFT,130,1);
//m_city.InsertColumn(2,"代价",LVCFMT_LEFT,95,2);
return TRUE; // return TRUE unless you set the focus to a control
}
void CGATSPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CGATSPDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGATSPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CGATSPDlg::OnButtonZhixing()
{
// TODO: Add your control notification handler code here
int best;
Chrom bestChrom; //全部种群中最佳染色体
bestChrom.fitness = 0;
double sumVarible = 0;
double sumFitness = 0;
initpop(oldpop); //产生初始种群
//通过不断进化,直到达到最大世代数
for(gen = 1;gen<=maxgen;gen++)
{
generation(oldpop,newpop); //从当前种群产生新种群
oldpop.pop_chrom.swap(newpop.pop_chrom);
oldpop.sumfitness = newpop.sumfitness;
newpop.pop_chrom.clear();
}
best = chooseBest(oldpop); //本次运行得出的最佳染色体
if(oldpop.pop_chrom[best].fitness > bestChrom.fitness)
bestChrom = oldpop.pop_chrom[best];
oldpop.pop_chrom.clear();
char result[10];
string tmp;
for(int k = 0; k < lchrom; k++)
{
sprintf(result,"%d",k);
m_city.InsertItem(k,result);
tmp = (bestChrom.chrom_gene[k])->name;
m_city.SetItemText(k,1,tmp.c_str());
loc[k] = (bestChrom.chrom_gene[k])->num; // 访问城市的编号顺序
}
m_total_price = bestChrom.varible;
UpdateData(FALSE);
}
void CGATSPDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
OnCancel();
}
void CGATSPDlg::OnButtonDraw()
{
// TODO: Add your control notification handler code here
CDRAWMAP dlg;
dlg.DoModal();
}
void CGATSPDlg::OnMenuExit()
{
// TODO: Add your command handler code here
OnCancel();
}
void CGATSPDlg::OnMenuShezhi()
{
// TODO: Add your command handler code here
CCANSHU dlg;
if(dlg.DoModal()== IDOK)
{
pcross = dlg.m_cross;
pmutation = dlg.m_mutation;
popsize = dlg.m_popsize;
maxgen = dlg.m_gen;
m_cross = dlg.m_cross;
m_mutation = dlg.m_mutation;
m_popsize = dlg.m_popsize;
m_gen = dlg.m_gen;
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -