|
杂草算法
IWO.cpp
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<time.h>
- #include<math.h>
- #include"roundx.h"
- #include"KP01.h"
- #include"maxValue.h"
- #include"maxValueIndex.h"
- #include"minValue.h"
- /* 杂草算法 */
- #define sizepop 10 /* 表示种群数 */
- #define maxiter 20 /* 进化次数 */
- #define C 2.6 /* 背包载重 */
- #define nvar 3 /* 3个变量 */
- extern double v[3][3] = { { 0.5, 0.4, 0.9 },
- { 0.4, 0.7, 1.1 },
- { 0.3, 0.7, 1.0 } };
- extern double w[3][3] = { { 0.6, 0.7, 0.8 },
- { 0.7, 0.4, 0.8 },
- { 0.5, 0.6, 0.7 } };
- void main(void)
- {
- typedef long clock_t;
- clock_t start, finish;
- double duration;
- start = clock(); //启动计时
- srand((unsigned int)time(NULL)); //随机种子
- srand(time(0)); // 初始化随机种子
- // 变量声明
- int pop[sizepop][nvar] = { 0 }, zbest[nvar] = { 0 }, newpop[nvar] = {0};
- int popmin[nvar] = { 1, 1, 1 };
- int popmax[nvar] = { 3, 3, 3 };
- int S = 0, Smin = 0, Smax = 5, Exponent = 2;
- double sigma_initial = 0.5, sigma_final = 0.001;
- double fitness[sizepop] = { 0 }, newfitness = 0.0;
- double temp = 0.0,randk = 0.0;
- double bestfitness = 0.0, sigma = 0.0, BestCost = 0.0, WorstCost=0.0,ratio=0.0;
- int bestLoc = 0;
- int xx[nvar][nvar] = { 0 };
- // 初始化种群
- for (int i = 0; i < sizepop; i++)
- {
- for (int j = 0; j < nvar; j++)
- {
- randk = double(rand()) / double(RAND_MAX); //0-1随机数
- temp = (popmin[j] + randk*(popmax[j] - popmin[j]));
- pop[i][j] = roundx(temp);
- }
- fitness[i] = KPO1(pop[i]); // 适应度函数
- }
- // 记录一组最优值
- bestfitness = maxValue(fitness);
- bestLoc = maxValueIndex(fitness);
- memcpy(zbest, pop[bestLoc], 12); // 3个整型,12个字节
-
- // 迭代寻优
- for (int iter = 0; iter < maxiter; iter++)
- {
- sigma = (((maxiter - iter) / (maxiter - 1)) ^ Exponent)*(sigma_initial - sigma_final) + sigma_final;
- BestCost = maxValue(fitness);
- WorstCost = minValue(fitness);
- for (int i = 0; i < sizepop; i++)
- {
- ratio = (fitness[i] - WorstCost) / (BestCost - WorstCost + 0.00000001);
- S = roundx(Smin + (Smax - Smin)*ratio);
- for (int j = 0; j < S; j++)
- {
- for (int k = 0; k < nvar; k++)
- {
- randk = double(rand()) / double(RAND_MAX); //0-1随机数
- newpop[k] = roundx(pop[i][k] + sigma*randk);
- if (newpop[k]>popmax[k])
- newpop[k] = popmax[k];
- else if (newpop[k] < popmin[k])
- newpop[k] = popmin[k];
- }
- newfitness = KPO1(newpop); // 适应度函数
- if (newfitness>fitness[i])
- {
- fitness[i] = newfitness;
- memcpy(pop[i], newpop, 12); // 3个整型,12个字节
- }
- if (newfitness > bestfitness)
- {
- bestfitness = newfitness;
- memcpy(zbest, newpop, 12); // 3个整型,12个字节
- }
- }
- }
- printf("最优适应度值为: %lf\n", bestfitness); // 输出显示
- }
- printf("\n"); // 输出显示
- // 最终的解
- printf("最终的解为: \n" ); // 输出显示
- for (int i = 0; i < nvar; i++)
- {
- if (zbest[i] == 1)
- xx[i][0] = 1;
- else if (zbest[i] == 2)
- xx[i][1] = 1;
- else if (zbest[i] == 3)
- xx[i][2] = 1;
- printf("%d\t %d\t %d\n", xx[i][0], xx[i][1], xx[i][2]); // C0-C4输出显示
- }
- printf("\n"); // 输出显示
- system("pause"); // 消除屏幕一闪即消失的情况
- }
复制代码 其中KP01.cpp、maxValue.cpp、maxValueIndex.cpp、minValue.cpp、roundx.cpp请参考教与学算法【http://halcom.cn/forum.php?mod=v ... &extra=page%3D1】
|
|