avin 发表于 2008-9-2 16:05

请问谁有粒子群算法在TSP问题上的应用源码

请问谁有粒子群算法在TSP问题上的应用源码,最好有详细的注释,因为我初学呢!我在做粒子群算法优化路由方面的研究,有兴趣的朋友一起交流啊!我的QQ42057388,邮箱:caojun241@sina.com

风花雪月 发表于 2008-9-5 09:19

下面是一个C语言写的例子

#include <stdlib.h>
#include "..\\Utils.h"
#include "..\\TSPDataSet.h"
#include "PSO.h"
static void PSO_MendCycle( TParticle *Particle )
{
    int tLooper1, tLooper2;
int HasFound;
tLooper1 = 1;
while( tLooper1 < Particle->Dimension )
{
//查找是否有重复节点
HasFound = 0;
for( tLooper2=0; tLooper2<tLooper1; tLooper2++ )
{
   if( Particle->X[ tLooper2 ] == Particle->X[ tLooper1 ] )
   {
    HasFound = 1;
    break;
   }
}
if( HasFound == 1 )
{
   Particle->X[ tLooper1 ] = ( Particle->X[ tLooper1 ] + 1 ) % Particle->Dimension;
}
else
{
   tLooper1++;
}
}
}
//适合度计算方法,必须定义
static double PSO_GetFit( CTSPDataSet *dat, TParticle *Particle )
{
int tResult = 0;
tResult = dat->GetCycleLength( Particle->Dimension, Particle->X );
return (double)tResult;
}
//计算群体各个微粒适合度
static void PSO_CalFit( CTSPDataSet *dat, TPSO *PSO )
{
if(!PSO)
{
return;
}
for(int i=0; i<PSO->PNumber; i++)
{
PSO->Particle.Fitness = PSO_GetFit( dat, &(PSO->Particle));
}
}
//初始化数据
static void PSO_InitializeData( CTSPDataSet *dat, TPSO *PSO, int PNumber, int PDimension )
{
if(!PSO)
{
return;
}
static int kk=(unsigned)time(NULL);
srand((unsigned)time(NULL)+kk++);
PSO->GBestIndex = 0;
for(int i=0; i<PNumber; i++)
{
for(int j=0; j<PSO->Particle.Dimension; j++)
{
   PSO->Particle.X = (int)(rand()/(double)RAND_MAX*(PSO->Xup-PSO->Xdown)+PSO->Xdown);//初始化坐标
   PSO->Particle.Velocity = rand()/(double)RAND_MAX*PSO->Vmax-PSO->Vmax/2;//初始化速度
}
PSO_MendCycle(&(PSO->Particle));
for(j=0; j<PSO->Particle.Dimension; j++)
{
   PSO->Particle.XBest = PSO->Particle.X;
}
PSO->Particle.Fitness = PSO_GetFit( dat, &(PSO->Particle)); //计算该微粒适合度
PSO->Particle.FitnessBest = PSO->Particle.Fitness; //设最优适合度初值
if(PSO->Particle.Fitness<PSO->Particle.Fitness)
{
   PSO->GBestIndex = i;//查找群体最优微粒
}
}
}
//初始化数据结构
static PSO_InitializeDataStruce( TPSO *PSO, int PNumber, int PDimension )
{
int tLooper;
PSO->GBestIndex = -1;
PSO->PNumber = PNumber;
PSO->PDimension = PDimension;
PSO->Xup = CC_SAFE_MALLOC( PDimension, int );
PSO->Xdown = CC_SAFE_MALLOC( PDimension, int );
for( tLooper=0; tLooper<PDimension; tLooper++ )
{
   PSO->Xup[ tLooper ] = PDimension - 1;
PSO->Xdown[ tLooper ] = 0;
}
PSO->Vmax = CC_SAFE_MALLOC( PDimension, double );
for( tLooper=0; tLooper<PDimension; tLooper++ )
{
   PSO->Vmax[ tLooper ] = ( PSO->Xup[ tLooper ] - PSO->Xdown[ tLooper ] ) * VelocityCoefficent;
}
PSO->Particle = CC_SAFE_MALLOC( PNumber, TParticle );
for( tLooper=0; tLooper<PNumber; tLooper++ )
{
   PSO->Particle[ tLooper ].Dimension = PDimension;
PSO->Particle[ tLooper ].X = CC_SAFE_MALLOC( PDimension, int );
PSO->Particle[ tLooper ].Velocity = CC_SAFE_MALLOC( PDimension, double );
PSO->Particle[ tLooper ].XBest = CC_SAFE_MALLOC( PDimension, int );
PSO->Particle[ tLooper ].Fitness = -1;
PSO->Particle[ tLooper ].FitnessBest = -1;
}
}
static void PSO_ReleaseDataStructure( TPSO *PSO )
{
int tLooper;
for( tLooper=0; tLooper<PSO->PDimension; tLooper++ )
{
CC_FREE( PSO->Particle[ tLooper ].XBest, int );
CC_FREE( PSO->Particle[ tLooper ].Velocity, double );
CC_FREE( PSO->Particle[ tLooper ].X, int );
}
CC_FREE( PSO->Particle, TParticle );
CC_FREE( PSO->Vmax, double );
CC_FREE( PSO->Xdown, int );
CC_FREE( PSO->Xup, int );
}
//微粒飞翔,产生新一代微粒
static void PSO_ParticleFly( CTSPDataSet *dat, TPSO *PSO )
{
static double FitBak;
if(!PSO)
{
return;
}
static int tt=(unsigned)time(NULL);
srand((unsigned)time(NULL)+tt++);
//整个群体飞向新的位置
for(int i=0; i<PSO->PNumber; i++)
{
//处理速度
for(int j=0; j<PSO->Particle.Dimension; j++)
{
   double tTemp;
   tTemp = (PSO->Particle).Velocity;
   //tTemp = WEIGHT * tTemp;
   tTemp += rand()/(double)RAND_MAX*C1*(PSO->Particle.XBest-PSO->Particle.X);
   tTemp += rand()/(double)RAND_MAX*C2*(PSO->Particle.XBest-PSO->Particle.X);
   (PSO->Particle).Velocity = tTemp;
}
//检查速度最大值
for(j=0; j<PSO->Particle.Dimension; j++)
{
   if(PSO->Particle.Velocity>PSO->Vmax)
   {
    PSO->Particle.Velocity = PSO->Vmax;
   }
   if(PSO->Particle.Velocity<-PSO->Vmax)
   {
    PSO->Particle.Velocity = -PSO->Vmax;
   }
}
//粒子飞翔
for(j=0; j<PSO->Particle.Dimension; j++)
{
   //修改坐标
   PSO->Particle.X = (int)(PSO->Particle.Velocity + PSO->Particle.X) % PSO->Xup;
   //保护
   if(PSO->Particle.X>PSO->Xup)
   {
    PSO->Particle.X=PSO->Xup;
   }
   if(PSO->Particle.X<PSO->Xdown)
   {
    PSO->Particle.X=PSO->Xdown;
   }
}
//对粒子飞翔后产生的新的个体进行修复
PSO_MendCycle( &(PSO->Particle) );
}
//计算各微粒适合度
PSO_CalFit( dat, PSO );
for(i=0; i<PSO->PNumber; i++)
{
FitBak = PSO->Particle.Fitness;
}
//设置新的个体最好位置
for(i=0; i<PSO->PNumber; i++)
{
if(PSO->Particle.Fitness<=PSO->Particle.FitnessBest)
{
   PSO->Particle.FitnessBest = PSO->Particle.Fitness;
   for(int j=0; j<PSO->Particle.Dimension; j++)
   {
    PSO->Particle.XBest = PSO->Particle.X;
   }
}
}

//设置新的最优个体
PSO->GBestIndex = 0;
for(i=0; i<PSO->PNumber; i++)
{
if(PSO->Particle.FitnessBest<=PSO->Particle.FitnessBest && i!=PSO->GBestIndex)
{
   PSO->GBestIndex = i;
}
}
}
//返回最佳个体
static void PSO_GetBest( TPSO *PSO, int *elist )
{
for(int i=0; i<PSO->Particle.Dimension; i++)
{
elist = PSO->Particle.XBest;
elist = PSO->Particle.XBest[(i+1)%PSO->Particle.Dimension];
}
}
//主程序
void PSO_Main( CTSPDataSet *dat, int PSONumber, int ecount, int *elist )
{
TPSO *PSO;
int counter=0;
PSO = CC_SAFE_MALLOC( 1, TPSO );
PSO_InitializeDataStruce( PSO, 20, ecount );
    PSO_InitializeData( dat, PSO, 20, ecount );
do
{
PSO_ParticleFly( dat, PSO );
counter++;
} while ( counter<PSONumber );
PSO_GetBest( PSO, elist );
PSO_ReleaseDataStructure( PSO );
CC_FREE( PSO, TPSO );
}

来自于:http://blog.sina.com.cn/s/blog_48efb3e7010002sf.html

CrazyJBP 发表于 2015-12-25 15:03

挺好的
页: [1]
查看完整版本: 请问谁有粒子群算法在TSP问题上的应用源码