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

📄 alssadlg.cpp

📁 一个有关生产排序的模拟退火算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ALSSADlg.cpp : implementation file
//

#include "StdAfx.h"
#include "ALSSA.h"
#include "ALSSADlg.h"
#include "ResertDlg.h"
#include "math.h"


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



//似乎必须放在下面,如果把这些全局变量定义放在最顶处则报错 why?
std::vector<SYCity> vecCitys;						//城市列表
std::vector<SYCityDistance> vecCityDistances;		//城市距离列表
std::vector<Hardpanish> vecHardpanish;
std::vector<WidethpanishP> vecWidethpanishP;
std::vector<WidethpanishN> vecWidethpanishN;
std::vector<Gaugepanish> vecGaugepanish;
std::vector<int> vecCityRouter;

int CityNumber = 0;									//城市个数
double InitialTemperature = 0.0;					//初始温度
double NowTemperature = 0.0;						//当前迭代温度
int	NowExternalIterNumber = 0;						//当前外循环迭代次数
int NowInnerIterNumber = 0;							//当前内循环迭代次数
BOOL IsComputing = FALSE;


/////////////////////////////////////////////////////////////////////////////
// CALSSADlg dialog

CALSSADlg::CALSSADlg(CWnd* pParent /*=NULL*/)
	: CDialog(CALSSADlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CALSSADlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CALSSADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CALSSADlg)

	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CALSSADlg, CDialog)
	//{{AFX_MSG_MAP(CALSSADlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_FILEOPEN, OnFileopen)
	ON_BN_CLICKED(IDC_COMPUT, OnComput)
	ON_BN_CLICKED(IDC_FILEHARD, OnFilehard)
	ON_BN_CLICKED(IDC_FILEWIDTH, OnFilewidth)
	ON_BN_CLICKED(IDC_FILEGAUGE, OnFilegauge)
	//}}AFX_MSG_MAP
//	ON_MESSAGE(WYWM_INFOVIEWAPPENDINFO, OnAppendOutputInfo)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CALSSADlg message handlers

