|
TLBO.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"
- #include"randi.h"
- /* 教与学算法 */
- #define sizepop 10 /* 表示种群数 */
- #define maxiter 50 /* 进化次数 */
- #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 }, Mean[nvar] = { 0 }, Teacher_pop[nvar] = { 0 }, Step[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, Teacher_fitness = 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, bestLoc2 = 0, TF = 1;
- 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++)
- {
- // Calculate Population Mean
- for (int k = 0; k < nvar; k++)
- Mean[k] = 0;
- for (int k = 0; k < nvar; k++)
- {
- for (int i = 0; i < sizepop; i++)
- {
- Mean[k] = Mean[k] + pop[i][k];
- }
- Mean[k] = (int)(Mean[k] / sizepop);
- }
- // Select Teacher
- Teacher_fitness = maxValue(fitness);
- bestLoc = maxValueIndex(fitness);
- memcpy(Teacher_pop, pop[bestLoc], 12); // 3个整型,12个字节
- // Teacher Phase
- for (int i = 0; i < sizepop; i++)
- {
- TF = randi(1, 2);
- // Teaching (moving towards teacher)
- S = roundx(Smin + (Smax - Smin)*ratio);
- for (int k = 0; k < nvar; k++)
- {
- randk = double(rand()) / double(RAND_MAX); //0-1随机数
- newpop[k] = roundx(pop[i][k] + (Teacher_pop[k] - TF*Mean[k])*randk);
- // 局部搜索
- if (iter>int(maxiter / 2))
- {
- newpop[k] = roundx(zbest[k] + (Teacher_pop[k] - TF*Mean[k])*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); // 输出显示
- }
- // Learner Phase
- for (int i = 0; i < sizepop; i++)
- {
- bestLoc = randi(1, sizepop);
- bestLoc2 = randi(1, sizepop);
- for (int k = 0; k < nvar; k++)
- {
- Step[k] = pop[i][k] - pop[bestLoc][k];
- }
- if (fitness[bestLoc] < fitness[i])
- {
- for (int k = 0; k < nvar; k++)
- {
- Step[k] = -Step[k];
- }
- }
- for (int k = 0; k < nvar; k++)
- {
- // 交叉
- newpop[k] = 0.5*pop[bestLoc][k] + 0.5*pop[bestLoc2][k];
- // 变异
- randk = double(rand()) / double(RAND_MAX); //0-1随机数
- if (randk>0.5)
- {
- newpop[k] = roundx(pop[i][k] + randk*Step[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"); // 消除屏幕一闪即消失的情况
- }
复制代码 roundx.cpp
- #include<stdio.h>
- #include<math.h>
- int roundx(double x)
- {
- int x1 = (int)x;
- if (x - x1 >= 0.5)
- return x1 + 1;
- else
- return x1;
- }
复制代码 randi.cpp
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<time.h>
- #include<math.h>
- #include"roundx.h"
- // 函数目的:随机产生在某个区间的整数
- int randi(int a, int b)
- {
- typedef long clock_t;
- clock_t start, finish;
- double duration;
- start = clock(); //启动计时
- srand((unsigned int)time(NULL)); //随机种子
- srand(time(0)); // 初始化随机种子
- double randk = double(rand()) / double(RAND_MAX); //0-1随机数
- return roundx(a + (b - a)*randk);
- }
复制代码 minValue.cpp
- #include<stdio.h>
- #include<math.h>
- // 函数目的:求解最小值
- #define sizepop 50 /* 表示种群数 */
- double minValue(double x[sizepop])
- {
- double temp = 100000.0;
- for (int i = 0; i < sizepop; i++)
- {
- if (x[i]<temp)
- temp = x[i];
- }
- return temp;
- }
复制代码 maxValueIndex.cpp
- #include<stdio.h>
- #include<math.h>
- // 函数目的:求解最大值对应的索引
- #define sizepop 50 /* 表示种群数 */
- int maxValueIndex(double x[sizepop])
- {
- int index = 0;
- double temp = 0.0;
- for (int i = 0; i < sizepop; i++)
- {
- if (x[i]>temp)
- {
- temp = x[i];
- index = i;
- }
- }
- return index;
- }
复制代码 maxValue.cpp
- #include<stdio.h>
- #include<math.h>
- // 函数目的:求解最大值
- #define sizepop 50 /* 表示种群数 */
- double maxValue(double x[sizepop])
- {
- double temp = 0.0;
- for (int i = 0; i < sizepop; i++)
- {
- if (x[i]>temp)
- temp = x[i];
- }
- return temp;
- }
复制代码 目标函数:
- #include<stdio.h>
- #include<math.h>
- /* 适应度函数 */
- /*全局变量*/
- #define C 2.6 /* 背包载重 */
- #define nvar 3 /* 3个变量 */
- extern double v[3][3], w[3][3];
- double KPO1(int *x)
- {
- int i = 0, j = 0;
- double xw = 0, xv = 0;
- int xx[nvar][nvar] = {0};
- for (i = 0; i < nvar; i++)
- {
- if (x[i] == 1)
- xx[i][0] = 1;
- else if (x[i] == 2)
- xx[i][1] = 1;
- else if (x[i] == 3)
- xx[i][2] = 1;
- }
- for (i = 0; i < nvar; i++)
- {
- for (j = 0; j < nvar; j++)
- {
- xw = xw + xx[i][j] * w[i][j];
- xv = xv + xx[i][j] * v[i][j];
- }
- }
- if (xw <= C)
- return xv;
- else
- return -1;
- }
复制代码
|
|