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

📄 c4.cpp

📁 粒子群算法求解TSP问题
💻 CPP
字号:
 // #include <stdafx.h>
  #include <math.h>
  #include <time.h>
  #include <iostream>
  #include <fstream>
  using namespace std;
  int c1=2; //加速因子
  int c2=2; //加速因子
  double w=1; //惯性权重
  double Wmax=1; //最大惯性权重
  double Wmin=0.6; //最小惯性权重
  int Kmax=110; //迭代次数
  int GdsCnt; //物资总数
  int const Dim=10; //粒子维数
  int const PNum=50; //粒子个数
  int GBIndex=0; //最优粒子索引
  double a=0.6; //适应度调整因子
  double b=0.5; //适应度调整因子
  int Xup[Dim]; //粒子位置上界数组
  int Xdown[Dim]=; //粒子位置下界数组
  int Value[Dim]; //初始急需度数组
  int Vmax[Dim]; //最大速度数组
  class PARTICLE; //申明粒子节点
  void Check(PARTICLE&,int); //约束函数
  void Input(ifstream&); //输入变量
  void Initial(); //初始化相关变量
  double GetFit(PARTICLE&); //计算适应度
  void CalculateFit(); //计算适应度
  void BirdsFly(); //粒子飞翔
  void Run(ofstream&,int=2000); //运行函数
  //微粒类
  class PARTICLE
  {
  public:
  int X[Dim]; //微粒的坐标数组
  int XBest[Dim]; //微粒的最好位置数组
  int V[Dim]; //粒子速度数组
  double Fit; //微粒适合度
  double FitBest; //微粒最好位置适合度
  };
  PARTICLE Parr[PNum]; //粒子数组
  int main() //主函数
  {
  ofstream outf("out.txt");
  ifstream inf("data.txt"); //关联输入文件
  inf>>GdsCnt; //输入物资总数
  Input(inf);
  Initial();
  Run(outf,100);
  system("pause");
  return 0;
  }
  void Check(PARTICLE& p,int count)//参数:p粒子对象,count物资数量
  {
  srand((unsigned)time(NULL));
  int sum=0;
  for (int i=0;i<Dim;i++)
  {
  if (p.X>Xup)
  {
  p.X=Xup;
  }
  else if (p.X<Xdown)
  {
  p.X=Xdown;
  }
  if (p.V>Vmax)
  {
  p.V=Vmax;
  }
  else if (p.V<0)
  {
  p.V=0;
  }
  sum+=p.X;
  }
  while (sum>count)
  {
  p.X[rand()%Dim]--;
  sum=0;
  for (int i=0;i<Dim;i++)
  {
  if (p.X>Xup)
  {
  p.X=Xup;
  }
  else if (p.X<Xdown)
  {
  p.X=Xdown;
  }
  if (p.V>Vmax)
  {
  p.V=Vmax;
  }
  else if (p.V<0)
  {
  p.V=0;
  }
  sum+=p.X;
  }
  }
  }
  void Input(ifstream& inf) //以inf为对象输入数据
  {
  for (int i=0;i<Dim;i++)
  {
  inf>>Xup;
  }
  for (int i=0;i<Dim;i++)
  {
  inf>>Value;
  }
  }
  void Initial() //初始化数据
  {
  GBIndex=0;
  srand((unsigned)time(NULL));//初始化随机函数发生器
  for (int i=0;i<Dim;i++)
  {
  Vmax=(int)((Xup-Xdown)*0.035);
  }
  for (int i=0;i {
  for (int j=0;j<Dim;j++)
  {
  Parr.X[j]=(int)(rand()/(double)RAND_MAX*(Xup[j]
  -Xdown[j])-Xdown[j]+0.5);
  Parr.XBest[j]=Parr.X[j];
  Parr.V[j]=(int)(rand()/(double)RAND_MAX*(Vmax[j] -Vmax[j]/2));
  }
  Parr.Fit=GetFit(Parr);
  Parr.FitBest=Parr.Fit;
  if (Parr.Fit>Parr[GBIndex].Fit)
  {
  GBIndex=i;
  }
  }
  }
  double GetFit(PARTICLE& p)//计算对象适应度
  {
  double sum=0;
  for (int i=0;i<Dim;i++)
  {
  for (int j=1;j<=p.X;j++)
  {
  sum+=(1-(j-1)*a/(Xup-b))*Value;
  }
  }
  return sum;
  }
  void CalculateFit()//计算数组内各粒子的适应度
  {
  for (int i=0;i {
  Parr.Fit=GetFit(Parr);
  }
  }
  void BirdsFly()//粒子飞行寻找最优解
  {
  srand((unsigned)time(NULL));
  static int k=10;
  w=Wmax-k*(Wmax-Wmin)/Kmax;
  k++;
  for (int i=0;i {
  for (int j=0;j<Dim;j++)
  {
  Parr.V[j]=(int)(w*Parr.V[j])
  +(int)(c1*rand()/(double)RAND_MAX*
  (Parr.XBest[j]-Parr.X[j])
  +c2*rand()/(double)RAND_MAX*
  (Parr[GBIndex].XBest[j]-Parr.X[j]));
  }
  Check(Parr,GdsCnt);
  for (int j=0;j<Dim;j++)
  {
  Parr.X[j]+=Parr.V[j];
  }
  Check(Parr,GdsCnt);
  }
  CalculateFit();
  for (int i=0;i {
  if (Parr.Fit>=Parr.FitBest)
  {
  Parr.FitBest=Parr.Fit;
  for (int j=0;j<Dim;j++)
  {
  Parr.XBest[j]=Parr.X[j];
  }
  }
  }
  GBIndex=0;
  for (int i=0;i {
  if (Parr.FitBest>Parr[GBIndex].FitBest&&i!=GBIndex)
  {
  GBIndex=i;
  }
  }
  }
  void Run(ofstream& outf,int num)//令粒子以规定次数num飞行
  {
  for (int i=0;i<num;i++)
  {
  BirdsFly();
  outf<<(i+1)<<ends< for (int j=0;j<Dim;j++)
  {
  outf< }
  outf<<endl;
  }
  cout<<"Done!"<<endl;
  }
  

⌨️ 快捷键说明

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