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

📄 cppview.txt

📁 蚁群算法是解决电力调配系统和商场供货系统问题的有效算法,这里提供了一种蚁群算法,供大家参考
💻 TXT
📖 第 1 页 / 共 2 页
字号:
		order[num].Add(sk);
	   } 
	else//search sk use Eq(1)
	{ 
		for(int m=0;m<citySet[num].GetSize() ;m++)  
		 {
			int r=0;
			int u = 0;
			int t=0;
			totalparam = 0.0;
		//确定点在City中的位置;以便更新信息素值;
		for(int d=0;d<m_CityNum;d++)
		{
	      if(rk == city.GetAt(d))
			{
			 r=d;
			}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) );
       
    		for(int i=0;i<citySet[num].GetSize();i++ )
			{
				for(int l=0;l<m_CityNum;l++)
				{
					if(citySet[num].GetAt(i) == city.GetAt(l))
						t = l;
				}
			 totalparam +=m_T[r][t]*pow(1/distance[i],m_BValue );
			}
		exploration[m]=m_T[r][u]*pow(1/distance[m],m_BValue )/totalparam;
		
	  }
	  double max = exploration[0];
	  //for (int n=0;n<m_CityNum-num-1 ;n++)
	for(int n=0;n<citySet[num].GetSize()-1;n++)
	  {
	   if(max<exploration[n])
	   {
		   max=exploration[n];
	       nextcity = n;
		   pDoc->nextcity =nextcity;
	   }
	  else nextcity=0;//用nextcity记录最佳位置;
	  }
    // sk = city.GetAt(nextcity);
	 sk = citySet[num].GetAt(nextcity);
	  for(int ab=0;ab<citySet[num].GetSize();ab++)
	   {
	    if(sk == citySet[num].GetAt(ab))
			citySet[num].RemoveAt(ab);
	   }	
		  order[num].Add(sk);
	 
	}
	//更新信息素值并且将刚访问过的点作为下一步的初始点;
	m_T[firstcity][nextcity]=(1-m_PValue)*m_T[firstcity][nextcity]+m_PValue *m_T0Value;
    m_T[nextcity][firstcity] = m_T[firstcity][nextcity];
	pDoc->m_T[firstcity][nextcity] = m_T[firstcity][nextcity];
	pDoc->m_T[nextcity][firstcity] = m_T[firstcity][nextcity];
	rk=sk ;
	}
	  else//all ants go back to initial city rk1
	  {
	   sk = rk1;
	   order[num].RemoveAll();
	   order[num].Add(sk);
	   rk = sk;
	  }
	 }
	} 
}

//下面的函数计算每只蚂蚁走过的路径的长度;
void CACSView::ComputeTourLength()
{
	CACSDoc* pDoc = GetDocument();
	for(int number=0;number<m_AntNum;number++)
		{
		    tourbyant[number]=0.0;
			for(int position=0;position<order[number].GetSize();position++)
			{
			 tour[position].x = order[number].GetAt(position).x;
			 tour[position].y = order[number].GetAt(position).y;
			
			}
			for(int kkk=0;kkk<m_CityNum-1;kkk++)
			{
			qqq = kkk +1;
            tourbyant[number] +=
				sqrt(pow((tour[kkk].x-tour[qqq].x),2)
				+pow((tour[kkk].y-tour[qqq].y),2));
			}
			antTour[number]= tourbyant[number];
	    	
	}
}

//该函数计算平均的路径长度;
void CACSView::CalculateAvergeLengtn()
{
	CACSDoc* pDoc = GetDocument();
	totalLength = 0;
  for(int i=0;i<m_AntNum;i++)
  {
   totalLength +=antTour[i];
  //CString str;
  // str.Format("%8.3f",antTour[i]);
  //MessageBox(str);
  }
  
  avergeLength = totalLength/m_AntNum;
  pDoc->avergeLength = avergeLength;
}

