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

📄 cppview.txt

📁 蚁群算法是解决电力调配系统和商场供货系统问题的有效算法,这里提供了一种蚁群算法,供大家参考
💻 TXT
📖 第 1 页 / 共 2 页
字号:
// ACSView.cpp : implementation of the CACSView class
//

#include "stdafx.h"
#include "ACS.h"
#include "MainFrm.h"
//#include <math.h>
#include "Tsp.h"

#include "SetDefaultDlg.h"
#include "SetParamDlg.h"
//#include "InputXYDlg.h"
#include "ACSDoc.h"
#include "ACSView.h"
#include "AcsHelpDlg.h"
#include "AsbaSortDlg.h"
#include "AsbaSortSetDefDlg.h"
#include "MyPropertySheet.h"


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

/////////////////////////////////////////////////////////////////////////////
// CACSView

IMPLEMENT_DYNCREATE(CACSView, CFormView)

BEGIN_MESSAGE_MAP(CACSView, CFormView)
	//{{AFX_MSG_MAP(CACSView)
	ON_COMMAND(ID_MENU_SETDEFAULTPARAM, OnMenuSetdefaultparam)
	ON_COMMAND(ID_MENUISETINITIAL, OnMenuisetinitial)
	ON_COMMAND(ID_MENURUNPROGRAM, OnMenurunprogram)
	ON_COMMAND(ID_ACS_HELP, OnAcsHelp)
	ON_COMMAND(ID_MENUSORT_SELECT, OnMenusortSelect)
	ON_COMMAND(ID_MENUSORT_SETPARAM, OnMenusortSetparam)
	ON_COMMAND(ID_MENUSORT_RUN, OnMenusortRun)
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CACSView construction/destruction

CACSView::CACSView()
	: CFormView(CACSView::IDD)
{
	//{{AFX_DATA_INIT(CACSView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here


}

CACSView::~CACSView()
{
}

void CACSView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CACSView)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BOOL CACSView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CACSView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CACSView printing

BOOL CACSView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CACSView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CACSView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CACSView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CACSView diagnostics

#ifdef _DEBUG
void CACSView::AssertValid() const
{
	CFormView::AssertValid();
}

