/*
Description:
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define Inf 1e-10;
int group;//群体规模
int dimension;//变量维数
int maxGen;//最大迭代次数
double CR;//交叉概率
double F;//缩放因子
double *x_min, *x_max;//各个变量的边界,维数为dimension
double **next , **now;
double *cost;//每个个体的目标函数值,维数为group
double *x_best;//维数为dimension
int r[3];
double Func(double *px)
{
return 3*px[0]+0.000001*pow(px[0], 3.0)\
+2*px[1]+(0.000002*pow(px[1],3.0))/3.0;
}
double Random()
{
double ans = rand()%group + 1;
return ans/group;
}
void RandomInt(int index)//产生三个不同的正整数
{
do{
r[0] = rand()%group;
}while(r[0] == index);
do{
r[1] = rand()%group;
}while(r[1] == r[0] || r[1] == index);
do{
r[2] = rand()%group;
}while(r[2] == r[1] || r[2] == r[0] || r[2] == index);
}
void Init()
{
int i, j;
dimension = 4;
group = 10*dimension;
maxGen = 500;
CR = 0.9;
F = 0.5;
x_min = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
x_min[0] = x_min[1] = 0;
x_min[2] = x_min[3] = -0.55;
x_max = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
x_max[0] = x_max[1] = 1200;
x_max[2] = x_max[3] = 0.55;
x_best = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
now = (double **)malloc(sizeof(double*)*group);//new double*[group];
next = (double **)malloc(sizeof(double*)*group);//new double*[group];
/* 初始化种群 */
for( i = 0; i < group; ++i){
now[i] = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
next[i] = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
for( j = 0; j < dimension; ++j)
now[i][j] = x_min[j] + Random()*(x_max[j] - x_min[j]);
}
cost = (double *)malloc(sizeof(double)*group);//new double[group];
}
double Constrain(int index, double *var)
{
if(index == 0)
return -var[3] + var[2] - 0.55;
if(index == 1)
return -var[2] + var[3] - 0.55;
if(index == 2)
return 1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0] - Inf ;
if(index == 3)
return 0.0-Inf-(1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0]);
if(index == 4)
return 1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1] - Inf ;
if(index == 5)
return 0.0-Inf-(1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1]);
if(index == 6)
return 1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8 - Inf;
if(index == 7)
return 0.0-Inf-(1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8);
}
double cta(double q)
{
if(q < 0.001)
return 5.0;
else if(q <= 0.1)
return 10.0;
else if(q <= 1.0)
return 15.0;
else return 30.0;
}
double Penality(int t, double *var)
{
int i;
double ans = 0.0;
double q, p;
for( i = 0; i <= 7; ++i){
q = Constrain(i, var);
q = (q > 0)?q:0;
p = (q >= 1)?q:1.0;
ans += cta(q)*q*p;
}
return ans*t*sqrt((double)(t));
}
void SelectBest(int t)
{
int i, j;
int minIndex = 0;
double p;
for(i = 0; i < group; ++i){
p = Penality(t, now[i]);
cost[i] = Func(now[i]) + p;
if(cost[minIndex] > cost[i]){
minIndex = i;
}
}
for(j = 0; j < dimension; ++j){
x_best[j] = now[minIndex][j];
}
}
int main()
{
int iter = 0;
int i, j;
double *pv;
double lamda;
double p;
int randIndex;
double temp;
srand(time(NULL));
Init();
while(iter < maxGen){
++iter;
SelectBest(iter);
for(i = 0; i < group; ++i){
RandomInt(i);
randIndex = rand()%group;
pv = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
lamda = 1 - ((double)(iter))/maxGen;
for(j = 0; j < dimension; ++j){
/*变异操作*/
pv[j] = lamda*(now[r[2]][j]-x_best[j])+x_best[j]+F*(now[r[1]][j]-now[r[0]][j]);
//pv[j] = lamda*x_best[j]+(1-lamda)*now[i][j]+F*(now[r[1]][j]-now[r[0]][j]);
/*越界处理*/
if(pv[j] < x_min[j]){
pv[j] = x_min[j]+Random()*(now[i][j]-x_min[j]);
}
if(pv[j] > x_max[j]){
pv[j] = now[i][j]+Random()*(x_max[j]-now[i][j]);
}
/*交叉操作*/
p = Random();
if(p <= CR || j == randIndex){
//NULL operation
}
else pv[j] = now[i][j];
}
/*选择操作*/
temp = Func(pv) + Penality(iter, pv);
if(cost[i] <= temp){
memcpy(next[i], now[i], sizeof(double)*dimension);
}
else{
memcpy(next[i], pv, sizeof(double)*dimension);
}
}
for(i = 0; i < group; ++i){
for(j = 0; j < dimension; ++j){
now[i][j] = next[i][j];
}
}
}
SelectBest(iter);
//打印结果
printf("The objective function value is : %lf,", cost[0]);
printf(" with precision %lf\n", Penality(iter,now[0]));
for(j = 0; j < dimension; ++j){
printf("x(%d) = %lf\n", j+1, now[0][j]);
}
system("pause");
return 0;
}
Description:
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define Inf 1e-10;
int group;//群体规模
int dimension;//变量维数
int maxGen;//最大迭代次数
double CR;//交叉概率
double F;//缩放因子
double *x_min, *x_max;//各个变量的边界,维数为dimension
double **next , **now;
double *cost;//每个个体的目标函数值,维数为group
double *x_best;//维数为dimension
int r[3];
double Func(double *px)
{
return 3*px[0]+0.000001*pow(px[0], 3.0)\
+2*px[1]+(0.000002*pow(px[1],3.0))/3.0;
}
double Random()
{
double ans = rand()%group + 1;
return ans/group;
}
void RandomInt(int index)//产生三个不同的正整数
{
do{
r[0] = rand()%group;
}while(r[0] == index);
do{
r[1] = rand()%group;
}while(r[1] == r[0] || r[1] == index);
do{
r[2] = rand()%group;
}while(r[2] == r[1] || r[2] == r[0] || r[2] == index);
}
void Init()
{
int i, j;
dimension = 4;
group = 10*dimension;
maxGen = 500;
CR = 0.9;
F = 0.5;
x_min = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
x_min[0] = x_min[1] = 0;
x_min[2] = x_min[3] = -0.55;
x_max = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
x_max[0] = x_max[1] = 1200;
x_max[2] = x_max[3] = 0.55;
x_best = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
now = (double **)malloc(sizeof(double*)*group);//new double*[group];
next = (double **)malloc(sizeof(double*)*group);//new double*[group];
/* 初始化种群 */
for( i = 0; i < group; ++i){
now[i] = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
next[i] = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
for( j = 0; j < dimension; ++j)
now[i][j] = x_min[j] + Random()*(x_max[j] - x_min[j]);
}
cost = (double *)malloc(sizeof(double)*group);//new double[group];
}
double Constrain(int index, double *var)
{
if(index == 0)
return -var[3] + var[2] - 0.55;
if(index == 1)
return -var[2] + var[3] - 0.55;
if(index == 2)
return 1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0] - Inf ;
if(index == 3)
return 0.0-Inf-(1000*sin(-var[2]-0.25) + 1000*sin(-var[3]-0.25)+894.8 - var[0]);
if(index == 4)
return 1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1] - Inf ;
if(index == 5)
return 0.0-Inf-(1000*sin(var[2]-0.25) + 1000*sin(var[2]-var[3]-0.25)+894.8 - var[1]);
if(index == 6)
return 1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8 - Inf;
if(index == 7)
return 0.0-Inf-(1000*sin(var[3]-0.25) + 1000*sin(var[3]-var[2]-0.25)+1294.8);
}
double cta(double q)
{
if(q < 0.001)
return 5.0;
else if(q <= 0.1)
return 10.0;
else if(q <= 1.0)
return 15.0;
else return 30.0;
}
double Penality(int t, double *var)
{
int i;
double ans = 0.0;
double q, p;
for( i = 0; i <= 7; ++i){
q = Constrain(i, var);
q = (q > 0)?q:0;
p = (q >= 1)?q:1.0;
ans += cta(q)*q*p;
}
return ans*t*sqrt((double)(t));
}
void SelectBest(int t)
{
int i, j;
int minIndex = 0;
double p;
for(i = 0; i < group; ++i){
p = Penality(t, now[i]);
cost[i] = Func(now[i]) + p;
if(cost[minIndex] > cost[i]){
minIndex = i;
}
}
for(j = 0; j < dimension; ++j){
x_best[j] = now[minIndex][j];
}
}
int main()
{
int iter = 0;
int i, j;
double *pv;
double lamda;
double p;
int randIndex;
double temp;
srand(time(NULL));
Init();
while(iter < maxGen){
++iter;
SelectBest(iter);
for(i = 0; i < group; ++i){
RandomInt(i);
randIndex = rand()%group;
pv = (double *)malloc(sizeof(double)*dimension);//new double[dimension];
lamda = 1 - ((double)(iter))/maxGen;
for(j = 0; j < dimension; ++j){
/*变异操作*/
pv[j] = lamda*(now[r[2]][j]-x_best[j])+x_best[j]+F*(now[r[1]][j]-now[r[0]][j]);
//pv[j] = lamda*x_best[j]+(1-lamda)*now[i][j]+F*(now[r[1]][j]-now[r[0]][j]);
/*越界处理*/
if(pv[j] < x_min[j]){
pv[j] = x_min[j]+Random()*(now[i][j]-x_min[j]);
}
if(pv[j] > x_max[j]){
pv[j] = now[i][j]+Random()*(x_max[j]-now[i][j]);
}
/*交叉操作*/
p = Random();
if(p <= CR || j == randIndex){
//NULL operation
}
else pv[j] = now[i][j];
}
/*选择操作*/
temp = Func(pv) + Penality(iter, pv);
if(cost[i] <= temp){
memcpy(next[i], now[i], sizeof(double)*dimension);
}
else{
memcpy(next[i], pv, sizeof(double)*dimension);
}
}
for(i = 0; i < group; ++i){
for(j = 0; j < dimension; ++j){
now[i][j] = next[i][j];
}
}
}
SelectBest(iter);
//打印结果
printf("The objective function value is : %lf,", cost[0]);
printf(" with precision %lf\n", Penality(iter,now[0]));
for(j = 0; j < dimension; ++j){
printf("x(%d) = %lf\n", j+1, now[0][j]);
}
system("pause");
return 0;
}
本文出自 “东方快翔” 博客,谢绝转载!
本文详细介绍了一种基于群体智能的优化算法——差分进化(DE)算法。该算法通过模仿自然界中的生物进化过程来寻找复杂问题的最优解。文章深入探讨了DE算法的具体实现,包括参数设置、变异、交叉和选择等关键步骤,并提供了一个针对特定目标函数求解的完整示例。
1万+

被折叠的 条评论
为什么被折叠?