//下面的函数计算最优路径的长度;
void CACSView::GetBestTour()
{
	CACSDoc* pDoc = GetDocument();
	double bestTourLength=antTour[0];
	bestTourPos = 0; 
	for (int pos=0; pos<m_AntNum ; pos++)
	 {
		 
		 if(bestTourLength > antTour[pos])
		 {
		  bestTourLength = antTour[pos];		  
          bestTourPos = pos;
		 }
		pDoc->besttour =bestTourLength;
	 }
	//将最优路径存入文档类变量中;
	for(int bestpos=0;bestpos<order[bestTourPos].GetSize();bestpos++)
	{
	 pDoc->order.Add(order[bestTourPos].GetAt(bestpos));
	}
}

void CACSView::UpdateAllTour()
{
	CACSDoc* pDoc = GetDocument();
	//用全局更新规则更新信息素值;
	for(int cou=0;cou<order[bestTourPos].GetSize();cou++)
	{
		freshcity[cou] = order[bestTourPos].GetAt(cou);
	}
    int a = -1;
	int first = 0;
	int second = 0;
	//确定最优路径中各点在原城市序列中的位置;记录于first,second中;
   while (a < city.GetSize()-1)
   {
   	   a++;
	   for(int b=0;b<city.GetSize()-1;b++ )
	   {
	    if (freshcity[a] == city.GetAt(b))
			first = b;
	   }
	   next = a+1;
	   for(int c=0;c<city.GetSize()-1;c++)
	   {
	    if (freshcity[next] == city.GetAt(c))
			second = c;
	   }
	m_T[first][second]=(1-m_AValue) *m_T[first][second]+m_AValue*(1/bestTourLength);
   	m_T[second][first] = m_T[first][second];
	pDoc->m_T[first][second] = m_T[first][second];
	pDoc->m_T[second][first] = m_T[first][second];

   }
}

//这是关于系统使用说明的函数;
void CACSView::OnAcsHelp() 
{
	// TODO: Add your command handler code here
    //CAcsHelpDlg helpdlg;
	//helpdlg.DoModal();
	CAcsHelpDlg* helpdlg= new CAcsHelpDlg ;
	helpdlg->Create(IDD_DIALOG_HELP,NULL);
	helpdlg->ShowWindow(SW_SHOW);
	
}

//该函数选择工件排序问题的工件与机器的关系;
void CACSView::OnMenusortSelect() 
{
	 CACSDoc* pDoc = GetDocument();
	// TODO: Add your command handler code here
	CAsbaSortDlg dlg;
	if(dlg.DoModal() == IDOK)
	{
	   pDoc->m_question = dlg.m_question; 
	   if(pDoc->m_question == 0)
	   {
		pDoc->m_ComponentNum = 7;
		pDoc->m_MachineNum = 3; 
		static	double	shuzu1[7][3] = { {6,6,7},{6,7,6},
						{4,4,4},{5,4,4},
						{5,5,4},{3,3,5},
						{2,3,4} };
		for(int i=0;i<7;i++)
			{
			for(int j=0;j<3;j++)
				pDoc->relation[i][j] = shuzu1[i][j];
			}
    	}
	   else if(pDoc->m_question == 1)
	   {
		pDoc->m_ComponentNum = 40;
		pDoc->m_MachineNum = 10; 
		static double shuzu2[40][10] = {
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},	
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},					{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0},
			{8.0,6.3,7.5,5.9,7.0,5.6,6.5,5.5,6.5,6.0}
			};
		for(int i=0;i<40;i++)
			{
			for(int j=0;j<10;j++)
			pDoc->relation[i][j] =shuzu2[i][j];
			 }
	   }   
	}
	CString str;
	str.Format("%3.1f",pDoc->relation[1][1]);
	AfxMessageBox(str);
	
}

//该函数设置工件排序问题的缺省参数;
void CACSView::OnMenusortSetparam() 
{
	// TODO: Add your command handler code here
		CACSDoc* pDoc = GetDocument();
		CAsbaSortSetDefDlg dlg;
		dlg.m_aInitial = pDoc->m_aInitial ;
		dlg.m_bInitial = pDoc->m_bInitial ;
		dlg.m_q0Initial = pDoc->m_q0Initial ;
		dlg.m_xInitial = pDoc->m_xInitial ;
		dlg.m_t0Initial = pDoc->m_t0Initial ;
		if(dlg.DoModal() == IDOK)
		{
			pDoc->m_aInitial=dlg.m_aInitial ;
			pDoc->m_bInitial=dlg.m_bInitial ;
			pDoc->m_q0Initial=dlg.m_q0Initial ;
			pDoc->m_xInitial=dlg.m_xInitial ;
			pDoc->m_t0Initial=dlg.m_t0Initial  ;	
		}
}

