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

📄 ant_testdlg.cpp

📁 蚁群算法的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ant_testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ant_test.h"
#include "ant_testDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <iostream.h>
int cur_iter;
double curbest[1000];
double aver1[1000];
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CAnt_testDlg dialog

CAnt_testDlg::CAnt_testDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAnt_testDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAnt_testDlg)
	m_edit2 = 20;
	m_edit3 = 500;
	m_edit4 = 0.2;
	m_edit5 = 0.2;
	m_edit6 = 0.8;
	m_edit7 = 0.01;
	m_edit8 = 20;
	m_edit9 = 0.0;
	m_edit10 = 0.0;
	m_edit11 = 240;
	m_edit12 = 0;
	m_edit13 = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAnt_testDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAnt_testDlg)
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	DDX_Text(pDX, IDC_EDIT3, m_edit3);
	DDX_Text(pDX, IDC_EDIT4, m_edit4);
	DDX_Text(pDX, IDC_EDIT5, m_edit5);
	DDX_Text(pDX, IDC_EDIT6, m_edit6);
	DDX_Text(pDX, IDC_EDIT7, m_edit7);
	DDX_Text(pDX, IDC_EDIT8, m_edit8);
	DDX_Text(pDX, IDC_EDIT9, m_edit9);
	DDX_Text(pDX, IDC_EDIT10, m_edit10);
	DDX_Text(pDX, IDC_EDIT11, m_edit11);
	DDX_Text(pDX, IDC_EDIT12, m_edit12);
	DDX_Text(pDX, IDC_EDIT13, m_edit13);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAnt_testDlg, CDialog)
	//{{AFX_MSG_MAP(CAnt_testDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, Onend)
	ON_WM_CANCELMODE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON2, Onrun)
	ON_BN_CLICKED(IDC_BUTTON3, OnShow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAnt_testDlg message handlers

BOOL CAnt_testDlg::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
	ShowWindow(SW_MAXIMIZE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CAnt_testDlg::Onend() //退出
{
	// TODO: Add your control notification handler code here
	PostQuitMessage(0);	
	
	
}

void CAnt_testDlg::Onrun() //运行
{
	// TODO: Add your control notification handler code here
	
	UpdateData(true);
	if(m_edit2<=0 || m_edit3<=0)
	{
		MessageBox("请正确输入参数!",NULL,MB_OK);
	}
	else
	{
        srand(time(NULL));
		alpha=m_edit4;
		num_ants=m_edit2;
		num_iters=m_edit3;
		num_layers=m_edit11;
		dimension=m_edit8;
		p0=m_edit6;
		rho=m_edit5;
		tau0=m_edit7;		
		allocate_vars();		
		int cur_layer, cur_ant;
		init_vars();
		cur_iter=0;
		SetTimer(1,10, NULL);
	}
}

void CAnt_testDlg::release_vars()  //释放
{
    int i,j;
	for (i=0; i<num_layers; i++)
	{
		for (j=0; j<10; j++)
			delete[] tau[i][j];
		delete[] tau[i];
	}
	delete[] tau;
	for (i=0; i<num_ants; i++)
		delete[] ant[i];
	delete[] ant;
	for (i=0; i<num_ants; i++)
		delete[] x[i];
	delete[] x;
	delete[] fit;
	delete[] y;
	delete[] gbant;
	delete[] ibant;
	need_release=false;
}
void CAnt_testDlg::allocate_vars()  //分配
{	
    int i,j;
	if(need_release==true)
		release_vars();	
	tau=new float**[num_layers];
	for (i=0; i<num_layers; i++)
	{
		tau[i]=new float*[10];
		for (j=0; j<10; j++)		
			tau[i][j]=new float[10 +1];	//the last element is used to store the accumulate value of the foregoing 10 numbers
	}
	ant=new int*[num_ants];
	for (i=0; i<num_ants; i++)
		ant[i]=new int[num_layers];

	//float *fit;
	fit=new float[num_ants];

	//float *y;
	y=new float[num_ants];

	//int *gbant;
	gbant=new int[num_layers];

	//int *ibant;
	ibant=new int[num_layers];

	//float **x;
	x=new float*[num_ants];
	for (i=0; i<num_ants; i++)
		x[i]=new float[dimension];
	need_release=true;
	layers_per_dimension=num_layers/dimension;
}
void CAnt_testDlg::init_vars()  //初始化
{
    int i, j, k;
	gbfit=100000;
	gby=0;
	for (i=0; i<num_layers; i++)
	{
		for (j=0; j<10; j++)
		{
			for (k=0; k<10; k++)
				//tau[i][j][k]=tau_max;
				tau[i][j][k]=tau0;
			//tau[i][j][10]=tau_max*10;		//sum of the foregoing ten tau
			tau[i][j][10]=tau0*10;
		}
	}
}

void CAnt_testDlg::ant_move(int ant_no, int layer)  //蚂蚁移动
{
	int last_pos, next_pos;
	int i;
	float t;
	if(layer%8==0)
		last_pos=0;
	else
		last_pos=ant[ant_no][layer-1];
	t=(float)rand()/RAND_MAX;
	if ( t<p0 )

⌨️ 快捷键说明

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