【PSO】PSO算法测试函数集CEC2013的demo加注释解析

下面是下载的demo源码(运行于Linux环境下),作者加上自己的注释方便理解。

#include "Header.h"
#include <sys/time.h>
#include <cstdio>
#include <unistd.h>

int main(){
  /*  Test the basic benchmark function */
  double* X;                                 # 定义粒子指针
  Benchmarks* fp=NULL;                       # 定义函数对象指针(不确定)
  unsigned dim = 1000;                       # 粒子维度,表示每个粒子所包含的参数个数
  unsigned funToRun[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};     # 要运行的函数编号
  // unsigned funToRun[] = {1};
  // unsigned funToRun[] = {15};
  unsigned funNum = 15;                      # 要运行的函数个数
  unsigned run = 1;                          # 运行次数(不确定)

  vector<double> runTimeVec;                 # 时间相关,windows下可删除
  struct timeval start, end;
  long seconds, useconds;    
  double mtime;
        
  X = new double[dim];                       # 定义粒子并初始化
  for (unsigned i=0; i<dim; i++){ 
    X[i]=0;
  }

  for (unsigned i=0; i<funNum; i++){         # 遍历函数并生成函数对象,打印编号和计算值;嵌套for循环
    fp = generateFuncObj(funToRun[i]); 
    printf("F %d value = %1.20E\n", fp->getID(), fp->compute(X));
    gettimeofday(&start, NULL);
    for (unsigned j=0; j < run; j++){
      fp->compute(X);
    }
    gettimeofday(&end, NULL);
    
    seconds  = end.tv_sec  - start.tv_sec;
    useconds = end.tv_usec - start.tv_usec;

    mtime = (((seconds) * 1000 + useconds/1000.0) + 0.5)/1000;

    runTimeVec.push_back(mtime);
    printf ( "F %d, Running Time = %f s\n\n", fp->getID(), mtime);
    
    delete fp;
  }

  delete []X;

  // for (unsigned i=0; i<runTimeVec.size(); i++){
  // 	printf ( "%f\n", runTimeVec[i] );
  // }

  return 0;
}

// create new object of class with default setting
Benchmarks* generateFuncObj(int funcID){                   # 根据输入函数ID创建类对象
  Benchmarks *fp;
  // run each of specified function in "configure.ini"
  if (funcID==1){
    fp = new F1();
  }else if (funcID==2){
    fp = new F2();
  }else if (funcID==3){
    fp = new F3();
  }else if (funcID==4){
    fp = new F4();
  }else if (funcID==5){
    fp = new F5();
  }else if (funcID==6){
    fp = new F6();
  }else if (funcID==7){
    fp = new F7();
  }else if (funcID==8){
    fp = new F8();
  }else if (funcID==9){
    fp = new F9();
  }else if (funcID==10){
    fp = new F10();
  }else if (funcID==11){
    fp = new F11();
  }else if (funcID==12){
    fp = new F12();
  }else if (funcID==13){
    fp = new F13();
  }else if (funcID==14){
    fp = new F14();
  }else if (funcID==15){
    fp = new F15();
  }else{
    cerr<<"Fail to locate Specified Function Index"<<endl;
    exit(-1);
  }
  return fp;
}


发布了392 篇原创文章 · 获赞 487 · 访问量 239万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览