资源简介

Shubert函数324个全局最优解问题,《演化优化及其在微分方程反问题中的应用》一文中提出了GMLE_DD算法,由于并行计算考试的需要,对论文中提出的方法进行了实现,在这里共享出来,C++实现。源代码是n = 4时的情况。算法性能简介http://blog.csdn.net/ryl219362/article/details/17100039

资源截图

代码片段和文件信息

#include  
#include  
#include  
#include “time.h“

#define Q_NUM 81//3//9//27//81 // 划分子空间的个数
#define POPSIZE 250//100//100//100//250 // 种群的数量
#define NVARS 4//1//2//3//4 // 变量的数量
#define M_SELECT 10 // 选择后的数量
#define GLOBAL_GEN 50000//2000//2000//5000//50000 // 演化代数
#define SUB_GEN 100000//5000//5000//20000//100000 // 子种群演化代数
#define MAX_NUM 10//5//10//10//10 // p的上界
#define MIN_NUM 5//1//3//4//5 // p的下界
#define N1_NUM 150//100//100//100//150 // 子种群的个体数量
#define LBOUND -10.0
#define UBOUND 10.0
#define ANS_NUM 324//3//18//81//324 // 最优解的个数
#define EPSILON_N 0.005//0.5//0.5//0.5//0.005 // p的下界
#define ALPHA 0.05//0.2//0.2//0.2//0.05 // p的下界

// a满足总和固定为1的,每个元素的范围是-0.5~1.5  的系数
double EPSILON = EPSILON_N A[M_SELECT];
int cur_best cur_worst; // 当前最好的当前最差的
double best_fitness worst_fitness; // 当前最好最坏的适应值

struct genotype

double gene[NVARS]; // 基因 
double fitness; // 适应度
double upper[NVARS]; // 上边界 
double lower[NVARS]; // 下界
}; 
struct genotype population[POPSIZE]; // 当前种群
struct genotype newpopulation[MAX_NUM]; // p个个体的种群 
struct genotype subpopulation[N1_NUM]; // 子种群
struct genotype ansPopulation[ANS_NUM]; // ANS_NUM个最优解

void generateA()
{ // 生成一个和为1固定的随机序列a
double sum = 0 minbound = 0.5 maxbound = 1.5;
for (int i = 0; i < M_SELECT - 1; i++)
{
A[i] =(maxbound - minbound) * rand() / (1.0 * RAND_MAX) + minbound;
sum = sum + A[i];
maxbound = 1.5 < 1.5-sum ? 1.5 : 1.5 - sum;
minbound = -0.5 > (-0.5-sum) ? -0.5 : (-0.5 - sum);
}
A[ M_SELECT - 1] = 1-sum;
}
double calfitness(double *x)
{ // 计算一个基因的适应度,shubert函数值取负
double result = 1.0 tmp = 0.0;
for (int i = 0; i < NVARS; i++)
{
tmp = 0.0;
for (double j = 1.0; j < 6; j++)
tmp += j * cos((j+1) * x[i] + j);
result *= tmp;
}
return -result;
}
void init1Gene(genotype &single double lower[] double upper[])
{ // 初始化一个个体
for (int i = 0 ; i  < NVARS; i++)
{
single.lower[i] = lower[i];
single.upper[i] = upper[i];
single.gene[i] = ((double)(rand() / (1.0*RAND_MAX))) * (upper[i] - lower[i]) + lower[i]; 
}
single.fitness = calfitness(single.gene);
}
void initialize(double lbound[] double ubound[]) 
{  // 初始化一个种群
EPSILON = EPSILON_N; // 初始化EPSILON
for (int j = 0; j < POPSIZE; j++) 
init1Gene(population[j] lbound ubound);

void keepbestworst(genotype *pops int sizes)
{ // 确定最好的和最差的个体
best_fitness = -INT_MAX worst_fitness = INT_MAX;
for (int mem = 0; mem < sizes; mem++) 

if (pops[mem].fitness < worst_fitness) 

cur_worst = mem; 
worst_fitness = pops[mem].fitness; 
}
if (pops[mem].fitness > best_fitness) 

cur_best = mem; 
best_fitness = pops[mem].fitness; 
}
}

void select(genotype pops[] int sizes) 
{ // 从种群中选取 M_SELECT 个个体多父体杂交
genotype single = pops[0];
int selected[M_SELECT] = {0};
for(int i = 0; i < M_SE

评论

共有 条评论