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

📄 dlgvrp.cpp

📁 一个求解车辆路径问题的粒子群算法的源码
💻 CPP
字号:
// DlgVrp.cpp : implementation file
//

#include "stdafx.h"
#include "VRP.h"
#include "DlgVrp.h"
#include "PsoVrp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgVrp dialog


CDlgVrp::CDlgVrp(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgVrp::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgVrp)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgVrp::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgVrp)
	DDX_Control(pDX, IDC_PROGRESS1, m_progress);
	DDX_Control(pDX, IDC_LIST1, m_ResultList);
	//}}AFX_DATA_MAP
}


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

/////////////////////////////////////////////////////////////////////////////
// CDlgVrp message handlers

void CDlgVrp::OnOK() 
{
	// TODO: Add extra validation here
	char* pszFileName1 = "VRP1.dat";
    CFile myFile;
    CFileException fileException;
	char        xu[12]="20";
    char  szBuffer[1024];
    int nActual;
	int i,data,pos,base;
	double data1;
	int index,x,y;
	double weight;
	DWORD dwStart, dwStop, dwLast,All_time;
	LVITEM		lvi;
	int			nNumofCount;
	double data_Vrp,Alll_data,min_data;
    CPsoVrp Vrp;
	if ( !myFile.Open(pszFileName1,CFile::modeRead), &fileException )
	{
       TRACE( "Can't open file %s, error = %u\n", pszFileName1, fileException.m_cause );
	}

	myFile.Seek( 0, CFile::begin );

    nActual = myFile.Read(szBuffer, sizeof(szBuffer));
	myFile.Close();
   pos=0;
   for(i=0;i<=TASK;i++)
   {	
   	data=0;
	while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
	while(szBuffer[pos]<='9'&&szBuffer[pos]>='0'){
			data=data*10+szBuffer[pos]-'0';
			pos++;
	}
	//读取整数序号
	while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
	//跳开间隔符
	index=data;
	
    data=0;
	while(szBuffer[pos]<='9'&&szBuffer[pos]>='0'){
			data=data*10+szBuffer[pos]-'0';
			pos++;
	}
	//读取整数序号
	while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
	//跳开间隔符
	x=data;

	data=0;
	while(szBuffer[pos]<='9'&&szBuffer[pos]>='0'){
			data=data*10+szBuffer[pos]-'0';
			pos++;
	}
	//读取整数序号
	while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
	//跳开间隔符
	y=data;

    data=0;
	while(szBuffer[pos]<='9'&&szBuffer[pos]>='0'){
			data=data*10+szBuffer[pos]-'0';
			pos++;
	}
	//读取整数序号
	//while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
	data1=0;
	//跳开间隔符
    if(szBuffer[pos]=='.')
	{
		base=1;
		pos++;
		while(szBuffer[pos]<='9'&&szBuffer[pos]>='0')
		{
			data1=data1*10+szBuffer[pos]-'0';
			pos++;
			base=base*10;
		}
		while(szBuffer[pos]>'9'||szBuffer[pos]<'0')pos++;
		//跳开间隔符
		data1=(double)data1/base;
	}
	
    weight=data+data1;  
    Vrp.GPNode.index[i]=index;
	Vrp.GPNode.x[i]=x;
	Vrp.GPNode.y[i]=y;
	Vrp.GPNode.weight[i]=weight;

   }
    srand( (unsigned)time( NULL ) );
    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;

	for(int Try=0;Try<MAXTRY;Try++)
	{
	   dwStart = GetTickCount(); 
       data_Vrp=Vrp.Vrp_Pso();
	   dwStop = GetTickCount();
	   dwLast = dwStop - dwStart;
	   //测试产生数据

  	    All_time+=dwLast;
  	   nNumofCount = m_ResultList.GetItemCount();
	   lvi.iItem = nNumofCount+1;
	   lvi.pszText = xu;
	   _itoa(nNumofCount,xu,10);
	   m_ResultList.InsertItem(nNumofCount,xu);
	   data=(int)data_Vrp;
	   _itoa(data,xu,10);
	   m_ResultList.SetItemText(nNumofCount,1, xu);
   	   _ltoa(dwLast,xu,10);
	   m_ResultList.SetItemText(nNumofCount,2, xu);
	   m_progress.SetPos(i);
	   UpdateData(FALSE);
	   m_progress.Invalidate();
	}


}

BOOL CDlgVrp::OnInitDialog() 
{
	CString strTemp[3] = {"序号","仿真结果", "完成时间"};
    int size[3] = {50,80,100};
  	int i;

	CDialog::OnInitDialog();

    for(i = 0; i < 3; i++)
	{
	 m_ResultList.InsertColumn(i, strTemp[i],LVCFMT_LEFT,size[i]);
	}
    
	m_ResultList.SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);
	m_ResultList.DeleteAllItems();

    UpdateData(FALSE);
    m_ResultList.Invalidate();	
	m_progress.SetRange(0,MAXTRY+1);
	m_progress.SetPos(MAXTRY);
	 UpdateData(FALSE);
	 m_progress.Invalidate();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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