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

📄 queen_gadlg.cpp

📁 使用人工智能的遗传算法来解N皇后问题
💻 CPP
字号:
// Queen_GADlg.cpp : implementation file
//

#include "stdafx.h"
#include "Queen_GA.h"
#include "Queen_GADlg.h"
#include "Queen.h"
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CQueen_GADlg dialog

CQueen_GADlg::CQueen_GADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQueen_GADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CQueen_GADlg)
	m_iteration = 0;
	m_length = 0;
	m_mutation = 0.0f;
	m_N = 0;
	m_time = 0.0f;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQueen_GADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQueen_GADlg)
	DDX_Control(pDX, IDC_STATIC_BOARD, m_Board);
	DDX_Text(pDX, IDC_ITERATION, m_iteration);
	DDV_MinMaxInt(pDX, m_iteration, 1, 100);
	DDX_Text(pDX, IDC_LENGTH, m_length);
	DDV_MinMaxInt(pDX, m_length, 5, 30);
	DDX_Text(pDX, IDC_MUTATION, m_mutation);
	DDV_MinMaxFloat(pDX, m_mutation, 0.f, 1.f);
	DDX_Text(pDX, IDC_POPULATION, m_N);
	DDV_MinMaxInt(pDX, m_N, 5, 1000);
	DDX_Text(pDX, IDC_TIME, m_time);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CQueen_GADlg, CDialog)
	//{{AFX_MSG_MAP(CQueen_GADlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BEGIN, OnBegin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueen_GADlg message handlers

