Hello Mat

 找回密码
 立即注册
查看: 2471|回复: 1

杂草算法

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2021-11-13 16:46:09 | 显示全部楼层 |阅读模式
杂草算法
IWO.cpp
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. #include<math.h>
  6. #include"roundx.h"
  7. #include"KP01.h"
  8. #include"maxValue.h"
  9. #include"maxValueIndex.h"
  10. #include"minValue.h"

  11. /* 杂草算法 */
  12. #define sizepop 10      /* 表示种群数 */
  13. #define maxiter 20     /* 进化次数 */
  14. #define C 2.6           /* 背包载重  */  
  15. #define nvar 3          /* 3个变量 */

  16. extern double v[3][3] = { { 0.5, 0.4, 0.9 },
  17.                                                   { 0.4, 0.7, 1.1 },
  18.                                                   { 0.3, 0.7, 1.0 } };
  19. extern double w[3][3] = { { 0.6, 0.7, 0.8 },
  20.                                                   { 0.7, 0.4, 0.8 },
  21.                                                   { 0.5, 0.6, 0.7 } };
  22. void main(void)
  23. {
  24.         typedef long clock_t;
  25.         clock_t start, finish;
  26.         double duration;
  27.         start = clock();  //启动计时
  28.         srand((unsigned int)time(NULL));  //随机种子
  29.         srand(time(0));  // 初始化随机种子

  30.         // 变量声明
  31.         int pop[sizepop][nvar] = { 0 }, zbest[nvar] = { 0 }, newpop[nvar] = {0};
  32.         int popmin[nvar] = { 1, 1, 1 };
  33.         int popmax[nvar] = { 3, 3, 3 };
  34.         int S = 0, Smin = 0, Smax = 5, Exponent = 2;
  35.         double sigma_initial = 0.5, sigma_final = 0.001;
  36.         double fitness[sizepop] = { 0 }, newfitness = 0.0;
  37.         double temp = 0.0,randk = 0.0;
  38.         double bestfitness = 0.0, sigma = 0.0, BestCost = 0.0, WorstCost=0.0,ratio=0.0;
  39.         int bestLoc = 0;
  40.         int xx[nvar][nvar] = { 0 };

  41.         // 初始化种群
  42.         for (int i = 0; i < sizepop; i++)
  43.         {
  44.                 for (int j = 0; j < nvar; j++)
  45.                 {
  46.                         randk = double(rand()) / double(RAND_MAX); //0-1随机数
  47.                         temp = (popmin[j] + randk*(popmax[j] - popmin[j]));
  48.                         pop[i][j] = roundx(temp);
  49.                 }
  50.                 fitness[i] = KPO1(pop[i]);   // 适应度函数
  51.         }
  52.         // 记录一组最优值
  53.         bestfitness = maxValue(fitness);
  54.         bestLoc = maxValueIndex(fitness);
  55.         memcpy(zbest, pop[bestLoc], 12); // 3个整型,12个字节
  56.         
  57.         // 迭代寻优
  58.         for (int iter = 0; iter < maxiter; iter++)
  59.         {
  60.                 sigma = (((maxiter - iter) / (maxiter - 1)) ^ Exponent)*(sigma_initial - sigma_final) + sigma_final;
  61.                 BestCost = maxValue(fitness);
  62.                 WorstCost = minValue(fitness);

  63.                 for (int i = 0; i < sizepop; i++)
  64.                 {
  65.                         ratio = (fitness[i] - WorstCost) / (BestCost - WorstCost + 0.00000001);
  66.                         S = roundx(Smin + (Smax - Smin)*ratio);
  67.                         for (int j = 0; j < S; j++)
  68.                         {
  69.                                 for (int k = 0; k < nvar; k++)
  70.                                 {
  71.                                         randk = double(rand()) / double(RAND_MAX); //0-1随机数
  72.                                         newpop[k] = roundx(pop[i][k] + sigma*randk);
  73.                                         if (newpop[k]>popmax[k])
  74.                                                 newpop[k] = popmax[k];
  75.                                         else if (newpop[k] < popmin[k])
  76.                                                 newpop[k] = popmin[k];
  77.                                 }

  78.                                 newfitness = KPO1(newpop);   // 适应度函数

  79.                                 if (newfitness>fitness[i])
  80.                                 {
  81.                                         fitness[i] = newfitness;
  82.                                         memcpy(pop[i], newpop, 12); // 3个整型,12个字节
  83.                                 }
  84.                                 if (newfitness > bestfitness)
  85.                                 {
  86.                                         bestfitness = newfitness;
  87.                                         memcpy(zbest, newpop, 12); // 3个整型,12个字节
  88.                                 }

  89.                         }
  90.                 }

  91.                 printf("最优适应度值为: %lf\n", bestfitness);         // 输出显示
  92.         }
  93.         printf("\n");         // 输出显示

  94.         // 最终的解
  95.         printf("最终的解为: \n" );         // 输出显示
  96.         for (int i = 0; i < nvar; i++)
  97.         {
  98.                 if (zbest[i] == 1)
  99.                         xx[i][0] = 1;
  100.                 else if (zbest[i] == 2)
  101.                         xx[i][1] = 1;
  102.                 else if (zbest[i] == 3)
  103.                         xx[i][2] = 1;
  104.                 printf("%d\t %d\t %d\n", xx[i][0], xx[i][1], xx[i][2]);   // C0-C4输出显示
  105.         }

  106.         printf("\n");         // 输出显示
  107.         system("pause");    // 消除屏幕一闪即消失的情况
  108. }
复制代码
其中KP01.cpp、maxValue.cpp、maxValueIndex.cpp、minValue.cpp、roundx.cpp请参考教与学算法http://halcom.cn/forum.php?mod=v ... &extra=page%3D1



算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
 楼主| 发表于 2021-11-14 16:51:20 | 显示全部楼层

bilibili视频观察代码运行效果:
https://www.bilibili.com/video/BV1rQ4y1m735/
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-19 22:21 , Processed in 0.208660 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表