BOOL CALSSADlg::OnInitDialog()
{
	CDialog::OnInitDialog();

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

	GetDlgItem(IDC_COMPUT)->EnableWindow(FALSE);
	GetDlgItem(IDC_FILEHARD)->EnableWindow(FALSE);
	GetDlgItem(IDC_FILEWIDTH)->EnableWindow(FALSE);
	GetDlgItem(IDC_FILEGAUGE)->EnableWindow(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CALSSADlg::OnFileopen() 
{
	// TODO: Add your control notification handler code here
	CString strFileName;
	char szFilter[200];
	int isok=0;
	CListBox * plistInfo = (CListBox*)GetDlgItem(IDC_LIST_INFO);
	strcpy( szFilter, "TXT Files (*.txt)|*.txt||" );
	CFileDialog *pFileDialog;
	pFileDialog = new CFileDialog( TRUE, 
								NULL, 
								NULL, 
								OFN_HIDEREADONLY,
								szFilter,
								this );
	if( IDOK == pFileDialog->DoModal() )
	{
		//CTspSAView *pView = (CTspSAView*)GetActiveView();
		ClearSA();
		//pView->ClearInfos();
		
		CString strValue, strTemp;
		strFileName = pFileDialog->GetPathName();
		CStdioFile DataFile( strFileName, CFile::modeRead );
		CString strReadString;
		int ncityindex = 1;
		while( DataFile.ReadString(strReadString) ) 
		{
			strReadString.TrimLeft();
			strReadString.TrimRight();

			CString cityName, citycodx, citycody;
			int nspace = 0;
			nspace = strReadString.Find(" ");
			if( nspace > 0 )
				cityName = strReadString.Left( nspace );

			strReadString = strReadString.Mid( nspace+1 );
			nspace = strReadString.Find(" ");
			if( nspace > 0 )
				citycodx = strReadString.Left( nspace );

			strReadString = strReadString.Mid( nspace+1 );
			citycody = strReadString;

			SYCity tmpCity;
			tmpCity.m_strName = "城市 "+cityName;
			tmpCity.m_nIndex = ncityindex;
			tmpCity.m_Coordinate.m_fcodx = atof( citycodx );
			tmpCity.m_Coordinate.m_fcody = atof( citycody );

			vecCitys.push_back( tmpCity );

			ncityindex++;
		}
		DataFile.Close();

		InitialSA();
		strTemp = "共读入城市信息";
		strValue.Format("%d",CityNumber);
		strTemp += strValue;
		strTemp += "个";
	//	pView->AddString( strTemp );
		plistInfo->AddString(strTemp);
		
		strTemp = "计算城市距离完成";
	//	pView->AddString( strTemp );
		plistInfo->AddString(strTemp);

		isok=1;	
	}	

	delete pFileDialog;

	if(isok)
	{
		GetDlgItem(IDC_COMPUT)->EnableWindow(true);
//		GetDlgItem(IDC_FILEHARD)->EnableWindow(true);
//		GetDlgItem(IDC_FILEWIDTH)->EnableWindow(true);
//		GetDlgItem(IDC_FILEGAUGE)->EnableWindow(true);
		GetDlgItem(IDC_FILEOPEN)->EnableWindow(FALSE);


	}
}

UINT SACompution(LPVOID pParam)
{
	IsComputing = TRUE;
//	CALSSADlg dlg;
	CListBox * plistInfo = (CListBox*)pParam;

//	dlg.GetDlgItem(IDC_COMPUT)->EnableWindow(FALSE);

	srand( (unsigned)time( NULL ) );
	//简单的说,如果在rand()前不调用srand(),那么,你得到的随机数将是一个固定值srand()用于生成一个随机种子, 由于种子不同,所以就能生成不同的随机数了.


	CFile cityfile("C:\\sacitysfile.txt", CFile::modeCreate|CFile::modeWrite);
	CFile iterfile("C:\\saitersfile.txt", CFile::modeCreate|CFile::modeWrite);

	CString strTemp, strValue;
	
//	CListBox * plistInfo = (CListBox*)(GetDlgItem(dlg.m_hWnd,IDC_LIST_INFO));
	
	
	//CALSSADlg *pView = (CALSSADlg*)pParam;
//	HWND ViewHWND = GetDlgItem(dlg.m_hWnd,IDC_LIST_INFO);
//	HWND ViewHWND = (HWND) pParam;
	strTemp = "开始计算";
//	::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
	plistInfo->AddString(strTemp);

	SYRouter ResultRouter( NowTemperature, NowExternalIterNumber, NowInnerIterNumber );

	strTemp = "生成初始路径:";
//	::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
	plistInfo->AddString(strTemp);
	strTemp = FormRouterString( ResultRouter );
//	::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
	plistInfo->AddString(strTemp);
	while(1)
	{
		strTemp = "新的内循环开始,当前温度为";
		strValue.Format("%10.4f",NowTemperature);
		strTemp += strValue;
//		::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
		plistInfo->AddString(strTemp);
		strTemp = "当前路径为:";
//		::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
		plistInfo->AddString(strTemp);
		strTemp = FormRouterString( ResultRouter );
//		::SendMessage( ViewHWND, WYWM_INFOVIEWAPPENDINFO, (WPARAM)(&strTemp), (LPARAM)0 );
		plistInfo->AddString(strTemp);

		double deltatotaldis = 0.0;
		while(1)
		{
			SYRouter SelRouter( ResultRouter.m_CityRouter, NowTemperature, NowExternalIterNumber, NowInnerIterNumber );

			deltatotaldis = SelRouter.m_fTotalDistance-ResultRouter.m_fTotalDistance;
			if( deltatotaldis <= 0.0 )
			{
				ResultRouter = SelRouter;
			}
			else
			{
				double chgprobability = exp( -(deltatotaldis/NowTemperature) );
				int randomnum = rand();
				double random = ((double)(randomnum%10000))/10000.0;
				if(chgprobability > random )
				{
					ResultRouter = SelRouter;
				}
			}

			if( JudgeOverInnerLoop(0) )
			{
				break;
			}
			else
				NowInnerIterNumber++;
		}

		if( JudgeOverExternalLoop(0) )
			break;
		else
		{
			NowTemperature = CountDownTemperature( NowTemperature, 0 );
			NowExternalIterNumber++;
			NowInnerIterNumber = 0;

			strValue.Format("%d %.4f\n",NowExternalIterNumber, ResultRouter.m_fTotalDistance );
			iterfile.Write( strValue, strValue.GetLength() );

		}
	}

⌨️ 快捷键说明

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