void CACSView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CACSDoc* CACSView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CACSDoc)));
	return (CACSDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CACSView message handlers

//这个函数输入TSP系统的各个参数:
void CACSView::OnMenuSetdefaultparam() 
{
	// TODO: Add your command handler code here
	CACSDoc* pDoc = GetDocument();
	CSetDefaultDlg dlg;
	dlg.m_BValue = pDoc->m_BValue;
	dlg.m_Q0Value = pDoc->m_Q0Value;
	dlg.m_AValue = pDoc->m_AValue;
	dlg.m_PValue = pDoc->m_PValue;
	dlg.m_T0Value = pDoc->m_T0Value;

    if(dlg.DoModal() == IDOK)
	{
	//取得输入的各个参数的值,赋给变量;
      pDoc->m_BValue = dlg.m_BValue ;
      pDoc->m_Q0Value = dlg.m_Q0Value ;
      pDoc->m_AValue = dlg.m_AValue ;
      pDoc->m_PValue = dlg.m_PValue ;
      pDoc->m_T0Value = dlg.m_T0Value ;
	}
}

//这个函数输入初始City数和Ant数以及City的位置信息;
void CACSView::OnMenuisetinitial() 
{
	CACSDoc* pDoc = GetDocument();
	CSetParamDlg dlg;
	if(dlg.DoModal() == IDOK)
	{
		if((dlg.m_CityNum <= 0) || (dlg.m_AntNum <= 0))
		{
			MessageBox("请输入一个大于0的city数和ant数!","您输入的数据有错误!",MB_OK);
		}
		else
		{
		pDoc->m_CityNum = dlg.m_CityNum;
	    pDoc->m_AntNum = dlg.m_AntNum ;	

		CInputXYDlg inputdlg;
		inputdlg.SetCityNum(pDoc->m_CityNum);
	
		if(inputdlg.DoModal() == IDOK)
		{
			//在此已要将数据移到VIEW中, 否则inputDlg将析构, 数据全没有了.
			for (int i=0;i<pDoc->m_CityNum ;i++)
			{
				pDoc->city.Add(inputdlg.GetCityPosition(i));
			}
				for (int j=0;j<pDoc->m_CityNum ;j++)
			{
				pDoc->citySet.Add(inputdlg.GetCityPosition(j));
			}
	
	//	EnableMenuItem(0,ID_MENURUNPROGRAM,MF_ENABLED);
		AfxMessageBox("您已经输入了数据,请选择运行系统!",MB_ICONEXCLAMATION|MB_OK,-1);
		}
		}
	}
}

//该函数运行TSP系统;
void CACSView::OnMenurunprogram() 
{
 // TODO: Add your command handler code here
	CACSDoc* pDoc = GetDocument();
 //get data from doc class
    AcceptValueFormDoc();  
 //begin the first cycle
    BeginTour();
 //in this phase compulate Lk.
    ComputeTourLength();
	CalculateAvergeLengtn();
 // follow to get the best tour position
   	GetBestTour();
 //update edges belong to Lbest use Eq(4)
   UpdateAllTour();
 //update the doc data.

   DrawToScreen();
/*
   CDC* pDC = GetDC();
   pDC->SetBkColor(RGB(192,192,192)); 
   pDC->SetTextColor(RGB(0,0,255));
   CString str;
   str = "蚂蚁群落系统运算结果如下:";
   pDC->TextOut(80,20,str);
   str = "(请选择:文件\\生成文本报表生成文本文件(*.txt))";
   pDC->TextOut(50,50,str); 
   str = "各City的初始位置:";
   pDC->TextOut(50,70,str);
   for(int j=0;j<pDoc->city.GetSize();j++)
   {
    CString str1;
	str1.Format("(%d,%d)",pDoc->city.GetAt(j));
	pDC->TextOut(50,90+20*j,str1); 
   }
   str = "最优路径的City顺序:";
   pDC->TextOut(200,70,str); 
   for(int i=0;i<pDoc->order.GetSize();i++)
   {
	   CString str;
	   str.Format("(%d,%d)",pDoc->order.GetAt(i)); 
	   pDC->TextOut(200,90+20*i,str); 
   }
  */ 
}

void CACSView::DrawToScreen()
{
	CACSDoc* pDoc = GetDocument();
	CDC* pDC = GetDC();
	CFont NewFont;
	NewFont.CreateFont(10,8,0,0,FW_DONTCARE,FALSE,FALSE,FALSE,
		DEFAULT_CHARSET,OUT_CHARACTER_PRECIS,CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
		"Times New Roman ");
	CFont *pOldFont;
	pOldFont = pDC->SelectObject(&NewFont);
   
   pDC->SetBkColor(RGB(192,192,192)); 
   pDC->SetTextColor(RGB(0,0,255));
   CString str;
   str = "蚂蚁群落系统运算结果如下:";
   pDC->TextOut(80,20,str);
   str = "(请选择:文件\\生成文本报表生成文本文件(*.txt))";
   pDC->TextOut(50,50,str); 
   str = "各City的初始位置:";
   pDC->TextOut(50,70,str);
   for(int j=0;j<pDoc->city.GetSize();j++)
   {
    CString str1;
	str1.Format("(%d,%d)",pDoc->city.GetAt(j));
	pDC->TextOut(50,90+20*j,str1); 
   }
   str = "最优路径的City顺序:";
   pDC->TextOut(200,70,str); 
   for(int i=0;i<pDoc->order.GetSize();i++)
   {
	   CString str;
	   str.Format("(%d,%d)",pDoc->order.GetAt(i)); 
	   pDC->TextOut(200,90+20*i,str); 
   }
    pDC->SelectObject(pOldFont);
}

void CACSView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
  CACSDoc* pDoc = GetDocument();
  //pDC->TextOut(70,50,"感谢您使用蚂蚁群落系统!");
  DrawToScreen();
 
}


