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

📄 tsp.cpp

📁 蚁群算法是解决电力调配系统和商场供货系统问题的有效算法,这里提供了一种蚁群算法,供大家参考
💻 CPP
字号:
// Tsp.cpp : implementation file


#include "stdafx.h"
#include "ACS.h"
#include "Tsp.h"
#include "math.h"

CTsp::CTsp()  
{
m_CityNum = 10;
 m_AntNum = 10;
 
 m_AValue =0.1; 
 m_T0Value = 1.0;
 m_BValue = 2.0;
 m_PValue = 0.1;
 m_Q0Value = 0.9;

 for(int i=0;i<m_CityNum;i++)
 {
  city.Add(CPoint(0,0));	
 }
 for(int j=0;j<m_CityNum;j++)
 {
  Tour.AddHead(CPoint(0,0));	
 }
 for(int k=0;k<m_CityNum;k++)
 {
  waitForVisit.AddHead(CPoint(0,0));	
 }	
}

CTsp::~CTsp()
{
 //donothing	
}

void CTsp::SetBValue(double bValue)
 {
 	m_BValue = bValue;
 }
void CTsp::SetQ0Value(double q0Value)
 {
        m_Q0Value = q0Value;
 }
void CTsp::SetAValue(double aValue)
  {
        m_AValue = aValue;	
  }
void CTsp::SetPValue(double pValue)
 {
        m_PValue = pValue;
 }
 void CTsp::SetT0Value(double t0Value)
 {
        m_T0Value = t0Value;
 }
 /*
 double CTsp::GetBValue ()
 {
	 return (m_BValue);
 }

 double CTsp::GetQ0Value()
 {
    return (m_Q0Value);
 }
 double CTsp::GetAValue ()
 {
     return (m_AValue);
 }
 double CTsp::GetPValue ()
 {
     return (m_PValue);
 }
 double CTsp::GetT0Value ()
 {
    return (m_T0Value);
 }
 */
 bool CTsp::LookForCity(CPoint s,CArray<CPoint,CPoint> city2)
  {
	 for(int i=0;i<city2.GetSize();i++)
	 {
      if ((s.x == city2.GetAt(i).x) && (s.y == city2.GetAt(i).y))
          return true;
      else 
          return false;   
	 }
	 return false;

  }

 int GetCityIndex(CPoint r,CArray<CPoint,CPoint> city3)
 {
  for(int pos=0;pos<city3.GetSize();pos++)
		{
		 if ((r.x == city3.GetAt(pos).x) && (r.y == city3.GetAt(pos).y))
		 return pos;
		}
   return 0;
 }
 double CalCulateDistance(CPoint r,CPoint s)
 {
	 double Citydistance=0;
     return Citydistance = sqrt(pow((r.x-s.x),2)+pow((r.y-s.y),2));
	  
 }

 double CTsp::FunAll(int m_CityNum,double m_TValue,double m_BValue,double Citydistance)
 {
	 double allprobility = 0;
   for(int i=0;i<m_CityNum;i++)
   {
    allprobility += m_TValue * pow(1/Citydistance,m_BValue);
   }
   return allprobility;
 }

 /* double CalculateProbability(CPoint s,CArray<CPoint,CPoint> city3,
	          double m_TValue,double m_BValue,double Citydistance)
  {
	  if (CTsp::LookForCity(s,city3))
   {
    probility = (m_TValue * pow(1/Citydistance,m_BValue))/FunAll(int m_CityNum,double m_TValue,double m_BValue,double Citydistance);
   }
   else probility = 0; 
  }
 */
 double CalculateProbability(int m_CityNum,CPoint s,CArray<CPoint,CPoint> city2,
	          double m_TValue,double m_BValue,double Citydistance)
 {
	 double probility =0;
	 double allprobility = 0;
    for(int i=0;i<m_CityNum;i++)
    {
    allprobility += m_TValue * pow(1/Citydistance,m_BValue);
	}
    for(int j=0;j<city2.GetSize();j++)
	 {
      if ((s.x == city2.GetAt(i).x) && (s.y == city2.GetAt(i).y))
        // probility = (m_TValue * pow(1/Citydistance,m_BValue))/CTsp.FunAll(int m_CityNum,double m_TValue,double m_BValue,double Citydistance); 
		probility = (m_TValue * pow(1/Citydistance,m_BValue))/allprobility; 
      else 
          probility = 0;   
	 }
	return probility;
 }

⌨️ 快捷键说明

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