GA-MicroSimulation 作业区优化算法

To minimize the negative effects on traffic efficiency and safety caused by work zone in urban road, land transportation authorities should choose proper work zone strategies, which include starting time and team strategy. However authorities always ignore the traffic control strategy, this paper applies two variable speed limit strategy to promote traffic efficiency and safety in urban road work zone. Considering the team work capacity, time window and traffic safety, this study proposes a total network delay minimization model to determine the optimal work zone strategy. To solve this proposed model, the genetic algorithm and Quaestone Paramics simulation are combined in this paper. Finally, a numerical example created from the Beixiaguan district in Beijing, china is used to assess the model and solution method proposed in this study. And also a sensitivities analysis is conducted to assess other factors’ impact.

   1 #include <stdlib.h>
   2 #include <stdio.h>
   3 #include <string.h>
   4 #include <math.h>
   5 #include <time.h>
   6 #include "programmer.h"
   7
   8 /*-----------------------------------------------------------------------------------------------
   9 * Data : 2014-12-11
  10 * Developer: Karel Zuo
  11 * Project: GA-Simulation算法的实现 —— The Hybrid GA-Simulation Methodology
  12 * e-mail: [email protected]
  13 *
  14 *-------------------------------------------------------------------------------------------------*/
  15
  16 #define SUM 4            //总共的染色体数量
  17 #define sumClosure 6     //需要施工的路段数量
  18 #define sumTeam 3        //总共可用的施工队数量
  19 #define hours 24         //一天时间表示为常数——24小时
  20 #define maxLanes 4       //所能关闭的最多车道数
  21 #define relatedLinks 3   //VSL控制中的所控制路段数量
  22 #define minLinks 2        //每个队至少负责的路段数
  23 #define maxLinks 2        //每个队至少负责的路段数
  24 #define VAR 500          //安全性标准
  25 #define HOUR 1800
  26
  27 /*------------------------------------------------------------------------------------------------
  28 * 程序中的全局变量
  29 --------------------------------------------------------------------------------------------------*/
  30 static int curTime;      //当前仿真时间(单位:秒)
  31 static int simuTimes;    //当前仿真次数(单位:秒)
  32 static int duration;     //一个仿真period的时间长度(单位:秒)
  33 static int sumDelay;     //一次完整仿真内的路网总延误(单位:秒)
  34 static int nlinks;        //路网上的路段总数
  35 const int secHOUR = 1800;   //定义一个常数HOUR,表示每小时等于3600秒
  36 static int sumVHC = 0;
  37 static float speedSUM=0;
  38 static int countVHC=0;
  39 const char *chromosome1 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\chromosome1.txt";
  40 const char *chromosome2 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\chromosome2.txt";
  41 const char *chromosome3 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\chromosome3.txt";
  42 const char *chromosome4 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\chromosome4.txt";
  43 const char *bestChromosome = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\bestChromosome.txt";
  44 const char *command_VSL = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\command_VSL.txt";
  45 const char *delay = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\delay.txt";
  46 const char *genLNK1 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\genLNK1.txt";
  47 const char *genLNK2 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\genLNK2.txt";
  48 const char *genLNK3 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\genLNK3.txt";
  49 const char *genLNK4 = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\genLNK4.txt";
  50 const char *simulationTimes = "C:\\Users\\Public\\paramics\\data\\bjtu\\GA-Simulation data\\simulation times.txt";
  51
  52 const float factorDMD[24] = {0.1f,0.5f,1.0f,1.0f,1.4f,2.9f,2.0f,1.75f,1.4f,0.8f,0.8f,0.85f,
  53 0.95f,1.06f,1.1f,1.1f,1.1f,1.25f,0.45f,0.8f,0.8f,0.9f,0.6f,0.7f}; //各个小时的OD矩阵的折算系数
  54
  55 /*------------------------------------------------------------------------------------------------
  56 * 程序中的全部自定义函数
  57 *------------------------------------------------------------------------------------------------*/
  58 int randomMaker(int lower,int upper);
  59 void initiate_GA();
  60 void evaluation_GA(int simulation_times);
  61 void cross_GA();
  62 void mutation();
  63 void rankedChromosomes();
  64 void selection();
  65 bool randomMutation();
  66 void replace(struct chromosome chro,int team_position,int hour_position);
  67 struct chromosome* repairGen(chromosome *c,int team_position);
  68 void simulationControl(int simulationTimes,int current_time);
  69 void SaveSimulationData(int simuTimes);
  70 void ReadSimulationData();
  71 void setDMD(int currentTime);
  72 float getVariance(float list[]);
  73 void rankedVSLCommand();
  74 void saveBestChromosome();
  75
  76 /*------------------------------------------------------------------------------------------------
  77 * 定义一个结构体,用于记录每个施工队的工作的起、止时间(单位:小时),取值范围0-24
  78 *------------------------------------------------------------------------------------------------*/
  79 struct workTeam
  80 {
  81     int startTime;
  82     int overTime;
  83     int workLink[maxLinks];//所负责的路段的序号
  84     int countLink;//已分配到的负责的路段的总数
  85     int workTime;
  86     int scheduel[hours];
  87 };
  88 struct workTeam team[sumTeam];
  89
  90 /*------------------------------------------------------------------------------------------------
  91 * 定义一个结构体,用于记录所需要关闭维修的路段的索引序号、施工所需时间、关闭车道序号
  92 *------------------------------------------------------------------------------------------------*/
  93 struct closureLNK
  94 {
  95     int index;
  96     int workTime;//表示施工时间长度。
  97     int closureLane[maxLanes];//所需关闭的车道的序号,0表示没有需要关闭的车道
  98     float var_Speed;
  99     bool flag;//表示该路段是否处于施工时间,是返回true,否则返回false.
 100     float speedSUM[3];
 101     int countVHC[3];
 102 };
 103 struct closureLNK closure_link[sumClosure];
 104
 105 /*-----------------------------------------------------------------------------------------------
 106 * 定义一个结构体,表示GA算法的染色体,即一种施工方案,包括了施工时间和每个方案的延误
 107 *------------------------------------------------------------------------------------------------*/
 108 struct chromosome
 109 {
 110     int Scheduel[sumTeam][hours];     //表示施工时间和施工路段的矩阵
 111     int genLNK[sumTeam][maxLinks];    //表示每个路段的基因值
 112     float delay;
 113     float avaragVAR_speed;
 114     float comprehensiveValue;
 115     int commandVSL[sumClosure];//该数组用于记录该方案中各个施工路段的VSL控制策略类型
 116 };
 117 //定义一个含有4个染色体的结构体组,染色体交叉时的指针,和变异时的指针
 118 struct chromosome chromosome_group[SUM],*chro_cross,*chro_mutation;
 119
 120 /*------------------------------------------------------------------------------------------------
 121 * 该函数在程序加载到路网时初始化各项参数.在每一次仿真开始时,需要读取上一次仿真数据,并且
 122 * 当第一次仿真时还需要初始化产生第一代染色体。
 123 *-------------------------------------------------------------------------------------------------*/
 124 void qpx_NET_postOpen(void)
 125 {
 126     qps_GUI_printf("Peform Start! Assigned value for parameters!");
 127     curTime = 0;
 128     simuTimes = 0;
 129     sumDelay = 0.0f;
 130     sumVHC = 0;
 131     duration = qpg_CFG_duration();
 132     nlinks = qpg_NET_links();
 133
 134     srand( (unsigned)time( NULL ));
 135
 136     //初始化,设置每个施工队的工作起止时间
 137     qps_GUI_printf("Assign value for every team‘s ‘start time‘ and ‘over time‘");
 138     team[0].startTime = 6;
 139     team[0].overTime = 20;
 140     team[0].workTime = 15;
 141     team[0].countLink = 0;
 142
 143     team[1].startTime = 6;
 144     team[1].overTime = 20;
 145     team[1].workTime = 15;
 146     team[1].countLink = 0;
 147
 148     team[2].startTime = 6;
 149     team[2].overTime = 20;
 150     team[2].workTime = 15;
 151     team[2].countLink = 0;
 152
 153     //初始化,设置所要关闭维修的路段
 154     qps_GUI_printf("Assign value for every link which will be closed");
 155
 156     int link0 = qpg_LNK_index(qpg_NET_link("27:28"));
 157     int link1 = qpg_LNK_index(qpg_NET_link("67:52"));
 158     int link2 = qpg_LNK_index(qpg_NET_link("66:64"));
 159     int link3 = qpg_LNK_index(qpg_NET_link("51:60"));
 160     int link4 = qpg_LNK_index(qpg_NET_link("62:63"));
 161     int link5 = qpg_LNK_index(qpg_NET_link("58:59"));
 162     qps_GUI_printf("Link0: %d  ",link0);
 163     qps_GUI_printf("Link1: %d  ",link1);
 164     qps_GUI_printf("Link2: %d  ",link2);
 165     qps_GUI_printf("Link3: %d  ",link3);
 166     qps_GUI_printf("Link4: %d  ",link4);
 167     qps_GUI_printf("Link5: %d  ",link5);
 168
 169     closure_link[0].index = link0;
 170     closure_link[0].workTime = 2;
 171     closure_link[0].closureLane[0] = 1;
 172     closure_link[0].closureLane[1] = 2;
 173     closure_link[0].closureLane[2] = 0;
 174     closure_link[0].closureLane[3] = 0;
 175     closure_link[0].flag = PFALSE;
 176
 177     closure_link[1].index = link1;
 178     closure_link[1].workTime = 2;
 179     closure_link[1].closureLane[0] = 1;
 180     closure_link[1].closureLane[1] = 2;
 181     closure_link[1].closureLane[2] = 0;
 182     closure_link[1].closureLane[3] = 0;
 183     closure_link[1].flag = PFALSE;
 184
 185     closure_link[2].index = link2;
 186     closure_link[2].workTime = 2;
 187     closure_link[2].closureLane[0] = 1;
 188     closure_link[2].closureLane[1] = 0;
 189     closure_link[2].closureLane[2] = 0;
 190     closure_link[2].closureLane[3] = 0;
 191     closure_link[2].flag = PFALSE;
 192
 193     closure_link[3].index = link3;
 194     closure_link[3].workTime = 2;
 195     closure_link[3].closureLane[0] = 1;
 196     closure_link[3].closureLane[1] = 0;
 197     closure_link[3].closureLane[2] = 0;
 198     closure_link[3].closureLane[3] = 0;
 199     closure_link[3].flag = PFALSE;
 200
 201     closure_link[4].index = link4;
 202     closure_link[4].workTime = 2;
 203     closure_link[4].closureLane[0] = 0;
 204     closure_link[4].closureLane[1] = 2;
 205     closure_link[4].closureLane[2] = 0;
 206     closure_link[4].closureLane[3] = 0;
 207     closure_link[4].flag = PFALSE;
 208
 209     closure_link[5].index = link5;
 210     closure_link[5].workTime = 2;
 211     closure_link[5].closureLane[0] = 1;
 212     closure_link[5].closureLane[1] = 0;
 213     closure_link[5].closureLane[2] = 0;
 214     closure_link[5].closureLane[3] = 0;
 215     closure_link[5].flag = PFALSE;
 216
 217     ReadSimulationData();
 218
 219     int m;
 220     for(m=0;m<SUM;m++)
 221     {
 222         qps_GUI_printf("The chromosome:%d  ,delay:%f  ",m,chromosome_group[m].delay);
 223     }
 224     simuTimes++;
 225
 226     //第一次仿真开始时还需要初始化产生第一代染色体
 227     if(simuTimes == 1)
 228     {
 229         initiate_GA();
 230         SaveSimulationData(simuTimes);
 231     }
 232     //测试语句:
 233     qps_GUI_printf("Current Simulation Times:%d ",simuTimes);
 234     qps_GUI_printf("Current data of chromosome:");
 235     int t1;
 236     int t2;
 237     for(t1=0;t1<SUM;t1++)
 238     {
 239         qps_GUI_printf("Chromosome:%d ",(t1+1));
 240         for(t2=0;t2<sumTeam;t2++)
 241         {
 242             qps_GUI_printf("%d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d",
 243                 chromosome_group[t1].Scheduel[t2][0],
 244                 chromosome_group[t1].Scheduel[t2][1],
 245                 chromosome_group[t1].Scheduel[t2][2],
 246                 chromosome_group[t1].Scheduel[t2][3],
 247                 chromosome_group[t1].Scheduel[t2][4],
 248                 chromosome_group[t1].Scheduel[t2][5],
 249                 chromosome_group[t1].Scheduel[t2][6],
 250                 chromosome_group[t1].Scheduel[t2][7],
 251                 chromosome_group[t1].Scheduel[t2][8],
 252                 chromosome_group[t1].Scheduel[t2][9],
 253                 chromosome_group[t1].Scheduel[t2][10],
 254                 chromosome_group[t1].Scheduel[t2][11],
 255                 chromosome_group[t1].Scheduel[t2][12],
 256                 chromosome_group[t1].Scheduel[t2][13],
 257                 chromosome_group[t1].Scheduel[t2][14],
 258                 chromosome_group[t1].Scheduel[t2][15],
 259                 chromosome_group[t1].Scheduel[t2][16],
 260                 chromosome_group[t1].Scheduel[t2][17],
 261                 chromosome_group[t1].Scheduel[t2][18],
 262                 chromosome_group[t1].Scheduel[t2][19],
 263                 chromosome_group[t1].Scheduel[t2][20],
 264                 chromosome_group[t1].Scheduel[t2][21],
 265                 chromosome_group[t1].Scheduel[t2][22],
 266                 chromosome_group[t1].Scheduel[t2][23]);
 267         }
 268         qps_GUI_printf("------------------------------------------------------------------------------------------------------");
 269         qps_GUI_printf("genLINK is : %d   %d   %d   %d   %d   %d   ",
 270         chromosome_group[0].genLNK[0][0],
 271         chromosome_group[0].genLNK[0][1],
 272         chromosome_group[0].genLNK[1][0],
 273         chromosome_group[0].genLNK[1][1],
 274         chromosome_group[0].genLNK[2][0],
 275         chromosome_group[0].genLNK[2][1]
 276         );
 277     }
 278 }
 279
 280 /*------------------------------------------------------------------------------------------------
 281 * 该函数用于控制仿真过程及遗传算法的流程。仿真过程中,每一个小时开始时,需要根据所执行
 282 * 的施工计划关闭相应的路段和车道。每一次仿真结束时,需要将本次仿真的数据保存以便下一次仿真时使用
 283 *--------------------------------------------------------------------------------------------------*/
 284 void qpx_NET_minute(void)
 285 {
 286     curTime =  (int)qpg_CFG_simulationTime();
 287     //测试语句:
 288
 289     //qps_GUI_printf("The Current simulation time is:%d ",curTime);
 290
 291     //仿真过程中每一小时开始时根据所执行的施工计划(当前染色体),关闭或开启相关路段,并修改OD矩阵
 292     if((curTime+secHOUR)%secHOUR== 0 || curTime == 60)
 293     {
 294         if(curTime == 60)
 295             curTime = 0;
 296         qps_GUI_printf("**********************************************************************");
 297         qps_GUI_printf("Now is %d  oclock",(curTime+secHOUR)/secHOUR);
 298         qps_GUI_printf("**********************************************************************");
 299         simulationControl(simuTimes,curTime);
 300         setDMD(curTime);
 301     }
 302 }
 303
 304 /*-----------------------------------------------------------------------------------------------------
 305 * 该函数用于控制仿真过程及遗传算法的流程。每一次仿真结束时,需要将本次仿真的数据保存以便下一次仿真时使用
 306 *-----------------------------------------------------------------------------------------------------*/
 307 void qpx_NET_complete(void)
 308 {
 309     evaluation_GA(simuTimes);
 310
 311     int i;
 312     for(i=0;i<6;i++)
 313     {
 314         //交叉过程中,判断最佳方案的VSL是否能够满足安全性要求,如果不能,则产生新的VSL控制命令。
 315         if( closure_link[i].var_Speed > VAR)
 316         {
 317             srand( (unsigned)time( NULL ));
 318             int newCommand = (int)(randomMaker(0,89)/30);
 319             chromosome_group[(simuTimes+SUM-1)%SUM].commandVSL[i] = newCommand;
 320         }
 321     }
 322
 323     //如果染色体种群内的4个染色体都执行完一次,则需要依据延误值进行排序、选择、交叉、变异产生新一代染色体
 324     if((simuTimes+SUM)%SUM == 0)
 325     {
 326         //测试语句:
 327         qps_GUI_printf("Every chromosome in this group has been simulate");
 328
 329         rankedChromosomes();
 330         rankedVSLCommand();
 331         saveBestChromosome();
 332         selection();
 333         cross_GA();
 334         mutation();
 335         SaveSimulationData(simuTimes);
 336     }
 337     SaveSimulationData(simuTimes);
 338 }
 339
 340 /*-----------------------------------------------------------------------------------------------------
 341 * 该函数用于获取每次仿真过程的总延误值
 342 *------------------------------------------------------------------------------------------------------*/
 343 void qpx_VHC_arrive(VEHICLE* vehicle, LINK* link, ZONE* zone)
 344 {
 345     sumDelay += qpg_VHC_existTime(vehicle);
 346     sumVHC++;
 347 }
 348
 349 /*---------------------------------------------------------------------------------------------
 350 * 该函数部分用于每个时间步长内读取各个控制路段的速度、交通量,用于计算平均速度
 351 *---------------------------------------------------------------------------------------------*/
 352 void qpx_NET_timeStep(void)
 353 {
 354     int i;
 355     for(i=0;i<sumClosure;i++)
 356     {
 357         if(closure_link[i].flag == PTRUE)
 358         {
 359             LINK *thelink0 = qpg_NET_linkByIndex(closure_link[i].index);
 360             LINK *thelink1 = qpg_LNK_backside(thelink0);
 361             LINK *thelink2 = qpg_LNK_backside(thelink1);
 362             int j;
 363             for(j=1;j<qpg_LNK_lanes(thelink0);j++)
 364             {
 365                 closure_link[i].speedSUM[0] += qpg_STA_speed(thelink0,j)*qpg_STA_count(thelink0,j);
 366                 closure_link[i].countVHC[0] += qpg_STA_count(thelink0, j);
 367             }
 368             int k;
 369             for(k=0;k<qpg_LNK_lanes(thelink1);k++)
 370             {
 371                 closure_link[i].speedSUM[1] += qpg_STA_speed(thelink1,k)*qpg_STA_count(thelink1,k);
 372                 closure_link[i].countVHC[1] += qpg_STA_count(thelink1, k);
 373             }
 374             int h;
 375             for(h=0;h<qpg_LNK_lanes(thelink2);h++)
 376             {
 377                 closure_link[i].speedSUM[2] += qpg_STA_speed(thelink2,h)*qpg_STA_count(thelink2,h);
 378                 closure_link[i].countVHC[2] += qpg_STA_count(thelink2,h);
 379             }
 380         }
 381     }
 382 }
 383
 384 /*----------------------------------------------------------------------------------------------
 385 *该函数用于初始化染色体种群,在第一次仿真开始时执行,随机产生染色体
 386 *---------------------------------------------------------------------------------------------*/
 387 void initiate_GA()
 388 {
 389     //测试语句:
 390     qps_GUI_printf("initiate");
 391
 392     int i;
 393     for(i=0; i<SUM; i++)//初始化4个方案
 394     {
 395         team[0].countLink = 0;
 396         team[1].countLink = 0;
 397         team[2].countLink = 0;
 398         int re;
 399         for(re=0;re<sumTeam;re++)
 400         {
 401             team[re].countLink = 0;
 402             qps_GUI_printf("now The team[%d].countLink is:%d   ",re,team[re].countLink);
 403             team[re].workLink[0]=0;
 404             qps_GUI_printf("now The team[%d].workLink[%d] is:%d   ",re,0,team[re].workLink[0]);
 405             team[re].workLink[1]=0;
 406             qps_GUI_printf("now The team[%d].workLink[%d] is:%d   ",re,1,team[re].workLink[1]);
 407
 408             int ev;
 409             for(ev=0;ev<hours;ev++)
 410             {
 411                 team[re].scheduel[ev]=0;
 412                 qps_GUI_printf("now The team[%d].scheduel[%d] is:%d   ",re,1,team[re].scheduel[ev]);
 413             }
 414         }
 415
 416
 417         int j;
 418         for(j=0;j<sumClosure;j++)
 419         {
 420             //选择哪个施工队负责
 421             int randomTeam;
 422             randomTeam = randomMaker(0,10*sumTeam);
 423
 424             if(randomTeam <= 10)
 425                 randomTeam = 0;
 426             else if(randomTeam > 10 && randomTeam <= 20)
 427                 randomTeam = 1;
 428             else if(randomTeam > 20)
 429                 randomTeam = 2;
 430
 431             if(team[randomTeam].countLink == maxLinks)
 432             {
 433                 if(randomTeam == 0)
 434                 {
 435                     if(team[1].countLink == maxLinks || team[2].countLink == maxLinks)
 436                     {
 437                         if(team[1].countLink == maxLinks)
 438                             randomTeam = 2;
 439                         else
 440                             randomTeam = 1;
 441                     }
 442                     else
 443                     {
 444                         if(randomMaker(0,100) < 50)
 445                             randomTeam = 1;
 446                         else
 447                             randomTeam = 2;
 448                     }
 449                 }
 450                 else if(randomTeam == 1)
 451                 {
 452                     if(team[0].countLink == maxLinks ||team[2].countLink == maxLinks)
 453                     {
 454                         if(team[0].countLink == maxLinks)
 455                             randomTeam = 2;
 456                         else
 457                             randomTeam = 0;
 458                     }
 459                     else
 460                     {
 461                         if(randomMaker(0,100) < 50)
 462                             randomTeam = 0;
 463                         else
 464                             randomTeam = 2;
 465                     }
 466                 }
 467                 else if(randomTeam == 2)
 468                 {
 469                     if(team[0].countLink == maxLinks ||team[1].countLink == maxLinks )
 470                     {
 471                         if(team[0].countLink == maxLinks)
 472                             randomTeam = 1;
 473                         else
 474                             randomTeam = 0;
 475                     }
 476                     else
 477                     {
 478                         if(randomMaker(0,100) < 50)
 479                             randomTeam = 0;
 480                         else
 481                             randomTeam = 1;
 482                     }
 483                 }
 484             }
 485             team[randomTeam].countLink++;
 486
 487             //选择该路段的施工起始时间
 488
 489             int randomHour = 1;
 490
 491
 492             bool pass = PFALSE;
 493             int reTimes = 0;
 494             qps_GUI_printf("---------------------------------------------------------------------------------------------------");
 495             qps_GUI_printf("Now is Link:%d   is select Hour! It ‘s Team is :%d   ",j,randomTeam);
 496             do
 497             {
 498                 randomHour = randomMaker(team[randomTeam].startTime,team[randomTeam].overTime-2);
 499                 qps_GUI_printf("first select hour:%d   ",randomHour);
 500                 if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
 501                 {
 502                     qps_GUI_printf("That is OK!Hour is:%d   ",randomHour);
 503                     team[randomTeam].workLink[team[randomTeam].countLink-1] = j+1;
 504                     team[randomTeam].scheduel[randomHour-1] = j+1;
 505                     team[randomTeam].scheduel[randomHour+1-1] = j+1;
 506                     pass = PTRUE;
 507                 }
 508                 else
 509                 {
 510                     qps_GUI_printf("The hour has been used!!:%d   ",randomHour);
 511                     if((randomHour- team[randomTeam].startTime) >= (team[randomTeam].overTime-(randomTeam+1)))
 512                     {
 513                         qps_GUI_printf("The before > after!!");
 514                         randomHour =  randomMaker(team[randomTeam].startTime,(randomHour-2));
 515                         qps_GUI_printf("new Hour is :%d   ",randomHour);
 516                         if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
 517                         {
 518                             qps_GUI_printf("new Hour is OK :%d   ",randomHour);
 519                             team[randomTeam].workLink[team[randomTeam].countLink-1] = j+1;
 520                             team[randomTeam].scheduel[randomHour-1] = j+1;
 521                             team[randomTeam].scheduel[randomHour+1-1] = j+1;
 522                             pass = PTRUE;
 523                         }
 524                         else
 525                         {
 526                             qps_GUI_printf("new Hour is used :%d   ",randomHour);
 527                             randomHour =  randomMaker(team[randomTeam].startTime,team[randomTeam].startTime+2);
 528                             qps_GUI_printf("once new Hour is :%d   ",randomHour);
 529                             team[randomTeam].workLink[team[randomTeam].countLink-1] = j+1;
 530                             team[randomTeam].scheduel[randomHour-1] = j+1;
 531                             team[randomTeam].scheduel[randomHour+1-1] = j+1;
 532                             pass = PTRUE;
 533                         }
 534                     }
 535                     else
 536                     {
 537                         qps_GUI_printf("The before < after!!");
 538                         randomHour = randomMaker(randomHour+2,team[randomTeam].overTime-2);
 539                         qps_GUI_printf("new Hour is :%d   ",randomHour);
 540                         if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
 541                         {
 542                             qps_GUI_printf("new Hour is OK :%d   ",randomHour);
 543                             team[randomTeam].workLink[team[randomTeam].countLink-1] = j+1;
 544                             team[randomTeam].scheduel[randomHour-1] = j+1;
 545                             team[randomTeam].scheduel[randomHour+1-1] = j+1;
 546                             pass = PTRUE;
 547                         }
 548                         else
 549                         {
 550                             qps_GUI_printf("new Hour is used :%d   ",randomHour);
 551                             randomHour = team[randomTeam].overTime-2;
 552                             qps_GUI_printf("once new Hour is :%d   ",randomHour);
 553                             team[randomTeam].workLink[team[randomTeam].countLink-1] = j+1;
 554                             team[randomTeam].scheduel[randomHour-1] = j+1;
 555                             team[randomTeam].scheduel[randomHour+1-1] = j+1;
 556                             pass = PTRUE;
 557                         }
 558                     }
 559                 }
 560             }
 561             while(!pass);
 562
 563             int k;
 564             for(k = randomHour;k<=(randomHour+1);k++)
 565                 chromosome_group[i].Scheduel[randomTeam][k-1] = closure_link[j].index;
 566             chromosome_group[i].genLNK[randomTeam][team[randomTeam].countLink-1] = closure_link[j].index;
 567
 568             int VSL_random = randomMaker(0,31);
 569             if(VSL_random <=10)
 570             {
 571                 //不执行VSL
 572                 chromosome_group[i].commandVSL[j] = 0;
 573
 574             }
 575             else if(VSL_random >10 && VSL_random <=20)
 576             {
 577                 //执行第1种VSL控制策略。
 578                 chromosome_group[i].commandVSL[j] = 1;
 579             }
 580             else if(VSL_random >20 && VSL_random <=30)
 581             {
 582                 //执行第2种VSL控制策略。
 583                 chromosome_group[i].commandVSL[j] = 2;
 584             }
 585         }
 586     }
 587     qps_GUI_printf("genLINK is : %d   %d   %d   %d   %d   %d   ",
 588         chromosome_group[0].genLNK[0][0],
 589         chromosome_group[0].genLNK[0][1],
 590         chromosome_group[0].genLNK[1][0],
 591         chromosome_group[0].genLNK[1][1],
 592         chromosome_group[0].genLNK[2][0],
 593         chromosome_group[0].genLNK[2][1]
 594         );
 595 }
 596
 597
 598 /*----------------------------------------------------------------------------------------------
 599 * 对染色体做出适应度评价
 600 *-----------------------------------------------------------------------------------------------*/
 601 void evaluation_GA(int simulation_times)
 602 {
 603     //测试语句:
 604     qps_GUI_printf("evaluation_GA");
 605
 606     //评价延误
 607     int index_chro = ((simulation_times-1) + SUM) % SUM;
 608     chromosome_group[index_chro].delay = sumDelay;
 609     //测试语句:
 610     qps_GUI_printf("-----------------------------------------------------------------------------");
 611     qps_GUI_printf("-----------------------------------------------------------------------------");
 612     qps_GUI_printf("current chromosome`s delay is:%d  ",(int)chromosome_group[index_chro].delay);
 613     qps_GUI_printf("-----------------------------------------------------------------------------");
 614     //评价安全性(平均速度的方差)
 615     int i;
 616     for(i=0;i<sumClosure;i++)
 617     {
 618         float arra[relatedLinks ] = {(closure_link[i].speedSUM[0]/closure_link[i].countVHC[0]),
 619         (closure_link[i].speedSUM[1]/closure_link[i].countVHC[1]),
 620         (closure_link[i].speedSUM[2]/closure_link[i].countVHC[2])};
 621
 622         closure_link[i].var_Speed = getVariance(arra);
 623     }
 624     int closure = sumClosure;
 625     while(closure > 0)
 626     {
 627         closure--;
 628         chromosome_group[index_chro].avaragVAR_speed += closure_link[closure].var_Speed;
 629     }
 630     chromosome_group[index_chro].avaragVAR_speed = chromosome_group[index_chro].avaragVAR_speed / sumClosure;
 631
 632     chromosome_group[index_chro].comprehensiveValue = chromosome_group[index_chro].delay*0.5 + chromosome_group[index_chro].avaragVAR_speed*0.5;
 633
 634     //测试语句:
 635     qps_GUI_printf("-----------------------------------------------------------------------------");
 636     qps_GUI_printf("-----------------------------------------------------------------------------");
 637     qps_GUI_printf("current chromosome`s variance of speed is:%d  ",(int)chromosome_group[index_chro].avaragVAR_speed);
 638     qps_GUI_printf("-----------------------------------------------------------------------------");
 639
 640     //测试语句:
 641     qps_GUI_printf("-----------------------------------------------------------------------------");
 642     qps_GUI_printf("-----------------------------------------------------------------------------");
 643     qps_GUI_printf("current chromosome`s comprehensive value is:%d  ",(int)chromosome_group[index_chro].comprehensiveValue);
 644     qps_GUI_printf("-----------------------------------------------------------------------------");
 645 }
 646
 647 /*------------------------------------------------------------------------------------------------
 648 * 交叉
 649 *-----------------------------------------------------------------------------------------------*/
 650 void cross_GA()
 651 {
 652     //将指针指向当前染色体组,用于控制各个染色体间交叉
 653     chro_cross = chromosome_group;
 654
 655     int i;
 656     for(i=0; i<(int)(SUM/2); i++)
 657     {
 658         srand( (unsigned)time( NULL ));
 659         int crossGen = randomMaker(0,90);
 660
 661         if(crossGen<=30)
 662             crossGen = 0;
 663         else if(crossGen>30 && crossGen<=60)
 664             crossGen = 1;
 665         else if(crossGen > 60)
 666             crossGen = 2;
 667         qps_GUI_printf("The team of  %d  will be cross!",crossGen);
 668
 669         int indexParent1 = 2*i;
 670         int indexParent2 = 2*i+1;
 671         int m;
 672         for(m=0; m<hours;m++)
 673         {
 674             int mid = chromosome_group[indexParent1].Scheduel[crossGen][m];
 675             chromosome_group[indexParent1].Scheduel[crossGen][m] = chromosome_group[indexParent2].Scheduel[crossGen][m];
 676             chromosome_group[indexParent2].Scheduel[crossGen][m] = mid;
 677         }
 678         int n;
 679         for(n=0;n<maxLinks;n++)
 680         {
 681             int transition = chromosome_group[indexParent1].genLNK[crossGen][n];
 682             chromosome_group[indexParent1].genLNK[crossGen][n] = chromosome_group[indexParent2].genLNK[crossGen][n];
 683             chromosome_group[indexParent2].genLNK[crossGen][n] = transition;
 684         }
 685
 686         qps_GUI_printf("genLINK is : %d   %d   %d   %d   %d   %d   ",
 687         chromosome_group[0].genLNK[0][0],
 688         chromosome_group[0].genLNK[0][1],
 689         chromosome_group[0].genLNK[1][0],
 690         chromosome_group[0].genLNK[1][1],
 691         chromosome_group[0].genLNK[2][0],
 692         chromosome_group[0].genLNK[2][1]
 693         );
 694
 695         //染色体中的VSL控制类型发生交换
 696         qps_GUI_printf("VSL command:%d   %d   %d   %d   %d   %d   ",chromosome_group[indexParent1].commandVSL[0],
 697             chromosome_group[indexParent1].commandVSL[1],chromosome_group[indexParent1].commandVSL[2],
 698             chromosome_group[indexParent1].commandVSL[3],chromosome_group[indexParent1].commandVSL[4],
 699             chromosome_group[indexParent1].commandVSL[5]);
 700         qps_GUI_printf("VSL command:%d   %d   %d   %d   %d   %d   ",chromosome_group[indexParent2].commandVSL[0],
 701             chromosome_group[indexParent2].commandVSL[1],chromosome_group[indexParent2].commandVSL[2],
 702             chromosome_group[indexParent2].commandVSL[3],chromosome_group[indexParent2].commandVSL[4],
 703             chromosome_group[indexParent2].commandVSL[5]);
 704         int in,jn,kn,hn;
 705         for(in=0;in< minLinks;in++)
 706         {
 707             for(jn=0;jn<sumClosure;jn++)
 708             {
 709                 if(chromosome_group[indexParent1].genLNK[crossGen][in] == closure_link[jn].index)
 710                 {
 711                     qps_GUI_printf("cross VSL command LINK1 is:%d   ",closure_link[jn].index);
 712                     for(hn=0;hn<sumClosure;hn++)
 713                     {
 714                         if(chromosome_group[indexParent2].genLNK[crossGen][in] == closure_link[hn].index)
 715                         {
 716                             qps_GUI_printf("cross VSL command LINK1** is:%d   ",closure_link[hn].index);
 717                             int temp = chromosome_group[indexParent1].commandVSL[jn];
 718                             chromosome_group[indexParent1].commandVSL[jn] = chromosome_group[indexParent2].commandVSL[hn];
 719                             chromosome_group[indexParent2].commandVSL[hn] = temp;
 720                         }
 721                     }
 722                 }
 723             }
 724         }
 725         //交换之后的基因值
 726         qps_GUI_printf("Afrer cross VSL command:%d   %d   %d   %d   %d   %d   ",chromosome_group[indexParent1].commandVSL[0],
 727             chromosome_group[indexParent1].commandVSL[1],chromosome_group[indexParent1].commandVSL[2],
 728             chromosome_group[indexParent1].commandVSL[3],chromosome_group[indexParent1].commandVSL[4],
 729             chromosome_group[indexParent1].commandVSL[5]);
 730         qps_GUI_printf("After cross VSL command:%d   %d   %d   %d   %d   %d   ",chromosome_group[indexParent2].commandVSL[0],
 731             chromosome_group[indexParent2].commandVSL[1],chromosome_group[indexParent2].commandVSL[2],
 732             chromosome_group[indexParent2].commandVSL[3],chromosome_group[indexParent2].commandVSL[4],
 733             chromosome_group[indexParent2].commandVSL[5]);
 734
 735         //修复基因中不满足约束条件的部分
 736
 737         int equalValue =0;//所交换的部分的相同的基因值
 738         int equalCount = 0;//所交换的部分的相同的基因数量
 739
 740         //寻找到交换部分的相同基因
 741         int h,p;
 742         for(h=0;h<maxLinks;h++)
 743         {
 744             for(p=0;p<maxLinks;p++)
 745             {
 746                 if(chromosome_group[indexParent1].genLNK[crossGen][h] == chromosome_group[indexParent2].genLNK[crossGen][p])
 747                     equalCount++;
 748                 if(equalCount == 1)
 749                     equalValue = chromosome_group[indexParent1].genLNK[crossGen][h];
 750                 else if(equalCount == 2)
 751                     equalValue = -1;
 752             }
 753         }
 754         //如果交换部分没有相同的基因
 755         if(equalValue == 0)
 756         {
 757             int tt,hh,pp;
 758             int number1 = 0;
 759             int number2 = 0;
 760             //在一个染色体中,出了被交换的基因以外的基因上寻找不满足约束条件的值
 761             for(tt=0;tt<sumTeam;tt++)
 762             {
 763                 if(tt != crossGen)
 764                 {
 765                     //比较该基因上和交换部分的基因是否有相同的值
 766                     for(hh=0;hh<maxLinks;hh++)
 767                     {
 768                         for(pp=0;pp<maxLinks;pp++)
 769                         {
 770                             //此处代码块搜索的是第1个父本
 771                             if(chromosome_group[indexParent1].genLNK[tt][hh] == chromosome_group[indexParent1].genLNK[crossGen][pp])
 772                             {
 773                                 //找到时,记录找到的是第几个相同的值
 774                                 number1++;
 775                                 if(number1 == 1)
 776                                 {
 777                                     qps_GUI_printf("Find:%d   is%d  ",number1,chromosome_group[indexParent1].genLNK[tt][hh]);
 778                                     //将不满足约束条件的部分作出修改
 779                                     chromosome_group[indexParent1].genLNK[tt][hh] = chromosome_group[indexParent2].genLNK[crossGen][0];
 780                                     qps_GUI_printf("Alter is%d  ",chromosome_group[indexParent1].genLNK[tt][hh]);
 781                                 }
 782                                 else
 783                                 {
 784                                     qps_GUI_printf("Find:%d   is%d  ",number1,chromosome_group[indexParent1].genLNK[tt][hh]);
 785                                     chromosome_group[indexParent1].genLNK[tt][hh] = chromosome_group[indexParent2].genLNK[crossGen][1];
 786                                     qps_GUI_printf("Alter:%d   is%d  ",chromosome_group[indexParent1].genLNK[tt][hh]);
 787                                 }
 788
 789                                 //在Scheduel记录表上修改不满足约束条件的值
 790                                 int s1;
 791                                 for(s1=0;s1<hours;s1++)
 792                                 {
 793                                     if(chromosome_group[indexParent1].Scheduel[tt][s1] == chromosome_group[indexParent1].genLNK[crossGen][pp])
 794                                     {
 795                                         chromosome_group[indexParent1].Scheduel[tt][s1] = chromosome_group[indexParent1].genLNK[tt][hh];
 796                                     }
 797                                 }
 798                             }
 799                             //此处代码块搜索的是第2个父本
 800                             if(chromosome_group[indexParent2].genLNK[tt][hh] == chromosome_group[indexParent2].genLNK[crossGen][pp])
 801                             {
 802                                 number2++;
 803                                 if(number2 == 1)
 804                                 {
 805                                     //将不满足约束条件的部分作出修改
 806                                     chromosome_group[indexParent2].genLNK[tt][hh] = chromosome_group[indexParent1].genLNK[crossGen][0];
 807                                 }
 808                                 else
 809                                 {
 810                                     chromosome_group[indexParent2].genLNK[tt][hh] = chromosome_group[indexParent1].genLNK[crossGen][1];
 811                                 }
 812
 813                                 //在Scheduel记录表上修改不满足约束条件的值
 814                                 int s2;
 815                                 for(s2=0;s2<hours;s2++)
 816                                 {
 817                                     if(chromosome_group[indexParent2].Scheduel[tt][s2] == chromosome_group[indexParent2].genLNK[crossGen][pp])
 818                                     {
 819                                         chromosome_group[indexParent2].Scheduel[tt][s2] = chromosome_group[indexParent2].genLNK[tt][hh];
 820                                     }
 821                                 }
 822                             }
 823                         }
 824                     }
 825                 }
 826             }
 827         }
 828         //所交换的部分中存在1个相同值(因为如果存在2个,equalValue值将修改为-1)
 829         else if(equalValue > 0)
 830         {
 831             int tt,hh,pp;
 832             int number1=0;
 833             int number2=0;
 834             //在出交换部分之外的基因中搜索
 835             for(tt=0;tt<sumTeam;tt++)
 836             {
 837                 if(tt != crossGen)
 838                 {
 839                     for(hh=0;hh<maxLinks;hh++)
 840                     {
 841                         for(pp=0;pp<maxLinks;pp++)
 842                         {
 843                             //在父本1上搜索,找到该基因上与交换部分值相同的基因
 844                             if(chromosome_group[indexParent1].genLNK[tt][hh] == chromosome_group[indexParent1].genLNK[crossGen][pp])
 845                             {
 846                                 int w;
 847                                 for(w=0;w<maxLinks;w++)
 848                                 {
 849                                     //找到另外一个父本上和交换部分的不相同的基因值
 850                                     if(chromosome_group[indexParent2].genLNK[crossGen][w] != equalValue)
 851                                     {
 852                                         chromosome_group[indexParent1].genLNK[tt][hh] = chromosome_group[indexParent2].genLNK[crossGen][w];
 853                                     }
 854                                 }
 855                                 //将不满足约束条件的基因值修改
 856                                 int f1;
 857                                 for(f1=0;f1<hours;f1++)
 858                                 {
 859                                     if(chromosome_group[indexParent1].Scheduel[tt][f1] == chromosome_group[indexParent1].genLNK[crossGen][pp])
 860                                     {
 861                                         chromosome_group[indexParent1].Scheduel[tt][f1] = chromosome_group[indexParent1].genLNK[tt][hh];
 862                                     }
 863                                 }
 864                             }
 865
 866                             //在父本2上搜索,找到该基因上与交换部分值相同的基因
 867                             if(chromosome_group[indexParent2].genLNK[tt][hh] == chromosome_group[indexParent2].genLNK[crossGen][pp])
 868                             {
 869                                 //找到另外一个父本上和交换部分的不相同的基因值
 870                                 int w;
 871                                 for(w=0;w<maxLinks;w++)
 872                                 {
 873                                     if(chromosome_group[indexParent1].genLNK[crossGen][w] != equalValue)
 874                                     {
 875                                         chromosome_group[indexParent2].genLNK[tt][hh] = chromosome_group[indexParent1].genLNK[crossGen][w];
 876                                     }
 877                                 }
 878                                 //将不满足约束条件的基因值修改
 879                                 int f2;
 880                                 for(f2=0;f2<hours;f2++)
 881                                 {
 882                                     if(chromosome_group[indexParent2].Scheduel[tt][f2] == chromosome_group[indexParent2].genLNK[crossGen][pp])
 883                                     {
 884                                         chromosome_group[indexParent2].Scheduel[tt][f2] = chromosome_group[indexParent2].genLNK[tt][hh];
 885                                     }
 886                                 }
 887                             }
 888                         }
 889                     }
 890                 }
 891             }
 892         }
 893     }
 894
 895     int t1;
 896     int t2;
 897     qps_GUI_printf("AFTER CROSS!!!!!!!!!!!!!!!!!!!!!!");
 898     for(t1=0;t1<SUM;t1++)
 899     {
 900         qps_GUI_printf("Chromosome:%d ",(t1+1));
 901         for(t2=0;t2<sumTeam;t2++)
 902         {
 903             qps_GUI_printf("%d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d",
 904                 chromosome_group[t1].Scheduel[t2][0],
 905                 chromosome_group[t1].Scheduel[t2][1],
 906                 chromosome_group[t1].Scheduel[t2][2],
 907                 chromosome_group[t1].Scheduel[t2][3],
 908                 chromosome_group[t1].Scheduel[t2][4],
 909                 chromosome_group[t1].Scheduel[t2][5],
 910                 chromosome_group[t1].Scheduel[t2][6],
 911                 chromosome_group[t1].Scheduel[t2][7],
 912                 chromosome_group[t1].Scheduel[t2][8],
 913                 chromosome_group[t1].Scheduel[t2][9],
 914                 chromosome_group[t1].Scheduel[t2][10],
 915                 chromosome_group[t1].Scheduel[t2][11],
 916                 chromosome_group[t1].Scheduel[t2][12],
 917                 chromosome_group[t1].Scheduel[t2][13],
 918                 chromosome_group[t1].Scheduel[t2][14],
 919                 chromosome_group[t1].Scheduel[t2][15],
 920                 chromosome_group[t1].Scheduel[t2][16],
 921                 chromosome_group[t1].Scheduel[t2][17],
 922                 chromosome_group[t1].Scheduel[t2][18],
 923                 chromosome_group[t1].Scheduel[t2][19],
 924                 chromosome_group[t1].Scheduel[t2][20],
 925                 chromosome_group[t1].Scheduel[t2][21],
 926                 chromosome_group[t1].Scheduel[t2][22],
 927                 chromosome_group[t1].Scheduel[t2][23]);
 928         }
 929         qps_GUI_printf("------------------------------------------------------------------------------------------------------");
 930     }
 931 }
 932
 933 /*-------------------------------------------------------------------------------------------------
 934 * 突变(原理是对排序后排名最后的染色体随机发生突变,生成新的染色体)
 935 *--------------------------------------------------------------------------------------------------*/
 936 void mutation()
 937 {
 938     qps_GUI_printf("mutation!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
 939
 940     //对排名最后的一个染色体随机发生突变,变异方式为随机产生一个新的染色体
 941     srand( (unsigned)time( NULL ));
 942     if(randomMutation())
 943     {
 944         qps_GUI_printf("IS Mutation!!!!!!!!!!!!!!");
 945         //首先将第最后一个染色体的所有数据清洗
 946         int i,j,k;
 947         for(i=0;i<sumTeam;i++)
 948         {
 949             for(j=0;j< hours;j++)
 950             {
 951                 chromosome_group[SUM-1].Scheduel[i][j] = 0;
 952             }
 953             for(k=0;k<2;k++)
 954             {
 955                 chromosome_group[SUM-1].genLNK[i][k] = 0;
 956             }
 957         }
 958         int n;
 959         for(n=0;n<sumClosure;n++)
 960         {
 961             chromosome_group[SUM-1].commandVSL[n] = 0;
 962         }
 963
 964         //重新设置随机数种子
 965         srand( (unsigned)time( NULL ));
 966
 967         //然后随机产生一个新的染色体
 968         team[0].countLink = 0;
 969         team[1].countLink = 0;
 970         team[2].countLink = 0;
 971         int re;
 972         for(re=0;re<sumTeam;re++)
 973         {
 974             team[re].countLink = 0;
 975             qps_GUI_printf("now The team[%d].countLink is:%d   ",re,team[re].countLink);
 976             team[re].workLink[0]=0;
 977             qps_GUI_printf("now The team[%d].workLink[%d] is:%d   ",re,0,team[re].workLink[0]);
 978             team[re].workLink[1]=0;
 979             qps_GUI_printf("now The team[%d].workLink[%d] is:%d   ",re,1,team[re].workLink[1]);
 980
 981             int ev;
 982             for(ev=0;ev<hours;ev++)
 983             {
 984                 team[re].scheduel[ev]=0;
 985                 qps_GUI_printf("now The team[%d].scheduel[%d] is:%d   ",re,1,team[re].scheduel[ev]);
 986             }
 987         }
 988
 989
 990         int jk;
 991         for(jk=0;jk<sumClosure;jk++)
 992         {
 993             //选择哪个施工队负责
 994             int randomTeam;
 995             randomTeam = randomMaker(0,10*sumTeam);
 996
 997             if(randomTeam <= 10)
 998                 randomTeam = 0;
 999             else if(randomTeam > 10 && randomTeam <= 20)
1000                 randomTeam = 1;
1001             else if(randomTeam > 20)
1002                 randomTeam = 2;
1003
1004             if(team[randomTeam].countLink == maxLinks)
1005             {
1006                 if(randomTeam == 0)
1007                 {
1008                     if(team[1].countLink == maxLinks || team[2].countLink == maxLinks)
1009                     {
1010                         if(team[1].countLink == maxLinks)
1011                             randomTeam = 2;
1012                         else
1013                             randomTeam = 1;
1014                     }
1015                     else
1016                     {
1017                         if(randomMaker(0,100) < 50)
1018                             randomTeam = 1;
1019                         else
1020                             randomTeam = 2;
1021                     }
1022                 }
1023                 else if(randomTeam == 1)
1024                 {
1025                     if(team[0].countLink == maxLinks ||team[2].countLink == maxLinks)
1026                     {
1027                         if(team[0].countLink == maxLinks)
1028                             randomTeam = 2;
1029                         else
1030                             randomTeam = 0;
1031                     }
1032                     else
1033                     {
1034                         if(randomMaker(0,100) < 50)
1035                             randomTeam = 0;
1036                         else
1037                             randomTeam = 2;
1038                     }
1039                 }
1040                 else if(randomTeam == 2)
1041                 {
1042                     if(team[0].countLink == maxLinks ||team[1].countLink == maxLinks )
1043                     {
1044                         if(team[0].countLink == maxLinks)
1045                             randomTeam = 1;
1046                         else
1047                             randomTeam = 0;
1048                     }
1049                     else
1050                     {
1051                         if(randomMaker(0,100) < 50)
1052                             randomTeam = 0;
1053                         else
1054                             randomTeam = 1;
1055                     }
1056                 }
1057             }
1058             team[randomTeam].countLink++;
1059
1060             //选择该路段的施工起始时间
1061
1062             int randomHour = 1;
1063
1064
1065             bool pass = PFALSE;
1066             int reTimes = 0;
1067             qps_GUI_printf("---------------------------------------------------------------------------------------------------");
1068             qps_GUI_printf("Now is Link:%d   is select Hour! It ‘s Team is :%d   ",j,randomTeam);
1069             do
1070             {
1071                 randomHour = randomMaker(team[randomTeam].startTime,team[randomTeam].overTime-2);
1072                 qps_GUI_printf("first select hour:%d   ",randomHour);
1073                 if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
1074                 {
1075                     qps_GUI_printf("That is OK!Hour is:%d   ",randomHour);
1076                     team[randomTeam].workLink[team[randomTeam].countLink-1] = jk+1;
1077                     team[randomTeam].scheduel[randomHour-1] = jk+1;
1078                     team[randomTeam].scheduel[randomHour+1-1] = jk+1;
1079                     pass = PTRUE;
1080                 }
1081                 else
1082                 {
1083                     qps_GUI_printf("The hour has been used!!:%d   ",randomHour);
1084                     if((randomHour- team[randomTeam].startTime) >= (team[randomTeam].overTime-(randomTeam+1)))
1085                     {
1086                         qps_GUI_printf("The before > after!!");
1087                         randomHour =  randomMaker(team[randomTeam].startTime,(randomHour-2));
1088                         qps_GUI_printf("new Hour is :%d   ",randomHour);
1089                         if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
1090                         {
1091                             qps_GUI_printf("new Hour is OK :%d   ",randomHour);
1092                             team[randomTeam].workLink[team[randomTeam].countLink-1] = jk+1;
1093                             team[randomTeam].scheduel[randomHour-1] = jk+1;
1094                             team[randomTeam].scheduel[randomHour+1-1] = jk+1;
1095                             pass = PTRUE;
1096                         }
1097                         else
1098                         {
1099                             qps_GUI_printf("new Hour is used :%d   ",randomHour);
1100                             randomHour =  randomMaker(team[randomTeam].startTime,team[randomTeam].startTime+2);
1101                             qps_GUI_printf("once new Hour is :%d   ",randomHour);
1102                             team[randomTeam].workLink[team[randomTeam].countLink-1] = jk+1;
1103                             team[randomTeam].scheduel[randomHour-1] = jk+1;
1104                             team[randomTeam].scheduel[randomHour+1-1] = jk+1;
1105                             pass = PTRUE;
1106                         }
1107                     }
1108                     else
1109                     {
1110                         qps_GUI_printf("The before < after!!");
1111                         randomHour = randomMaker(randomHour+2,team[randomTeam].overTime-2);
1112                         qps_GUI_printf("new Hour is :%d   ",randomHour);
1113                         if(team[randomTeam].scheduel[randomHour-1] == 0 && team[randomTeam].scheduel[randomHour+1-1] == 0)
1114                         {
1115                             qps_GUI_printf("new Hour is OK :%d   ",randomHour);
1116                             team[randomTeam].workLink[team[randomTeam].countLink-1] = jk+1;
1117                             team[randomTeam].scheduel[randomHour-1] = jk+1;
1118                             team[randomTeam].scheduel[randomHour+1-1] = jk+1;
1119                             pass = PTRUE;
1120                         }
1121                         else
1122                         {
1123                             qps_GUI_printf("new Hour is used :%d   ",randomHour);
1124                             randomHour = team[randomTeam].overTime-2;
1125                             qps_GUI_printf("once new Hour is :%d   ",randomHour);
1126                             team[randomTeam].workLink[team[randomTeam].countLink-1] = jk+1;
1127                             team[randomTeam].scheduel[randomHour-1] = jk+1;
1128                             team[randomTeam].scheduel[randomHour+1-1] = jk+1;
1129                             pass = PTRUE;
1130                         }
1131                     }
1132                 }
1133             }
1134             while(!pass);
1135
1136             int k;
1137             for(k = randomHour;k<=(randomHour+1);k++)
1138                 chromosome_group[SUM-1].Scheduel[randomTeam][k-1] = closure_link[jk].index;
1139             chromosome_group[SUM-1].genLNK[randomTeam][team[randomTeam].countLink-1] = closure_link[jk].index;
1140
1141             int VSL_random = randomMaker(0,31);
1142             if(VSL_random <=10)
1143             {
1144                 //不执行VSL
1145                 chromosome_group[SUM-1].commandVSL[jk] = 0;
1146             }
1147             else if(VSL_random >10 && VSL_random <=20)
1148             {
1149                 //执行第1种VSL控制策略。
1150                 chromosome_group[SUM-1].commandVSL[jk] = 1;
1151             }
1152             else if(VSL_random >20 && VSL_random <=30)
1153             {
1154                 //执行第2种VSL控制策略。
1155                 chromosome_group[SUM-1].commandVSL[jk] = 2;
1156             }
1157         }
1158     }
1159 }
1160
1161 /*-------------------------------------------------------------------------------------------------------
1162 * 该函数用于在给定一个区间上下界后产生一个区间内的随机数
1163 *------------------------------------------------------------------------------------------------------*/
1164 int randomMaker(int lower,int upper)
1165 {
1166     int r = rand();
1167     int max_r = 32767;
1168     r = (int) (((float) r / (float) max_r)*(upper - lower)) + lower;
1169     return r;
1170 }
1171
1172 /*-------------------------------------------------------------------------------------------------------
1173 * 该函数用根据延误值的大小对染色体组内的各个染色体按照从小到大的顺序进行排序
1174 *-------------------------------------------------------------------------------------------------------*/
1175 void rankedChromosomes()
1176 {
1177     //测试语句
1178     qps_GUI_printf("Ranked Chromosome");
1179
1180     int i;
1181     int j;
1182
1183     //基于冒泡算法,对染色体组中的染色体体进行直接排序
1184     for(i = 0 ; i < SUM - 1 ; i++)
1185     {
1186         for(j = i + 1 ; j < SUM ; j++)
1187         {
1188             if( chromosome_group[i].delay > chromosome_group[j].delay )
1189             {
1190                 chromosome middle_chro = chromosome_group[i];
1191                 chromosome_group[i] = chromosome_group[j];
1192                 chromosome_group[j] = middle_chro;
1193             }
1194         }
1195     }
1196 }
1197
1198 /*-------------------------------------------------------------------------------------------------------
1199 * 该函数用根据安全性指标的大小对染色体组内的各个VSL控制方案按照从小到大的顺序进行排序
1200 *-------------------------------------------------------------------------------------------------------*/
1201 void rankedVSLCommand()
1202 {
1203     //测试语句
1204     qps_GUI_printf("Ranked Chromosome");
1205
1206     int i;
1207     int j;
1208
1209     //基于冒泡算法,对染色体组中的染色体体进行直接排序
1210     for(i = 0 ; i < SUM - 1 ; i++)
1211     {
1212         for(j = i + 1 ; j < SUM ; j++)
1213         {
1214             if( chromosome_group[i].delay > chromosome_group[j].delay )
1215             {
1216                 int ii;
1217                 for(ii=0;ii<sumClosure;ii++)
1218                 {
1219                     int temp = chromosome_group[i].commandVSL[ii];
1220                     chromosome_group[i].commandVSL[ii] = chromosome_group[j].commandVSL[ii];
1221                     chromosome_group[j].commandVSL[ii] = temp;
1222                 }
1223             }
1224         }
1225     }
1226 }
1227 /*-------------------------------------------------------------------------------------------------------
1228 * 该函数用于将排序后的染色体进行配对,配对完成后,1,2是一对父本,3,4是一对父本
1229 *------------------------------------------------------------------------------------------------------*/
1230 void selection()
1231 {
1232     //测试语句:
1233     qps_GUI_printf("Selection");
1234
1235     //在排序后的第2和第3染色体之间选择一个和1一起组成父本,剩下的和4组成父本
1236     int mySelection = 0;
1237     srand( (unsigned)time( NULL ));
1238     if(randomMaker(0,100) < 86)
1239         mySelection = 2;
1240     else
1241         mySelection = 3;
1242
1243     //测试语句:
1244     qps_GUI_printf("Parent Selection Result:%d",mySelection);
1245
1246     if(mySelection == 2)
1247     {
1248         chromosome middle_chro = chromosome_group[1];
1249         chromosome_group[1] = chromosome_group[2];
1250         chromosome_group[2] = middle_chro;
1251     }
1252 }
1253
1254 /*------------------------------------------------------------------------------------------------------
1255 * 该函数用于在变异发生之前通过产生随机数的方式确定当前染色体是否发生突变,并返回PTRUE(发生
1256 * 或PFALSE(不发生)
1257 *-------------------------------------------------------------------------------------------------------*/
1258 bool randomMutation()
1259 {
1260     bool mutation = PFALSE;
1261     srand( (unsigned)time( NULL ));
1262     int random = randomMaker(0,100);
1263     if(random<=25)
1264     {
1265         mutation = PTRUE;
1266     }
1267     return mutation;
1268 }
1269
1270 /*----------------------------------------------------------------------------------------------------
1271 * 该函数用于按照施工计划(染色体)执行仿真过程中的车道关闭过程
1272 *------------------------------------------------------------------------------------------------------*/
1273 void simulationControl(int simulationTimes,int current_time)
1274 {
1275     //测试语句:
1276     qps_GUI_printf("simulation Control");
1277
1278     int i;
1279     for(i=0;i<sumTeam;i++)
1280     {
1281         int index = chromosome_group[(simulationTimes + SUM - 1)%SUM].Scheduel[i][(int)(current_time/secHOUR)];
1282         int per_index = chromosome_group[(simulationTimes + SUM - 1)%SUM].Scheduel[i][(int)(current_time/secHOUR)];
1283         //测试语句:
1284         qps_GUI_printf("current ‘index‘=%d  chromosome_group:%d  Schedule_Team:%d %d  ",index,(simulationTimes + SUM - 1)%4,i,(int)(current_time/secHOUR));
1285
1286         //如果施工计划矩阵中数值不为x(x不等于0),则表示这一小时内x路段施工关闭
1287         if(index != 0)
1288         {
1289             int s;
1290             for(s=0;s<sumClosure;s++)
1291             {
1292                 if(closure_link[s].index == index)
1293                 {
1294                     closure_link[s].flag = PTRUE;
1295                     int z1;
1296                     for(z1=0;z1<maxLanes;z1++)
1297                     {
1298                         if(closure_link[s].closureLane[z1] != 0)
1299                         {
1300                             qps_LNK_closure(qpg_NET_linkByIndex(index),closure_link[s].closureLane[z1],PFALSE);
1301                             //测试语句:
1302                             qps_GUI_printf("Link:%s lane:%d  closed!",qpg_LNK_name(qpg_NET_linkByIndex(index)),closure_link[s].closureLane[z1]);
1303                         }
1304                     }
1305                 }
1306             }
1307         }
1308
1309         if(current_time/secHOUR != 0)
1310         {
1311             if(index != chromosome_group[(simulationTimes + SUM - 1)%4].Scheduel[i][(current_time/secHOUR)-1])
1312             {
1313                 //如果施工计划矩阵中当前数值和前一位为x(x不等于0)不相等且前一位数值不为0,则这一小时内,该路段结束施工开放
1314                 if( chromosome_group[(simulationTimes + SUM - 1)%4].Scheduel[i][((current_time-1)/secHOUR)-1] != 0)
1315                 {
1316                     int index = chromosome_group[(simulationTimes + SUM - 1)%4].Scheduel[i][(current_time/secHOUR)-1];
1317                     int f;
1318                     for(f=0;f<sumClosure;f++)
1319                     {
1320                         if(closure_link[f].index == index)
1321                         {
1322                             closure_link[f].flag = PFALSE;
1323                             int z2;
1324                             for(z2=1;z2<=maxLanes;z2++)
1325                             {
1326                                 qps_LNK_closure(qpg_NET_linkByIndex(index),closure_link[f].closureLane[z2],PTRUE);
1327                                 //测试语句:
1328                                 qps_GUI_printf("Link:%s lane:%d  open again!",qpg_LNK_name(qpg_NET_linkByIndex(index)),closure_link[f].closureLane[z2]);
1329                             }
1330                         }
1331                     }
1332                 }
1333             }
1334         }
1335     }
1336 }
1337
1338 /*-----------------------------------------------------------------------------------------------------
1339 * 该函数用于保存此次仿真数据
1340 *------------------------------------------------------------------------------------------------------*/
1341 void SaveSimulationData(int simuTimes)
1342 {
1343     int index = (simuTimes + 4) % 4;
1344
1345     //清除“chromosome”文件中的数据,并将最新仿真数据写入对应的文件中
1346     int chro;
1347     for(chro=1;chro<=SUM;chro++)
1348     {
1349         FILE *file_chromosome;
1350         FILE *file_genLNK;
1351         if(chro == 1)
1352         {
1353             file_chromosome = fopen(chromosome1,"w");
1354             file_genLNK = fopen(genLNK1,"w");
1355             qps_GUI_printf("write chromosome 1 %d",chro);
1356         }
1357         else if(chro == 2)
1358         {
1359             file_chromosome = fopen(chromosome2,"w");
1360             file_genLNK = fopen(genLNK2,"w");
1361             qps_GUI_printf("write chromosome 2 %d",chro);
1362         }
1363         else if(chro == 3)
1364         {
1365             file_chromosome = fopen(chromosome3,"w");
1366             file_genLNK = fopen(genLNK3,"w");
1367             qps_GUI_printf("write chromosome 3 %d",chro);
1368         }
1369         else if(chro == 4)
1370         {
1371             file_chromosome = fopen(chromosome4,"w");
1372             file_genLNK = fopen(genLNK4,"w");
1373             qps_GUI_printf("write chromosome 4 %d",chro);
1374         }
1375         int i,j;
1376         for(i=0;i<sumTeam;i++)
1377         {
1378             for(j=0;j<hours;j++)
1379             {
1380                 if(index == 0)
1381                     fprintf(file_chromosome,"%d ",chromosome_group[chro-1].Scheduel[i][j]);
1382                 else
1383                     fprintf(file_chromosome,"%d ",chromosome_group[chro-1].Scheduel[i][j]);
1384             }
1385             fputc(‘\n‘,file_chromosome);
1386         }
1387         fclose(file_chromosome);
1388
1389         int n,m;
1390         for(n=0;n<sumTeam;n++)
1391         {
1392             for(m=0;m<maxLinks;m++)
1393             {
1394                 fprintf(file_genLNK,"%d ",chromosome_group[chro-1].genLNK[n][m]);
1395             }
1396             fputc(‘\n‘,file_genLNK);
1397         }
1398         fclose(file_genLNK);
1399     }
1400     //打开“delay”文件中的数据,并将最新仿真数据写入对应的文件中
1401     FILE *file_delay;
1402     file_delay = fopen(delay,"w");
1403     int a;
1404     for(a=0;a<SUM;a++)
1405     {
1406             fprintf(file_delay,"%f ",chromosome_group[a].delay);
1407     }
1408
1409     //打开“command_VSL文件”中的数据,并将最新仿真数据写入对应的文件中
1410     FILE *file_command_VSL;
1411
1412     file_command_VSL = fopen(command_VSL,"w");
1413     int i,j;
1414     for(i=0;i<SUM;i++)
1415     {
1416         for(j=0;j<sumClosure;j++)
1417             fprintf(file_command_VSL,"%d ",chromosome_group[i].commandVSL[j]);
1418         fputc(‘\n‘,file_command_VSL);
1419     }
1420     fclose(file_command_VSL);
1421
1422
1423     //保存仿真次数
1424     FILE *file_simuTimes;
1425     file_simuTimes = fopen(simulationTimes,"w");
1426     fprintf(file_simuTimes,"%d",simuTimes);
1427
1428     fclose(file_delay);
1429     fclose(file_simuTimes);
1430 }
1431
1432 /*------------------------------------------------------------------------------------------------------
1433 * 该函数用于读取上一次仿真的数据
1434 *-------------------------------------------------------------------------------------------------------*/
1435 void ReadSimulationData()
1436 {
1437     FILE *file_chromosome;
1438     FILE *file_delay;
1439     FILE *file_closelink;
1440     FILE *file_simuTimes;
1441     FILE *file_command_VSL;
1442     FILE *file_genLNK;
1443
1444     //将上一次仿真的数据读入本次仿真过程
1445     int i;
1446     for(i=0;i<SUM;i++)
1447     {
1448         if(i==0)
1449         {
1450             file_chromosome = fopen(chromosome1,"rt");
1451             file_genLNK = fopen(genLNK1,"rt");
1452             qps_GUI_printf("read chromosome 1 %d",i+1);
1453         }
1454         else if(i==1)
1455         {
1456             file_chromosome = fopen(chromosome2,"rt");
1457             file_genLNK = fopen(genLNK2,"rt");
1458             qps_GUI_printf("read chromosome 2 %d",i+1);
1459         }
1460         else if(i==2)
1461         {
1462             file_chromosome = fopen(chromosome3,"rt");
1463             file_genLNK = fopen(genLNK3,"rt");
1464             qps_GUI_printf("read chromosome 3 %d",i+1);
1465         }
1466         else if(i==3)
1467         {
1468             file_chromosome = fopen(chromosome4,"rt");
1469             file_genLNK = fopen(genLNK4,"rt");
1470             qps_GUI_printf("read chromosome 4 %d",i+1);
1471         }
1472
1473         int j;
1474         int k;
1475         for(j=0;j<sumTeam;j++)
1476         {
1477             for(k=0;k<hours;k++)
1478             {
1479                  fscanf(file_chromosome,"%d",&chromosome_group[i].Scheduel[j][k]);
1480             }
1481         }
1482
1483         int n,m;
1484         for(n=0;n<sumTeam;n++)
1485         {
1486             for(m=0;m<maxLinks;m++)
1487             {
1488                 fscanf(file_genLNK,"%d",&chromosome_group[i].genLNK[n][m]);
1489             }
1490         }
1491     }
1492
1493     //将delay数据读入当前simulation
1494     int n;
1495     file_delay = fopen(delay,"rt");
1496     for(n=0;n<SUM;n++)
1497         fscanf(file_delay,"%f ",&chromosome_group[n].delay);
1498
1499     //将“command_VSL”文件中的数据读入到当前的simulation中
1500     int g,q;
1501     file_command_VSL = fopen(command_VSL,"rt");
1502     for(g=0;g<SUM;g++)
1503     {
1504         for(q=0;q<sumClosure;q++)
1505         {
1506             fscanf(file_command_VSL,"%d ",&chromosome_group[g].commandVSL[q]);
1507         }
1508     }
1509
1510     //仿真次数
1511     file_simuTimes = fopen(simulationTimes,"rt");
1512     fscanf(file_simuTimes,"%d",&simuTimes);
1513
1514     fclose(file_chromosome);
1515     fclose(file_delay);
1516     fclose(file_genLNK);
1517     fclose(file_simuTimes);
1518 }
1519
1520 /*------------------------------------------------------------------------------------------------------
1521 *该函数用于求解一系列数的方差
1522 *------------------------------------------------------------------------------------------------------*/
1523 float getVariance(float list[])
1524 {
1525     float value = 0;
1526     float avarage = 0;
1527
1528     int i;
1529     int n = sizeof(list)/sizeof(float);
1530     for(i=0;i<n;i++)
1531         avarage += list[i];
1532     avarage = avarage/n;
1533
1534     int j;
1535     for(j=0;j<n;j++)
1536         value += (list[i]-avarage)*(list[i]-avarage);
1537     value = value/n;
1538
1539     return value;
1540 }
1541
1542 /*-------------------------------------------------------------------------------------------------------
1543 *该函数用于将VSL控制命令写入存储文本文件中
1544 *-------------------------------------------------------------------------------------------------------*/
1545 void controlVSL(int list[])
1546 {
1547     FILE *file_command_VSL;
1548     //打开“delay”文件中的数据,并将最新仿真数据写入对应的文件中
1549     file_command_VSL = fopen(command_VSL,"w");
1550     int i,j;
1551     for(i=0;i<SUM;i++)
1552     {
1553         for(j=0;j<sumClosure;j++)
1554             fprintf(file_command_VSL,"%d ",chromosome_group[i].commandVSL[j]);
1555         fputc(‘\n‘,file_command_VSL);
1556     }
1557
1558 }
1559
1560 /*-----------------------------------------------------------------------------------------------------
1561 * 该函数用于在每一小时开始时设置OD矩阵,以实现一天之中的OD动态变化
1562 *-------------------------------------------------------------------------------------------------------*/
1563 void setDMD(int currentTime)
1564 {
1565     int h;
1566     for(h=0;h<24;h++)
1567     {
1568         if(h*HOUR == currentTime)
1569         {
1570             //测试语句:
1571             qps_GUI_printf("Current OD Matrix:");
1572
1573             int o;
1574             int d;
1575             for(o=1;o<=8;o++)
1576             {
1577                 for(d=1;d<=8;d++)
1578                 {
1579                     float old_OD = qpg_DMD_demand(1,o,d);
1580                     int new_OD = (int)(old_OD*factorDMD[h]);
1581                     qps_DMD_demand(1,o,d,(float)new_OD);
1582
1583                 }
1584
1585                 //测试语句:
1586                 qps_GUI_printf("%f   %f   %f   %f   %f   %f  ",qpg_DMD_demand(1,o,1),qpg_DMD_demand(1,o,2),qpg_DMD_demand(1,o,3),qpg_DMD_demand(1,o,4),qpg_DMD_demand(1,o,5));
1587             }
1588             break;
1589         }
1590     }
1591 }
1592
1593 /*-----------------------------------------------------------------------------------------------------------------------
1594 * 该函数用于持续记录每一代种群中最优方案(时间表和VSL控制命令)和该染色体的适应度值
1595 *-----------------------------------------------------------------------------------------------------------------------*/
1596 void saveBestChromosome()
1597 {
1598     FILE *file_bestChromosome;
1599     file_bestChromosome = fopen(bestChromosome,"a+");
1600     fprintf(file_bestChromosome,"Generation index:%d   ",(int)(simuTimes/SUM));
1601     fputc(‘\n‘,file_bestChromosome);
1602     fprintf(file_bestChromosome,"Tollaly Delay:%f   ",(float)chromosome_group[0].delay);
1603     fputc(‘\n‘,file_bestChromosome);
1604     fprintf(file_bestChromosome,"*----------------------------------------------------------------------------------*");
1605     fputc(‘\n‘,file_bestChromosome);
1606     int i,j,k;
1607     for(i=0;i<sumTeam;i++)
1608     {
1609         for(j=0;j<hours;j++)
1610         {
1611             fprintf(file_bestChromosome,"%d ",chromosome_group[0].Scheduel[i][j]);
1612         }
1613         fputc(‘\n‘,file_bestChromosome);
1614     }
1615     fputc(‘\n‘,file_bestChromosome);
1616     fprintf(file_bestChromosome,"*----------------------------------------------------------------------------------*");
1617     fputc(‘\n‘,file_bestChromosome);
1618     for(k=0;k<sumClosure;k++)
1619     {
1620         fprintf(file_bestChromosome,"%d ",chromosome_group[0].commandVSL[k]);
1621     }
1622     fputc(‘\n‘,file_bestChromosome);
1623     fprintf(file_bestChromosome,"*----------------------------------------------------------------------------------*");
1624     fputc(‘\n‘,file_bestChromosome);
1625     fputc(‘\n‘,file_bestChromosome);
1626     fclose(file_bestChromosome);
1627 }
时间: 2025-01-17 09:17:25

GA-MicroSimulation 作业区优化算法的相关文章

细菌觅食优化算法:理论基础,分析,以及应用(未完)

原作者:Swagatam Das,Arijit Biswas,Sambarta Dasgupta,和Ajith Abraham  [摘 要]细菌觅食优化算法(Bacterial foraging optimization algorithm[BFOA])已经被分布式优化和控制的同行们当作一种全局性的优化算法接受.BFOA是由大肠杆菌的群体觅食行为所启发而总结出来 的.BFOA已经吸引了足够多的研究者的注意,由于它出现在解决真实世界中一些应用领域上优化问题的高效性.E.coli 的群体策略的生物基

粒子群优化算法(PSO)

1. 概念 粒子群优化算法(PSO:Particle swarm optimization) 是一种进化计算技术(evolutionary computation). 源于对鸟群捕食的行为研究.粒子群优化算法的基本思想:是通过群体中个体之间的协作和信息共享来寻找最优解. PSO的优势:在于简单容易实现并且没有许多参数的调节.目前已被广泛应用于函数优化.神经网络训练.模糊系统控制以及其他遗传算法的应用领域. 2. 算法 2.1 问题抽象 鸟被抽象为没有质量和体积的微粒(点),并延伸到N维空间,粒子

优化算法——遗传算法

与遗传算法的第一次接触 遗传算法的基本概念 基本定义 遗传算法的基本流程 遗传算法过程中的具体操作 参数的编码 二进制编码 Gray编码 实数编码 有序编码 初始群体的设定 适应度函数的计算 遗传操作设计 选择selection 交叉crossover 变异mutation 控制参数的设定 求解优化问题的实例 问题描述 问题分析 算法设计 个体编码 适应值函数 选择策略 杂交算子 变异算子 参数设置 初始化 终止条件 实验代码 最终结果 与遗传算法的第一次接触 遗传算法是我进入研究生阶段接触的第

优化算法 无处不在

一:起因 (0)优化算法(Optimization Algorithm),即求目标函数的最优值问题:如何评价你的当前解的值是最优的?这就需要构造评价函数:如何从当前的位置(解)更新到新的搜索空间?这就需要变换的形式的定义. (1)优化算法无处不在 -- 实际生活中的 物资调配,一定的生产资料如何得到最大的产出,一定的投资如何得到最佳的收益等等,都可以转化为最优化问题的求解:就连我们平常生活中的一次旅游计划也离不开优化问题:三天的假期,我是去西藏 还是去北京还是去上海,我得受制于假期的期限,以及路

粒子群优化算法简介

好好学数学. 一.问题来源 经朋友介绍,接了一份工作,就是做PSO及其优化,恰好我导师也研究这个,刚开学也有接触,那我就接了.......赚点生活费. 欢迎大家和我联系做算法类项目,QQ:791909235,Tel:13137910179. 二.背景介绍 2.1 人工生命 人工生命:研究具有某些生命基本特征的人 工系统.包括两方面的内容: 1.研究如何利用计算技术研究生物现象: 2. 研究如何利用生物技术研究计算问题. 我们关注的是第二点.已有很多源于生物现象的计算技巧,例如神经网络和遗传算法.

最优化算法&mdash;&mdash;常见优化算法分类及总结

之前做特征选择,实现过基于群智能算法进行最优化的搜索,看过一些群智能优化算法的论文,在此做一下总结. 在生活或者工作中存在各种各样的最优化问题,比如每个企业和个人都要考虑的一个问题"在一定成本下,如何使利润最大化"等.最优化方法是一种数学方法,它是研究在给定约束之下如何寻求某些因素(的量),以使某一(或某些)指标达到最优的一些学科的总称. 工程设计中最优化问题(optimalization problem)的一般提法是要选择一组参数(变量),在满足一系列有关的限制条件(约束)下,使设计

深度解读最流行的优化算法:梯度下降

深度解读最流行的优化算法:梯度下降 By 机器之心2016年11月21日 15:08 梯度下降法,是当今最流行的优化(optimization)算法,亦是至今最常用的优化神经网络的方法.本文旨在让你对不同的优化梯度下降法的算法有一个直观认识,以帮助你使用这些算法.我们首先会考察梯度下降法的各种变体,然后会简要地总结在训练(神经网络或是机器学习算法)的过程中可能遇到的挑战.(本文的中文版 PDF 下载地址) 目录: 梯度下降的各种变体 批量梯度下降(Batch gradient descent)

粒子群优化算法

2011-12-05 19:49:55 标签:群智能 休闲 蚁群优化 粒群优化 粒子群优化 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://nxlhero.blog.51cto.com/962631/734212 粒子群优化算法属于群智能(swarm intelligence)优化算法.群智能分两种,一种是粒群优化,另一种是蚁群优化. 群智能概念 假设你和你的朋友正在寻宝,每个人有个探测器,这个探测器可以知道宝藏到探测器的距离

量子粒子群优化算法

参考文献: 量子行为粒子群优化算法研究[D] ,孙俊,博士论文2.4节. #include <iostream> #include <math.h> #include <time.h> using namespace std; #define M 50 //群体数目50 #define N 4 //每个粒子的维数4 //测试类 class TestFunction { public: double resen(double x1,double x2,double x3,