//该函数运行ASBA算法对工件进行排序;

void CACSView::OnMenusortRun() 
{
	// TODO: Add your command handler code here
	GetInitialValue();
}

void CACSView::GetInitialValue()
{
	CACSDoc* pDoc = GetDocument();
		m_bInitial = pDoc->m_bInitial ;
		m_q0Initial = pDoc->m_q0Initial ;
		m_aInitial = pDoc->m_aInitial ;
		m_xInitial = pDoc->m_xInitial ;
		m_t0Initial = pDoc->m_t0Initial ;
		m_ComponentNum = pDoc->m_ComponentNum;
		m_MachineNum = pDoc->m_MachineNum ;
		for(int i=0;i<m_ComponentNum;i++)
		{
			for(int j=0;j<m_MachineNum;j++)
				relaiton[i][j] = pDoc->relation[i][j]; 
		}
		for(int m=0;m<m_ComponentNum;m++)
			{
			component.Add(m);
			}
		for(int com=0;com<m_MachineNum;com++)
			{
			for(int k=0;k<m_ComponentNum;k++)
				{
				componentSet[com].Add(k);
				}
			}
 //初始化信息素的值;
		for(int line=0;line<m_CityNum;line++)
		{
		for(int col=0;col<m_CityNum;col++)
		{
		 m_xinxisu[line][col]=pDoc->m_t0Initial;
		}
	}
}

//该函数对工件进行排序;工件相当于City;机器相当于蚂蚁
/*
void CACSView::BeginAsbaSort()
{
	CACSDoc* pDoc = GetDocument();
	for(int loop=0;loop<m_ComponentNum;loop++)
	{
	for(componentnumber=0;componentnumber<m_ComponentNum;componentnumber++)
	{
	 srand( 800 );
	 randnumber = rand()%m_MachineNum;
	 iniMachine = randnumber;
	 machine[iniMachine].Add(componentnumber);
//	 componentSet[componentnumber].RemoveAt(componentnumber);
	 for(int k=0;k<m_MachineNum;k++)
	 {
		if(k<m_MachineNum)
		{
			srand(800);
			m_q = (rand()%10)/10.0;
			if(m_q < m_q0Initial)
			{
				for(int m=0;m<m_MachineNum;m++)
				{
					xuanze1[m] = 
						pow(m_xinxisu[componentnumber][m],m_aInitial)*pow(relation[componentnumber][m],m_bInitial);
				}
				double max = xuanze1[0];
				nextmachine= 0;
			for(int p=0;p<m_MachineNum;p++)
			{
			 if(max<xuanze1[p])
				{
				  max=xuanze1[p];
				  nextmachine = p;
				}
				else nextmachine = 0;
			}
			machine[nextmachine].Add(componentnumber);
		}
		else{
        for(int m=0;m<m_MachineNum;m++)
		{
			
		}
	 }

	}
	}
	}
*/



void CACSView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	if(nChar == VK_ESCAPE)//如果按下ESC键
	{
		//获取主框架窗口的指针;
		CMainFrame * pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
		//调用主窗口类的自定义函数,退出全屏显示
		pFrame->EndFullScreen(); 
	}
	CFormView::OnKeyDown(nChar, nRepCnt, nFlags);
}




void CACSView::OnWizard()
{
	// TODO: The property sheet attached to your project
	// via this function is not hooked up to any message
	// handler.  In order to actually use the property sheet,
	// you will need to associate this function with a control
	// in your project such as a menu item or tool bar button.

	CMyPropertySheet propSheet;

	propSheet.DoModal();

	// This is where you would retrieve information from the property
	// sheet if propSheet.DoModal() returned IDOK.  We aren't doing
	// anything for simplicity.
}

⌨️ 快捷键说明

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