BOOL CQueen_GADlg::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
	m_iteration=15;
	m_N=100;
	m_mutation=0.005;
	m_length=8;	
	UpdateData(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQueen_GADlg::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 CQueen_GADlg::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 CQueen_GADlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CQueen_GADlg::OnBegin() 
{
	// TODO: Add your control notification handler code here
	UpdateData(1);
	int i,k=0,g=0,num=0;	
	char  a='g';
	if(m_length<=30)
	{
		clock_t start, finish;
		start = clock(); 
		Queen nQueen(m_N,m_iteration,m_mutation,m_length);
		InitChessboard(m_length);
		nQueen.initialColony();             //随机生成初始群体
	//	CString as,gs;
		while(g==0 && num<nQueen.Iteration)
		{	
			num++;
			g=0;
			for (k=0;k<=nQueen.Population-1;k++)
			{
				nQueen.fillChess(k);                  //填入棋盘
				nQueen.CostMatrix[k]=nQueen.costFuction(k);		//计算各染色体的适应值,并记录到CostMatrix中	
			}
			nQueen.chromosomeSort();//对染色体按适应强度排序
			
				
			if (nQueen.CostMatrix[0]==0)//如果适应值为0,即为解
				g=1;
			clearBoard(m_length);
			for(i=0;i<=m_length-1;i++)
			{
				placeQueen(m_length,i,nQueen.ChromosomeMatrix[i][0]);
				FillMatrix[i]=nQueen.ChromosomeMatrix[i][0];
			}
		//	if(m_Graphic && m_Animate)Sleep(500);

				
			nQueen.generateCrossOverMatrix();//生成交配位
				
			nQueen.mating();//交配

			nQueen.mutate();//变异
		}
/*		gs="";
		for (i=0;i<=m_ChBoard-2;i++)
		{
			as.Format("%d_",nQueen.ChromosomeMatrix[i][0]);
			gs+=as;
		}
		as.Format("%d",nQueen.ChromosomeMatrix[m_ChBoard-1][0]);
		gs+=as;
			
		m_BestAnswer=gs;
			
		gs.Format("%d",nQueen.CostMatrix[0]);
		m_BestCost=gs;

		m_Generation=num;
					
		UpdateData(0);
		if (m_BestDisplay && !m_Graphic)
		{
			InitializeBoard(m_ChBoard);
			ClearArea(m_ChBoard);
			for(i=0;i<=m_ChBoard-1;i++)
			{
				PlaceQueen(m_ChBoard,i,nQueen.ChromosomeMatrix[i][0]);
				FillMatrix[i]=nQueen.ChromosomeMatrix[i][0];
			}
			
		}*/
		finish=clock();
		m_time=finish-start;
		UpdateData(0);
	}
	else
	{
		MessageBox("请设置棋盘的大小小于30");
	}
	
}

void CQueen_GADlg::InitChessboard(int Length)
{
	int tt,yy;
	CDC* mmm;
	mmm=m_Board.GetDC();

	
	mmm->SetMapMode (MM_ISOTROPIC);
    mmm->SetWindowExt (1000, 1000);
    mmm->SetViewportExt (1000, 1000);
    mmm->SetViewportOrg (0, 0);
	

	COLORREF crColor=RGB(0,255,0);
	CPen penGreen (PS_SOLID, 1, crColor);
	mmm->SelectObject (&penGreen);

	mmm->MoveTo(0,0);
	mmm->LineTo(0,300);
	mmm->LineTo(300,300);
	mmm->LineTo(300,0);
	mmm->LineTo(0,0);

	crColor=::GetSysColor(COLOR_3DFACE);
	CBrush brsSys(crColor);
	mmm->SelectObject(&brsSys);
	mmm->FloodFill(1,1,RGB(0,255,0));
	

	crColor=RGB(0,0,255);
	CPen penBlue (PS_SOLID, 1, crColor);
	mmm->SelectObject (&penBlue);
	
	
	for (int i=0;i<=Length;i++)
	{
		
		mmm->MoveTo(3,(303/Length)*i+3);
		mmm->LineTo(303,(303/Length)*i+3);
		
		mmm->MoveTo((303/Length)*i+3,3);
		mmm->LineTo((303/Length)*i+3,303);
	}

	
	CBrush brsWhite(RGB(255,255,255));
	mmm->SelectObject(&brsWhite);

	for (int j=0;j<Length;j+=2)
		for (i=0;i<Length;i+=2)
		{
			tt=(300/Length)*i+(150/Length);
			yy=(300/Length)*j+(150/Length);
			mmm->FloodFill(tt,yy,RGB(0,0,255));
		}

	for (j=1;j<Length;j+=2)
		for (i=1;i<Length;i+=2)
		{
			tt=(300/Length)*i+(150/Length);
			yy=(300/Length)*j+(150/Length);
			mmm->FloodFill(tt,yy,RGB(0,0,255));
		}

	CBrush brsBlack(RGB(0,0,0));
	mmm->SelectObject(&brsBlack);

	for (j=1;j<Length;j+=2)
		for (i=0;i<Length;i+=2)
		{
			tt=(300/Length)*i+(150/Length);
			yy=(300/Length)*j+(150/Length);
			mmm->FloodFill(tt,yy,RGB(0,0,255));
		}

	for (j=0;j<Length;j+=2)
		for (i=1;i<Length;i+=2)
		{
			tt=(300/Length)*i+(150/Length);
			yy=(300/Length)*j+(150/Length);
			mmm->FloodFill(tt,yy,RGB(0,0,255));
		}

}

void CQueen_GADlg::clearBoard(int Length)        //清除棋盘
{
	int tt,yy,i,j;
	CDC* mmm;
	mmm=m_Board.GetDC();

	COLORREF  crColor=RGB(0,0,255);
	CPen penBlue (PS_SOLID, 1, crColor);
	mmm->SelectObject (&penBlue);
	
	
	for (i=0;i<=Length;i++)
	{
		
		mmm->MoveTo(3,(303/Length)*i+3);
		mmm->LineTo(303,(303/Length)*i+3);
		
		mmm->MoveTo((303/Length)*i+3,3);
		mmm->LineTo((303/Length)*i+3,303);
	}
	
	CBrush brsWhite(RGB(255,255,255));
	mmm->SelectObject(&brsWhite);

	for (j=0;j<Length;j+=2)
		for (i=0;i<Length;i+=2)
		{
			if (FillMatrix[j]==i)
			{
				tt=(300/Length)*i+(150/Length);
				yy=(300/Length)*j+(150/Length);
				mmm->FloodFill(tt,yy,RGB(0,0,255));
			}
		}

	for (j=1;j<Length;j+=2)
		for (i=1;i<Length;i+=2)
		{
			if (FillMatrix[j]==i)
			{
				tt=(300/Length)*i+(150/Length);
				yy=(300/Length)*j+(150/Length);
				mmm->FloodFill(tt,yy,RGB(0,0,255));
			}
		}
	
	CBrush brsBlack(RGB(0,0,0));
	mmm->SelectObject(&brsBlack);

	for (j=1;j<Length;j+=2)
		for (i=0;i<Length;i+=2)
		{
			if (FillMatrix[j]==i)
			{
				tt=(300/Length)*i+(150/Length);
				yy=(300/Length)*j+(150/Length);
				mmm->FloodFill(tt,yy,RGB(0,0,255));
			}
		}

	for (j=0;j<Length;j+=2)
		for (i=1;i<Length;i+=2)
		{
			if (FillMatrix[j]==i)
			{
				tt=(300/Length)*i+(150/Length);
				yy=(300/Length)*j+(150/Length);
				mmm->FloodFill(tt,yy,RGB(0,0,255));
			}
		}
}

void CQueen_GADlg::placeQueen(int Length,int row,int column)  //放置皇后,用图案方式表示,圆圈为皇后
{
	int x,y;
	
	CDC* mmm;
	mmm=m_Board.GetDC();
	
	
	COLORREF crColor=RGB(255,0,0);
	CPen pen1 (PS_SOLID, 2, crColor);
	mmm->SelectObject (&pen1);
	

	y=(303/Length)*row+(160/Length);
	x=(303/Length)*column+(160/Length);
	
	int r=(75/Length);
	mmm->MoveTo(x+r,y);
	mmm->AngleArc(x,y,r,0,360);
}

⌨️ 快捷键说明

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