#include "stdafx.h"
#include "iomanip.h"
#include "TSP.h"
#include "TSP_ACS.h"#include "TSP_node.h"
#include "TSP_Ant.h"
#include "TSP_data.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
TSP_ACS::TSP_ACS(): m_aTau(NULL), m_aDist(NULL), m_aVis(NULL),
  m_dTau0(-0.5), m_nBeta(5), m_dRho(0.1), m_dQ0(0.9), m_nM(10), m_nTMax(5100),m_nAlpha(1)  //"::"说明该符号之后的函数属于该符号之前的类
{
}TSP_ACS::~TSP_ACS()
{
if (m_aTau != NULL) // all done in group!
{
// destroy 2D matrices
int nDim = GetDim();
for (int i=0; i<nDim; i++)
{
delete[] m_aTau[i];
delete[] m_aDist[i];
delete[] m_aVis[i];
}
delete[] m_aTau;
delete[] m_aDist;
delete[] m_aVis;
} DestroyAnts();
}// This asssumes we cannot modify the world (i.e. world document is static)
void TSP_ACS::Initialize()
{
int nDim = GetDim(); if (m_aTau == NULL) // all done in group!
{
int i=0, j=0; // create 2D matrices
if (nDim != 0)
{
m_aTau = new double*[nDim];    //分配新的内存空间
m_aDist = new int*[nDim];
m_aVis = new double*[nDim];
} for (i=0; i<nDim; i++)
{
m_aTau[i] = new double[nDim];  
m_aDist[i] = new int[nDim];
m_aVis[i] = new double[nDim];
} // initialize distance & visibility arrays
for (i=0; i<nDim; i++)
{
for (j=0; j<nDim; j++)
{
// SetDistAt(i, j, ComputeDistance(GetNodeAt(i), GetNodeAt(j)));
SetDistAt(i, j, m_pTSPData->GetWeightAt(i, j));
SetVisAt(i, j, 1.0/GetDistAt(i, j));  //可见度
}
}
} if (m_aAnts.GetSize() != GetM())
CreateAnts(); // initialize the NNH tour & Tau0
if ((m_dTau0 < 0.0) && (nDim != 0))
{
m_dTau0 = 1.0/(nDim * ComputeNNTour());
}
}void TSP_ACS::CreateAnts()
{
if (GetDim() == 0) // no use for ants
return; DestroyAnts(); // in case recreating // ant initialization
m_aAnts.SetSize(GetM());
    //srand( (unsigned)time( NULL ) );
for (int i=0; i<GetM(); i++)
{
// chose a city to host the ant
int nRand = rand() % GetDim();
// create the ant
TSP_Ant* pAnt = new TSP_Ant(nRand);
m_aAnts.SetAt(i, pAnt);
}
}void TSP_ACS::DestroyAnts()
{
// destroy ants
int nAnts = m_aAnts.GetSize();
for (int i=0; i<nAnts; i++)
{
TSP_Ant* pAnt = m_aAnts.GetAt(i);
delete pAnt;
}
m_aAnts.RemoveAll();
}void TSP_ACS::ComputeTour()
{
// counters & constants
int i=0, j=0;
int nDim = GetDim(); // get size, quit if no nodes or not properly initialized if (m_aTau == NULL)
Initialize(); // in case not previously done // ant initialization if number of ants changed from last Initialize()
if (m_aAnts.GetSize() != GetM())
CreateAnts(); // data initialization (reset tau to tau0)
for (i=0; i<nDim; i++)
{
for (int j=0; j<nDim; j++)
{
SetTauAt(i, j, m_dTau0);
}
} // shortest tour so far
double nLPlus = 1000000.0; //TDO : should this also be done after each time iteration??? // randomize the ants
//srand( (unsigned)time( NULL ) );
for(i=0; i<GetM(); i++)
{
// chose a city to host the ant
int nRand = rand() % nDim;
m_aAnts.GetAt(i)->SetHome(nRand);
} // main loop
for(int t=0; t<GetTMax(); t++)          //t:运行次数

{
double nBestTourLen = nLPlus; // best this time
int nBestTourAnt = -1;
double nTourLen = -1.0; // build tour for each ant
for(i=0; i<GetM(); i++)
{

TSP_Ant* pAnt = m_aAnts.GetAt(i); // current ant nTourLen = pAnt->BuildTour(this); // build tour // in case we find an improve tour this time
if(nTourLen < nBestTourLen)
{
nBestTourLen = nTourLen;
nBestTourAnt = i;
//cout<<nBestTourLen<<"->"<<endl;
}
           
} // check if improved tour is found
if(nBestTourLen != nLPlus)
{
 double nLPlus = nBestTourLen; TSP_Ant* pAnt = m_aAnts.GetAt(nBestTourAnt);
CList<int, int>* plBestTour = pAnt->GetTour(); // make a copy of the best tour
m_lBestTour.RemoveAll();
POSITION pos = plBestTour->GetHeadPosition(); while(pos != NULL)
{
m_lBestTour.AddTail(plBestTour->GetNext(pos));
}
cout <<"当t="<<t<<"时"<<endl;
cout <<nLPlus<<"路径如下:"<<endl;
POSITION pss=m_lBestTour.GetHeadPosition();
for(int k=0;k<m_lBestTour.GetCount();k++)
{
int node=m_lBestTour.GetNext(pss);
cout<<node+1<<"->";
}
cout<<endl;
} // update edge pheromone trails
POSITION pos = m_lBestTour.GetHeadPosition();
int nodei = m_lBestTour.GetNext(pos);
int nodefirst = nodei, nodej;
while(pos != NULL)
{
nodej = m_lBestTour.GetNext(pos); // global pheremone update rule
SetTauAt(nodei, nodej, (1-GetRho())*GetTauAt(nodei, nodej) + GetQ0()*GetRho()/exp(nLPlus-nBestTourLen)); // symmetric case
SetTauAt(nodej, nodei, GetTauAt(nodei, nodej)); nodei = nodej;
}
// last to first;
SetTauAt(nodej, nodefirst, (1-GetRho())*GetTauAt(nodej, nodefirst) + GetQ0()*GetRho()/exp(nLPlus-nBestTourLen)); // symmetric case
SetTauAt(nodefirst, nodej, GetTauAt(nodej, nodefirst));
if(t%500==0)
cout<<t<<endl;
}}
这是源文件,其他头文件就先不贴出来了,我想把参数m_dRho变成分段函数,即在最初的迭代中让其为0.1,当迭代陷入停滞状态时,让其为0.6,使循环跳出停滞状态,这个该如何改代码?有劳大家了

解决方案 »

  1.   

    改动点其实只涉及VC,跟算法没关系,只是时间太紧,在下VC欠缺,所以想请各位指点一下
      

  2.   

    在你的代码中, m_dRho 只出现了一次,而且还只是初始化我想把参数m_dRho变成分段函数,即在最初的迭代中让其为0.1,当迭代陷入停滞状态时,让其为0.6,使循环跳出停滞状态,这个该如何改代码?有劳大家了你的  m_dRho 在哪使用了,怎么使用的,都没有贴出来,你需要改什么,应该把相应的代码贴出来