void CACSView::AcceptValueFormDoc()
{
	CACSDoc* pDoc = GetDocument();
	//从DOC类中取得各个参数的初始值;
    m_CityNum = pDoc->m_CityNum ;
	m_AntNum = pDoc->m_AntNum ;
	m_BValue = pDoc->m_BValue ;
	m_AValue = pDoc->m_AValue ;
	m_Q0Value = pDoc->m_Q0Value ;
	m_PValue = pDoc->m_PValue ;
	m_T0Value = pDoc->m_T0Value;
	for (int i=0;i<m_CityNum;i++)
	{
	 city.Add(pDoc->city.GetAt(i));
	}
	//citySet[num]保存未被访问过的城市的集合;
	for(num=0;num<m_AntNum;num++)
	{
		for(int j=0;j<m_CityNum;j++)
		{
	      citySet[num].Add(pDoc->city.GetAt(j));
		 }
	}
	//用m_T0Value值初始化信息素值m_T[][];
    for(int line=0;line<m_CityNum;line++)
	{
		for(int col=0;col<m_CityNum;col++)
		{
		 m_T[line][col]=pDoc->m_T0Value;
		}
	}
}

void CACSView::BeginTour()
{
   CACSDoc* pDoc = GetDocument();
   for( num=0;num < m_AntNum;num++)
	{
	   //每只蚂蚁随机选取一个点作为初始位置;
	 srand( 800 );
	 index = rand()%m_CityNum ;//index介于0~m_CityNum-1之间;
     rk1 = city.GetAt(index);
	 citySet[num].RemoveAt(index);
	 order[num].Add(rk1 );
	 rk = rk1;

	 for(int k=1;k<m_CityNum;k++)
	 {
		if(k<m_CityNum)
		{
		 srand( 800 );
		 q=(rand()%10)/10.0;
		 //根据q值选择 Eq(3)或者Eq(1);
			if(q <= m_Q0Value)//use Eq(3)
			{
			 for(int m=0;m<citySet[num].GetSize();m++)
				{
				 int r = 0;
				 int u = 0;
			//确定点在CITY中的位置,以便更新信息素值;
			for(int c=0;c<m_CityNum;c++)
			{
				if(rk == city.GetAt(c))
				{
				r=c;
				}else 
				r=0;
			}
			firstcity = r;
			for(int nextdot=0;nextdot<m_CityNum;nextdot++)
			{
			 if(citySet[num].GetAt(m) == city.GetAt(nextdot))
			 {
				u = nextdot;
			 }
			 else u=0;
			}
 //计算该点到将要访问的citySet[num]中各点的距离;
			distance[m]= sqrt(pow((rk.x
							-citySet[num].GetAt(m).x),2)
							+ pow((rk.y
							-citySet[num].GetAt(m).y),2) );
			exploitation[m]=m_T[r][u]*pow(1/distance[m],m_BValue );
			}
		double max = exploitation[0];
		nextcity = 0;
	//	for (int p=1;p<m_CityNum - num - 1 ;p++)
		for(int p=1;p<citySet[num].GetSize()-1;p++)
		{
            if(max<exploitation[p])
			{
		     max=exploitation[p];
	         nextcity = p;
		     pDoc->nextcity =nextcity;
			}
			else nextcity = 0;
		}
		//sk = city.GetAt(nextcity);
		sk = citySet[num].GetAt(nextcity);
		//将刚访问的点从citySet[num]中删除,并作为下一步的初始位置;
        for(int ab=0;ab<citySet[num].GetSize();ab++)
		{
			 if(sk == citySet[num].GetAt(ab))
				citySet[num].RemoveAt(ab);
		}	
		//order[num].AddHead(sk); 

⌨️ 快捷键说明

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